数据结构实现-栈和队列

顺序栈

cpp 复制代码
#include <iostream>
using namespace std;
#define MaxSize 50

//顺序栈
template<typename ElemType>
struct SqStack{
    ElemType data[MaxSize];
    int top;
};

//初始化
template<typename ElemType>
void InitStack(SqStack<ElemType>&s){
    s.top=-1;
}

//判断栈空
template<typename ElemType>
bool StackEmpty(SqStack<ElemType>&s){
    return s.top==-1;
}

//进栈
template<typename ElemType>
bool Push(SqStack<ElemType>&s,ElemType x){
    if (s.top==MaxSize-1){
        return false;
    }
    s.data[++s.top]=x;
    return true;
}

//出栈
template<typename ElemType>
bool Pop(SqStack<ElemType>&s,ElemType&x){
    if (s.top==-1){
        return false;
    }
    x=s.data[s.top--];
    return true;
}

//读栈顶元素
template<typename ElemType>
bool GetTop(SqStack<ElemType>&s,ElemType&x){
    if (s.top==-1)
        return false;
    x=s.data[s.top];
    return true;
}

链栈

cpp 复制代码
#include <iostream>
using namespace std;
template<typename ElemType>
struct LinkNode{
    ElemType data;
    LinkNode<ElemType>*next;
};
template<typename ElemType>
struct LinkStack{
    LinkNode<ElemType>*front,*rear;
};

//初始化
template<typename ElemType>
void InitStack(LinkStack<ElemType> &S){
    S.front = S.rear = new LinkNode<ElemType>;
}

//判栈空
template<typename ElemType>
bool StackEmpty(LinkStack<ElemType>S){
    return S.front==S.rear;
}

//进栈
template<typename ElemType>
void Push(LinkStack<ElemType>&s,ElemType x){
    LinkNode<ElemType>*newNode = new LinkNode<ElemType>;
    newNode->data=x;
    newNode->next=s.rear;
    s.rear=newNode;
}

//出栈
template<typename ElemType>
bool Pop(LinkStack<ElemType>&s,ElemType&x){
    if (StackEmpty(s)){
        return false;
    }
    LinkNode<ElemType>*temp = s.rear;
    x = temp->data;
    s.rear = s.rear->next;
    delete temp;
    return true;
}

//读栈顶元素
template<typename ElemType>
bool GetTop(LinkStack<ElemType>&s,ElemType&x){
    if (StackEmpty(s))
        return false;
    x = s.rear->data;
    return true;
}

顺序队列

cpp 复制代码
#include <iostream>
using namespace std;
#define MaxSize 10
template<typename ElemType>
struct SqQueue{
    ElemType data[MaxSize];
    int front,rear;
};

//初始化
template<typename ElemType>
void InitQueue(SqQueue<ElemType>&Q){
    Q.front=Q.rear=0;//默认队列的头指针指向对头,尾指针指向尾部元素的下一个。
}

//判队空
template<typename ElemType>
bool isEmpty(SqQueue<ElemType>&Q){
    return Q.front==Q.rear;
}

//入队
template<typename ElemType>
bool EnQueue(SqQueue<ElemType>&Q,ElemType x){
    if ((Q.rear+1)%MaxSize==Q.front){
        return false;
    }
    Q.data[Q.rear] = x;
    Q.rear = (Q.rear+1)%MaxSize;
    return true;
}

//出队
template<typename ElemType>
bool DeQueue(SqQueue<ElemType>&Q,ElemType&x){
    if (Q.rear==Q.front)
        return false;
    x=Q.data[Q.front];
    Q.front = (Q.front+1)%MaxSize;
    return true;
}


int main(){
    SqQueue<int>sq;
    InitQueue(sq);
    for (int i = 0; i < 8; ++i) {
        EnQueue(sq,i);
    }
    int x;
    for (int i = 0; i < 3; ++i) {
        DeQueue(sq,x);
    }
    for (int i = 8; i < 10; ++i) {
        EnQueue(sq,i);
    }
    cout<<endl;
}

链队列

cpp 复制代码
#include <iostream>
using namespace std;
template<typename ElemType>
struct LinkNode{
    ElemType data;
    LinkNode<ElemType>* next;
};
template<typename ElemType>
struct LinkQueue{
    LinkNode<ElemType>*rear,*front;
};

//初始化
template<typename ElemType>
void InitQueue(LinkQueue<ElemType>&Q){
    Q.front = Q.rear = new LinkNode<ElemType>;
    Q.front->next = nullptr;
}

template<typename ElemType>
bool IsEmpty(LinkQueue<ElemType>&Q){
    return Q.front==Q.rear;
}

template<typename ElemType>
void EnQueue(LinkQueue<ElemType>&Q,ElemType x){
    LinkNode<ElemType>*s = new LinkNode<ElemType>;
    s->data = x;
    s->next= nullptr;
    Q.rear->next=s;
    Q.rear = s;
}

template<typename ElemType>
bool DeQueue(LinkQueue<ElemType>&Q,ElemType&x){
    if (Q.front==Q.rear)
        return false;
    LinkNode<ElemType>*p=Q.front->next;
    x=p->data;
    Q.front->next=p->next;
    if (Q.rear==p)
        Q.rear=Q.front;
    delete p;
    return true;
}
相关推荐
努力写代码的熊大29 分钟前
随机链表的复制数据结构oj题(力口138)
数据结构·链表
Am心若依旧4092 小时前
C++设计模式之创建型模式
java·开发语言·数据结构·c++·设计模式
手握风云-2 小时前
Java数据结构第二十五期:红黑树传奇,当二叉树穿上 “红黑铠甲” 应对失衡挑战
数据结构
努力努力再努力wz2 小时前
【c++深入系列】:万字详解list(附模拟实现的list源码)
运维·c语言·开发语言·数据结构·c++·list
减瓦2 小时前
极致简单的哈希表
java·数据结构
仟濹3 小时前
【数据结构】「栈」(顺序栈、共享栈、链栈)
c语言·数据结构·算法
凤年徐3 小时前
【数据结构】栈和队列-----数据结构中的双生花
c语言·开发语言·数据结构·c++·笔记·算法·链表
艾莉丝努力练剑13 小时前
【数据结构与算法】数据结构初阶:详解顺序表和链表(四)——单链表(下)
c语言·开发语言·数据结构·学习·算法·链表
笑衬人心。15 小时前
Hashtable 与 HashMap 的区别笔记
java·数据结构·笔记
秋说15 小时前
【PTA数据结构 | C语言版】根据层序序列重构二叉树
c语言·数据结构·算法