[求助]求一个栈的程序,有插入,删除等功能
求一个栈的程序,有插入,删除等功能
typedef struct snode
{
DataType 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,DataType x)
{
LSNode *p;
if((p=(LSNode*)malloc(sizeof(LSNode)))==NULL)
{
printf("There is not enough space for being inserted in Function StackPush\n");
printf("Press any key to end .\n");
getch();
return 0;
}
p->data=x;
p->next=head->next;
head->next=p;
return 1;
}
int StackPop(LSNode *head,DataType *d)
{
LSNode *p=head->next;
if(p==NULL)
{
printf("Stack is empty in Function StackPop !\n");
printf("Press any key to end.\n");
getch();
return 0;
}
head->next=p->next;
*d=p->data;
free(p);
return 1;
}
int StackTop(LSNode *head,DataType *d)
{
LSNode *p=head->next;
if(p==NULL)
{
printf("Stack is empty in StackTop.\n");
printf("Press any key to end .\n");
getch();
return 0;
}
*d=p->data;
return 1;
}
void StackDestroy(LSNode *head)
{
LSNode *p,*p1;
p=head;
while(p!=NULL)
{
p1=p;
p=p->next;
free(p1);
}
return ;
}