实现栈和队列的不同方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36124194/article/details/78074401

实现栈和队列的不同方法

本博客通过顺序和链式两种方式分别去实现了栈和队列,仅供参考

//main.cpp
#include <iostream>
#include "source.hpp"
#include "arrayOfStack.hpp"
#include "arrayOfQueue.hpp"
#include "linkOfStack.hpp"
#include "LinkOfQueue.hpp"
#include <string>
using namespace std;

int main(){
    while(1){
        string commend;
        showMenu1();
        cout << "@user: ~$";
        cin >> commend;
        if(commend == "uas") {
            ArrayStack<string> *stack = new ArrayStack<string>();//a stack with data string
            while(1) {
                MenuStack();
                string commendOfArrayOfStack;
                cout << "@user: ~$";
                cin >>  commendOfArrayOfStack;
                if( commendOfArrayOfStack == "pu"){
                    cout <<  "Please enter the data(string) you want to push!" << endl;
                    cout << "@user: ~$";
                    string data;
                    cin >> data;
                    if(stack->push(data)) {
                        cout << "Push success!" << endl;
                    } else {
                        cout << "Push fail!" << endl;
                    }
                    cout << endl;
                } else if( commendOfArrayOfStack == "po") {
                    if(stack->pop()){
                        cout << "Pop success!" << endl;
                    } else {
                        cout << "Pop fail!" << endl;
                    }
                    cout << endl;
                } else if( commendOfArrayOfStack == "em") {
                    if(stack->empty()){
                        cout << "The stack is empty!" << endl;
                    } else {
                        cout << "The stack is not empty!" << endl;
                     }
                     cout << endl;
                } else if( commendOfArrayOfStack == "to") {
                    if(stack->empty()){
                        cout << "Warning! The stack is empty! You cannot pop anything!" << endl;
                    } else {
                        cout << "Top data is: " << stack->top() << endl;
                     }
                     cout << endl;
                } else if( commendOfArrayOfStack == "si") {
                    cout << "The stack size is " << stack->size() << endl;
                    cout << endl;
                } else if( commendOfArrayOfStack == "di") {
                    stack->display();
                } else if( commendOfArrayOfStack == "q") {
                    break;
                } else {
                    cout <<  "Commend error!" << endl << endl;
                }
            }
        } else if(commend == "uaq") {
            ArrayQueue<string> *queue = new ArrayQueue<string>();//a queue with data string
            while(1){
                MenuQueue();
                string commendArrayOfQueue;
                cout << "@user: ~$";
                cin >> commendArrayOfQueue;
                if(commendArrayOfQueue == "ap") {
                    if(queue->full()) {
                        cout << " Warning! The queue is fulled!" << endl;
                    } else {
                        cout << "Please enter the data you want to append!" << endl;
                        cout << "@user: ~$";
                        string data;
                        cin >> data;
                        if(queue->append(data)) {
                            cout << "Append success!" << endl;
                        } else {
                            cout << "Append fail!" << endl;
                        }
                    }
                    cout << endl;
                } else if(commendArrayOfQueue == "se") {
                    if(queue->empty()) {
                        cout << " Warning! The queue is empty!" << endl;
                    } else {
                        if(queue->serve()) {
                            cout << "Serve success!" << endl;
                        } else {
                            cout << "Serve fail!" << endl;
                        }
                    }
                    cout << endl;
                } else if(commendArrayOfQueue == "em") {
                    if(queue->empty()) {
                        cout << "The queue is empty!" << endl;
                    } else {
                        cout << "The queue is not enpty!" << endl;
                    }
                    cout << endl;
                } else if(commendArrayOfQueue == "re") {
                    if(queue->empty()) {
                        cout << "Warning! The queue is empty!" << endl;
                    } else {
                        cout << queue->retrieve() << endl;
                    }
                    cout << endl;
                } else if(commendArrayOfQueue == "si") {
                    cout << "The queue's size is " << queue->size() << endl;
                    cout << endl;
                } else if(commendArrayOfQueue == "di") {
                    queue->display();
                } else if(commendArrayOfQueue == "q") {
                    break;
                } else {
                    cout << "Commend error!" << endl;
                }

            }
        } else if(commend == "uls") {
            LinkStack<string> *stack = new LinkStack<string>();//a stack with data string
            while(1) {
                MenuStack();
                string commendOfLinkOfStack;
                cout << "@user: ~$";
                cin >>  commendOfLinkOfStack;
                if( commendOfLinkOfStack == "pu"){
                    cout <<  "Please enter the data(string) you want to push!" << endl;
                    cout << "@user: ~$";
                    string data;
                    cin >> data;
                    if(stack->push(data)) {
                        cout << "Push success!" << endl;
                    } else {
                        cout << "Push fail!" << endl;
                    }
                    cout << endl;
                } else if(commendOfLinkOfStack == "po") {
                    if(stack->pop()){
                        cout << "Pop success!" << endl;
                    } else {
                        cout << "Pop fail!" << endl;
                    }
                    cout << endl;
                } else if(commendOfLinkOfStack == "em") {
                    if(stack->empty()){
                        cout << "The stack is empty!" << endl;
                    } else {
                        cout << "The stack is not empty!" << endl;
                     }
                     cout << endl;
                } else if(commendOfLinkOfStack == "to") {
                    if(stack->empty()){
                        cout << "Warning! The stack is empty! You cannot pop anything!" << endl;
                    } else {
                        cout << "Top data is: " << stack->top() << endl;
                     }
                     cout << endl;
                } else if(commendOfLinkOfStack == "si") {
                    cout << "The stack size is " << stack->size() << endl;
                    cout << endl;
                } else if(commendOfLinkOfStack == "di") {
                    stack->display();
                } else if(commendOfLinkOfStack == "q") {
                    break;
                } else {
                    cout <<  "Commend error!" << endl << endl;
                }
            }
        } else if(commend == "ulq") {
            LinkQueue<string> *queue = new LinkQueue<string>();//a queue with data string
            while(1){
                MenuQueue();
                string commendLinkOfQueue;
                cout << "@user: ~$";
                cin >> commendLinkOfQueue;
                if(commendLinkOfQueue == "ap") {
                    cout << "Please enter the data you want to append!" << endl;
                    cout << "@user: ~$";
                    string data;
                    cin >> data;
                    if(queue->append(data)) {
                        cout << "Append success!" << endl;
                    } else {
                        cout << "Append fail!" << endl;
                    }
                    cout << endl;
                } else if(commendLinkOfQueue == "se") {
                    if(queue->empty()) {
                        cout << " Warning! The queue is empty!" << endl;
                    } else {
                        if(queue->serve()) {
                            cout << "Serve success!" << endl;
                        } else {
                            cout << "Serve fail!" << endl;
                        }
                    }
                    cout << endl;
                } else if(commendLinkOfQueue == "em") {
                    if(queue->empty()) {
                        cout << "The queue is empty!" << endl;
                    } else {
                        cout << "The queue is not enpty!" << endl;
                    }
                    cout << endl;
                } else if(commendLinkOfQueue == "re") {
                    if(queue->empty()) {
                        cout << "Warning! The queue is empty!" << endl;
                    } else {
                        cout << queue->retrieve() << endl;
                    }
                    cout << endl;
                } else if(commendLinkOfQueue == "si") {
                    cout << "The queue's size is " << queue->size() << endl;
                    cout << endl;
                } else if(commendLinkOfQueue == "di") {
                    queue->display();
                } else if(commendLinkOfQueue == "q") {
                    break;
                } else {
                    cout << "Commend error!" << endl;
                }

            }
        } else if(commend == "q") {
            break;
        } else {
            cout << "Commend error!" << endl << endl;
        }
    }


    return 0;
}
//source.hpp
#include <iostream>
using namespace std;
#ifndef SOURCE_HPP_
#define SOURCE_HPP_

#define MAXCOUNT 10

void showMenu1(){
    cout << "Using ArrayOfStack----------uas" << endl;
    cout << "Using LinkOfStack-----------uls" << endl;
    cout << "Using ArrayOfQueue----------uaq" << endl;
    cout << "Using LinkOfQueue-----------ulq" << endl;
    cout << "Exit------------------------q"  << endl;
}

void MenuStack(){
    cout << "Push------------pu" << endl;
    cout << "Pop-------------po" << endl;
    cout << "Empty-----------em" << endl;
    cout << "Top-------------to" << endl;
    cout << "Size------------si" << endl;
    cout << "Display---------di" << endl;
    cout << "Return----------q" << endl;
}

void MenuQueue(){
    cout << "Append----------ap" << endl;
    cout << "Serve-----------se" << endl;
    cout << "Empty-----------em"  << endl;
    cout << "Retrieve--------re" << endl;
    cout << "Size------------si" << endl;
    cout << "Display---------di" << endl;
    cout << "Return----------q" << endl;
}


#endif

//arrayOfStack.hpp
#include <iostream>
using namespace std;
#ifndef ARRAYOFSTACK_HPP_
#define ARRAYOFSTACK_HPP_

#define MAXCOUNT 10

template <class T>
class ArrayStack{
public: 
    ArrayStack();
    ~ArrayStack();
    bool push(T a);
    bool pop();
    T top();
    int size();
    bool empty();
    void display();
private:
    int count;
    T type[MAXCOUNT];
};

