一文教你数据结构体栈和队列的实现

前言:

        关于c语言的学习已经差不多更新完毕,如果发现个别知识点,我还会继续更新,但目前已经准备往c++和数据结构的重心挪动,这篇文章就是向大家讲述数据结构中栈和队列的实现。 

 💞 💞  欢迎来到小马学习代码博客!!!! 

          你来人间一趟,你要看看太阳!!!!

目录

前言:

一、数据结构体栈 

1.1栈的概念及结构:

1.2栈的实现:

 1.2.1栈的定义:

1.2.2栈的初始化:     

1.2.3栈的销毁:

 1.2.4判断栈是否为空:

1.2.5栈的压栈(push):

1.2.6栈的出栈(Pop):

 1.2.7栈顶的元素查看:    

1.2.7栈的元素个数:     

1.3栈实现的全部代码: 

1.3.1  .c

1.3.2   .h

1.3.3   test

二、数据结构队列:

2.1数列的概念和结构:

2.2队列的实现:

 2.2.1队列的定义: 

2.2.2队列的初始化:   

 2.2.3队列的销毁:   

 2.2.4队列判断是否为空:   

 2.2.5队列插入(push):   

 2.2.6队列出队(Pop):   

2.2.7队头元素:   

2.2.8队尾元素:   

2.2.9队的人员个数: 

2.3队列实现的全部代码: 

2.3.1  .c

2.3.2  .h

2.3.3  test

3、总结:


 

一、数据结构体栈 

1.1栈的概念及结构:

栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。

压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
 
出栈:栈的删除操作叫做出栈。出数据也在栈顶

 ~~~~记住一个原则,先进的后出,后进的先出。~~~~

1.2栈的实现:

              栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。我们保证首先插入的后面出来就行。

 1.2.1栈的定义:

typedef int STDataType;  //重定义int
typedef struct Stack{
    STDataType* a;            
    STDataType top;        //栈顶
    STDataType capicity;
    
}ST;

1.2.2栈的初始化:     

void StackInit(ST*ps)
{
    assert(ps);
    ps->a=NULL;  //先指向空指针
    ps->top=ps->capicity=0;    初始化都为零
}

1.2.3栈的销毁:

void StackDestroy(ST*ps)
{
    assert(ps);  //断言一下防止空指针
    free(ps);        //进行释放因为我申请的是动态内存
    ps=NULL;        
    ps->top=0;
    ps->capicity=0;
}

 1.2.4判断栈是否为空:

bool StackEmpty(ST*ps)
{
    assert(ps);
    return ps->top==0; //如果top等于0则证明栈为空,返回true
}

1.2.5栈的压栈(push):

void StackPush(ST*ps,STDataType x)
{
    assert(ps); //断言一下
    if(ps->top==ps->capicity)  //如果top(栈顶)=capacity则证明容量已经满了需要扩容一下
    {
        int  newcapicity=ps->capicity==0?4:(ps->capicity)*2;  //和顺序表扩容一样
        STDataType* ptr = (STDataType*)realloc(ps->a, newcapicity*sizeof(STDataType));   //进行动态扩容
        if(ptr==NULL)     //防止扩容失败
        {
            perror("StackPush");
            exit(-1);
        }
        ps->a=ptr;    //把扩容成功的地址传给a
        ps->capicity=newcapicity;    //把扩容后的新newcapicity给capacity
    }
    ps->a[ps->top]=x;   // 插入元素x
    ++ps->top;        //top进行++
}

1.2.6栈的出栈(Pop):

void StackPop(ST*ps)
{
    assert(ps);
    assert(!StackEmpty(ps)); //这里判断是否为空栈如果为空栈就不能在进行出栈了
    --ps->top;  //只需要让top减一个就行了
}

 1.2.7栈顶的元素查看:    

STDataType StackTop(ST* ps)
{
    assert(ps);
    assert(!StackEmpty(ps));
    
    return ps->a[ps->top - 1]; //因为进行push后top++了一下,所以栈顶需要top-1
}

1.2.7栈的元素个数:     

int StackSize(ST* ps)
{
    assert(ps);

    return ps->top;
}

栈还是比较容易实现的,如果熟练掌握顺序表我相信大家还是很容易就能把栈写出来。

1.3栈实现的全部代码: 

1.3.1  .c

