c语言链表队列max,[数据结构] C语言队列(链表实现) | 赤道企鹅的博客

英语课日常摸鱼,写个队列。

懒得封装了,一团糊

队列结构

c8df4608aa432a7fbee3cae1d96e22ae.png

无论是用数组还是链表来实现队列,关键就在于用front和rear指针去维护整个队列。

注意考虑好各个方法实现的逻辑完整性,不然很容易出现漏洞。

Code

#include

#include

#include

#include

#include

#include

using namespace std;

#define MAXQSIZE 10

#define MAXMSGSIZE 64u

struct Msg{

int msgSize;

char *content;

Msg *next;

};

typedef struct Queue{

int count;

Msg *front;

Msg *rear;

}QUEUE;

void initNewChunk(Msg *raw){

raw->msgSize = 0;

raw->content = NULL;

raw->next = NULL;

}

QUEUE *init(){

QUEUE *queue_ptr = (QUEUE*)malloc(sizeof(QUEUE));

queue_ptr->front = queue_ptr->rear =(Msg*)malloc(sizeof(Msg));

queue_ptr->count=0;

initNewChunk(queue_ptr->rear);

return queue_ptr;

}

void getInput(char *buf,unsigned int maxlen){

for(int i=0;i

read(0,buf+i,1);

if(*(buf+i) == '\n'){

*(buf+i) = '\x00';

break;

}

}

*(buf+maxlen-1) = '\x00';

}

bool full(QUEUE *queue){

return (queue->count == MAXQSIZE)?true:false;

}

void queue_flush(QUEUE *queue){

Msg *tmp;

while(queue->rear!=queue->front){

free(queue->front->content);

tmp = queue->front->next;

free(queue->front);

queue->front = tmp;

}

queue->count=0;

}

void pre_exit(QUEUE *queue){

printf("exit...");

queue_flush(queue);

free(queue->rear);

exit(-1);

}

void push(QUEUE *queue){

unsigned int len;

printf("How long is your message (maxlen=%d): ",MAXMSGSIZE);

scanf("%d",&len);

len = (len>MAXMSGSIZE) ? MAXMSGSIZE:len;

queue->rear->msgSize = len;

queue->rear->content = (char *)malloc(len+1);

if(!(queue->rear->content)){

printf("Malloc Error!");

pre_exit(queue);

}

memset(queue->rear->content,0,len+1);

printf("Please leave your message: ");

getInput(queue->rear->content,len+1);

fflush(stdin);

printf("Successfully!");

queue->rear->next = (Msg*)malloc(sizeof(Msg));

if(!(queue->rear->next)){

printf("Malloc Error!");

pre_exit(queue);

}

queue->count++;

initNewChunk(queue->rear->next);

queue->rear = queue->rear->next;

}

void pop(QUEUE *queue){

Msg *tmp;

puts("Pop a message\n----------------");

printf("msglen: %d\ncontent: ",queue->front->msgSize);

write(1,queue->front->content,queue->front->msgSize);

puts("\n----------------");

queue->count--;

free(queue->front->content);

tmp=queue->front->next;

free(queue->front);

queue->front = tmp;

}

void check(QUEUE *queue){

printf("-------------\ncount:%d MAXQSIZE:%d\n-------------\n",queue->count,MAXQSIZE);

}

int main(){

int s = 0;

QUEUE *myqueue = init();

while(1){

printf("\nSelect:\n0. Push\n1. Pop\n2. Check queue\n3. flush\n4. Exit\n>>> ");

scanf("%d",&s);

fflush(stdin);

switch(s){

case 0:

if(full(myqueue))

printf("Queue is full, you can try tu flush.\n");

else

push(myqueue);

break;

case 1:

if(myqueue->count == 0)

printf("-------------\nQueue is emptr.\n-------------\n");

else

pop(myqueue);

break;

case 2:

check(myqueue);

break;

case 3:

queue_flush(myqueue);

break;

case 4:

return 0;

default:

printf("I don't konw!");

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值