数据结构之C++实现栈与队列的建立及插入删除操作

数据结构之C++实现栈与队列的建立及插入删除操作

栈的顺序存储结构

#include <iostream>
using namespace std;
#define MAXSIZE 50
class SqStack
{
public:
    SqStack();
    //~SqStack();
    void Push(int x);//在栈顶插入元素x
    void Pop(void);//删除栈顶元素
    void show(void);
private:
    int data[MAXSIZE];
    int top;
};
SqStack::SqStack()
{
    top=0;
}
void SqStack::Push(int x)
{
    if(top == MAXSIZE-1) //栈满
    {
        cout <<"SqStack is full"<<endl;
        exit(1);
    }
    data[top]=x;
    top++;
}
void SqStack::Pop(void)
{
    if(top == 0) //栈空
    {
        cout<<"SqStack is empty"<<endl;
        exit(1);
    }
    top--;
}
void SqStack::show(void)
{
    cout<<"stack is "<<endl;
    for(int i=0;i<top;i++) cout<<data[i]<<"  ";
    cout<<endl<<"length is "<<top<<endl;
}
int main()
{
    SqStack stack;
    stack.Push(1);
    stack.Push(2);
    stack.Push(3);
    stack.show();
    return 0;
}

栈的链式存储结构

#include <iostream>
using namespace std;
struct StackNode
{
    int data;
    StackNode* next;
};
class LinkStack
{
public:
    LinkStack();
    //~LinkStack();
    void Push(int x);
    void show(void);
    void Pop(void);
private:
    StackNode* top;
    int count;
};
LinkStack::LinkStack()
{
    top = new StackNode;
    top->next = NULL;
    count=0;
}
void LinkStack::Push(int x)
{
    StackNode* s=new StackNode;
    s->data=x;
    s->next=top->next;
    top->next = s;
    count++;
}
void LinkStack::show(void)
{
    StackNode* p=top->next;
    while(p!=NULL)
    {
        cout << p->data << " ";
        p = p->next;
    }
    cout<<endl<<"length is "<<count<<endl;
}
void LinkStack::Pop(void)
{
    if(top->next == NULL) 
    {
        cout<<"LinkStack is empty!!!"<<endl;
        exit(1);
    }
    StackNode* p=top->next;
    top->next=p->next;
    delete(p);
    count--;
}
int main()
{
    LinkStack* stack=new LinkStack;
    for(int i=0;i<10;i++) stack->Push(i);
    stack->show();
    for(int i=0;i<5;i++) stack->Pop();
    stack->show();
    return 0;
}

循环队列的顺序存储结构

#include <iostream>
using namespace std;
#define MAXSIZE 5
class SqQueue
{
public:
    SqQueue();//初始化队列
    int QueueLength(void);//获取队列长度
    bool isqueuefull(void);//判断队列是否为满
    void Insert(int x);//向队列队尾插入元素X
    void Delete(void);//删除队列对头一个元素
//private:
    int data[MAXSIZE];//队列
    int front;//头指针
    int rear;//尾指针
};
SqQueue::SqQueue()
{
    front=0;
    rear=0;
}
int SqQueue::QueueLength(void)
{
    return (rear-front+MAXSIZE)%MAXSIZE;
}
bool SqQueue::isqueuefull(void)
{
    if((rear+1)%MAXSIZE == front) return true;
    else return false;
}
void SqQueue::Insert(int x)
{
    if(isqueuefull()) 
    {
        cout<<"queue is full!!!"<<endl;
        exit(1);
    }
    data[rear]=x;
    rear=(rear+1)%MAXSIZE;
}
void SqQueue::Delete(void)
{
    if(front==rear)
    {
        cout<<"queue is empty!!!"<<endl;
        exit(1);
    }
    front=(front+1)%MAXSIZE;
}
int main()
{
    SqQueue* Queue = new SqQueue;
    Queue->Insert(26);
    Queue->Insert(26);
    Queue->Insert(26);
    Queue->Insert(26);
    cout<<"front is "<<Queue->front<<". rear is "<<Queue->rear<<endl;
    Queue->Delete();
    cout<<"front is "<<Queue->front<<". rear is "<<Queue->rear<<endl;
    if(Queue->isqueuefull()) cout<<"queue is full"<<endl;
    else cout<<"queue is not full"<<endl;
    return 0;
}

队列的链式存储结构

#include <iostream>
using namespace std;
struct Qnode
{
    int data;
    Qnode* next; //指向下一个节点
};
class LinkQueue
{
public:
    LinkQueue();//初始化队列
    //~LinkQueue();
    void Insert(int x);//向队尾插入元素X
    void show(void);
    void Delete(void);
private:
    Qnode* front;
    Qnode* rear;
    Qnode* first; 
    int length;
};
LinkQueue::LinkQueue()
{
    first = new Qnode;
    first->data=0;
    first->next=NULL;
    front = rear = first;
    length=0;
}
void LinkQueue::Insert(int x)
{
    Qnode* s= new Qnode;
    s->data = x; //赋值给新节点
    s->next = NULL;//作为新的尾节点指向为空
    rear->next=s;//更新队尾指针指向新的队尾节点
    rear=s;//把当前的S设置为队尾节点
    length++;
}
void LinkQueue::show(void)
{
    if(length == 0)
    {
        cout<<"LinkQueue is empty!!!"<<endl;
        exit(1);
    }
    cout<<"LinkQueue's length is "<<length<<endl;
    Qnode* p=front->next;
    cout<<"LinkQueue is "<<endl;
    while(p!=NULL)
    {
        cout<<p->data<<"  ";
        p=p->next;
    }
    cout<<endl;
}
void LinkQueue::Delete(void)
{
    if(front==rear)
    {
        cout<<"LinkQueue is empty!!!"<<endl;
        exit(1);
    }
    Qnode* p=front->next;
    front->next=p->next;
    if(rear==p) rear=front;//如果此时正好队尾是对头,则将队尾更新到对头
    delete(p);
    length--;
}
int main()
{
    LinkQueue* ptr=new LinkQueue;
    ptr->Insert(1);
    ptr->Insert(23);ptr->Insert(25);ptr->Insert(26);ptr->Insert(28);
    ptr->show();
    ptr->Delete();
    ptr->show();   
    return 0;
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早茶&&月光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值