#include "Stack.hpp"
void StackInit(ST*ps)
{
    assert(ps);
    ps->a=NULL;
    ps->top=ps->capicity=0;
}
void StackDestroy(ST*ps)
{
    assert(ps);
    free(ps);
    ps=NULL;
    ps->top=0;
    ps->capicity=0;
}
void StackPush(ST*ps,STDataType x)
{
    assert(ps);
    
   
    if(ps->top==ps->capicity)
    {
        int  newcapicity=ps->capicity==0?4:(ps->capicity)*2;
        STDataType* ptr = (STDataType*)realloc(ps->a, newcapicity*sizeof(STDataType));
        if(ptr==NULL)
        {
            perror("StackPush");
            exit(-1);
        }
        ps->a=ptr;
        ps->capicity=newcapicity;
    }
    ps->a[ps->top]=x;
    ++ps->top;
}
bool StackEmpty(ST*ps)
{
    assert(ps);
    return ps->top==0;
}
void StackPop(ST*ps)
{
    assert(ps);
    assert(!StackEmpty(ps));
    --ps->top;
}
STDataType StackTop(ST* ps)
{
    assert(ps);
    assert(!StackEmpty(ps));
    
    return ps->a[ps->top - 1];
}
int StackSize(ST* ps)
{
    assert(ps);

    return ps->top;
}

1.3.2   .h

#ifndef Stack_hpp
#define Stack_hpp

#include <stdio.h>
#include<assert.h>
#include<stdbool.h>
#include<stdlib.h>
typedef int STDataType;
typedef struct Stack{
    STDataType* a;
    STDataType top;
    STDataType capicity;
    
}ST;
void StackInit(ST*ps);
void StackDestroy(ST*ps);
void StackPush(ST*ps,STDataType x);
void StackPop(ST*ps);
bool StackEmpty(ST*ps);
STDataType StackTop(ST* ps);
int StackSize(ST* ps);

#endif /* Stack_hpp */

1.3.3   test

测试我这里是随便测试了一下,大家可以自己进行不同的测试就行不必和我一样

#include "Stack.hpp"
    void TestStack()
    {
        ST st;
        StackInit(&st);
        StackPush(&st, 1);
        StackPush(&st, 2);
        StackPush(&st, 3);
        printf("%d ", StackTop(&st));
        printf("%d ",StackSize(&st));
        StackPop(&st);
        //printf("%d ", StackTop(&st));
        StackPop(&st);

        StackPush(&st, 4);
        StackPush(&st, 5);

        while (!StackEmpty(&st))
        {
            printf("%d ", StackTop(&st));
            StackPop(&st);
        }
        printf("\n");
    }
    
int main()
{
    TestStack();
    return 0;
}

二、数据结构队列:

2.1数列的概念和结构:

        队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出 FIFO(First In First Out) 入队列:进行插入操作的一端称为队尾 出队列:进行删除操作的一端称为队头。  队列和栈的区别就是先进的后出,就和排队买东西一样,先排队的先买,说白了就是先到先得。

2.2队列的实现:

        队列要用到头删,因为第一个进入的要第一个删除,这样用数组就不太合适,因为数组的头删是0(n),时间复杂度比较大,这里我们用链表就是比较合适的选择。

 2.2.1队列的定义: 

typedef int QDataType;
typedef struct QueueNode
{
    struct QueueNode*next;
    QDataType data;
}QNode;
typedef struct Queue
{
    QNode* head;   //队列要有一个头
    QNode* rear;    //队列要有一个尾
    int size;    
}Queue;

2.2.2队列的初始化:   

void QueueInit(Queue*pq)
{
    assert(pq); //断言防止传入空指针
    pq->head=NULL;
    pq->rear=NULL;
    pq->size=0;
}

 2.2.3队列的销毁:   

void QueueDestroy(Queue*pq)
{
    assert(pq);
    QNode *cur=pq->head; //定一个节点进行遍历一个个销毁
    while(cur)
    {
        QNode *del=cur; 
        free(cur);
        cur=del->next;
    }
    pq->head=NULL;
    pq->rear=NULL;
}

 2.2.4队列判断是否为空:   

bool QueueEmpty(Queue* pq)
{
    assert(pq);
    return pq->head == NULL && pq->rear == NULL;
}

 2.2.5队列插入(push):   

void QueuePush(Queue* pq, QDataType x)
{
    assert(pq);
    QNode*newnode=(QNode*)malloc(sizeof(QNode)); //插入前先创建一个节点
    if(newnode==NULL)            //这是判断是否申请成功
    {
        perror("newnode");
        exit(-1);
    }
    else
    {
        newnode->data=x;
        newnode->next=NULL;
    }
    if(pq->rear==NULL)        //两种情况这是尾节点位空的时候
    {
        pq->rear=pq->head=newnode;
    }
    else                //这是尾节点不为空的时候
    {
        pq->rear->next=newnode;
        pq->rear=newnode;
    }
    pq->size++;
}

 2.2.6队列出队(Pop):   

