linstack.h

来自「编写程序」· C头文件 代码 · 共 77 行

H
77
字号
typedef struct snode
{	
	SDataType data;
	struct snode *next;
} LSNode;

//初始化
void StackInitiate(LSNode **head)
{		
	if((*head=(LSNode*)malloc(sizeof(LSNode)))==NULL)
		exit(1);
	(*head)->next=NULL;
}

//非空否
int StackNotEmpty(LSNode *head)
{	
	if(head->next==NULL)
		return 0;
	else return 1;
}

//入栈
int StackPush(LSNode *head,SDataType x)
{	
	LSNode *p;
	if((p=(LSNode*)malloc(sizeof(LSNode)))==NULL)
	{	
		printf("内存空间不足无法插入!");
		return 0;
	}
	p->data=x;
	p->next=head->next;
	head->next=p;
	return 1;
}

//出栈
int StackPop(LSNode *head,SDataType *d)
{	
	LSNode *p=head->next;
	if(p==NULL)
	{	
		printf("堆栈已空出错!\n");
		return 0;
	}
	head->next=p->next;	
	*d=p->data;
	free(p);
	return 1;
}

//取栈顶数据元素
int StackTop(LSNode *head,SDataType *d)
{	
	if(head->next==NULL)
	{	
		printf("堆栈已空出错!\n");
		return 0;
	}
	*d=head->next->data;
	return 1;
}

//撤销动态申请空间
void StackDestroy(LSNode **head)
{	
	LSNode *p,*q;
	p=*head;
	if(p!=NULL)
	{	
		q=p;
		p=p->next;
		free(q);
	}
	*head=NULL;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?