25 栈和队列

栈的基本操作

顺序存储实现栈

typedef struct{
    Elemtype data[50];
    int top;
}SqStack;
SqStack S;

链式存储实现栈(相对不重要,考的概率极低)

LiStack Lhead=(LiStack)malloc(sizeof(struct Linknode));
Lhead->next=NULL;
LiStack top=NULL;
top=(LiStack)malloc(sizeof(struct Linknode));
top->next=NULL;
top->data=1;
top->next=Lhead->next;
Lhead->next=top;

元素出栈

c=top->data;
Lhead->next=top->next;
free(top);
top=Lhead->next;

栈空栈满

Lhead->next == NULL 为真,则栈为空。只要存在剩余内存,那么栈就可以继续添加元素。

【例】初始化栈-入栈-出栈实战

代码实战步骤依次是初始化栈,判断栈是否为空,压栈,获取栈顶元素,弹栈。注意S.top 为-1 时,代表栈为空,每次是先对 S.top 加 1 后,再放置元素。

#include <cstdio>
#include <cstdlib>

#define MaxSize 50

typedef int ElemType;

typedef struct
{
    ElemType data[MaxSize];//数组
    int top;
}SqStack;

void InitStack(SqStack &S)
{
    S.top=-1;//代表栈为空
}

bool StackEmpty(SqStack &S)
{
    if(S.top==-1)
        return true;
    else
        return false;
}

//入栈
bool Push(SqStack &S,ElemType x)
{
    if(S.top==MaxSize-1)//数组的大小不能改变,避免访问越界
    {
        return false;
    }
    S.data[++S.top]=x;
    return true;
}

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

//读取栈顶元素
bool GetTop(SqStack &S,ElemType &x)
{
    if(-1==S.top)//说明栈为空
        return false;
    x=S.data[S.top];
    return true;
}

//实现栈可以用数组,也可以用链表,这里使用数组
int main()
{
    SqStack S;//先进后出 FILO LIFO
    bool flag;
    ElemType m;//用来存放拿出的元素
    InitStack(S);//初始化
    flag= StackEmpty(S);
    if(flag)
    {
        printf("栈是空的\n");
    }
    Push(S,3);//入栈元素3
    Push(S,4);//入栈元素4
    Push(S,5);
    flag= GetTop(S,m);//用来获取栈顶元素
    if(flag)
    {
        printf("获取栈顶元素为 %d\n",m);
    }
    flag= Pop(S,m);//弹出栈顶元素
    if(flag)
    {
        printf("弹出元素为%d\n",m);
    }
    return 0;
}

队列

循环队列

#define MaxSize 5

typedef int ElemType;

typedef struct{
    ElemType data[MaxSize];//数组,存储MaxSize-1个元素
    int front,rear;//队列头 队列尾
}SqQueue;

SqQueue Q;

循环队列元素入队

bool EnQueue(SqQueue &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;
}

循环队列元素出队

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

队列的链式存储

队列的链式表示称为链队列,它实际上是一个同时带有队头指针和队尾指针单链表。头指针指向队头结

点,尾指针指向队尾结点,即单链表的最后一个结点。

存储结构

typedef int ElemType;

typedef struct LinkNode{
    ElemType data;
    struct LinkNode *next;
}LinkNode;//链表结点的结构体

typedef struct{
    LinkNode *front,*rear;//链表头 链表尾
}LinkQueue;//先进先出

LinkQueue Q;//相对于院有编写的链表增加了尾指针

【例】循环队列实战

代码实战步骤依次是初始化循环队列,判断循环队列是否为空,入队,出队。

#include <cstdio>
#include <cstdlib>

#define MaxSize 5

typedef int ElemType;

typedef struct
{
    ElemType data[MaxSize];//数组,存储MaxSize-1个元素
    int front,rear;//队列头,队列尾
}SqQueue;

void InitQueue(SqQueue &Q)
{
    Q.rear=Q.front=0;
}

//判空
bool isEmpty(SqQueue &Q)
{
    if(Q.rear==Q.front)//不需要为零
        return true;
    else
        return false;
}

//入队
bool EnQueue(SqQueue &Q,ElemType x)
{
    if((Q.rear+1)%MaxSize==Q.front)//判断是否队满
    {
        return false;
    }
    Q.data[Q.rear]=x;//3 4 5 6
    Q.rear=(Q.rear+1)%MaxSize;
    return true;
}

//出队
bool DeQueue(SqQueue &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 Q;
    bool ret;//存储返回值
    ElemType element;//存储出队元素
    InitQueue(Q);
    ret= isEmpty(Q);
    if(ret)
    {
        printf("队列为空\n");
    }else{
        printf("队列不为空\n");
    }
    EnQueue(Q,3);
    EnQueue(Q,4);
    EnQueue(Q,5);
    ret= EnQueue(Q,6);
    ret= EnQueue(Q,7);
    if(ret)
    {
        printf("入队成功\n");
    }else{
        printf("入队失败\n");
    }
    ret= DeQueue(Q,element);
    if(ret)
    {
        printf("出队成功,元素值为%d\n",element);
    }else{
        printf("出队失败\n");
    }
    ret=DeQueue(Q,element);
    if(ret)
    {
        printf("出队成功,元素值为 %d\n",element);
    }else{
        printf("出队失败\n");
    }
    ret=EnQueue(Q,8);
    if(ret)
    {
        printf("入队成功\n");
    }else{
        printf("入队失败\n");
    }
    return 0;
}

【例】队列的实战(通过链表实现)

代码实战步骤依次是初始化队列,入队,出队。

#include <cstdio>
#include <cstdlib>


typedef int ElemType;

typedef struct LinkNode
{
    ElemType data;
    struct LinkNode *next;
}LinkNode;

typedef struct{
    LinkNode *front,*rear;//链表头 链表尾
}LinkQueue;//先进先出

void InitQueue(LinkQueue &Q)
{
    Q.front=Q.rear=(LinkNode*)malloc(sizeof(LinkNode));//头和尾指向同一个结点
    Q.front->next=NULL;//头结点的next指针为NULL
}

bool isEmpty(LinkQueue &Q)
{
    if(Q.front==Q.rear)//不需要为零
        return true;
    else
        return false;
}

//入队,尾部插入法
void EnQueue(LinkQueue &Q,ElemType x)
{
    LinkNode *s=(LinkNode *) malloc(sizeof (LinkNode));
    s->data=x;s->next=NULL;
    Q.rear->next=s;//rear始终指向尾部
    Q.rear=s;
}

//出队 头部删除法
bool DeQueue(LinkQueue &Q,ElemType &x)
{
    if(Q.front==Q.rear)
        return false;//队列为空
    LinkNode *p=Q.front->next;//头结点什么都没存,所以头结点的下一个节点才有数据
    x=p->data;
    Q.front->next=p->next;//断链
    if(Q.rear==p)//删除的是最后一个元素
        Q.rear=Q.front;//队列置为空
    free(p);
    return true;
}

int main()
{
    LinkQueue Q;
    bool ret;//存储返回值
    ElemType element;//存储出队元素
    InitQueue(Q);
    EnQueue(Q,3);
    EnQueue(Q,4);
    EnQueue(Q,5);
    EnQueue(Q,6);
    EnQueue(Q,7);
    ret= DeQueue(Q,element);
    if(ret){
        printf("出队成功,元素值为%d\n",element);
    } else{
        printf("出队失败\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值