溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C語言如何實(shí)現(xiàn)雙向鏈表

發(fā)布時(shí)間:2022-04-02 10:29:47 來源:億速云 閱讀:179 作者:iii 欄目:編程語言

本篇內(nèi)容介紹了“C語言如何實(shí)現(xiàn)雙向鏈表”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

雙向鏈表的基本操作

    1.利用尾插法建立一個(gè)雙向鏈表。

    2.遍歷雙向鏈表。

    3.實(shí)現(xiàn)雙向鏈表中刪除一個(gè)指定元素。

    4.在非遞減有序雙向鏈表中實(shí)現(xiàn)插入元素e仍有序算法。

    5.判斷雙向鏈表中元素是否對稱若對稱返回1否則返回0。

    6.設(shè)元素為正整型,實(shí)現(xiàn)算法把所有奇數(shù)排列在偶數(shù)之前。

    7.在主函數(shù)中設(shè)計(jì)一個(gè)簡單的菜單調(diào)試上述算法。

實(shí)例代碼:

//排序的時(shí)候因?yàn)闆]有說明奇數(shù)和偶數(shù)需不需要各自再排序,我就沒有排序,只是將奇數(shù)放在偶數(shù)后面。
//創(chuàng)建鏈表的時(shí)候,因?yàn)檫@個(gè)實(shí)驗(yàn)沒有要求輸出鏈表的長度,所以我就輸入了一個(gè)長度為n的鏈表。
#include<stdio.h>
#include<stdlib.h>
struct node
{
 int data;
 node *pre,*next;
}*h,*end;
void CreatList()//創(chuàng)建一個(gè)雙向鏈表
{
 int n;
 node *s,*e;
 printf("請輸入鏈表長度:    ");
 scanf("%d",&n);
 printf("請輸入數(shù)據(jù):      ");
 h=(node *)malloc(sizeof(node));
 s=(node *)malloc(sizeof(node));
 h->pre=NULL;
 e=h;
 e->next=s;
 s->pre=e;
 while(n--)
 {
 e=s;
 scanf("%d",&s->data);
 s=(node *)malloc(sizeof(node));
 e->next=s;
 s->pre=e;
 }
 s->next=NULL;
 end=s;
 return ;
}
void PrintList()//輸出鏈表
{
 node *s;
 s=h->next;
 printf("鏈表數(shù)據(jù):       ");
 while(s!=end)
 {
 printf("%d ",s->data);
 s=s->next;
 }
 printf("\n");
 return ;
}
void DeletList()//刪除鏈表中的某個(gè)元素
{
 int x;
 int flag;
 node *s,*e;
 printf("請輸入需刪除元素:   ");
 scanf("%d",&x);
 s=h->next;
 e=h;
 flag=0;
 while(s!=end)
 {
 if(s->data==x)
 {
  e->next=s->next;
  s->next->pre=e;
  free(s);
  flag=1;
  break;
 }
 e=s;
 s=e->next;
 }
 if(!flag)
 printf("鏈表中不存在值為%d的元素。\n",x);//如果鏈表中沒有x,輸出這句話。
 return ;
}
void InsetList()//在有序鏈表中插入某個(gè)元素
{
 int x;
 node *s,*e;
 printf("輸入需要插入的元素:  ");
 scanf("%d",&x);
 s=h->next;
 while(1)
 {
 if(s->data>=x)
 {
  e=(node *)malloc(sizeof(node));
  e->data=x;
  e->next=s;
  e->pre=s->pre;
  s->pre->next=e;
  s->pre=e;
  break;
 }
 else if(s==end)//將x放入鏈表末尾
 {
  end=(node *)malloc(sizeof(node));
  s->data=x;
  end->pre=s;
  end->next=NULL;
  s->next=end;
  break;
 }
 s=s->next;
 }
 return ;
}
void JudgeList()//判斷雙向鏈表是否對稱
{
 node *s,*e;
 int flag=0;
 s=h->next;
 e=end->pre;
 while(s->data==e->data&&s!=end&&e!=h)
 {
 s=s->next;
 e=e->pre;
 }
 if(s==end&&e==h)
 printf("鏈表對稱。\n");
 else
 printf("鏈表不對稱。\n");
 return ;
}
void SortList()//將鏈表中的奇數(shù)放在偶數(shù)后面
{
 node *s;
 node *odd;
 int temp;
 odd=h->next;
 s=h->next;
 while(s!=end)
 {
 if(s->data%2!=0)
 {
  temp=odd->data;
  odd->data=s->data;
  s->data=temp;
  odd=odd->next;
  s=s->next;
 }
 else
  s=s->next;
 }
 return ;
}
int PrintMenu()//打印目錄
{
 int T;
 printf("******************目錄******************\n");
 printf("創(chuàng)建一個(gè)雙向鏈表:           1\n");
 printf("輸出鏈表:               2\n");
 printf("刪除鏈表中的指定元素:         3\n");
 printf("向鏈表中插入元素:           4\n");
 printf("判斷鏈表是否對稱:           5\n");
 printf("排列鏈表:               6\n");
 printf("操作結(jié)束:               0\n");
 printf("輸入操作指令:     ");
 scanf("%d",&T);
 switch(T)
 {
 case 1:CreatList();break;
 case 2:PrintList();break;
 case 3:DeletList();break;
 case 4:InsetList();break;
 case 5:JudgeList();break;
 case 6:SortList();break;
 case 0:return 1;
 default:printf("輸入錯(cuò)誤。請重新輸入。\n");
 }
 return 0;
}
int main()
{
 int flag;
 while(1)
 {
 flag=PrintMenu();
 if(flag)//通過flag控制循環(huán)的跳出
  break;
 }
 printf("謝謝使用!\n");
 return 0;
}

“C語言如何實(shí)現(xiàn)雙向鏈表”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI