实现栈和队列的不同方法

标签: 队列 c++
89人阅读 评论(0) 收藏 举报
分类:

实现栈和队列的不同方法

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

//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.

查看评论

数据结构的Java实现——栈和队列

栈(Stack)作为一个先进后出(FILO) 的线性结构,只支持在栈顶的插入和弹出。队列(Queue)作为一个先进先出(FIFO) 的线性结构,支持在队首获取元素,在对尾插入元素。 栈的实现: p...
  • baoyiming1991
  • baoyiming1991
  • 2011-03-21 22:35:00
  • 6114

数据结构:栈和队列-迷宫问题求解

//--------------------文件名:Maze.cpp------------------------//----------------------By SunxySong------...
  • sunxysong
  • sunxysong
  • 2005-05-27 08:28:00
  • 5442

用栈和队列求解迷宫问题

1:①、定义一个顺序栈;     ②、编写走出迷宫代码;     ③、编写主函数。 1、栈: #include #include #include #include #define MaxSize ...
  • xdf0101
  • xdf0101
  • 2015-12-26 20:38:53
  • 1869

JavaScript的栈和队列

这次要介绍的东西是JavaScript中的栈和队列,我们都都知道JavaScript是一种弱类型的编程语言,从Array中更是体现出了这一点,我们可以在Array中插入一个字符串,也可以插入一个数字,...
  • LeyYang
  • LeyYang
  • 2016-03-28 10:24:58
  • 1677

java中栈和队列的实现和API用法

在java中要实现栈和队列,需要用到java集合的相关知识,特别是Stack、LinkedList等相关集合类型。 一、栈的实现 栈的实现,有两个方法:一个是用java本身的集合类型Stack类型;另...
  • ls5718
  • ls5718
  • 2016-06-17 16:09:29
  • 3660

java中堆栈和队列的实现方式

java中堆栈和队列的实现方式队列和栈区别:队列先进先出,栈先进后出。 队列的实现—利用Queue接口 堆栈的实现—利用Deque接口 使用ArrayDeque类实例化队列和栈 队列的实现—利用Que...
  • xiongmao_dada
  • xiongmao_dada
  • 2015-11-24 21:51:58
  • 411

栈和队列的实现

  • 2015年09月16日 20:14
  • 6KB
  • 下载

栈与队列的实现方法

如何实现栈? 后进先出 ~ 有两种方式:
  • xiaoxiaxiaen
  • xiaoxiaxiaen
  • 2017-04-08 17:21:27
  • 132

栈和队列的基本操作实现

抽象数据类型——栈 顺序存储结构:
  • Sometimes__
  • Sometimes__
  • 2014-11-10 19:10:26
  • 2872

Java数组实现堆栈和队列

数组堆栈: public class Stack { private long[] stackArray; private int top; private int maxSize; p...
  • lingzhm
  • lingzhm
  • 2015-03-11 18:31:11
  • 1303
    个人资料
    持之以恒
    等级:
    访问量: 3395
    积分: 168
    排名: 99万+
    文章分类