亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C語言版實(shí)現(xiàn)鏈隊(duì)列

 更新時(shí)間:2018年07月23日 09:24:31   作者:o0非誠勿擾0o  
這篇文章主要為大家詳細(xì)介紹了C語言版實(shí)現(xiàn)鏈隊(duì)列,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C語言實(shí)現(xiàn)鏈隊(duì)列的具體代碼,供大家參考,具體內(nèi)容如下

源文件部分:  指針沒學(xué)好的同學(xué)很難看懂^_^,有點(diǎn)亂,希望對(duì)大家有點(diǎn)幫助。

#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)拥?
 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("請(qǐng)輸入你所需要完成的指令:\n");
 do{
 scanf("%d",&n);
 if(n<1||n>10)
  printf("對(duì)不起,你輸入的指令編號(hào)是無效的,請(qǐng)重新輸入!!!\n");
 }while(n<1||n>10);
 switch(n)
 {
 case 1:
  Init_queue(&head);
  printf("已完成鏈隊(duì)列初始化,請(qǐng)輸入你要添加的元素個(gè)數(shù)!\n");
  scanf("%d",&n);
  while(n--)
  {
  int x;
  scanf("%d",&x);
  Enqueue(&head,x);
  }
  printf("完成建隊(duì)操作!\n");
  break;
 case 2:
  printf("請(qǐng)輸入你要添加的元素個(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("請(qǐng)輸入你所要查找的元素:\n");
  scanf("%d",&m);
  index=Insearch_queue(head,m);
  if(index)
  printf("你所要查找的元素位于隊(duì)列的第%d個(gè)位置上!!!\n",index);
  break;
 case 6:
  printf("請(qǐng)輸入你更改的元素隊(duì)列位置:\n");
  do{
  scanf("%d",&a);
  if(a<1||a>Lenght_queue(head))
   printf("對(duì)不起,你所輸入的元素位置不在區(qū)域內(nèi),請(qǐng)重新輸入!!!\n");
  }while(a<1||a>Lenght_queue(head));
  printf("請(qǐng)輸入修改后的值:\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);
}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • FFmpeg實(shí)戰(zhàn)之利用ffplay實(shí)現(xiàn)自定義輸入流播放

    FFmpeg實(shí)戰(zhàn)之利用ffplay實(shí)現(xiàn)自定義輸入流播放

    ffplay是FFmpeg提供的一個(gè)極為簡單的音視頻媒體播放器,可以用于音視頻播放、可視化分析。本文將利用ffplay實(shí)現(xiàn)自定義輸入流播放,需要的可以參考一下
    2022-12-12
  • C語言庫函數(shù)中qsort()的用法

    C語言庫函數(shù)中qsort()的用法

    大家好,本篇文章主要講的是C語言庫函數(shù)中qsort()的用法,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • C/C++中時(shí)間庫函數(shù)的使用詳解

    C/C++中時(shí)間庫函數(shù)的使用詳解

    這篇文章主要為大家詳細(xì)介紹了C/C++中的時(shí)間相關(guān)知識(shí)總結(jié),例如時(shí)間庫函數(shù)的使用以及獲取本地時(shí)間的不同方法,文中的示例代碼講解詳細(xì),需要的可以參考一下
    2022-11-11
  • C++中判斷成員函數(shù)是否重寫

    C++中判斷成員函數(shù)是否重寫

    這篇文章主要介紹了C++中判斷成員函數(shù)是否重寫的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • C++日期類的實(shí)現(xiàn)日期計(jì)算器舉例詳解

    C++日期類的實(shí)現(xiàn)日期計(jì)算器舉例詳解

    這篇文章主要給大家介紹了關(guān)于C++日期類實(shí)現(xiàn)日期計(jì)算器的相關(guān)資料,我們要考慮日期的增加和減少,自增和自減,以及兩個(gè)日期類的比較,以及當(dāng)前日期類的日期顯示和用戶的輸入輸出,需要的朋友可以參考下
    2024-05-05
  • C語言中函數(shù)返回值不一致問題

    C語言中函數(shù)返回值不一致問題

    這篇文章主要介紹了C語言中函數(shù)返回值不一致問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • C++中的三大函數(shù)和操作符重載(Boolan)

    C++中的三大函數(shù)和操作符重載(Boolan)

    本文主要介紹了C++中的三大函數(shù)和操作符重載(Boolan)的相關(guān)知識(shí)。具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-02-02
  • 詳解C++?STL模擬實(shí)現(xiàn)list

    詳解C++?STL模擬實(shí)現(xiàn)list

    這篇文章主要為大家詳細(xì)介紹了C++如何模擬實(shí)現(xiàn)STL容器list,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C++有一定幫助,需要的可以參考一下
    2023-01-01
  • 深入c++中臨時(shí)對(duì)象的析構(gòu)時(shí)機(jī)的詳解

    深入c++中臨時(shí)對(duì)象的析構(gòu)時(shí)機(jī)的詳解

    本篇文章對(duì)c++中臨時(shí)對(duì)象的析構(gòu)時(shí)機(jī)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C語言每日練習(xí)之動(dòng)態(tài)顯示系統(tǒng)時(shí)間

    C語言每日練習(xí)之動(dòng)態(tài)顯示系統(tǒng)時(shí)間

    這篇文章主要介紹了C語言動(dòng)態(tài)顯示系統(tǒng)時(shí),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-11-11

最新評(píng)論