数据结构 分别实现顺序队列和链式队列

分别实现顺序队列和链式队列

实现入队列, 出队列, 和取队首元素

顺序队列

seqqueue.h
#pragma once

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

#define TITLE printf("\n====================%s====================",__FUNCTION__);


#define SeqQueueMaxSize 1000

typedef char SeqQueueType;

typedef struct SeqQueue
{
	SeqQueueType data[SeqQueueMaxSize];
	size_t head;
	size_t tail;
	size_t size;
}SeqQueue;

void SeqQueueInit(SeqQueue* q); //初始化
void SeqQueueDestroy(SeqQueue *q);//销毁
void SeqQueuePrint(SeqQueue* q);//打印
void SeqQueuePush(SeqQueue*q,SeqQueueType value);//入队列
void SeqQueuePop(SeqQueue*q);//出队列
int SeqQueueGet(SeqQueue* q,char* value);//获取队首元素
seqqueue.c
#include "seqqueue.h"

void SeqQueueInit(SeqQueue* q)
{
	if(q == NULL)
	{
		//非法输入
		return;
	}
	q->size = 0;
	q->head = 0;
	q->tail = 0;
	return;
}

void SeqQueueDestroy(SeqQueue* q)
{
	if(q == NULL)
	{
		//非法输入
		return;
	}
	q->size = 0;
	q->head = 0;
	q->tail = 0;
	return;
}

//实际上队列是不允许被打印的,这里仅为测试代码是否正确
void SeqQueuPrint(SeqQueue* q,const char* msg)
{
	printf("\n[%s]:\n",msg);
	if(q == NULL)
	{
		//非法输入
		return;
	}
	if(q->size == 0)
	{
		//空队列
		return;
	}
	//tail在head后面
	if(q->head < q->tail)
	{
		int i = q->head;
		for(;i<q->tail;i++)
		{
			printf("[%c] ",q->data[i]);
		}	
		printf("\n");
		return;
	}
	// tail在head前
	else
	{
		int i = q->head;
		for(;i<SeqQueueMaxSize;i++)
		{
			printf("[%c] ",q->data[i]);
		}
		i = 0;
		for(;i<q->tail;i++)
		{
			printf("[%c] ",q->data[i]);
		}
		printf("\n");
	}
}

void SeqQueuePush(SeqQueue*q,SeqQueueType value)
{
	if(q == NULL)
	{
		//非法输入
		return;
	}
	if(q->size >= SeqQueueMaxSize)
	{
		printf("队列已满\n");
		return;
	}
	q->data[q->tail] = value;
	q->tail++;
	q->size++;
	if(q->tail >= SeqQueueMaxSize)
	{
		q->tail = 0;
	}
}

void SeqQueuePop(SeqQueue*q)
{
	if(q == NULL)
	{
		return;
	}
	if(q->size == 0)
	{
		//空队列
		return;
	}
	if(q->head >= SeqQueueMaxSize)
	{
		q->head = 0;
	}
	q->head++;
	q->size--;
}	

int SeqQueueGet(SeqQueue* q,char* value)
{
	if(q == NULL)
	{
		//非法输入
		return 0;
	}
	if(q->size == 0)
	{
		//空队列
		return 0;
	}
	*value = q->data[q->head];
	q->head++;
	q->size--;
}


//////////////////////////////////////////////
//              以下为测试代码              //
/////////////////////////////////////////////

void TestSeqQueuePush()
{
	TITLE;
	SeqQueue q;
	SeqQueueInit(&q);
	SeqQueuePush(&q,'a');
	SeqQueuePush(&q,'b');
	SeqQueuePush(&q,'c');
	SeqQueuePush(&q,'d');
	SeqQueuPrint(&q,"入队列4个元素");
}

void TestSeqQueuePop()
{
	TITLE;
	SeqQueue q;
	SeqQueueInit(&q);
	SeqQueuePush(&q,'a');
	SeqQueuePush(&q,'b');
	SeqQueuePush(&q,'c');
	SeqQueuePush(&q,'d');
	SeqQueuPrint(&q,"入队列4个元素");
	SeqQueuePop(&q);
	SeqQueuPrint(&q,"出队列1个元素");
	SeqQueuePop(&q);
	SeqQueuePop(&q);
	SeqQueuePop(&q);
	SeqQueuPrint(&q,"出队列3个元素");	
}

void TestSeqQueueGet()
{
	TITLE;
	SeqQueueType value;
	SeqQueue q;
	SeqQueueInit(&q);
	SeqQueuePush(&q,'a');
	SeqQueuePush(&q,'b');
	SeqQueuePush(&q,'c');
	SeqQueuePush(&q,'d');
	SeqQueuPrint(&q,"入队列4个元素");
	SeqQueueGet(&q,&value);	
	printf("取得队首元素为:[%c] \n",value);
}

int main()
{
	TestSeqQueuePush();
	TestSeqQueuePop();
	TestSeqQueueGet();
	return 0;
}
运行结果:



链式队列

