链栈的基本操作问题

来源:百度知道 编辑:UC知道 时间:2024/09/28 09:45:02
#include <stdio.h>
#include <stdlib.h>
typedef struct STACK
{
int data;
struct STACK *next;
}Stack;
void InitStack(Stack *S)
{
if((S=(Stack *)malloc(sizeof(Stack)))==NULL)
return;
S->next=NULL;
}
void GetTop(Stack *S,int &e)
{
if(S->next==NULL)
{
printf("空栈...\n");
return;
}
e=S->next->data;
}
void Push(Stack *S)
{
int item;
Stack *p;
printf("请输入要入栈的元素(输入-1结束):\n");
while(1)
{
scanf("%d",&item);
if(item==-1)
break;
if((p=(Stack *)malloc(sizeof(Stack)))==NULL)
return;
p->data=item;
p->next=S->next;
S->next=p;
}
}
void Pop(Stack *S)
{
if(S->next==NULL)
{
printf("空栈...\n");
return;
}
S->next=S->next->next;
}
v

OK了,下面程序可以编译,欢迎在线讨论;共同进步。。。
#include <stdio.h>
#include <stdlib.h>
typedef struct STACK
{
int data;
struct STACK *next;
}Stack;

Stack* InitStack()
{
Stack *S;
if((S=(Stack *)malloc(sizeof(Stack)))==NULL)
return 0;
S->next=NULL;
return S;
}
void GetTop(Stack *S,int &e)
{
if(S->next==NULL)
{
printf("空栈...\n");
return;
}
e=S->next->data;
}
void Push(Stack *S)
{
int item;
Stack *p;
printf("请输入要入栈的元素(输入-1结束):\n");
while(1)
{
scanf("%d",&item);
if(item==-1)
break;
if((p=(Stack *)malloc(sizeof(Stack)))==NULL)
return;
p->data=item;
p->next=S->next;
S->next=p;
}

}
void Pop(Stack *S)
{
if(S->next==NULL)