#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define ERROR -1
#define OK 1
///链栈的实现:链栈是运算受限的单链表,其插入和删除只能在表头位置上进行。
typedef struct Stack_Node
{
int data;
struct Stack_Node *next;
}Stack_Node;
///初始化链栈
Stack_Node *Init_Link_Stack()
{
Stack_Node *top;
top=(Stack_Node*)malloc(sizeof(Stack_Node));
top->next=NULL;
return top;
}
///元素进栈
int push_Stack_Node(Stack_Node *top, int e)
{
Stack_Node *p;
p=(Stack_Node*)malloc(sizeof(Stack_Node));
if (!p)
{
return ERROR;
}
p->data=e;
p->next=top->next;
top->next=p;
return OK;
}
///元素出栈
int pop_Stack_Node(Stack_Node *top, int m)
{
Stack_Node *p;
if(top->next==NULL)
{
printf("无元素可出栈\n");
return ERROR;
}
p=top->next;
m=p->data;
top->next=p->next;
free(p);
return OK;
}
///遍历链栈
int Stack_Node_Travel(Stack_Node *top)
{
int e;
if(top==NULL)
{
printf("链栈中无元素\n");
}
Stack_Node *ptr;
ptr=top;
while(ptr->next)
{
ptr=ptr->next;
e=ptr->data;
printf("%d\n",e);
}
return OK;
}
///取链栈的栈顶元素
int GetTopStack_Node(Stack_Node *top)
{
int e;
if(top->next!=NULL)
{
top=top->next;
e=top->data;
printf("The element of top:%d\n",e);
}
}
int main()
{
int x;
int e;
Stack_Node *top=Init_Link_Stack();
for(x=1;x<5;x++)
{
push_Stack_Node(top,x);
}
Stack_Node_Travel(top);
GetTopStack_Node(top);
pop_Stack_Node(top,e);
Stack_Node_Travel(top);
#include <stdlib.h>
#include <malloc.h>
#define ERROR -1
#define OK 1
///链栈的实现:链栈是运算受限的单链表,其插入和删除只能在表头位置上进行。
typedef struct Stack_Node
{
int data;
struct Stack_Node *next;
}Stack_Node;
///初始化链栈
Stack_Node *Init_Link_Stack()
{
Stack_Node *top;
top=(Stack_Node*)malloc(sizeof(Stack_Node));
top->next=NULL;
return top;
}
///元素进栈
int push_Stack_Node(Stack_Node *top, int e)
{
Stack_Node *p;
p=(Stack_Node*)malloc(sizeof(Stack_Node));
if (!p)
{
return ERROR;
}
p->data=e;
p->next=top->next;
top->next=p;
return OK;
}
///元素出栈
int pop_Stack_Node(Stack_Node *top, int m)
{
Stack_Node *p;
if(top->next==NULL)
{
printf("无元素可出栈\n");
return ERROR;
}
p=top->next;
m=p->data;
top->next=p->next;
free(p);
return OK;
}
///遍历链栈
int Stack_Node_Travel(Stack_Node *top)
{
int e;
if(top==NULL)
{
printf("链栈中无元素\n");
}
Stack_Node *ptr;
ptr=top;
while(ptr->next)
{
ptr=ptr->next;
e=ptr->data;
printf("%d\n",e);
}
return OK;
}
///取链栈的栈顶元素
int GetTopStack_Node(Stack_Node *top)
{
int e;
if(top->next!=NULL)
{
top=top->next;
e=top->data;
printf("The element of top:%d\n",e);
}
}
int main()
{
int x;
int e;
Stack_Node *top=Init_Link_Stack();
for(x=1;x<5;x++)
{
push_Stack_Node(top,x);
}
Stack_Node_Travel(top);
GetTopStack_Node(top);
pop_Stack_Node(top,e);
Stack_Node_Travel(top);
}