template <class T>
ArrayStack<T>::ArrayStack() {
    count = 0;
}

template <class T>
ArrayStack<T>::~ArrayStack() {
    ;
}

template <class T>
bool ArrayStack<T>::push(T a) {
    if(count >= 10){
        return false;
    } else {
        type[count++] = a;
        return true;
    }
}

template <class T>
bool ArrayStack<T>::pop() {
    if(empty()){
        return false;
    } else {
        count--;
        return true;
    }
}

template <class T>
T ArrayStack<T>::top() {
    return type[count-1];
}

template <class T>
bool ArrayStack<T>::empty() {
    if(count <= 0) {
        return true;
    } 
    return false;
}

template <class T>
void ArrayStack<T>::display() {
    for(int i = 0; i < count; i++) {
        if(i != count-1) {
            cout << type[i] << " -> ";
        } else{
            cout << type[i];
        }
    }
    cout << endl;
}

template <class T>
int ArrayStack<T>::size(){
    return count;
}



#endif
//LinkOfStack.hpp
#include <iostream>
using namespace std;
#ifndef LINKOFSTACK_HPP_
#define LINKOFSTACK_HPP_

#define maxqueue 10

template <class S>
class Node{
public:
    S data;
    Node<S> *next;
    Node();
    Node(S _data, Node *_next);
};

template <class S>
Node<S>::Node(){
    data = 0;
    next = NULL;
}

template <class S>
Node<S>::Node(S _data, Node *_next) {
    data = _data;
    next = _next;
}


//---------------------------------------------------
//class stack
template <class T>
class LinkStack{
public:
    LinkStack();
    ~LinkStack();
    bool empty();
    bool push(T _data);
    bool pop();
    T top();
    int size();
    void display();
private:
    Node<T> *top_node;
};

template <class T>
LinkStack<T>::LinkStack() {
    top_node = NULL;
}

template <class T>
bool LinkStack<T>::empty(){
    if(top_node == NULL){
        return true;
    }
    return false;
}

template <class T>
bool LinkStack<T>::push(T _data) {
    Node<T> *newNode = new Node<T>(_data, top_node);
    if(newNode == NULL){
        return false;
    }
    top_node = newNode;
    return true;
}

template <class T>
bool LinkStack<T>::pop() {
    Node<T> *old_top = top_node;
    if(top_node == NULL){
        return false;
    }
    top_node = old_top->next;
    delete old_top;
    return true;
}

template <class T>
T LinkStack<T>::top() {
    if(top_node == NULL){
        return 0;
    }
    return top_node->data;
}

template <class T>
LinkStack<T>::~LinkStack(){
    while(!empty()){
        pop();
    }
}

template <class T>
int LinkStack<T>::size() {
    int size = 0;
    Node<T> *temp_node = top_node; 
    while(temp_node) {
        size++;
        temp_node = temp_node->next;
    }
    return size;
}

template <class T>
void LinkStack<T>::display() {
    if(empty()) {
        cout << "This queue is empty!" << endl;
    } else {
        Node<T> *temp_node = top_node;
        int count = 0;
        while(temp_node) {
            if(count != size()-1) {
                cout << temp_node->data << " -> ";
                temp_node = temp_node->next;
                count++;
            } else {
                cout << temp_node->data;
                temp_node = temp_node->next;
            }

        }
        cout << endl;
    }
    cout << endl;
}


