/****date:2014.12.08*****/
The basic operation of the/*** sequence stack ***//*** last on First Out (LIFO) ***/
Sequential stack: Using contiguous memory cells to save the data in the stack, you can define a structure array of a specified size as a stack, the stack base element ordinal is 0, the top element of the stack is top;
The elements in the stack follow the "LIFO" principle, which operates only at one end of the stack, that is, the elements inside the stack at the top of the stack.
Just understand the truth: GetChar () for all the keyboard operation is counted, hitting the ENTER key is also counted as an input signal.
#define MaxLen 3
typedef struct
{
Char name[10];
int age;
}data;
typedef struct STACK
{
DATA data[maxlen+1];
int top;
}stacktype;
Initialize stack
Stacktype * Sinit ()
{
Stacktype * p;
if (p= (Stacktype *) malloc (sizeof (Stacktype)));
{
p->top=0;
return p;
}
return NULL;
}
Judging the empty stack
int Sisempty (Stacktype * s)
{
int t;
T= (s->top==0);
return t;
}
Judge Full stack
int Sisfull (Stacktype * s)
{
int t;
T= (S->top==maxlen);
return t;
}
Empty stack
void Sclear (Stacktype * s)
{
s->top=0;
}
Free space
void Sfree (Stacktype * s)
{
if (s)
{
Free (s);
}
}
In-stack operation
int Spush (Stacktype * s,data DATA)
{
if (S->top+1>maxlen)
{
printf ("Stack overflow \ n");
return 0;
}
Else
{
s->data[++s->top]=data;
return 1;
}
}
Out of stack operation
DATA SPop (Stacktype * s)
{
if (s->top==0)
{
printf ("Stack is empty \ n");
System ("pause");
return 0;
Exit (0);
}
Else
{
Return (s->data[s->top--]);
}
}
Read Stack top information
DATA Sgettop (Stacktype * s)
{
if (s->top==0)
{
printf ("Stack is empty \ n");
return 0;
Exit (0);
}
Else
{
Return (S->data[s->top]);
}
}
Basic operation of the 3_ sequence stack