Basic operation of the 3_ sequence stack

Source: Internet
Author: User

/****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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.