数据结构顺序栈,链栈,顺序队列,链队的c++实现

数据结构顺序栈,链栈,顺序队列,链队的c++实现

#include <cstdio>
#include <windows.h>
#define ERR -1
typedef int ElmType;
struct Stack {
    ElmType *stk;
    int N;
    int top;
    int bottom;
    Stack(int n = 100) {
        N = n;
        top = 0;
        bottom = 0;
    }
    bool resize(int n) {
        N = n;
        stk = new ElmType[N];
        printf("容量为 %d 的栈已创建\n",n);
        return true;
    }

    int Top() {
        if(top == bottom) {
            printf("栈为空,取元素失败\n");
            return ERR;
        }else {

            return stk[top - 1];
        }   
    }
    bool push(ElmType x) {
        if(top - bottom == N) {
            printf("栈已满,无法再入栈\n");
            return false;
        }else {
            stk[top ++] = x;
            return true;
        }
    }
    bool pop(){
        if(top == bottom) {
            printf("此时栈为空,无法出栈\n");
            return ERR;
        }else {
            top --;
            return true;
        }
    }
    bool empty() {
        return top == bottom;
        // if(top == bottom) {
        //     printf("栈为空\n");
        //     return true;
        // }else {
        //     printf("栈不空\n");
        //     return false;
        // }
    }
    int size() {
        return top - bottom;
    }
    void show() {
        printf("当前栈中元素为:");
        for(int i = bottom;i < top;i ++) {
            printf("%d ",stk[i]);
        }
        puts("");
    }
};
struct SNode {
    ElmType date;
    SNode *next;
};
struct LinkStack{
    SNode *head;
    bool init() {
        head = new SNode;
        head->next = NULL;
        printf("初始化成功\n");
        return true;
    }
    ElmType top() {
        if(head->next == NULL) {
            printf("栈为空,取元素失败\n");
            return ERR;
        }else {
            return head->next->date;
        }
    }
    void show() {
        SNode *p = head->next;
        printf("当前栈中元素为:");
        while(p) {
            printf("%d ",p->date);
            p = p->next;
        }
        puts("");
    }
    void push(ElmType x) {
        SNode *p = new SNode;
        p->date = x;
        p->next = head->next;
        head->next = p;
    }
    void pop() {
        if(head->next == NULL) {
            printf("当前栈为空,弹出失败\n");
            return;
        }
        SNode *p = head->next;
        head->next = head->next->next;
        delete p;
    }
    bool empty() {
        // if(!head) return true;
        if(head->next == NULL) return true;
        return false;
    }
};
struct Queue {
    int N;
    int front,rear;
    ElmType *que;
    Queue(int n = 100) {
        N = 100;
        que = new ElmType[N];
        front = 0;
        rear = 0;
    }
    void resize(int n) {
        N = n;
        que = new ElmType[N];
        printf("容量为 %d 的队列已创建\n",n);
        return;
    }
    bool push(ElmType x) {
        if((rear + 1) % N == front) {
            printf("队列已满\n");
            return false;
        }else {
            que[rear] = x;
            rear = (rear + 1) % N;
            return true;
        }
    }
    bool pop() {
        if(rear == front) {
            printf("队列为空,出队失败\n");
            return false;
        }else {
            rear = (rear - 1 + N) % N;
            return true;
        }
    }
    void show() {
        printf("当前队列中元素为:");
        int now = front;
        while(now != rear) {
            printf("%d ",que[now]);
            now = (now + 1) % N;
        }
        puts("");
    }
    ElmType Front() {
        if(rear == front) {
            printf("此时队列为空,获取失败\n");
            return ERR;
        }else {
            return que[front];
        }
    }
    int size() {
        return (rear - front + N) % N;
    }
    bool empty() {
        return (rear == front);
    }
};
struct QNode {
    ElmType date;
    QNode *next;
};
struct LinkQueue {
    QNode *front,*rear;
    void init() {
        front = new QNode;
        front->next = NULL;
        rear = front;
        printf("创建成功\n");
    }
    void push(ElmType x) {
        QNode *p = new QNode;
        p->date = x;
        p->next = NULL;
        rear->next = p;
        rear = p;
    }
    void pop() {
        if(front == rear) {
            printf("当前队列为空,无法出队\n");
            return;
        }
        QNode *p = front->next;
        front->next = front->next->next;
        delete p;
        if(front->next == NULL) {
            rear = front;
        }
    }
    ElmType Front() {
        if(front == rear) {
            printf("队列为空,获取失败\n");
            return ERR;
        }else {
            return front->next->date;
        }
    }
    bool empty() {
        return (front == rear);
    }
    void show() {
        printf("当前队中元素为:");
        QNode *p = front->next;
        while(p) {
            printf("%d ",p->date);
            p = p->next;
        }
        puts("");
    }
};
void tip() {
    printf("请选择操作对象:\n");
    printf("顺序栈操作请按1\n");
    printf("链栈操作请按2\n");
    printf("顺序队列操作请按3\n");
    printf("链队操作请按4\n");
    printf("退出请输入-1\n");
}
void op1() {
    printf("1----创建顺序栈\n");
    printf("2----入栈操作\n");
    printf("3----出栈操作\n");
    printf("4----获取栈顶元素\n");
    printf("5----判断栈是否为空\n");
    printf("6----当前栈中元素个数\n");
    printf("7----显示当前栈中元素\n");
    printf("输入一个负数返回上层\n");
    Stack stk;
    while(1) {
        int t;
        scanf("%d",&t);
        if(t < 0) {
            break;
        }
        if(t == 1) {
            printf("请输入栈容量:");
            int n;
            scanf("%d",&n);
            stk.resize(n);
        }else if(t == 2) {
            printf("请输入要入栈元素:\n");
            ElmType x;
            scanf("%d",&x);
            stk.push(x);
            stk.show();
        }else if(t == 3) {
            stk.pop();
            stk.show();
        }else if(t == 4) {
            ElmType x = stk.Top();
            if(x != ERR)
                printf("当前栈顶元素为:%d\n",stk.Top());
        }else if(t == 5) {
            if(stk.empty()) {
                printf("当前栈为空\n");
            }else printf("当前栈不空\n");
        }else if(t == 6) {
            printf("当前栈中元素个数为 %d\n",stk.size());
        }else if(t == 7) {
            stk.show();
        }
    }
}
void op2() {
    printf("1----创建链栈\n");
    printf("2----入栈操作\n");
    printf("3----出栈操作\n");
    printf("4----获取栈顶元素\n");
    printf("5----判断栈是否为空\n");
    printf("6----显示当前栈中元素\n");
    printf("输入一个负数返回上层\n");
    LinkStack stk;
    while(1) {
        int t;
        scanf("%d",&t);
        if(t < 0) {
            break;
        }
        if(t == 1) {
            stk.init();
        }else if(t == 2) {
            printf("请输入要入栈元素:\n");
            ElmType x;
            scanf("%d",&x);
            stk.push(x);
            stk.show();
        }else if(t == 3) {
            stk.pop();
            stk.show();
        }else if(t == 4) {
            ElmType x = stk.top();
            if(x != ERR)
                printf("当前栈顶元素为:%d\n",stk.top());
        }else if(t == 5) {
            if(stk.empty()) {
                printf("当前栈为空\n");
            }else printf("当前栈不空\n");
        }else if(t == 7) {
            //printf("当前栈中元素个数为 %d\n",stk.size());
        }else if(t == 6) {
            stk.show();
        }
    }
}
void op3() {
    printf("1----创建顺序队列\n");
    printf("2----入队操作\n");
    printf("3----出队操作\n");
    printf("4----获取队头元素\n");
    printf("5----判断队是否为空\n");
    printf("6----显示当前队中元素\n");
    printf("输入一个负数返回上层\n");
    Queue q;
    while(1) {
        int t;
        scanf("%d",&t);
        if(t < 0) {
            break;
        }
        
        if(t == 1) {
            printf("请输入队列容量:");
            int n;
            scanf("%d",&n);
            q.resize(n);
        }else if(t == 2) {
            printf("请输入入队元素:");
            int x;
            scanf("%d",&x);
            q.push(x);
            q.show();
        }else if(t == 3) {
            q.pop();
            q.show();
        }else if(t == 4) {
            ElmType x = q.Front();
            if(x != -1) {
                printf("队首元素为:%d\n",q.Front());
            }
        }else if(t == 5) {
            if(q.empty()) {
                printf("队列为空\n");
            }else printf("队列不空\n");
        }else if(t == 6) {
            q.show();
        }
    }
}
void op4() {
    printf("1----创建链队列\n");
    printf("2----入队操作\n");
    printf("3----出队操作\n");
    printf("4----获取队头元素\n");
    printf("5----判断队是否为空\n");
    printf("6----显示当前队中元素\n");
    printf("输入一个负数返回上层\n");
    LinkQueue q;
    while(1) {
        int t;
        scanf("%d",&t);
        if(t < 0) {
            break;
        }
        
        if(t == 1) {
            q.init();
        }else if(t == 2) {
            printf("请输入入队元素:");
            int x;
            scanf("%d",&x);
            q.push(x);
            q.show();
        }else if(t == 3) {
            q.pop();
            q.show();
        }else if(t == 4) {
            ElmType x = q.Front();
            if(x != -1) {
                printf("队首元素为:%d\n",q.Front());
            }
        }else if(t == 5) {
            if(q.empty()) {
                printf("队列为空\n");
            }else printf("队列不空\n");
        }else if(t == 6) {
            q.show();
        }
    }
}
int main(){
    while(1) {
        tip();
        int t;
        scanf("%d",&t);
        if(t == -1) {
            break;
        }else if(t == 1) {
            op1();
        }else  if(t == 2) {
            op2();
        }else  if(t == 3) {
            op3();
        }else if(t == 4) {
            op4();
        }
        system("cls");
    }   
    return 0;
}
posted @ 2022-04-07 10:32  Mxrurush  阅读(48)  评论(0编辑  收藏  举报