Home > Back-end >  Insert output algorithm C language implementation of the stack
Insert output algorithm C language implementation of the stack

Time:06-05

#include
#include
#include
# define N 100
Typedef struct//structure definition
{
The int data [N].//stack element
Int top;//array subscript subscript said stack, equivalent to a top pointer
} srack;


Void initsrack srack * (s)//initialization stack son function
{
S - & gt; Top=1;
}


Void srackempty srack * (s)//whether the stack is empty
{
If (s - & gt; Top==1)
Printf (" stack is empty \ n ");
The else
Printf (" stack is empty \ n ");
}


Pop (srack is void * s)//remove the top element
{
Int e;
If (s - & gt; Top==1)//if (top==base) retur error, a pointer is
Printf (" stack is empty ");
The else
{
E=s - & gt; Data [s - & gt; top];
S - & gt; Top -;
Printf (" remove elements after % d % d left element \ n ", e, s - & gt; Top + 1);
}
}


Void push srack * (s)//into the stack
{
If (N - s - & gt; Top==1)//determine whether stack full
{
Printf (" the stack is full \ n ");
}
The else
{
Printf (" input into the value of the stack: ");
The scanf (" % d ", & amp; S - & gt; Data [+ + s - & gt; top]);
Printf (" stack success \ n ");
}
}

Void the print ()//output menu command function
{
Printf (" input password: \ n ");
Printf (" 1. Whether the stack is empty \ n ");
Printf (" (2) into the stack \ n ");
Printf (" 3. Display the stack elements number and stack elements \ n ");
Printf (" 4. Delete the top element \ n ");
Printf (" 5. Exit \ n \ n \ n ");
}

Void the display (srack * s)//panel displays
{
If (s - & gt; Top==1)
Printf (" stack is empty ");
The else
Printf (" stack at this time there are % d elements, elements of the stack for % d \ n ", s - & gt; Top + 1, s - & gt; The data/s - & gt; top);
}



Int main ()//main function
{
Print ();//display menu
int n;//password variable
Int c=1;
Srack * s;
S=(srack *) malloc (sizeof (srack));//distribution
Initsrack (s);//initialize the stack function called

While (c)
{
Printf (" \ n input password operation: \ n ");
The scanf (" % d ", & amp; N);
system("cls");//C command to remove all show that the cursor at the upper left screen
Print ();

The switch (n)
{
Case 1: srackempty (s); break; Whether the stack is empty function called//
Case 2: push (s); break;//call stack function
Case 3: the display (s); break;//display, output function
Case 4: pop (s); break;//the stack function
Case 5: c=0;
}
}
}

CodePudding user response:

I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10581430.html
I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10768339.html
  • Related