[算法学习笔记]数据结构之栈和队列

来源:互联网 发布:淘宝修改宝贝详情影响 编辑:程序博客网 时间:2024/05/01 08:55

栈是一种后进先出的数据结构
C++ 实现代码

#include <iostream>using namespace std;class Stack{private:    int *A;    int maxSize;    int curSize;public:    Stack();    Stack(int maxSize);    ~Stack();    bool isEmpty() const;    bool isFull() const;    int length() const;    void push(int n);    int pop();    void display();};int main(){    Stack stack(5);    int choose, n;    cout << "menu: 1> push   2> pop  3> display   0> eixt\nInput: ";    while(scanf("%d", &choose) && choose){        switch (choose) {            case 1:                cout << "Input: ";                cin >> n;                try{                    stack.push(n);                    cout << "Output: success!" << endl;                }catch(const char *s){                    cout << "Output: fail! " << s << endl;                }                break;            case 2:                try{                    n = stack.pop();                    cout << "Output: " << n << " is pop!" << endl;                }catch(const char *s){                    cout << "Output: fail! " << s << endl;                }                break;            case 3:                cout << "Output: ";                stack.display();                cout << endl;                break;            default:                printf("Output: error choose\n");                break;        }        cout << "Input: ";    }    cout <<"Output: Done!\n";    return 0;}Stack::Stack(){    maxSize = 10;    curSize = 0;    A = new int[maxSize];}Stack::Stack(int maxSize){    this->maxSize = maxSize;    curSize = 0;    A = new int[this->maxSize];}Stack::~Stack(){    delete [] A;}bool Stack::isEmpty() const {    return curSize == 0;}bool Stack::isFull() const {    return curSize == maxSize   ;}int Stack::length() const {    return curSize;}void Stack::push(int n){    if(isFull())        throw "Stack is full!";    A[curSize] = n;    curSize++;}int Stack::pop(){    if(isEmpty())        throw "Stack is empty!";    curSize--;    return A[curSize];}void Stack::display(){    for(int i = 0; i < curSize; i++)        cout << A[i] << " ";}

队列

队列是一种先进先出的数据结构
C++实现

#include <iostream>using namespace std;class Queue{private:    int *A;    int maxSize;    int curSize;public:    Queue();    Queue(int maxSize);    ~Queue();    bool isEmpty() const;    bool isFull() const;    int length() const;    void push(int n);    int pop();    void display();};int main(){    Queue queue(5);    int choose, n;    cout << "menu: 1> push   2> pop  3> display   0> eixt\nInput: ";    while(scanf("%d", &choose) && choose){        switch (choose) {            case 1:                cout << "Input: ";                cin >> n;                try{                    queue.push(n);                    cout << "Output: success!" << endl;                }catch(const char *s){                    cout << "Output: fail! " << s << endl;                }                break;            case 2:                try{                    n = queue.pop();                    cout << "Output: " << n << " is pop!" << endl;                }catch(const char *s){                    cout << "Output: fail! " << s << endl;                }                break;            case 3:                cout << "Output: ";                queue.display();                cout << endl;                break;            default:                printf("Output: error choose\n");                break;        }        cout << "Input: ";    }    cout <<"Output: Done!\n";    return 0;}Queue::Queue(){    maxSize = 10;    curSize = 0;    A = new int[maxSize];}Queue::Queue(int maxSize){    this->maxSize = maxSize;    curSize = 0;    A = new int[this->maxSize];}Queue::~Queue(){    delete [] A;}bool Queue::isEmpty() const {    return curSize == 0;}bool Queue::isFull() const {    return curSize == maxSize   ;}int Queue::length() const {    return curSize;}void Queue::push(int n){    if(isFull())        throw "Queue is full!";    A[curSize] = n;    curSize++;}int Queue::pop(){    if(isEmpty())        throw "Queue is empty!";    int front = A[0];    curSize--;    for(int i = 0; i < curSize; i++){        A[i] = A[i+1];    }    return front;}void Queue::display(){    for(int i = 0; i < curSize; i++)        cout << A[i] << " ";}

这两种数据结构比较简单就不特别说明了

1 0