Implementation of sequential storage and chained storage for C + + stacks

Source: Internet
Author: User

Stack is the most common data structure, which is characterized by a LIFO (last on First out) is also a special form of the list, so as with the chain list, there are two ways of storage, the first is the sequential storage stack, easy to read and write data, but the length of the stack must be fixed first, the second is a chain storage stack, Can not define the length of the stack, you can insert a large number of data, if not the physical memory used, you can store a large amount of data.

First, the implementation of the sequential stored stack, the code is as follows:

#pragma once#define MAXSIZE 10template<typename eletype>class orderstack{public:orderstack (); ~OrderStack (); BOOL GetTop (eletype& e); bool Push (const eletype& e); bool Pop (eletype& e); void Show () const;private:eletype Data[maxsize];int top;bool Empty () Const;bool full () const;int stacklength () const;};

#include "OrderStack.h" #include <iostream>using namespace Std;template<typename eletype>bool orderstack <eletype&gt::P op (eletype& e) {if (Empty ()) {cout << "the stack is empty!\n"; return false;} e = Data[top];--top;return true;} Template<typename Eletype>bool orderstack<eletype>::P ush (const eletype& e) {if (full ()) {return false;} ++top;data[top] = E;return true;} Template<typename Eletype>bool orderstack<eletype>::gettop (eletype& e) {if (Empty ()) {return false;} e = Data[top];return true;} Template<typename Eletype>orderstack<eletype>::~orderstack () {}template<typename EleType> Orderstack<eletype>::orderstack (): Top ( -1) {}template<typename eletype>int orderstack<eletype>:: Stacklength () Const{return top + 1;} Template<typename eletype>bool orderstack<eletype>::full () const{return (top = = MAXSIZE-1);} Template<typename eletype>bool Orderstack<eletype>::empty () const{return (top = =-1);}template<typename eletype>void orderstack<eletype>::show () const{if (Empty ()) {cout << "the stack is Empty!\n "; return;} cout << "The stack length is:" << stacklength () << endl;cout << ' The stack is: '; for (int i = Top ; I >= 0;--i) {cout << data[i] << "";} cout << Endl;}

Second, the stack of chained storage, the code is as follows:


#pragma oncetemplate<typename eletype>class chainstack{public:chainstack (); ~chainstack (); bool GetTop (EleType & e); bool Push (const eletype& e); bool Pop (eletype& e); void Show () const;private:bool Empty () const;//bool Full () Const;int stacklength () const;struct node{eletype data; Node* Next; Node () {next = nullptr;}}; node* top;int length;};

#include "ChainStack.h" #include <iostream>using namespace Std;template<typename eletype>int chainstack <eletype>::stacklength () Const{return length;} Template<typename eletype>//bool Chainstack<eletype>::full () const//{////}template<typename Eletype>bool Chainstack<eletype>::empty () const{return (length = = 0);} Template<typename eletype>void chainstack<eletype>::show () const{if (Empty ()) {cout << "the stack is Empty!\n "; return;} cout << "The stack length" << length << endl;cout << "the stack is"; node* temp=top;for (int i = 1; I <= length;++i) {cout << temp->data << ""; temp = temp->next;} cout << Endl;} Template<typename Eletype>bool Chainstack<eletype>::P op (eletype& e) {if (Empty ()) {cout << "the Stack is empty!\n "; return false;} E = top->data; node* temp = Top;top = Top->next;--length;delete Temp;return true;} Template<typename Eletype>bool CHainstack<eletype&gt::P ush (const eletype& e) {node* temp = new Node;temp->data = E;if (Empty ()) {top = temp;} Else{temp->next = Top;top = temp;} ++length;return true; }template<typename Eletype>bool chainstack<eletype>::gettop (eletype& e) {if (Empty ()) {cout << " The stack is empty!\n "; return false;} e = Top->data;return true;} Template<typename Eletype>chainstack<eletype>::~chainstack () {node* temp = top;while (top) {temp = Top;top = Temp->next;delete temp;}} Template<typename eletype>chainstack<eletype>::chainstack (): Length (0), top (nullptr) {}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Implementation of sequential storage and chained storage for C + + stacks

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.