首页 > 其他分享 >第三章 C语言:栈和队列

第三章 C语言:栈和队列

时间:2023-03-08 23:56:57浏览次数:35  
标签:第三章 队列 Pop next assert Push cq C语言 size

一、栈

特性:先进后出,顺序存储

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>

#define CAPACITY 3  //默认初识容量
typedef int SData;

typedef struct Stack {
    SData* data;
    size_t size;
    size_t capacity;
}Stack;

void Init(Stack* s) {
    assert(s);
    s->data = (SData*)malloc(sizeof(SData) * CAPACITY);
    if (s == NULL) {
        perror("init::malloc");
        exit(1);
    }
    s->size = 0;
    s->capacity = CAPACITY;
}

bool Empty(Stack* s) {
    assert(s);
    return s->size == 0;
}

void CheckCapacity(Stack* s) {
    assert(s);
    if (s->size == s->capacity) {
        SData* tmp = (SData*)realloc(s->data, sizeof(SData) * (s->size + CAPACITY));
        if (tmp == NULL) {
            perror("realloc");
            exit(1);
        }
        s->data = tmp;
        s->capacity += CAPACITY;
    }
}

void Push(Stack* s, SData x) {
    assert(s);
    CheckCapacity(s);
    s->data[s->size] = x;
    ++s->size;
}

void Pop(Stack* s) {
    assert(s);
    if (!Empty(s)) {
        --s->size;
    }
}

size_t Size(Stack* s) {
    assert(s);
    printf("the stack size is %d\n", s->size);
    return s->size;
}

void PrintStack(Stack* s) {
    assert(s);
    if (!Empty(s)) {
        int i = 0;
        while (i < s->size) {
            printf("%d ", s->data[i]);
            ++i;
        }
        printf("\n");
    }

}

int main() {
    Stack s;
    Init(&s);
    Push(&s, 1);
    Push(&s, 3);
    Push(&s, 2);
    Push(&s, 4);
    Size(&s);
    PrintStack(&s);
    Pop(&s);
    Pop(&s);
    Pop(&s);
    Pop(&s);
    Pop(&s);
    Size(&s);
    PrintStack(&s);
    return 0;
}

 

 

二、队列

特性:先进先出,链式存储

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>

typedef int QData;

typedef struct Queue {
    QData data;
    struct Queue* next;
}Queue;

void Init(Queue* q) {
    assert(q);
    q->next = (Queue*)malloc(sizeof(Queue));
    q->next->next = NULL;
}

bool Empty(Queue* q) {
    assert(q);
    return q->next->next == NULL;
}

void Push(Queue* q, QData x) {
    assert(q);
    Queue* node = (Queue*)malloc(sizeof(Queue));
    node->data = x;
    node->next = q->next->next;
    q->next->next = node;
}

void Pop(Queue* q) {
    assert(q);
    if (!Empty(q)) {
        Queue* cur = q->next->next;
        q->next->next = cur->next;
        free(cur);
        cur = NULL;
    }
}

size_t Size(Queue* q) {
    assert(q);
    size_t size = 0;
    if (!Empty(q)) {
        Queue* cur = q->next->next;
        while (cur) {
            ++size;
            cur = cur->next;
        }
    }
    printf("the list size is %d\n", size);
    return size;
}

void PrintQueue(Queue* q) {
    assert(q);
    if (!Empty(q)) {
        Queue* cur = q->next->next;
        printf("%d ", cur->data);
        while (cur->next) {
            printf("-> %d ", cur->next->data);
            cur = cur->next;
        }
        printf("\n");
    }
}

int main() {
    Queue q;
    Init(&q);
    Push(&q, 1);
    Push(&q, 3);
    Push(&q, 4);
    Push(&q, 2);
    Size(&q);
    PrintQueue(&q);
    Pop(&q);
    Pop(&q);
    Pop(&q);
    Pop(&q);
    Pop(&q);
    Size(&q);
    PrintQueue(&q);
    return 0;
}

 

 

三、循环队列

特性:先进先出,顺序存储,空间有限并循环利用,留一个空位区分空和满

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>

#define CAPACITY 5     //总容量为5,数据容量为4
typedef int CQData;

typedef struct CirQue {
    CQData* data;
    int head;
    int tail;
    int size;
}CirQue;

void Init(CirQue* cq) {
    assert(cq);
    cq->data = (CQData*)malloc(sizeof(CQData) * CAPACITY); //多开辟一个空间
    cq->head = cq->tail = cq->size = 0;
}

bool Empty(CirQue* cq) {
    assert(cq);
    return cq->head == cq->tail;
}

