C + + implementation of the basic chain stack operation (with the main function can be compiled to run) __ function

Source: Internet
Author: User

Data structure teacher arranged the basic operation of the chain stack, let us try to use class to achieve, and then I refer to a part of the internet, I wrote a, there are deficiencies, I implore you to point out


* * File name: Chain stack. c
* Implementation of the chain stack
* Version: 2.0
* Time: 2016.11.4
* Author: Wangyan
* Suppose the condition: assuming that the base type of the element is int, and that only one integer information per element needs to be pressed stack
*
*/

#include <iostream>
#define OK 1
#define ERROR-1

using namespace Std;

typedef int ELEMTYPE;
typedef struct STACKNODE
{
Elemtype data;
struct Stacknode *next;
}snode;

Class Linkstack
{
Public
Linkstack ();
int Push (elemtype data); Into the stack
int Pop (); Out Stack elemtype &data
int clear (); Empty chain Stack
BOOL IsEmpty (); Short sentence
int print () const; Print the elements in the chain stack

~linkstack (); Deconstruction, destruction
static int Count; Size of storage stack
Snode *top; Stack top pointer
Protected

};

/** initialization Stack Length * *
int linkstack::count = 0;


/***********************
Initializing chain stacks
Calling constructors
***********************/
Linkstack::linkstack ()
{
top = NULL; top = NULL indicates that the chain stack is empty
}


/***********************
Destroy Chain Stack
destructor
***********************/
Linkstack::~linkstack ()
{
Snode *p = NULL;
while (top)//Loop release node
{
p = top->next;
Delete top;
top = p;
}
}


/***********************
In-stack operation
Push
Data is in the stack
***********************/
int Linkstack::P ush (elemtype data)
{
Snode *snew = new Snode; Create a new node

Snew->data = data; New node Assignment
Snew->next = top; New stack top pointing to old stack top
top = snew; Top point to new stack
Count + +;

return OK;
}


/***********************
Out stack operation
Pop
***********************/
int Linkstack::P op ()
{
if (!top)
return ERROR;
if (isempty () = = 1)
{
cout<< "Stack is empty" <<endl;
return OK;
}

Snode *p = top->next; Pre-stored pointer to next node
data = top->data; Returns the top element of the stack to the main function using

Delete top; Free Stack Top space
top = p; Stack top Move Down
Count--;

return OK;
}

/***************
Determine if the chain stack is empty
IsEmpty
****************/
BOOL Linkstack::isempty ()
{
return (top = NULL); The top of the stack is empty, return 1, NOT NULL return 0
}

/***********************
Empty chain Stack
Clear
Form-and-destructor functions
***********************/
int Linkstack::clear ()
{
Snode *p;
while (top)//Loop release node
{
p = top;
top = top->next;
Delete p;
}
return OK;
}

/***********************
Print the elements in the chain stack
Print
Print element values by traversing the chain stack
***********************/
int Linkstack::P rint () const
{
Snode *p = top;
while (P && Count)//stack is not empty && there is data in the stack
{
cout<<p->data<<ends<<ends; Traverse the data in the print stack
p = p->next;
}
return OK;
}

int main ()
{
Linkstack ls; Define Object ls
int x; Select number and enter the element value of the chain stack

cout<< "*******************************" <<endl;
cout<< "Please select the action to be performed:" <<endl;
cout<< "Initialization" <<endl;
cout<< "1: Press stack" <<endl;
cout<< "2: Out of Stack" <<endl;
cout<< "3: Determine whether the chain stack is empty" <<endl;
cout<< "4: Empty chain stack" <<endl;
cout<< "5: Display chain stack Length" <<endl;
cout<< "6: Print chain stack" <<endl;
cout<< "Destroy chain Stack" <<endl;
cout<< "*******************************" <<endl;
cout<< "Please enter the action to be performed:" <<endl;

while (CIN&GT;&GT;X)
{
Switch (x)
{
Case 1:
cout<< "Input pressure stack data: (end input 0)" <<endl;
cin>>x;
while (x)
{
Ls. Push (x);
cin>>x;
}

cout<<endl<< "Please enter next action:";
Break
Case 2:
if (Ls.isempty ())
cout<< "Chain stack is empty stack" <<endl;
while (Ls.top)
{
Ls. Pop ();
}
cout<<endl<< "Please enter next action:";
Break
Case 3:
if (Ls.isempty ())
cout<< "Chain stack is empty stack" <<endl;
Else
cout<< "Chain stack is not empty" <<endl;

cout<<endl<< "Please enter next action:";
Break
Case 4:
Ls. Clear ();
cout<<endl<< "Please enter next action:";
Break
Case 5:
Cout<<ls. count<<endl;
cout<<endl<< "Please enter next action:";
Break
Case 6:
if (Ls.isempty ())
cout<< "Chain stack is empty stack" <<endl;
Else
Ls.print ();
cout<<endl<< "Please enter next action:";
Break
Default:break;
}

}


return 0;
}

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.