void QueuePop(Queue* pq)
{
    assert(pq);
    assert(!QueueEmpty(pq));  //判断队列是否为空
    if(pq->head->next==NULL)    //如果只有一个头节点情况
    {
        free(pq->head);
        pq->head=pq->rear=NULL;
    }
    else                //不止只有头节点的情况
{          
    QNode* del;
    del=pq->head;
    pq->head=pq->head->next;
    free(del);
    del=NULL;
    }
    pq->size--;
}

2.2.7队头元素:   

QDataType QueueFront(Queue* pq)
{
    assert(pq);
    assert(!QueueEmpty(pq));

    return pq->head->data;
}

2.2.8队尾元素:   

QDataType QueueBack(Queue* pq)
{
    assert(pq);
    assert(!QueueEmpty(pq));
    return pq->rear->data;
}

2.2.9队的人员个数: 

int QueueSize(Queue* pq)
{
    assert(pq);
    return pq->size;
}

2.3队列实现的全部代码: 

2.3.1  .c

#include"Queue.h"
void QueueInit(Queue*pq)
{
    assert(pq);
    pq->head=NULL;
    pq->rear=NULL;
    pq->size=0;
}
void QueueDestroy(Queue*pq)
{
    assert(pq);
    
    QNode *cur=pq->head;
    while(cur)
    {
        QNode *del=cur;
        free(cur);
        cur=del->next;
    }
    pq->head=NULL;
    pq->rear=NULL;
}
void QueuePush(Queue* pq, QDataType x)
{
    assert(pq);
    QNode*newnode=(QNode*)malloc(sizeof(QNode));
    if(newnode==NULL)
    {
        perror("newnode");
        exit(-1);
    }
    else
    {
        newnode->data=x;
        newnode->next=NULL;
    }
    if(pq->rear==NULL)
    {
        pq->rear=pq->head=newnode;
    }
    else
    {
        pq->rear->next=newnode;
        pq->rear=newnode;
    }
    pq->size++;
}
void QueuePop(Queue* pq)
{
    assert(pq);
    assert(!QueueEmpty(pq));
    if(pq->head->next==NULL)
    {
        free(pq->head);
        pq->head=pq->rear=NULL;
    }
    else{
    QNode* del;
    del=pq->head;
    pq->head=pq->head->next;
    free(del);
    del=NULL;
    }
    pq->size--;
}
bool QueueEmpty(Queue* pq)
{
    assert(pq);
    return pq->head == NULL && pq->rear == NULL;
}
QDataType QueueFront(Queue* pq)
{
    assert(pq);
    assert(!QueueEmpty(pq));

    return pq->head->data;
}

QDataType QueueBack(Queue* pq)
{
    assert(pq);
    assert(!QueueEmpty(pq));
    return pq->rear->data;
}
int QueueSize(Queue* pq)
{
    assert(pq);
    return pq->size;
}

2.3.2  .h


#ifndef Queue_h
#define Queue_h

#include <stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int QDataType;
typedef struct QueueNode
{
    struct QueueNode*next;
    QDataType data;
}QNode;
typedef struct Queue
{
    QNode* head;
    QNode* rear;
    int size;
}Queue;
void QueueInit(Queue*pq);
void QueueDestroy(Queue*pq);
void QueuePush(Queue* pq, QDataType x);
void QueuePop(Queue* pq);
bool QueueEmpty(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
int QueueSize(Queue* pq);



#endif /* Queue_h */

2.3.3  test

#include"Queue.h"
void test()
{
    Queue q;
    QueuePush(&q,1);
    QueuePush(&q,2);
    QueuePush(&q,3);
    QueuePush(&q,4);
    QueuePush(&q,5);
    printf("%d \n",QueueSize(&q));
    printf("%d ",QueueFront(&q));
    QueuePop(&q);
    printf("%d ",QueueFront(&q));
    QueuePop(&q);
    printf("%d ",QueueFront(&q));
    printf("%d ",QueueBack(&q));
    
    QueueDestroy(&q);
    
}
int main()
{
    test();
    return 0;
}

3、总结:

        关于栈和队列是数据结构中比较重要的一点,这篇博客我把栈和队列的实现全部写出来了,同时最后有他全部的代码,但我的建议是大家可以尝试一下自己去完成栈和队列的代码实现,在实现过程中也就是我们的学习过程!!!!!!!

 最后小马码文不易,如果觉得有帮助就多多支持哈!!!^ _ ^

  • 95
    点赞
  • 178
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 66
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

️小马️

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

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

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

打赏作者

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

抵扣说明:

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

余额充值