bool Full(CirQue* cq) {
    assert(cq);
    return (cq->tail + 1) % CAPACITY == cq->head;
}

int Size(CirQue* cq) {
    assert(cq);
    return (cq->tail + CAPACITY - cq->head) % CAPACITY;
}

void Push(CirQue* cq, CQData x) {
    assert(cq);
    if (!Full(cq)) {
        cq->data[cq->tail] = x;
        cq->tail = (cq->tail + 1) % CAPACITY;
    }
}

void Pop(CirQue* cq) {
    assert(cq);
    if (!Empty(cq)) {
        cq->head = (cq->head + 1) % CAPACITY;
    }
}

void PrintCirQue(CirQue* cq) {
    assert(cq);
    if (!Empty(cq)) {
        int pos = cq->head;
        int size = Size(cq);
        while (size--) {
            printf("%d ", cq->data[pos]);
            pos = (pos + 1) % CAPACITY;
        }
        printf("\n");
    }
}

int main () {
    CirQue cq;
    Init(&cq);
    Push(&cq, 1);
    Push(&cq, 3);
    Push(&cq, 5);
    printf("the CirQue size is %d\n", Size(&cq));
    PrintCirQue(&cq);//1 3 5
    Push(&cq, 2);
    Push(&cq, 4);
    Push(&cq, 6);
    printf("the CirQue size is %d\n", Size(&cq));
    PrintCirQue(&cq);//1 3 5 2
    Pop(&cq);
    Pop(&cq);
    Pop(&cq);
    printf("the CirQue size is %d\n", Size(&cq));
    PrintCirQue(&cq);//2
    Push(&cq, 10);
    Push(&cq, 20);
    Push(&cq, 30);
    Push(&cq, 40);
    printf("the CirQue size is %d\n", Size(&cq));
    PrintCirQue(&cq);//2 10 20 30
    Pop(&cq);
    Pop(&cq);
    Pop(&cq);
    Pop(&cq);
    Pop(&cq);
    Pop(&cq);
    printf("the CirQue size is %d\n", Size(&cq));
    PrintCirQue(&cq);
    return 0;
}

 

标签:第三章,队列,Pop,next,assert,Push,cq,C语言,size
From: https://www.cnblogs.com/phoenixflyzzz/p/17196745.html

相关文章

  • 6.C语言第6天
    1.putchar和getchar:单字符输入输出a)putchar:带入一个整数,输出一个形状(参照ASCII编号表)。这个整数可以是100,105,66等,也可以是'x','#'这些整数。2.puts和gets:一串输入输出a......
  • 中断下文tasklet、工作队列
    tasklettasklet结构体structtasklet_struct{};unsignedlongdata还可以区分tasklettasklet相关函数示例核心代码......
  • c语言实现简单的飞机小游戏
    在今天浏览csdn的过程中,看到了一个用c语言做的简单的飞机小游戏,感觉非常有意思,代码如下:#include<stdio.h>#include<stdlib.h>#include<conio.h>intmain(){intx=15......
  • 【栈】LeetCode 剑指 Offer 09. 用两个栈实现队列
    题目链接剑指Offer09.用两个栈实现队列思路两个栈模拟代码classCQueue{Deque<Integer>inStack;Deque<Integer>outStack;publicCQueue(){......
  • 学习C语言第三弹:函数(1)
    函数是什么?数学中我们常见到函数的概念。维基百科中对函数的定义:子程序。   ·在计算机科学中,子程序(英语:Subroutine,procedure,function,routine,method,subprogram,callab......
  • 嵌入式C语言九大数据结构操作方式详解
          数据结构想必大家都不会陌生,对于一个成熟的程序员而言,熟悉和掌握数据结构和算法也是基本功之一。数据结构本身其实不过是数据按照特点关系进行存储或者组织......
  • 利用阻塞队列完成异步操作
    //异步阻塞队列变量privateBlockingQueue<VoucherOrder>orderTasks=newArrayBlockingQueue<>(1024*1024);//createthethreadpoolsignalthread......
  • Linux & 标准C语言学习 <DAY9_1>
    //补08_2函数  2、函数传参:    1.函数中定义的变量属于该函数,出了该函数就不能再被别的函数直接使用    2.实参与形参之间是以赋值的方式进行......
  • 实验1 C语言开发环境使用和数据类型、运算符、表达式
    //打印一个字符小人#include<stdio.h>intmain(){printf("o\n");printf("<H>\n");printf("II\n");return0;}#include<stdio.h>......
  • mq延时队列
    importcom.example.delayedmsg.config.QueueConfig;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.amqp.rabbit.annotation.R......