Sequential Stack example of C + + data structure and algorithm

Source: Internet
Author: User

Stack, top of stack, bottom of stack, like a quilt, advanced upon the
SeqStack.h

#define STACKSIZE 100
typedef struct
{
DataType Stack[stacksize];
int top;
}seqstack;

void Initstack (Seqstack *s)
* * to initialize stack to empty stack just put the top of the stack pointer to 0*/
{
s->top=0; * * Put the top of the stack pointer to 0*/
}
int Stackempty (Seqstack S)
/* To determine whether the stack is empty, the stack is empty return 1, otherwise return 0*/
{
if (s.top==0)/* Determine if top of the stack pointer is 0*/
return 1; /* When the stack is empty, return 1; otherwise return 0*/
Else
return 0;
}
int GetTop (Seqstack S, DataType *e)
/* Take the top element of the stack. Returns the top element value of the stack to E and returns 1 to indicate success, otherwise 0 indicates failure. */
{
if (s.top<=0)/* Before taking the top element of the stack, determine if the stack is empty * *
{
printf ("Stack already empty!\n");
return 0;
}
Else
{
*E=S.STACK[S.TOP-1]; /* In the top of the stack elements * *
return 1;
}
}
int Pushstack (Seqstack *s,datatype e)
/* The element e into the stack, the element into the stack successfully returned 1, otherwise return 0.*/
{
if (s->top>=stacksize)/* Before the element is put into the stack, determine if the stack is full * *
{
printf ("Stack is full, not into the stack!") \ n ");
return 0;
}
Else
{
s->stack[s->top]=e; /* element e into the stack * *
s->top++; /* Modify Stack top pointer * *
return 1;
}
}
int Popstack (seqstack *s,datatype *e)
/* out stack operation. Stack the top element of the stack and assign it to E. The stack returns 1 successfully, otherwise it returns 0*/
{
if (s->top<=0)/* element is out of stack, determine if the stack is empty * *
{
printf ("Stack has no elements, can not be out of stack!\n");
return 0;
}
Else
{
s->top--; /* First modify the top of the stack pointer, that is out stack * *
*e=s->stack[s->top]; /* Assign the stack element to the e*/
return 1;
}
}
int Stacklength (Seqstack S)
* To find the length of the stack, that is, the number of elements in the stack, the top of the stack pointer value is equal to the number of elements in the stack * *
{
return s.top;
}

Main.c

#include <stdio.h>
#include <stdlib.h>
typedef char DataType;
#include "SeqStack.h"

int main (void)
{
Seqstack S;
int i;
DataType a[]={' A ', ' B ', ' C ', ' d ', ' e '};
DataType e;
Initstack (&s); Initialize stack
For (I=0;i<sizeof (a)/sizeof (a[0]); i++)
{
if (Pushstack (&s,a[i]) ==0)
{
printf ("Stack is full, can't go into stack!");
return 0;
}
}

printf ("Elements of the Stack:");
if (Popstack (&s,&e) ==1)
printf ("%4c", e);
if (Popstack (&s,&e) ==1)
printf ("%4c\n", e);
printf ("Current stack top element is:");
if (GetTop (s,&e) ==1)
printf ("%4c\n", e);

if (Pushstack (&s, ' F ') ==0)
{
printf ("Stack is full, cannot enter stack!\n");
return 0;
}
if (Pushstack (&s, ' G ') ==0)
{
printf ("Stack is full, cannot enter stack!\n");
return 0;
}

printf ("Number of elements in current stack:%d\n", Stacklength (S));
printf ("Sequence of elements out stack:");
while (! Stackempty (S))
{
Popstack (&s,&e);
printf ("%4c", e);
}
printf ("\ n");

return 0;
}

Related Article

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.