Stack is a special linear table, LIFO , since it is a linear table, will be divided into sequential storage and chain storage, the following is the stack of the chain storage part, also known as chain stack. Single linked list is a head pointer node, usually the stack top of the stack is equivalent to the head pointer, because the initial chain stack is empty, that is, Top=null, because the top of the stack, the first node of a single linked table has no meaning, it does not need. For a chain stack, there is almost no stack full, unless the computer is full of memory. Chain stack operation and the operation of a single linked list is very similar, just insert, delete, get the difference is large, the implementation code is as follows:
#include <stdio.h> #include <stdlib.h> typedef int LSELETYPE;//Defines the data type of a chain stack store typedef struct STACKNODE{//
The Structure Body (node) Lseletype data that defines chain stack storage;
struct Stacknode *next;
}stacknode;
The typedef stacknode *stacklinkp;//defines the stack top of the pointer type (equivalent to the head pointer of the linked list) typedef struct STACKLINK{//defines the attributes STACKLINKP top of the chain stack;
int count;
}stacklink;
Stacklink init () {Stacklink sl;
Sl.top=null;
sl.count=0;
return SL;
int Stackempty (Stacklink s) {int result;
if (s.count==0) {result=0;
}else{result=1;
return result;
} void push (Stacklink *s,lseletype e) {//defines a pointer to a node (equivalent to a linked-head pointer) STACKLINKP p;
Create nodes through pointers and assign values to p= (STACKLINKP) malloc (sizeof (Stacknode));
p->data=e;
p->next=s->top;//the top of the stack to him and let the top of the stack point to him s->top=p; s->count++;//chain stack length increased} void Pop (Stacklink *s,lseletype *e) {int r=stackempty (*s);//To determine whether the stack is empty if (r==1) {St
ACKLINKP p=s->top;//Get the node to delete (*e) =p->data;
s->top=p->next;//let the top of the stack point to the next node Free (p);
s->count--;
}else{return;
} void Getele (Stacklink s,lseletype *e) {STACKLINKP p=s.top;
*e=p->data;
int main () {Stacklink s=init ();
Lseletype G;
Lseletype e=666,f=888;
printf ("Whether the chain stack is empty: \n%d\n", Stackempty (s));
Push (&S,E);
Push (&S,F);
printf ("Whether the chain stack is empty: \n%d\n", Stackempty (s));
Getele (S,&G);
printf ("Current stack top data is: \n%d\n", g);
Pop (&S,&G);
Getele (S,&G);
printf ("Delete a data, the current stack top data is: \n%d\n", g);
Pop (&S,&G);
printf ("Whether the chain stack is empty: \n%d\n", Stackempty (s));
return 0;
}