链队列小程序

中午学习的队列程序。我的博客好水~~~ 水水水~~~
q->front->next是头指针   q->front不保存数据
获得第一个数据:
          qnode* p;
          p=q->front->next
          p->data
添加数据:
          q->rear->next=(qnode*)malloc(sizeof(qnode));
          q->rear=q->rear->next
          q->real->data=e;
          q->real->next=NULL:
销毁队列:利用q->rear保存了队列下一个node
          while(q->front)
          {
             q->rear=q->front->next;
             free(q->front);
             q->front=q->rear;
          } 

程序:

// linkqueue.cpp : 定义控制台应用程序的入口点。
//
#include <stdio.h>
#include "stdafx.h"
#include <stdlib.h>

#define OK   1
#define ERROR 0
#define TRUE  1
#define FALSE 0
typedef struct Qnode
{
	int data;
	struct Qnode* next;
}qnode;

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

void init(linkqueue* q)
{
	//q->front 不存data
	q->front = (qnode*)malloc(sizeof(qnode));
	q->rear = q->front;
	(q->front)->next = NULL;
}
int enqueue(linkqueue* q,int e)
{
	q->rear->next = (qnode*)malloc(sizeof(qnode));
	q->rear = q->rear->next;
	if (!q->rear)
		return ERROR;
	q->rear->data = e;
	q->rear->next = NULL;
	return OK;
}
int dequeue(linkqueue* q,int& e)
{
	if (q->front == q->rear)
	{
		printf("linkqueue is empty . can't dequeue\n");
			return ERROR;
	}
	qnode* p;
	p = q->front->next;
	(q->front)->next = p->next;
	e = p->data;
	free(p);
	return OK;
}
int gethead(linkqueue* q)
{
	if (q->front == q->rear)
		return -1;
	else
		return q->front->next->data;
}
void visit(linkqueue* q)
{
	qnode* p;
	p = q->front->next;
	if (q->front == q->rear)
		printf("linkqueue is empty");
	else
	{
		while (p)
		{
			printf("%d-> ", p->data);
			p = p->next;
		}
		printf("\n");
	}
}
void destroy(linkqueue* q)
{
	while (q->front)
	{
		q->rear = q->front->next;
		free(q->front);
		q->front = q->rear;
	}
}
int _tmain(int argc, _TCHAR* argv[])
{
	int n,i;
	linkqueue* queue=(linkqueue*)malloc(sizeof(linkqueue));
	int a,select;
	int e;
	printf("create a empty linkqueue\n");
	init(queue);
	printf("please input linkqueue length\n");
	scanf("%d", &n);
	printf("please input linkqueue calue\n");
	for (i = 0; i < n; i++)
	{
		scanf("%d", &a);
		enqueue(queue, a);
	}
	visit(queue);
	printf("select 1---destroy()\n");
	printf("select 2---visit()\n");
	printf("select 3---gethead()\n");
	printf("select 4---enqueue()\n");
	printf("select 5---dequeue()\n");
	printf("select 6---quit()\n");
	while (1)
	{
		printf("please select (1--6):\n");
		scanf("%d", &select);
		switch (select)
		{
		case 1:
			destroy(queue);
			break;
		case 2:
			visit(queue);
			break;
		case 3:
			printf("queue->front->%d\n", gethead(queue));
			break;
		case 4:
			printf("please inter the value:\n");
			scanf("%d", &e);
			enqueue(queue, e);
			visit(queue);
			break;
		case 5:
			dequeue(queue, e);
			printf("delete head %d\n", e);
			visit(queue);
			break;
		case 6:
			exit(0);
		default:
			break;
		}
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值