Sequential stack operations--Data structure (c + +) edition

Source: Internet
Author: User

Recently learning data structure, the first touch feel good difficult, decadent a period of time, and then re-opened the study, suddenly feel a great interest. There are some new understandings of the use of these codes. Below is a brief talk about the latest learning of the sequence stack, do not know when you will have the feeling of learning, the book is so simple, but I write is not right, not here is wrong there is wrong, and finally conquered, the psychological slowly sense of accomplishment.

Topic: Enter a string of integers, if the number is not equal to 1, on the stack, if equal to 1, the output stack top integer and out of the stack. At the same time, the algorithm gives the situation of dealing with anomalies.

Code implementation

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAX 100
typedef int ELEMTYPE;
typedef struct
{
Elemtype *top;
Elemtype *base;
int stacksize;
}sqstack;
Give stack initialization
BOOL Initstack (Sqstack &s)
{
S.base=new Elemtype[max];
if (! S.base) return false;
S.top=s.base;
S.stacksize=max;
return true;

}
//output stack top element
Char GetTop (Sqstack S)
{
if (s.top!=s.base)
printf ("Get stack topness:%d\n", * (s.top-1));
}
//into the stack
BOOL Pop (sqstack &s,elemtype &e)
{
if (s.top==s.base) return false;
E=*--s.top;
printf ("Number of Stacks:%d\n", e);
return true;
}
//out of Stack
BOOL Push (sqstack &s,elemtype &e)
{
if (s.top-s.base==s.stacksize) return false;
if (e==-1)
{
GetTop (S);
Pop (s,e);
return true;
}
*s.top++=e;
printf ("Number of Stacks:%d\n", e);
return true;
}
//Determine if it is empty
int IsEmpty (Sqstack S)
{
if (s.top==s.base) return 1;
Else
return 0;
}
//To determine if it is full
int Isfull (Sqstack S)
{
if (s.top-s.base==s.stacksize) return 1;
Else
return 0;
}
//Destroy
int Destroystack (Sqstack &s)
{
if (s.base)
{
Delete s.base;
s.stacksize = 0;
S.base = S.top = NULL;
}
return 1;
}

Main function Section
int main ()
{
Sqstack s;
Elemtype e;
int i;
printf ("(1) initialize \ n");
Initstack (s);
printf ("Initialization succeeded:%s\n", (Initstack (s)? ") Yes: "no"));
printf ("(2) push:\n");
for (i = 0; i < 5;i++)
{
E=i;
Push (S, e);
}
printf ("(3) test: When input number is-1 o'clock \ n");
E=-1;
Push (S, e);
printf ("(4) output stack is empty for%s\n", (IsEmpty (s)? " Null ":" Non-Empty "));
printf ("(5) output stack is full%s\n", (Isfull (s)? " Full ":" Not Full "));
printf ("(6) Destroy Stack%s\n", (Destroystack (s)? " Success ":" Failure "));
return 0;
}

Problem:

1.base Opera of has non-pointer type ...

Sometimes compile software in use, and then error, but modified to s.base is correct

2. There is a little sign question & and * There is a difference, pay attention to see

3. The remaining questions are not listed.

Sequential stack operations--Data structure (c + +) edition

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.