A stack is a linear table that can be inserted or deleted only at one end of the table (called the top of the stack). A stack cannot be operated at the other end of the table (called the bottom of the stack, it is also a special case of linear tables.
The sequential storage of stacks is implemented using arrays. To indicate the stack, we need a top pointer to indicate the top position of the stack. The following code implements operations on the sequence stack.
# Include <stdio. h> # include <stdlib. h> # define maxsize 100 typedef struct {int data [maxsize]; int top; // stack top pointer} sqstack; sqstack * sqstack_create (); // create a stack int sqstack_gettop (sqstack * q); // obtain the stack top int sqstack_in (sqstack * q, int dat); // The inbound stack int sqstack_out (sqstack * q ); // output stack void sqstack_setnull (sqstack * q); // stack empty int sqstack_empty (sqstack * q); // determine whether the stack is empty void showsqstack (sqstack * q ); // output the display stack int main (atevoid) {sqstack * q; int ch OICE; int ans, dat; printf ("sequential stack operation exercises: \ n"); q = sqstack_create (); While (1) {printf ("sequential stack related operations: \ n "); printf (" 1. stack top node \ n "); printf (" 2. inbound stack \ n "); printf (" 3. outbound stack \ n "); printf (" 4. output display stack \ n "); printf (" 5. exit program \ n "); printf (" select: "); scanf (" % d ", & choice); Switch (choice) {Case 1: ans = sqstack_gettop (Q); If (ANS =-1) printf ("An error occurred while obtaining the top node of the stack! \ N "); elseprintf (" the top node of the stack is % d \ n ", ANS); break; Case 2: printf (" Enter the data you want to import into the stack :"); scanf ("% d", & dat); ans = sqstack_in (Q, dat); If (! Ans) printf ("failed to import stack \ n"); elseprintf ("successful import stack \ n"); break; Case 3: ANS = sqstack_out (Q ); if (ANS =-1) printf ("Stack output failed! \ N "); elseprintf (" Stack output successful! \ N "); break; Case 4: showsqstack (Q); break; Case 5: Return 0; break; default: printf (" the selection is invalid! \ N "); break;} return 1;} // void sqstack_setnull (sqstack * q) {q-> Top =-1; // when the stack is empty, the top pointer of the stack is-1} // judge whether the stack is empty int sqstack_empty (sqstack * q) {If (Q-> Top =-1) // If the stack top is-1, return 1 is blank; elsereturn 0;} // create sqstack * sqstack_create () {sqstack * q; int num, I; Q = (sqstack *) malloc (sizeof (sqstack); sqstack_setnull (Q); printf ("input the number of stack data to be created:"); scanf ("% d ", & num); If (Num> MAXSIZE-1) return NULL; printf ("sequential input stack data:"); for (I = 0; I <num; I ++) {scanf ("% d", & Q-> data [I]); q-> top ++;} return Q ;} // obtain the stack top int sqstack_gettop (sqstack * q) {If (sqstack_empty (q) // first determine whether the stack is empty and return-1; elsereturn Q-> data [q-> top];} // inbound stack int sqstack_in (sqstack * q, int dat) {If (Q-> Top = MAXSIZE-1) // determine whether the stack is full. Return 0; else {q-> top + = 1; q-> data [q-> top] = dat; return 1 ;}} // output stack int sqstack_out (sqstack * q) {If (sqstack_empty (q) // first, judge whether the stack is empty. Return-1; elsereturn Q-> data [q-> top --];} // The output shows the stack void showsqstack (sqstack * q) {int I; for (I = 0; I <= Q-> top; I ++) {printf ("% d", Q-> data [I]) ;}printf ("\ n ");}