C语言【数据结构】线性表--队列

顺序队列--循环队列

sequeue.h
#define N 5

typedef int data_type;

typedef struct{
	data_type data[N];
	int front, rear; // front:队头  rear:队尾的下一个节点
}sequeue;

sequeue * queue_create();
int enqueue(sequeue * q, data_type value);
data_type dequeue(sequeue * q);
int queue_empty(sequeue * q);
int queue_full(sequeue * q);
int queue_clear(sequeue * q);
int queue_free(sequeue * q);
sequeue.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sequeue.h"
sequeue * queue_create(){
	sequeue * q;
	q = (sequeue *)malloc(sizeof(sequeue));
	if(q==NULL){
		printf("%s\n", "malloc failed");
		return NULL;
	}
	memset(q->data, 0, sizeof(q->data));
	q->front = 0;
	q->rear = 0;
	return q;
}

int enqueue(sequeue * q, data_type value){
	if(q==NULL){
		printf("%s\n", "queue is NULL");
		return -1;
	}
    // 循环队列
	if((q->rear+1)%N == q->front){
		printf("%s\n", "queue is full");
		return -1;
	}
	q->data[q->rear] = value;
    // 循环队列
	q->rear = (q->rear+1)%N;
	return 0;
}

data_type dequeue(sequeue * q){
	if(q==NULL){
		printf("%s\n", "queue is NULL");
		return -1;
	}
	if(q->front == q->rear){
		printf("%s\n", "queue is emprty");
		return -1;
	}
	data_type data;
	data = q->data[q->front];
	q->front = (q->front+1)%N;
	return data;
}

int queue_empty(sequeue * q){
	if(q==NULL){
		printf("%s\n", "queue is NULL");
		return -1;
	}
	return (q->front == q->rear ? 1 : 0);
}
int queue_full(sequeue * q){
	if(q==NULL){
		printf("%s\n", "queue is NULL");
		return -1;
	}
	if((q->rear+1)%N == q->front){
		return 1;
	}
	return 0;
}
int queue_clear(sequeue * q){
	if(q==NULL){
		printf("%s\n", "queue is NULL");
		return -1;
	}
	q->rear = q->front = 0;
	return 0;
}
int queue_free(sequeue * q){
	if(q==NULL){
		printf("%s\n", "queue is NULL");
		return -1;
	}
	free(q);
	q=NULL;
	return 0;
}
test.c
#include <stdio.h>
#include <stdlib.h>
#include "sequeue.h"

int main(int argc, char const *argv[]){
	sequeue * q;
	data_type data;
	q = queue_create();
	enqueue(q,10);
	enqueue(q,20);
	enqueue(q,30);
	queue_clear(q);
	enqueue(q,40);
	enqueue(q,50);
	enqueue(q,60);
	enqueue(q,70);

	while(!queue_empty(q)){
		data = dequeue(q);
		printf("dequeue: %d\n", data);
	}
	queue_clear(q);
	queue_free(q);
	return 0;
}

链式队列

linkqueue.h
typedef int data_type;

typedef struct node{
	data_type data;
	struct node * next;
}linknode;

typedef struct{
	linknode * front;
	linknode * rear;
}linkqueue;

linkqueue * queue_create();
int enqueue(linkqueue * q, data_type value);
data_type dequeue(linkqueue * q);
int queue_empty(linkqueue * q);
int queue_free(linkqueue * q);
int queue_clear(linkqueue * q);
linkqueue.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linkqueue.h"

linkqueue * queue_create(){
	linkqueue * q;
	q = (linkqueue *)malloc(sizeof(linkqueue));
	if(q==NULL){
		printf("%s\n", "malloc failed\n");
		return NULL;
	}

	q->front = q->rear = (linknode *)malloc(sizeof(linknode));
	if(q->front==NULL){
		printf("%s\n", "malloc failed\n");
		return NULL;
	}
	q->front->data = 0;
	q->front->next = NULL;
	return q;
}

int enqueue(linkqueue * q, data_type value){
	// ...省略q==NULL校验

	// 建立新节点
	linknode * p;
	p = (linknode *)malloc(sizeof(linknode));
	if(p == NULL){
		printf("%s\n", "malloc failed");
		return -1;
	}
	p->data = value;
	p->next = NULL;

	// 将新节点追加到链表
	q->rear->next = p;

	// 修改队列的rear指向新节点
	q->rear = p;

	return 0;
}

int dequeue(linkqueue * q){
	// ...省略q==NULL校验

	// H:队头元素
	data_type data;
	linknode * H;
	H = q->front->next;
	data = H->data;

	// 只有一个队元素
	if(H->next == NULL){
		q->front->next = NULL;
		q->rear = q->front;
		free(H);
		H = NULL;
		return data;
	}
	
	// 多个队元素
	q->front->next = H->next;
	free(H);
	H = NULL;
	return data;
}

int queue_empty(linkqueue * q){
	// ...省略q==NULL校验

	return (q->front == q->rear ? 1 : 0);
}
int queue_free(linkqueue * q){
	// ...省略q==NULL校验

	linknode * s;
	while(q->front){
		s = q->front;
		q->front = s->next;
		printf("free: %d\n", s->data);
		free(s);
	}
	free(q);
	q = NULL;
	return 0;
}
int queue_clear(linkqueue * q){
	// ...省略q==NULL校验
	linknode * p;
	while(q->front->next != NULL){
		p = q->front;
		q->front = p->next;
		printf("clear: %d\n", p->data);
		free(p);
	}
	return 0;
}
test.c
#include <stdio.h>
#include <stdlib.h>
#include "linkqueue.h"

int main(int argc, char const *argv[]){
	linkqueue * q;
	q = queue_create();
	enqueue(q,10);
	enqueue(q,20);
	enqueue(q,30);
	enqueue(q,40);

	while(!queue_empty(q)){
		printf("dequeue: %d\n", dequeue(q));
	}
	
	// queue_free(q);

	queue_clear(q);
	enqueue(q,40);
	while(!queue_empty(q)){
		printf("dequeue: %d\n", dequeue(q));
	}
	return 0;
}
posted @ 2023-10-15 21:49  扬帆去远航  阅读(26)  评论(0编辑  收藏  举报