#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace Std;
typedef struct STACKNODE///Structure node definition
{
int data;
struct Stacknode *next;
}stacknode,*linkstack;
int Stackempty (linkstack top)///Stack Empty
{
if (top->next==null)
return 1;
Else
return 0;
}
void Push (Linkstack top,int value)///into the stack
{
Stacknode * NEWP = (Stacknode *) malloc (sizeof (Stacknode));
if (newp! = NULL)
{
newp->data=value;
newp->next=top->next;
top->next=newp;
}
Else
cout<< "No valid memory space to store new nodes" <<endl;
return;
}
int Pop (linkstack top)///out Stack
{
Stacknode * TEMP;
int t;
if (Stackempty (top))
cout<< "The current stack is empty!!! "<<endl;
Else
{
temp=top->next;
t=temp->data;
Top->next = temp->next;
Free (temp);
}
return t;
}
void Printstack (Linkstack top)///print operation
{
if (top->next==null)
cout<< "The current stack is empty!!! "<<endl;
Else
{
while (Top->next!=null)
{
cout<<top->next->data<< "";
top=top->next;
}
}
}
int Stacktop (linkstack top)///return stack top element
{
if (Stackempty (top))
cout<< "The current stack is empty!!! "<<endl;
Return top->next->data;
}
int Stacklen (linkstack top)//length of stack
{
int len=0;
while (Top->next!=null)
{
len++;
top=top->next;
}
return Len;
}
void Destroystack (Linkstack top)///Stack Destruction
{
Linkstack Q;
q=top->next;
while (Top!=null)
{///This part of the blogger was wrong, killed me, he actually head node to release the sweat,
top=q->next;
Free (q); After the destruction, the position of the original top (he stored the value of the previous, but this is the wild pointer)
Q=top;
}
printf ("Destruction succeeded");
}
void Initstack (Linkstack top)///initialization stack + purge operation
{
top->next=null;
}
void instruction (void)///function Interface description
{
cout<< "0------Exit Program" <<endl
<< "1------into the stack operation" <<endl
<< "2------out of stack operation" <<endl
<< "3------take the top element of the stack" <<endl
<< "4------Judging if the stack is empty" <<endl
<< "5------Returns the number of elements in the stack" <<endl
<< "6------# # # #初始化栈 # # #" <<endl
<< "7------display Stack" <<endl
<< "8------Destruction Stack" <<endl
<< "9------Exit Program" <<endl;
}
int main ()///main function part
{
Linkstack Top=null;
top= (linkstack) malloc (sizeof (Stacknode)); Head node
instruction ();
int i,value;
cin>>i;
while (i)
{
Switch (i)
{
Case 1:
Initstack (top);
cout<< "Please enter an integer and terminate to 0 end!!! "<<endl;
cin>>value;
while (value!=0)
{
Push (Top,value);
cin>>value;
}
Printstack (top);
cout<<endl;
Break
Case 2:
if (top->next!=null)
cout<< "Stack top element is:" <<pop (top) <<endl;
Else
cout<< "The current stack is empty!!! "<<endl;
Break
Case 3:
if (Stackempty (top) ==1)
cout<< "The current stack is empty!!! "<<endl;
Else
Cout<<stacktop (top) <<endl;
Break
Case 4:
if (Stackempty (top) ==1)
cout<< "The current stack is empty!!! "<<endl;
Else
cout<< "The current stack is empty!!! "<<endl;
Break
Case 5:
if (Stackempty (top) ==1)
cout<< "The current stack is empty!!! "<<endl;
Cout<<stacklen (top) <<endl;
cout<<endl;
Break
Case 6:initstack (top);
Case 7:
Printstack (top);
cout<<endl;
Break
Case 8:
Destroystack (top);
cout<<endl;
top->next=null; This step is important, otherwise it will result in no element or an arbitrary number of outputs.
Break
Case 9:goto End;break; Interesting goto statement
Default
cout<< "Invalid choice!!! "<<endl;
Break
}
instruction ();
cin>>i;
}
End:;
return 0;
}
Link stacks-C language version