使用C語言怎么實(shí)現(xiàn)鏈隊(duì)列-創(chuàng)新互聯(lián)

這篇文章給大家介紹使用C語言怎么實(shí)現(xiàn)鏈隊(duì)列,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

目前成都創(chuàng)新互聯(lián)公司已為1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管網(wǎng)站托管運(yùn)營、企業(yè)網(wǎng)站設(shè)計(jì)、源城網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
#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)  //初始化+清空操作==其實(shí)這里的清空是指將頭節(jié)點(diǎn)后的節(jié)點(diǎn)給丟棄掉 
{
 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)    //入隊(duì)操作
{
 LQueue p=NULL;
 p=(LQueue)malloc(sizeof(LQnode));
 if(!p)
 {
 printf("已無更多的內(nèi)存單元得到分配!\n");
 return ;
 }
 p->data=e;
 p->next=NULL;         //插入時(shí),隊(duì)首指針是不需要?jiǎng)拥?nbsp;
 arrow->rear->next=p;
 arrow->rear=p; 
 return ;
}
 
void Dequeue(Deque *arrow,Elemtype *e)    //出隊(duì)操作
{
 LQnode *p=NULL;
 if(Empty_queue(arrow))
 {
 printf("當(dāng)前鏈隊(duì)列為空,無法完成出隊(duì)操作!!!\n");
 return ;
 }
 p=arrow->front->next;
 (*e)=p->data;
 arrow->front->next=p->next;
 printf("元素%d已退出隊(duì)列!!!\n",*e);
 if(Lenght_queue(*arrow)==0)
 return ;            //當(dāng)最后一個(gè)元素出列以后,arrow->rear不知道指向了哪里   
 free(p);
 return ;
}
 
int Queue_top(Deque *arrow)  //返回隊(duì)首元素 
{
 if(Empty_queue(arrow))
 {
 printf("當(dāng)前鏈隊(duì)列為空,隊(duì)首元素不存在!!!\n");
 return 0;
 }
 printf("當(dāng)前隊(duì)首元素是:%d\n",arrow->front->next->data);
}
 
void Destroy_queue(Deque *arrow)  //鏈隊(duì)列的銷毀
{
 LQnode *p=NULL;
 if(Empty_queue(arrow))
 {
 printf("當(dāng)前鏈隊(duì)列為空,無須完成銷毀操作!!!\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)  //修改函數(shù)
{
 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)      //查找函數(shù)
{
 LQnode *p=NULL;
 int i=1;
 if(Empty_queue(&arrow))
 {
 printf("當(dāng)前鏈隊(duì)列為空,沒有元素可查找!!!\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("查找失敗,隊(duì)列內(nèi)無該元素存在!\n");
 return 0;
}
 
void instruction(Deque head)
{
 int n,m,t,a,b,len1,index;
 printf("\t\t1、隊(duì)列初始化 \n");
 printf("\t\t2、新增隊(duì)列元素\n");
 printf("\t\t3、返回隊(duì)首元素\n");
 printf("\t\t4、元素出隊(duì)列 \n");
 printf("\t\t5、查找隊(duì)列元素\n");
 printf("\t\t6、修改隊(duì)列元素\n");
 printf("\t\t7、銷毀隊(duì)列  \n");
 printf("\t\t8、隊(duì)列的長度 \n");
 printf("\t\t9、打印隊(duì)列元素\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("已完成鏈隊(duì)列初始化,請輸入你要添加的元素個(gè)數(shù)!\n");
  scanf("%d",&n);
  while(n--)
  {
  int x;
  scanf("%d",&x);
  Enqueue(&head,x);
  }
  printf("完成建隊(duì)操作!\n");
  break;
 case 2:
  printf("請輸入你要添加的元素個(gè)數(shù)!\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("你所要查找的元素位于隊(duì)列的第%d個(gè)位置上!!!\n",index);
  break;
 case 6:
  printf("請輸入你更改的元素隊(duì)列位置:\n");
  do{
  scanf("%d",&a);
  if(a<1||a>Lenght_queue(head))
   printf("對不起,你所輸入的元素位置不在區(qū)域內(nèi),請重新輸入!!!\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āng)前鏈隊(duì)列的長度為:%d\n",len1);
  break;
 case 9:
  Print_queue(head);
  break;
 case 10:
  return;
 default:
  instruction(head);
  break;
 }
 instruction(head);
}

關(guān)于使用C語言怎么實(shí)現(xiàn)鏈隊(duì)列就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站muchs.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)頁題目:使用C語言怎么實(shí)現(xiàn)鏈隊(duì)列-創(chuàng)新互聯(lián)
標(biāo)題網(wǎng)址:http://muchs.cn/article16/dddedg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、標(biāo)簽優(yōu)化、靜態(tài)網(wǎng)站、面包屑導(dǎo)航軟件開發(fā)、電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司