溫馨提示×

溫馨提示×

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

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

C語言中怎么實現(xiàn)雙向鏈表

發(fā)布時間:2021-07-02 16:54:48 來源:億速云 閱讀:125 作者:Leah 欄目:編程語言

今天就跟大家聊聊有關(guān)C語言中怎么實現(xiàn)雙向鏈表,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

雙向鏈表的基本操作

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

    2.遍歷雙向鏈表。

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

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

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

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

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

實例代碼:

//排序的時候因為沒有說明奇數(shù)和偶數(shù)需不需要各自再排序,我就沒有排序,只是將奇數(shù)放在偶數(shù)后面。
//創(chuàng)建鏈表的時候,因為這個實驗沒有要求輸出鏈表的長度,所以我就輸入了一個長度為n的鏈表。
#include<stdio.h>
#include<stdlib.h>
struct node
{
 int data;
 node *pre,*next;
}*h,*end;
void CreatList()//創(chuàng)建一個雙向鏈表
{
 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()//刪除鏈表中的某個元素
{
 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()//在有序鏈表中插入某個元素
{
 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)建一個雙向鏈表:           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("輸入錯誤。請重新輸入。\n");
 }
 return 0;
}
int main()
{
 int flag;
 while(1)
 {
 flag=PrintMenu();
 if(flag)//通過flag控制循環(huán)的跳出
  break;
 }
 printf("謝謝使用!\n");
 return 0;
}

看完上述內(nèi)容,你們對C語言中怎么實現(xiàn)雙向鏈表有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

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

AI