Introduction
Stack is a widely used linear data structure that allows for insertion or deletion only at one end of a table, so that the stack can also be called a linear table with limited operation . In the stack, the one end that allows insertions or deletions is called the top of the stack (top) and the other end of the insert and delete is called the bottom of the stack (bottom);
as Follows:
This article simply implements the stack and its basic operations with an array.
The code is as Follows:
#define MaxSize-struct{ int data[maxsize]; int top;} seqstack;
View Code
Note: This assumes that the data stored in the stack is an integer (int)
Basic Operations 1. Initialization of stacks
void init (seqstack* s) { s->top =-1; // }
View Code2. Incoming stack
void int x) { If(s->top<maxsize-1) // s->data[++ (s->top)] = x;}
View Code3. The Stack
int Pop (seqstack* s) { if(s->top>-1) // return (s->data[s->top--]);}
View Code4. Emptying the Stack
void Clear (seqstack* s) { s->top =-1;}
View Code5. Determine if the stack is empty
bool isEmpty (seqstack* s) { if(s->top==-1) { Returntrue; } Else { returnfalse; }}
View Code6. Calculate the length of the stack
int length (seqstack* s) { return (s->top+1);}
View Code
To Test:
intMainvoid) {seqstack s; Init (&s); Push (&s,1); Push (&s,2); Push (&s,3); printf ("%d\n", Length (&s)); if(isEmpty (&S)) {printf ("stack is empty \ n"); }Else{printf ("stack is not empty \ n"); } printf ("%d\n", Pop (&s)); printf ("%d\n", Pop (&s)); printf ("%d\n", Pop (&s)); if(isEmpty (&S)) {printf ("stack is empty \ n"); }Else{printf ("stack is not empty \ n"); } push (&s,3); Clear (&s); if(isEmpty (&S)) {printf ("stack is empty \ n"); }Else{printf ("stack is not empty \ n"); } return 0;}
View CodeTest Results:
Full code
#include <stdio.h>#defineMaxSize 100typedefstruct{ intdata[maxsize]; inttop;} seqstack;voidInit (seqstack*s);BOOLIsEmpty (seqstack*s);voidPush (seqstack* s,intx);intPop (seqstack*s);voidClear (seqstack*s);intLength (seqstack*s);intMainvoid) {seqstack s; Init (&s); Push (&s,1); Push (&s,2); Push (&s,3); printf ("%d\n", Length (&s)); if(isEmpty (&S)) {printf ("stack is empty \ n"); }Else{printf ("stack is not empty \ n"); } printf ("%d\n", Pop (&s)); printf ("%d\n", Pop (&s)); printf ("%d\n", Pop (&s)); if(isEmpty (&S)) {printf ("stack is empty \ n"); }Else{printf ("stack is not empty \ n"); } push (&s,3); Clear (&s); if(isEmpty (&S)) {printf ("stack is empty \ n"); }Else{printf ("stack is not empty \ n"); } return 0;}voidInit (seqstack*S) {s->top =-1;//the array subscript starts at 0, so this indicates that the stack is empty}BOOLIsEmpty (seqstack*S) { if(s->top==-1){ return true; }Else{ return false; }}voidPush (seqstack* s,intx) { if(s->top<maxsize-1)//stack is not full to enter the stacks->data[++ (s->top)] =x;}intPop (seqstack*S) { if(s->top>-1)//stack is not empty to be out of stack return(s->data[s->top--]); }voidClear (seqstack*S) {s->top =-1;}intLength (seqstack*S) { return(s->top+1);}
View Code
Simple implementation of the Stack (1)-array implementation