数据结构—链式堆栈表

  • 格式:wps
  • 大小:16.50 KB
  • 文档页数:3

#include

#include

typedef struct node

{struct node *next;

int data;

}linkstack;

void Initstack(linkstack*top)

{

top->next=NULL;

printf("initstack ok!");

}

void printstack(linkstack*top)

{

linkstack *p;

p=top->next;

while (p!=NULL)

{ printf("%d",p->data);

p=p->next;

}

}

int emptystack(linkstack *top)

{ if (top->next==NULL)

{printf ("empty!");

return(0);}

else

{printf("unempty!");

return (1);}

}

void push(linkstack *top,int x)

{ linkstack *s;

s=(linkstack*)malloc(sizeof(linkstack));

s->data=x;

s->next=top->next;

top->next=s;

}

void popstack(linkstack *top)

{ linkstack*p;

if(emptystack(top)==0)

printf("失败");

else

{

p=top->next;

top->next=p->next;

free(p);

printf("popstack ok!");

}

}

void Gettopstack(linkstack *top, int *p)

{if (emptystack(top)!=1)

{ printf("empty!");

*p=0;

}

else

*p=top->next->data;

}

void main()

{

linkstack *top;int x,y;int f;

top=(linkstack *)malloc(sizeof(linkstack));

printf("\nplease input f to choose flowing op,-1 to end:");

printf("\n1:Initstack(top)");

printf("\n2:printstack(top)");

printf("\n3:Employstack(top)");

printf("\n4:Pushstack(top)");

printf("\n5:Poptack(top)\n");

printf("\n6:Gettopstack(top)");

scanf("%d",&f);

while(f!=-1)

{

switch (f)

{ case 1:Initstack(top);break;

case 2:printstack(top);break;

case 3:emptystack(top);break;

case 4:printf("\nplease input x:",&x);

scanf("%d",&x);

push(top,x);break;

case 5: popstack(top);break;

case 6: Gettopstack(top,&y);

printf("%d",y);break;

}

printf("\nplease input f to choose flowing op,-1 to end:");

printf("\n1:Initstack(top)");

printf("\n2:print(top)");

printf("\n3:Employstack(top)");

printf("\n4:Pushstack(top)");

printf("\n5:Poptack(top)\n");

printf("\n6:Gettopstack(top)");

scanf("%d",&f);

}

}

󰀀