Teaching Purpose: To grasp the storage representation of stacks and the implementation of Stack basic operation
Teaching Focus: The basic operation of the stack implementation method, stack application
Teaching Difficulty: storage representation of stacks
Experiment Content:
The realization of a stack
Implement sequential storage of stacks.
Stack Implementation sample
#include <stdio.h> #include <malloc.h> #include <conio.h> #define ERROR 0 #define TRUE 1 #define FALSE 0 # Define OK 1 #define EQUAL 1 #define OVERFLOW-1 #define STACK_INIT_SIZE #define STACKINCREMENT a typedef int STATUS
;
struct stu{char name[20];
Char stuno[10];
int age;
int score;
};
typedef struct STU Selemtype;
struct STACK {selemtype *base;
Selemtype *top;
int stacksize;
};
typedef struct STACK Sqstack;
typedef struct STACK *psqstack;
Status Initstack (Sqstack **s);
Status Destroystack (Sqstack *s);
Status Clearstack (Sqstack *s);
Status stackempty (Sqstack S);
int Stacklength (Sqstack S);
Status GetTop (Sqstack s,selemtype *e);
Status Push (Sqstack *s,selemtype e);
Status POPs (Sqstack *s,selemtype *e);
Status Stacktraverse (Sqstack s,status (*visit));
Status Initstack (Sqstack **s) {(*s) = (Sqstack *) malloc (sizeof (sqstack));
(*s)->base= (Selemtype *) malloc (stack_init_size *sizeof (Selemtype)); if (!) (
*s)->base) exit (OVERFLOW); (*s)->top= (*s)->base;
(*s)->stacksize=stack_init_size;
return OK;
Status Destroystack (Sqstack *s) {free (s->base);
Free (S);
Status Clearstack (Sqstack *s) {s->top=s->base;}
Status stackempty (Sqstack S) {if (s.top==s.base) return TRUE;
else return FALSE;
int Stacklength (Sqstack S) {int i;
Selemtype *p;
i=0;
P=s.top;
while (p!=s.base) {p++;
i++;
Status GetTop (sqstack s,selemtype *e) {if (s.top==s.base) return ERROR;
*e=* (s.top-1);
return OK; Status Push (Sqstack *s,selemtype e) {/* if (s->top-s->base>=s->stacksize) {s->base= (Selem
Type *) realloc (s->base, (s->stacksize + stackincrement) * sizeof (Selemtype)); if (!
s->base) exit (OVERFLOW);
s->top=s->base+s->stacksize;
S->stacksize + = stackincrement;
} * * * (s->top++) =e;
return OK;
Status Pop (sqstack *s,selemtype *e) {if (s->top==s->base) return ERROR;
*e=*--s->top; RetuRN OK;
Status Stackprintelem (Selemtype * e) {printf ("%s%s%d%d\n", E->name,e->stuno,e->age,e->score);}
Status Stacktraverse (Sqstack s,status (*visit) ()) {while (s.top!=s.base) visit (--s.top);}
Main () {Selemtype E;
Sqstack *sa;
CLRSCR ();
printf ("\ n \ nthe-------------------sqstack Demo is running ...----------------\ n");
printf ("The ' is Push function.\n");
Initstack (&SA);
strcpy (e.name, "stu1");
strcpy (E.stuno, "100001");
e.age=80;
e.score=1000;
printf ("Now Stack is empty.\n");
Stacktraverse (*sa,stackprintelem);
Push (sa,e);
printf ("Now Stack has one element.\n");
Stacktraverse (*sa,stackprintelem);
strcpy (e.name, "stu3");
strcpy (E.stuno, "100002");
e.age=80;
e.score=1000;
Push (sa,e);
printf ("Now Stack has another element.\n");
Stacktraverse (*sa,stackprintelem);
printf ("Now pops stack,the top elem put into variable e.\n");
Pop (sa,&e); printf ("%s\n%s\n%d\n%d\n", E.name,e.stuno,e.age,e.scoRE);
printf ("Let's" is the left of Stack ' s elem:\n ");
Stacktraverse (*sa,stackprintelem);
Getch ();
printf ("\n\n\nwelcom to visit http://zmofun.topcool.net\n\n"); }