Sequential stack of data structures (c + + version)

Source: Internet
Author: User

#include <iostream>
#include <stdlib.h>
#define MAXLISTSIZE 100//Preset storage space Max capacity
using namespace Std;
typedef string Elemtype;
typedef struct{
Elemtype *base; Storage space Base
int top; Stack top pointer
int stacksize; Maximum allowable storage space in elements
}stack;

void Initstack (Stack &s);
void Destroystack (Stack &s);
void Push (Stack &s);
void Pop (Stack &s);
void GetTop (Stack S);
void Stacklength (Stack S);
void Stackempty (Stack S);

int main (void)
{
Stack S;
int z;
cout << "+---------------------------+" << ' \ n ';
cout << "|----------order Stack-----------|" << ' \ n ';
cout << "+---------------------------+" << ' \ n ';
cout << "Tip: To ensure your operation is saved, please exit the system in normal order ^_^" << ' \ n ';
Do
{
cout << ' \ n ' << ' \ t ' << ' \ t ' << ' \ t ' << '--------------------------------' << ' \ n ';
cout << ' \ t ' << ' \ t ' << ' \ t ' << ' + main Menu | ' << ' \ n ';
cout << ' \ t ' << ' \ t ' << ' \ t ' << ' +--------------------------------' << ' \ n ';
cout << ' \ t ' << ' \ t ' << ' \ t ' << ' + [1]----sequence Stack initialization operation | "<< ' \ n ';
cout << ' \ t ' << ' \ t ' << ' \ t ' << ' + [2] destroy operations----sequential Stacks | "<< ' \ n ';
cout << ' \ t ' << ' \ t ' << ' \ t ' << ' + [3]----The stacking operation of the sequential stack | "<< ' \ n ';
cout << ' \ t ' << ' \ t ' << ' \ t ' << ' + [4]----stack operation on the order stack | "<< ' \ n ';
cout << ' \ t ' << ' \ t ' << ' \ t ' << ' + [5]----order stack top Element | ' << ' \ n ';
cout << ' \ t ' << ' \ t ' << ' \ t ' << ' + [6]----Order stack operation | "<< ' \ n ';
cout << ' t ' << ' \ t ' << ' \ t ' << ' + [7] empty operation----Order stack | "<< ' \ n ';
cout << ' \ t ' << ' \ t ' << ' \ t ' << ' + [0]----exit System | "<< ' \ n ';
cout << ' \ t ' << ' \ t ' << ' \ t ' << '-----------------------------------' << ' \ n ';
cout << "Please enter your choice:";
Cin >> Z;
Switch (z)
{
Case 0:break;
Case 1:initstack (S);
Case 2:destroystack (S);
Case 3:push (S);
Case 4:pop (S);
Case 5:gettop (S);
Case 6:stacklength (S);
Case 7:stackempty (S);
Default:cout << "Invalid option!" << ' \ n '; system ("pause");
}
}
while (z!= 0);
}

void Initstack (Stack &s)
{
Constructs an empty stack with a maximum storage capacity of maxsize S
int maxsize;
cout << "Please enter the maximum storage capacity of the stack:";
Cin >> MaxSize;
if (maxsize = = 0)
MaxSize = maxlistsize;
S.base = new Elemtype[maxsize];
if (! S.base) exit (1); Storage Allocation failure
S.stacksize = maxsize;
S.top = 0;//The number of elements in the empty stack is 0
System ("pause");
}

void Push (Stack &s)
{
Elemtype e;
Int J, N;
if (s.stacksize = = 0)
{
cout << "stack destroyed or stack uninitialized, select 1 initialization Stack" << Endl;
System ("pause");
Return
}
if (s.top = = s.stacksize)
{
cout << "Stack is full, unable to insert" << Endl;
System ("pause");
Return
}
cout << "Please enter the number of elements pressed into the stack (the number of elements must be less than or equal to" << s.stacksize-s.top << "):";
CIN >> N;
cout << "Enter the elements of the push stack:";
for (j = 0; J < N; j + +)
{
CIN >> E;
* (S.base + s.top) = e; Insert a new element
++s.top; Stack top pointer moves back
}
System ("pause");
}

void Pop (Stack &s)
{
Elemtype e;
Int J, N;
if (s.stacksize = = 0)
{
cout << "stack destroyed or stack uninitialized, select 1 initialization Stack" << Endl;
System ("pause");
Return
}
if (S.top = = 0)
{
cout << "Stack is empty, unable to stack" << Endl;
System ("pause");
Return
}
cout << "Please enter the number of elements in the stack (the number of elements in the stack should be less than or equal to" << s.top << "):";
CIN >> N;
cout << "The elements of the stack are:";
for (j = 0; J < N; j + +)
{
E = * (S.base + s.top-1); Returns the top element of the stack in a non-empty stack
cout << e << ';
--s.top; Stack top pointer move forward
}
cout << Endl;
System ("pause");
}

void GetTop (Stack S)
{
Elemtype e;
if (s.stacksize = = 0)
{
cout << "stack destroyed or stack uninitialized, select 1 initialization Stack" << Endl;
System ("pause");
Return
}
if (S.top = = 0)
{
cout << "Stack is empty, unable to get stack top element" << Endl;
System ("pause");
Return
}
E = * (S.base + s.top-1); Returns the top element of the stack in a non-empty stack
cout << "Stack top elements are:" << e << Endl;
System ("pause");
}

void Stackempty (Stack S)
{
if (s.stacksize = = 0)
{
cout << "stack destroyed or stack uninitialized, select 1 initialization Stack" << Endl;
System ("pause");
Return
}
if (S.top = = 0)
cout << "The stack is empty" << Endl;
Else
cout << "This sequence stack is not empty stack" << Endl;
System ("pause");
}

void Stacklength (Stack S)
{
if (s.stacksize = = 0)
{
cout << "stack destroyed or stack uninitialized, select 1 initialization Stack" << Endl;
System ("pause");
Return
}
Returns the number of elements of S, that is, the length of the stack
cout << "stack length:" << s.top << Endl;
System ("pause");
}

void Destroystack (Stack &s)
{
Destroy Stack s,s no longer exists
if (s.stacksize = = 0)
{
cout << "stack destroyed or stack uninitialized, select 1 initialization Stack" << Endl;
System ("pause");
Return
}
Delete[] S.base;
s.top = 0;
s.stacksize = 0;
cout << "stack destroyed" << Endl;
System ("pause");
}

Sequential stack of data structures (c + + version)

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.