这篇文章给大家介绍使用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);
}
关于使用C语言怎么实现链队列就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。