//头文件 Stack.h
#ifndef _Stack_h
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;
typedef int ElementType;
int IsEmpty(Stack S);
Stack CreateStack(void);
void DisposeStack(Stack S);
void MakeEmpty(Stack S);
void Push(ElementType X,Stack S);
ElementType Top(Stack S);
int Pop(Stack S);
#endif /*_Stack_h*/
//主文件 功能自己看函数名字~~见名知意
#include "Stack.h"
#include
#include
struct Node
{
ElementType Element;
PtrToNode Next;
};
Stack CreateStack(void)
{
Stack S;
S=malloc(sizeof(struct Node));
if(S==NULL)
printf("出错啦");
S->Next=NULL;
MakeEmpty(S);
return S;
}
int IsEmpty(Stack S)
{
return S->Next==NULL;
}
void MakeEmpty(Stack S)
{
if(S==NULL)
printf("出错啦");
else
while(!IsEmpty(S))
Pop(S);
}
void Push(ElementType X,Stack S)
{
PtrToNode TmpCell;
TmpCell=malloc(sizeof(struct Node));
if(TmpCell==NULL)
printf("出错啦");
else
{
TmpCell->Element =X;
TmpCell->Next =S->Next ;
S->Next =TmpCell;
}
}
ElementType Top(Stack S)
{
if(!IsEmpty(S))
return S->Next ->Element;
printf("出错啦");
return 0;
}
int Pop(Stack S)
{
PtrToNode FirstCell;
int x=0;
if(IsEmpty(S))
printf("出错啦");
else
{
FirstCell=S->Next ;
x=FirstCell->Element ;
S->Next =S->Next ->Next ;
free(FirstCell);
}
return x;
}
void main()
{
int x;
Stack S,top;
S=CreateStack();
top=S;
Push(3,S);
Push(2,S);
Push(4,S);
//Pop(S);
S=top;
for(x=0;x<3;x++)
{
printf("%d\n",S->Next->Element);
S=S->Next ;
}
}
#define m 100
typedef struct
{
int stack[m];
int top;
} stackstru;
init(stackstru *s) /*装入栈*/
{
s->top=0;
return 1;
}
int push(stackstru *s,int x) /*入栈操作*/
{
if (s->top==m)
printf("the stack is overflow!\n");
else
{
s->top=s->top+1;
s->stack[s->top]=x;
}
}
void display(stackstru *s) /*显示栈所有数据*/
{
if(s->top==0)
printf("the stack is empty!\n");
else
{
while(s->top!=0)
{
printf("%d->",s->stack[s->top]);
s->top=s->top-1;
}
}
}
int pop(stackstru *s) /*出栈操作并返回被删除的那个记录*/
{
int y;
if(s->top==0)
printf("the stack is empty!\n");
else
{
y=s->stack[s->top];
s->top=s->top-1;
return y;
}
}
int gettop(stackstru *s) /*得到栈顶数*/
{
int e;
if(s->top==0)
return 0;
else
e=s->stack[s->top];
return e;
}
main(stackstru *p) //函数使用演示
{
int n,i,k,h,x1,x2,select;
printf("create a empty stack!\n");
init(p);
printf("input a stack length:\n");
scanf("%d",&n);
for(i=0;i
printf("input a stack value:\n");
scanf("%d",&k);
push(p,k);
}
printf("select 1:display()\n");
printf("select 2:push()\n");
printf("select 3:pop()\n");
printf("select 4:gettop()\n");
printf("input a your select(1-4):\n");
scanf("%d",&select);
switch(select)
{
case 1:
{
display(p);
break;
}
case 2:
{
printf("input a push a value:\n");
scanf("%d",&h);
push(p,h);
display(p);
break;
}
case 3:
{
x1=pop(p);
printf("x1->%d\n",x1);
display(p);
break;
}
case 4:
{
x2=gettop(p);
printf("x2->%d",x2);
break;
}
}
}