#endif
//arrayOfQueue.hpp
#include <iostream>
using namespace std;
#ifndef ARRAYOFQUEUE_HPP_
#define ARRAYOFQUEUE_HPP_

#define maxqueue 10

template <class T>
class ArrayQueue{
public:
    ArrayQueue();
    bool empty();
    bool full();
    bool serve();
    bool append(T _data);
    T retrieve();
    int size();
    void display();
private:
    int count;
    int front, rear;
    T data[maxqueue];
};

template <class T>
ArrayQueue<T>::ArrayQueue() {
    count = 0;
    front = 0;
    rear = maxqueue-1;
}

template <class T>
bool ArrayQueue<T>::empty() {
    if(count == 0){
        return true;
    }
    return false;
}

template <class T>
bool ArrayQueue<T>::full() {
    if(count == maxqueue) {
        return true;
    } 
    return false;
}

template <class T>
bool ArrayQueue<T>::serve() {
    if(count <= 0) {
        return false;
    }
    count--;
    front = (front+1)%maxqueue;
    return true;
}

template <class T>
bool ArrayQueue<T>::append(T _data) {
    if(count >= maxqueue){
        return false;
    }
    count++;
    rear = (rear+1)%maxqueue;
    data[rear] = _data;
    return true;
}

template <class T>
T ArrayQueue<T>::retrieve() {
    if(count <= 0){
        return 0;
    }
    return data[front];
}

template <class T>
int ArrayQueue<T>::size() {
    return count;
}

template <class T>
void ArrayQueue<T>::display() {
    if(empty()) {
        cout << "This queue is empty!" << endl;
    } else {
        for(int i = 0; i < size(); i++) {
            if(i != size() - 1)
                cout << data[(front+i)%maxqueue] << " -> ";
            else {
                cout << data[(front+i)%maxqueue];
            }
        }
        cout << endl;
    }
    cout << endl;
}

#endif
//LinkOfQueue.hpp
using namespace std;
#include "linkOfStack.hpp"
#ifndef LINKOFQUEUE_HPP_
#define LINKOFQUEUE_HPP_


template <class T>
class LinkQueue{
public:
    LinkQueue();
    ~LinkQueue();
//  LinkQueue(const Queue &original);
    bool empty();
    bool append(T data);
    bool serve();
    T retrieve();
    int size();
    void display();
//  void operator=(const Queue &original);
private:
    Node<T> *front, *rear;
    int count;
};

template <class T>
LinkQueue<T>::LinkQueue(){
    front = rear = NULL;
    count = 0;
}

template <class T>
LinkQueue<T>::~LinkQueue() {
    while(front != rear) {
        Node<T> *temp = front;
        front = front->next;
        delete temp;
    }
    delete front;
}

//template <class T>
//LinkQueue<T>::LinkQueue(const Queue &original);

template <class T>
bool LinkQueue<T>::empty() {
    if(count <= 0) {
        return true;
    }
    return false;
}

template <class T>
bool LinkQueue<T>::append(T data) {
    Node<T> *newNode = new Node<T>(data,NULL);
    if(newNode == NULL) {
        return false;
    }
    if(rear == NULL) {
        front = rear = newNode;
    } else {
        rear->next = newNode;
        rear = newNode;
    }
    count++;
    return true;
}

template <class T>
bool LinkQueue<T>::serve() {
    if(front == NULL) {
        return false;
    }
    Node<T> *oldNode = front;
    front = front->next;
    if(front == NULL) {
        rear = NULL;
    }
    delete oldNode;
    count--;
    return true;
}

template <class T>
T LinkQueue<T>::retrieve() {
    return front->data;
}

template <class T>
int LinkQueue<T>::size() {
    return count;
}

template <class T>
void LinkQueue<T>::display() {
    Node<T> *temp = front;
    for(int i=0;i < count; i++) {
        if(i != count-1) {
            cout << temp->data << " -> ";
        } else {
            cout << temp->data;
        }
        temp = temp->next;
    }
    cout << endl;   
}

#endif

Written by Vilin.

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页