C语言版实现链队列

所属分类: 软件编程 / C 语言 阅读数: 118
收藏 0 赞 0 分享

本文实例为大家分享了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++中四种对象生存期和作用域以及static的用法总结分析

以下是对C++中四种对象生存期和作用域以及static的用法进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++嵌套类与局部类详细解析

从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用该类名时,需要加名字限定
收藏 0 赞 0 分享

C++空类详解

以下是对C++中的空类进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++之友元:友元函数和友元类详解

友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元
收藏 0 赞 0 分享

C++中返回指向函数的指针示例

int (*ff(int)) (int *,int);表示:ff(int)是一个函数,带有一个int型的形参,该函数返回int (*) (int *,int),它是一个指向函数的指针,所指向的函数返回int型并带有两个分别是Int*和int型的形参
收藏 0 赞 0 分享

C数据结构之单链表详细示例分析

以下是对C语言中的单链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C数据结构之双链表详细示例分析

以下是对c语言中的双链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅析如何在c语言中调用Linux脚本

如何在c语言中调用Linux脚本呢?下面小编就为大家详细的介绍一下吧!需要的朋友可以过来参考下
收藏 0 赞 0 分享

深入解析unsigned int 和 int

以下是对unsigned int和int进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅谈C++中的string 类型占几个字节

本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节
收藏 0 赞 0 分享
查看更多