队列的基本操作

这两天学习了”队列“这种数据结构,自己写了一个小程序,进行队列的一些基本操作。

与之前的链表相比,队列的操作要简单一点,它只允许在表尾插入,在表头删除。

仍然采用菜单的形式,只不过这次队列打算存人名,所以节点就采用字符数组了。

进队时要注意队列是否为空,出队时要注意队列是否只有1个节点。

程序中还设置了队列的长度上限,如果队列已满,就不再允许进队。

运行的部分截图:


完整的代码:

#include <STDIO.H>
#include <STDLIB.H>
#include <CONIO.H>

#define MAX_SIZE 10		//队列最大长度
#define NAME_LENGTH 20	//姓名最大长度

struct node{
	char name[NAME_LENGTH];
	struct node *next;
};

struct queue{
	struct node *front;
	struct node *rear;
	int length;
};

typedef struct node Node;
typedef struct queue Queue;

void InitQueue (Queue *);
bool IsQueueEmpty (const Queue *);
bool IsQueueFull (const Queue *);
int GetQueueLength (const Queue *);
void ShowQueue (const Queue *);
void ShowNode (Node *);
void Insert (Queue *);
void IntoQueue (Queue *, Node *);
void OutOfQueue (Queue *);
void ClearQueue(Queue *);
void ShowMenu (void);
void ClearBuffer (void);

int main (void)
{
	Queue m_Queue;
	Queue *q;
	char response;
	
	q = &m_Queue;
	InitQueue(q);
	ShowMenu();
	while ((scanf("%c", &response) != 1) || (response != 'q'))
	{
		if (response == '\r' || response == '\n')
		{
			printf("输入有误。\n");
		} 
		else if (response < 'a' || response > 'e')
		{
			printf("输入有误。\n");
			ClearBuffer();
		}
		else
		{
			ClearBuffer();
			switch (response)
			{
			case 'a':
				Insert(q);
				break;
			case 'b':
				OutOfQueue(q);
				break;
			case 'c':
				ShowQueue(q);
				break;
			case 'd':
				printf("队列长度为%d。\n", GetQueueLength(q));
				break;
			case 'e':
				ClearQueue(q);
				break;
			}
		}
		printf("\n按任意键继续...\n");
		getch();
		printf("\n");
		ShowMenu();
	}
	
	return 0;
}

void InitQueue (Queue *q)
{
	q->front = NULL;
	q->rear = NULL;
	q->length = 0;
}

bool IsQueueEmpty (const Queue *q)
{
	if (q->length == 0)
	{
		return true;
	} 
	else
	{
		return false;
	}
}

bool IsQueueFull (const Queue *q)
{
	if (q->length == MAX_SIZE)
	{
		return true;
	} 
	else
	{
		return false;
	}
}

int GetQueueLength (const Queue *q)
{
	return q->length;
}

void ShowQueue (const Queue *q)
{
	if (IsQueueEmpty(q))
	{
		printf("当前队列为空。\n");
	} 
	else
	{
		Node *p = q->front;
		printf("当前队列为:\n");
		while (p != q->rear)
		{
			ShowNode(p);
			printf("->");
			p = p->next;
		}
		ShowNode(p);
		printf("\n");
	}
}

void ShowNode (Node *p)
{
	int k = 0;
	while (p->name[k] != '\0')
	{
		putchar(p->name[k]);
		k++;
	}
}

void Insert (Queue *q)
{
	if (IsQueueFull(q))
	{
		printf("当前队列已满,无法进队。\n");
	} 
	else
	{
		Node *p = (Node *)malloc(sizeof(Node));
		printf("输入姓名:");
		scanf("%s", p->name);
		ClearBuffer();
		p->next = NULL;
		IntoQueue(q, p);
		printf("完成进队。");
		ShowQueue(q);
	}
}

void IntoQueue (Queue *q, Node *p)
{
	if (IsQueueEmpty(q))
	{
		q->front = p;
		q->rear = p;
	} 
	else
	{
		q->rear->next = p;
		q->rear = p;
	}
	q->length++;
}

void OutOfQueue (Queue *q)
{
	Node *p;

	if (IsQueueEmpty(q))
	{
		printf("队列为空,无法出队。\n");
	} 
	else
	{
		p = q->front;
		if (GetQueueLength(q) == 1)
		{
			q->front = NULL;
			q->rear = NULL;
		} 
		else
		{
			q->front = p->next;
		}
		q->length--;
		printf("出队者的姓名是:");
		ShowNode(p);
		free(p);
		printf("\n出队完成。");
		ShowQueue(q);
	} 
}

void ClearQueue(Queue *q)
{
	if (IsQueueEmpty(q))
	{
		printf("当前队列已经为空。\n");
	} 
	else
	{
		Node *p;

		while (q->front != q->rear)
		{
			p = q->front;
			q->front = p->next;
			free(p);
		}
		p = q->front;
		q->front = NULL;
		q->rear = NULL;
		q->length = 0;
		free(p);
		printf("已清空当前队列。\n");
	}
}

void ShowMenu (void)
{
	printf("请选择:\na.进队  b.出队  c.显示  d.长度  e.清空  q.退出\n");
}

void ClearBuffer (void)
{
	while (getchar() != '\n')
		continue;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值