listqueue.h
#include <stdio.h>
#include <stdlib.h>

#define TITLE printf("\n======================%s====================\n",__FUNCTION__);

typedef char ListQueueType;

typedef struct ListQueue
{
	ListQueueType data;
	struct ListQueue* next;
}ListQueue;



void ListQueueInit(ListQueue** head);//初始化
void ListQueueDestroy(ListQueue** head);//销毁队列
void ListQueuePush(ListQueue** head,ListQueueType value);//入队列
ListQueue* ListQueueCreate(ListQueueType value);//创建结点
void ListQueuePrint(ListQueue* head,const char *msg);//打印队列
void ListQueuePop(ListQueue** head);//出队列
int ListQueueGet(ListQueue** head,char* value);//取队列首元素,成功返回1,否则返回0

listqueue.c
#include"listqueue.h"

void ListQueueInit(ListQueue** head)
{
	if(head == NULL)
	{
		//非法输入
		return;
	}
	*head = NULL;
	return;
}


void ListQueueDestroy(ListQueue** head)
{
	if(head == NULL)
	{
		return;	
	}
	if(*head == NULL)
	{
		return;
	}
	ListQueue* cur = *head;
	ListQueue* to_delete = NULL;
	while(cur != NULL)
	{
		to_delete = cur;
		cur = cur->next;
		free(to_delete);
	}
	return;
}

ListQueue* ListQueueCreate(ListQueueType value)
{
	ListQueue* new_node = (ListQueue*)malloc(sizeof(ListQueue));
	new_node->data = value;
	new_node->next = NULL;
}

void ListQueuePrint(ListQueue* head,const char *msg)
{
	printf("[%s]:\n",msg);
	if(head == NULL)
	{
		//空队列
		return;
	}
	ListQueue* cur = head;
	while(cur != NULL)
	{
		printf("[%c] ",cur->data);	
		cur = cur->next;
	}
	printf("\n");
}


void ListQueuePush(ListQueue**head,ListQueueType value)
{
	if(head == NULL)	
	{
		return;
	}
	if(*head == NULL)
	{
		*head = ListQueueCreate(value);
		return;
	}
	ListQueue* cur = *head;
	while(cur->next != NULL)
	{
		cur = cur->next;
	}
	cur->next = ListQueueCreate(value);
	return;
}

void ListQueuePop(ListQueue** head)
{
	if(head == NULL)
	{
		return;
	}
	if(*head == NULL)
	{
		return;
	}
	if((*head)->next == NULL)
	{
		ListQueueDestroy(head);
		return;
	}
	ListQueue* to_delete = *head;
	*head = (*head)->next;
	free(to_delete);
	return;
}

int ListQueueGet(ListQueue** head,char* value)
{
	if(head == NULL)
	{
		return 0;
	}
	if(*head == NULL)
	{
		return 0;
	}
	*value = (*head)->data;		
	return 1;
}




/////////////////////////////////////////////////////////////
//                      以下为测试函数                     //
/////////////////////////////////////////////////////////////

void TestListQueuePush()
{
	TITLE;
	ListQueue* head;
	ListQueueInit(&head);
	ListQueuePush(&head,'a');	
	ListQueuePush(&head,'b');	
	ListQueuePrint(head,"入队列2个元素");
	ListQueuePush(&head,'c');	
	ListQueuePush(&head,'d');	
	ListQueuePrint(head,"入队列2个元素");
}


void TestListQueueDestroy()
{
	TITLE;
	ListQueue* head;
	ListQueueInit(&head);
	ListQueuePush(&head,'a');	
	ListQueuePush(&head,'b');	
	ListQueuePrint(head,"入队列2个元素");
	ListQueueDestroy(&head);
	ListQueuePrint(head,"销毁队列");
}


void TestListQueuePop()
{
	TITLE;
	ListQueue* head;
	ListQueueInit(&head);
	ListQueuePush(&head,'a');	
	ListQueuePush(&head,'b');	
	ListQueuePush(&head,'c');	
	ListQueuePush(&head,'d');	
	ListQueuePrint(head,"入队列4个元素");
	ListQueuePop(&head);	
	ListQueuePop(&head);	
	ListQueuePrint(head,"出队列2个元素");
	ListQueuePop(&head);	
	ListQueuePop(&head);	
	ListQueuePrint(head,"出队列2个元素");
}

void TestListQueueGet()
{
	
	TITLE;
	ListQueueType value;
	int ret = 0;
	ListQueue* head;
	ListQueueInit(&head);
	ListQueuePush(&head,'a');	
	ListQueuePush(&head,'b');	
	ListQueuePush(&head,'c');	
	ListQueuePush(&head,'d');	
	ListQueuePrint(head,"入队列4个元素");
	ret = ListQueueGet(&head,&value);
	printf("ret = %d\n队首元素为:%c\n",ret,value);
}


int main()
{
	TestListQueuePush();
	TestListQueueDestroy();
	TestListQueuePop();
	TestListQueueGet();
	return;
}
运行结果:


阅读更多
个人分类: 数据结构
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