Sequential stack operations-data structure (C ++) version,-Data Structure

Source: Internet
Author: User

Sequential stack operations-data structure (C ++) version,-Data Structure

Recently I learned about the data structure, and it was difficult to get started with the data structure. After a while, I began to learn again and suddenly felt a lot of interest. I have a new understanding of the use of these codes. The following is a brief description of the recently learned sequence stack. I don't know if you will feel it when you study it. The book is as simple as it is, but what I write is wrong, either this is wrong or there is something wrong. In the end, it is still overcome, and the psychological sense of accomplishment is gradually realized.

Question: enter a string of integers. If this number is not equal to-1, it is added to the stack. If it is equal to-1, the top integer of the stack is output and the stack is output. At the same time, the algorithm provides a response to exceptions.

Code Implementation

# Include <stdio. h>
# Include <stdlib. h>
# Include <malloc. h>
# Deprecision MAX 100
Typedef int ElemType;
Typedef struct
{
ElemType * top;
ElemType * base;
Int stacksize;
} SqStack;
// Initialize the stack
Bool InitStack (SqStack & S)
{
S. base = new ElemType [MAX];
If (! S. base) return false;
S. top = S. base;
S. stacksize = MAX;
Return true;

}
// Outputs the top element of the stack.
Char GetTop (SqStack S)
{
If (S. top! = S. base)
Printf ("Get the number of stacks: % d \ n", * (S. top-1 ));
}
// Stack entry
Bool Pop (SqStack & S, ElemType & e)
{
If (S. top = S. base) return false;
E = * -- S. top;
Printf ("Number of outgoing stacks: % d \ n", e );
Return true;
}
// Output 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 incoming stacks: % d \ n", e );
Return true;
}
// Judge whether it is null
Int IsEmpty (SqStack S)
{
If (S. top = S. base) return 1;
Else
Return 0;
}
// Determine whether 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) initialization \ n ");
InitStack (s );
Printf ("initialization successful: % 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 the number of inputs is-1 \ n ");
E =-1;
Push (s, e );
Printf ("(4) is the output stack empty as % s \ n", (IsEmpty (s )? "Null": "Not empty "));
Printf ("(5) is the output stack full % s \ n", (IsFull (s )? "Full": "Not full "));
Printf ("(6) destroy stack % s \ n", (DestroyStack (s )? "Successful": "failed "));
Return 0;
}

 

Problem:

1. base opera of-> has non-pointer type...

Sometimes an error is reported when the compilation software is in use->, but the modification to S. base is correct.

2. There is another symbol problem & which is different from *. Please check it out.

3. The remaining questions are not listed.

 

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.