链队列-C语言版

源文件部分:  指针没学好的同学很难看懂^_^,有点乱,希望对大家有点帮助。
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
typedef int Elemtype;
#include"LQueue.h"
int main()
{
	Deque head;
	instruction(head);
	return 0;
}
头文件部分:
typedef struct Queue
{
	Elemtype data;
	struct Queue *next;
}LQnode,*LQueue;

typedef struct
{
	LQnode *front;
	LQnode *rear;
}Deque;

void Init_queue(Deque *head)    //初始化+清空操作==其实这里的清空是指将头节点后的节点给丢弃掉  
{
	LQnode *p=NULL;
	p=(LQueue)malloc(sizeof(LQnode));
	head->front=p;
	head->rear=p;		
	p->next=NULL;
}

int Empty_queue(Deque *head)           //判空
{
	if(head->front->next==head->rear->next)
		return 1;
	return 0;
}

int Lenght_queue(Deque arrow)
{
	LQnode *p=NULL;
	int len=0;
	p=arrow.front->next;
	while(p)
	{
		len++;
		p=p->next;
	}
	return len;
}

void Enqueue(Deque *arrow,Elemtype e)       //入队操作
{
	LQueue p=NULL;
	p=(LQueue)malloc(sizeof(LQnode));
	if(!p)
	{
		printf("已无更多的内存单元得到分配!\n");
		return ;
	}
	p->data=e;
	p->next=NULL;                 //插入时,队首指针是不需要动的  
	arrow->rear->next=p;
	arrow->rear=p;  
	return ;
}

void Dequeue(Deque *arrow,Elemtype *e)       //出队操作
{
	LQnode *p=NULL;
	if(Empty_queue(arrow))
	{
		printf("当前链队列为空,无法完成出队操作!!!\n");
		return ;
	}
	p=arrow->front->next;
	(*e)=p->data;
	arrow->front->next=p->next;
	printf("元素%d已退出队列!!!\n",*e);
	if(Lenght_queue(*arrow)==0)
		return ;                       //当最后一个元素出列以后,arrow->rear不知道指向了哪里     
	free(p);
	return ;
}

int Queue_top(Deque *arrow)   //返回队首元素 
{
	if(Empty_queue(arrow))
	{
		printf("当前链队列为空,队首元素不存在!!!\n");
		return 0;
	}
	printf("当前队首元素是:%d\n",arrow->front->next->data);
}

void Destroy_queue(Deque *arrow)    //链队列的销毁
{
	LQnode *p=NULL;
	if(Empty_queue(arrow))
	{
		printf("当前链队列为空,无须完成销毁操作!!!\n");
		return ;
	}
	while(arrow->front->next)
	{
		p=arrow->front->next;
		arrow->front->next=p->next;
		if(Lenght_queue(*arrow)==0)
			break;      
		free(p);
	}
	printf("销毁成功!\n");
	return ;
}

void Print_queue(Deque arrow)
{
	LQnode *p=NULL;
	p=arrow.front->next;
	while(p)
	{
		printf("%d ",p->data);
		p=p->next;
	}
	printf("\n");
}

void Modify_queue(Deque *arrow,Elemtype index,Elemtype e)    //修改函数
{
	int i=0;
	LQnode *p=NULL;
	p=arrow->front->next;
	while(i<index-1)
	{
		p=p->next;
	}
	p->data=e;
	printf("已完成修改操作!\n");
}

int Insearch_queue(Deque arrow,Elemtype e)           //查找函数
{
	LQnode *p=NULL;
	int i=1;
	if(Empty_queue(&arrow))
	{
		printf("当前链队列为空,没有元素可查找!!!\n");
		return 0;
	}
	p=arrow.front->next;
	while(p!=NULL)
	{
		if(e==p->data)
		{
			return i;
			break;
		}
		i++;
		p=p->next;
	}
	if(p==NULL)
		printf("查找失败,队列内无该元素存在!\n");
	return 0;
}

void instruction(Deque head)
{
	int n,m,t,a,b,len1,index;
	printf("\t\t1、队列初始化  \n");
	printf("\t\t2、新增队列元素\n");
	printf("\t\t3、返回队首元素\n");
	printf("\t\t4、元素出队列  \n");
	printf("\t\t5、查找队列元素\n");
	printf("\t\t6、修改队列元素\n");
	printf("\t\t7、销毁队列    \n");
	printf("\t\t8、队列的长度  \n");
	printf("\t\t9、打印队列元素\n");
	printf("\t\t10、退出程序    \n");
	printf("请输入你所需要完成的指令:\n");
	do{
		scanf("%d",&n);
		if(n<1||n>10)
			printf("对不起,你输入的指令编号是无效的,请重新输入!!!\n");
	}while(n<1||n>10);
	switch(n)
	{
		case 1:
			Init_queue(&head);
			printf("已完成链队列初始化,请输入你要添加的元素个数!\n");
			scanf("%d",&n);
			while(n--)
			{
				int x;
				scanf("%d",&x);
				Enqueue(&head,x);
			}
			printf("完成建队操作!\n");
			break;
		case 2:
			printf("请输入你要添加的元素个数!\n");
			scanf("%d",&n);
			while(n--)
			{
				int x;
				scanf("%d",&x);
				Enqueue(&head,x);
			}
			printf("增添成功!\n");
			break;
		case 3:
			Queue_top(&head);
			break;
		case 4:
			Dequeue(&head,&t);
			break;
		case 5:
			printf("请输入你所要查找的元素:\n");
			scanf("%d",&m);
			index=Insearch_queue(head,m);
			if(index)
				printf("你所要查找的元素位于队列的第%d个位置上!!!\n",index);
			break;
		case 6:
			printf("请输入你更改的元素队列位置:\n");
			do{
				scanf("%d",&a);
				if(a<1||a>Lenght_queue(head))
					printf("对不起,你所输入的元素位置不在区域内,请重新输入!!!\n");
			}while(a<1||a>Lenght_queue(head));
			printf("请输入修改后的值:\n");
			scanf("%d",&b);
			Modify_queue(&head,a,b);
			break;
		case 7:
			Destroy_queue(&head);
			break;
		case 8:
			len1=Lenght_queue(head);
			printf("当前链队列的长度为:%d\n",len1);
			break;
		case 9:
			Print_queue(head);
			break;
		case 10:
			return;
		default:
			instruction(head);
			break;
	}
	instruction(head);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值