Tag:return space element
seqstack.htypedef struct stack{data data[size+1]; //Data Elements int top; //Top }SeqStack; Seqstack *seqstackinit () {seqstack *p;if (p= (seqstack *) (malloc) (sizeof (Seqstack))) // Application stack memory {p->top = 0; //set stack top is zero return p; //returns pointer to stack }return null;} Int seqstackisempty (seqstack *s) //determine if the stack is empty {return (s->top==0);} Void seqstackfree (seqstack *s) //freed Stack (s) free (s); } void Seqstackclera (seqstack *s) // empty stack {s->top = 0; } int seqstackfull ( seqstack *s) //judgment stack is full {return (s->top==size) ;} Int seqstackpush (seqstack *s, data data) // into stack operation {if ((s->top+1) >size) { printf ("Stack Overflow!\n"); return 0;} s->data[++s->top] = data; //stack element into the stack return 1; } data seqstackpop ( seqstack *s) The stack operation {if (s->top==0) {printf ("Stack is empty!"); Exit (0);} return (s->data[s->top--]);} Data seqstackpeek (seqstack *s) // read stack top data {if (s->top==0) {printf ("Stack is empty! "); exit (0);} return (S->data[s->top]); }
seqstacktest.h#include<stdio.h> #include <stdlib.h> #define SIZE 50typedef Struct{char name[15];int age;} DATA; #include "SeqStack.h" int main () {Seqstack *stack;data data,data1;stack=seqstackinit ();//Initialize stack printf ("into stack operation: \ n"); printf ("Enter the name age for the stack operation:"); scanf ("%s%d", data.name,data.age); Seqstackpush (Stack,data);p rintf ("Enter the name age for the stack operation:"); scanf ("%s%d", data.name,data.age); Seqstackpush (Stack,data);p rintf ("\ nthe stack operation: \ n Press any key to do the stack operation:"), Getch ();d ata1=seqstackpop (Stack);p rintf ("The data for the stack is (%s,%d) \ n ", Data1.name,data1.age);p rintf (" Press any key for the stack operation "), Getch ();d ata1=seqstackpop (Stack);p rintf (" The data on the stack is (%s,%d) \ n ", Data1.name,data1.age); Seqstackfree (stack); Free space occupied by the stack getch (); return 0;}
Stack Learning Note 2