数据结构第二章--栈和队列

使用语言:c++

1.什么是栈?

栈的特点:先进后出
栈是一种只能在一端进行插入或删除操作的线性表
线性表:栈的逻辑结构属于线性表,只不过在操作上加了一些约束。
一端:可以插入或者删除元素的一端叫栈顶,另一端叫栈底

2.顺序栈

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
#define maxSize 10

void push(int* a,int &top,int elem){
    a[++top]=elem;
}

void pop(int *a,int &top){
    if(top==-1){
        cout<<"空栈"<<endl;
        top=-1;
    }else
        cout<<"pop:"<<a[top--]<<endl;
}

int main(){
    int arr[maxSize];
    int top=-1;
    push(arr,top,5);
    push(arr,top,4);
    push(arr,top,3);
    pop(arr,top);
    pop(arr,top);
    return 0;
}

数组大小动态增加型,当数组的满栈时,使用stackFull函数,把内存变成原来的两倍。

#include<iostream>
using namespace std;


typedef struct
{
    int *data;
    int top;
    int maxSize;
}sqStack;

int push(sqStack &st,int x);
int stackFull(sqStack &s);
int pop(sqStack &st,int &x);
int getTop(sqStack &s,int &x);

int push(sqStack &st,int x)
{
    if(st.top==st.maxSize-1)
    {
        if(stackFull(st)==0)
            return 0;
    }
    st.data[++st.top]=x;
    return 1;
}

int pop(sqStack &st,int &x)
{
    if(st.top==-1)
        return 0;
    x=st.data[st.top--];
    return 1;
}

int getTop(sqStack &s,int &x)
{
    if(s.top==-1)
        return 0;
    x=s.data[s.top];
    return 1;
}

int stackFull(sqStack &s)
{
    int *temp=(int*)malloc(2*s.maxSize*sizeof(int));
    if(temp==NULL)
        return 0;
    for(int i=0;i<=s.top;i++)
        temp[i]=s.data[i];
    free(s.data);
    s.data=temp;
    cout<<s.maxSize<<endl;
    s.maxSize=2*s.maxSize;
    cout<<s.maxSize<<endl;
    return  1;
}

int main()
{
    sqStack s;
    int x;
    s.maxSize=4;
    s.top=-1;
    s.data=(int*)malloc(s.maxSize*sizeof(int));
    push(s,100);
    push(s,90);
    push(s,80);
    push(s,80);
    push(s,70);
    pop(s,x);
    cout<<x<<endl;
    pop(s,x);
    getTop(s,x);
    cout<<x<<endl;
}

3.链栈

#include <bits/stdc++.h>
using namespace std;

typedef struct LNode{
    int data;
    struct LNode* next;
}LNode;

void push(LNode *&head,int elem){
    LNode *top=new LNode();
    top->data=elem;
    top->next=head;
    head=top;
}

void pop(LNode *&head){
    if(head->data!=INT_MAX){
        LNode *p=head;
        head=head->next;
        cout<<"pop:"<<p->data<<endl;
        delete p;
    }else{
        cout<<"栈空"<<endl;
    }
}

int main(){
    LNode *head=new LNode();
    head->data=INT_MAX;
    head->next=NULL;
    push(head,5);
    push(head,4);
    push(head,3);
    pop(head);
    pop(head);
    pop(head);
    pop(head);
    return 0;
}

结果:
在这里插入图片描述

队列

1.什么是队列?

队的特点:先进先出
队列是一种插入元素只能在一端能进,删除元素只能在另一端进行的线性表
线性表:栈的逻辑结构属于线性表,只不过在操作上加了一些约束。
一端:可以插入元素的一端叫队尾
另一端:可以删除元素的一段叫队头

2.第一种顺序队列

在这里插入图片描述
思路:
通过rear来实现数据的入队,每次入队就+1,通过front来实现数据的出队,每次出队都+1,当front等于rear时,队列为空。

#include <bits/stdc++.h>
using namespace std;
#define maxSize 100

void push(int *a,int &rear,int data){
    a[rear++]=data;
}

void pop(int *a,int &front,int rear){
    if(front!=rear){
        cout<<"pop:"<<a[front++]<<endl;
    }else{
        cout<<"队空"<<endl;
    }
}

int main(){
    int queue[maxSize];
    int front=0,rear=0;
    push(queue,rear,5);
    push(queue,rear,4);
    push(queue,rear,3);
    pop(queue,front,rear);
    pop(queue,front,rear);
    pop(queue,front,rear);
    return 0;
}

结果:
在这里插入图片描述

3.第二种顺序队列

在这里插入图片描述

入栈:

 rear=(rear+1)%maxSize;
 a[rear]=x;

出栈:

front=(front+1)%maxSize;
cout<<"pop:"<<a[front]<<endl;

当front!=(rear+1)%maxSize显示为栈满,front!=rear显示为栈空,这种条件只能存取maxSize-1个元素

#include <bits/stdc++.h>
using namespace std;
#define maxSize 4

void push(int *a,int front,int &rear,int x){
    if(front!=(rear+1)%maxSize){
        rear=(rear+1)%maxSize;
        a[rear]=x;
    }else
        cout<<"FULL"<<endl;
}

void pop(int *a,int &front,int rear){
    if(front!=rear){
        front=(front+1)%maxSize;
        cout<<"pop:"<<a[front]<<endl;
    }else{
        cout<<"NULL"<<endl;
    }   
}

int main(){
    int queue[maxSize];
    int front=0,rear=0;
    push(queue,front,rear,4);
    push(queue,front,rear,3);
    push(queue,front,rear,2);
    pop(queue,front,rear);
    pop(queue,front,rear);
    pop(queue,front,rear);
    return 0;
}

结果:
在这里插入图片描述

4.链队

#include <bits/stdc++.h>
using namespace std;
typedef struct LNode{
    int data;
    struct LNode* next;
}LNode;

void push(LNode *&rear,int a){
    LNode *top=new LNode();
    top->data=a;
    top->next=NULL;
    rear->next=top;
    rear=top;
}

void pop(LNode *&front,LNode *&rear){
    if(front->next==NULL){
        cout<<"NULL"<<endl;
    }
    LNode *p=front->next;
    cout<<p->data<<endl;
    front->next=p->next;
    if(rear==p){
        rear=front;
    }
    delete p;
}

int main(){
    LNode *front,*rear;
    rear=new LNode();
    rear->next=NULL;
    front=rear;
    push(rear,4);
    push(rear,3);
    push(rear,2);
    pop(front,rear);
    pop(front,rear);
    pop(front,rear);
    pop(front,rear);
    return 0;
}

结果:
在这里插入图片描述

参考原文博客:http://c.biancheng.net/view/3351.html

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值