C語言之雙向鏈表詳解及實例代碼
1,雙向鏈表簡介。
雙向鏈表也叫雙鏈表,是鏈表的一種,它的每個數(shù)據(jù)結(jié)點中都有兩個指針,分別指向直接后繼和直接前驅(qū)。所以,從雙向鏈表中的任意一個結(jié)點開始,都可以很方便地訪問它的前驅(qū)結(jié)點和后繼結(jié)點。一般我們都構(gòu)造雙向循環(huán)鏈表。
2,例子要求:
完成雙向鏈表的插入、刪除以及查找,將學生管理系統(tǒng)使用的數(shù)組,以雙向鏈表的方式實現(xiàn),能夠支持無限制的學生人數(shù)的增刪改查以及保存。
3,代碼實現(xiàn)。
#include <stdio.h> #include <string.h> #include <stdarg.h> #include <stdlib.h> typedef struct Student{ char name[20]; int score; char phoneNum[14]; } str_student; typedef struct Node{ str_student data; struct Node *prior; //指向前驅(qū)結(jié)點 struct Node *next; //指向后繼結(jié)點 }Node, *DLinkList; // 初始化一個學生鏈表 DLinkList initDouLinkList() { Node *L,*p,*r; char name[20]; char phone[14]; int score; L = (Node *)malloc(sizeof(Node)); L->next = NULL; r = L; r->next = NULL; while(1) { p = (Node *)malloc(sizeof(Node)); printf("input name is out exit,input student name:\n"); scanf("%s",name); if (strcmp(name,"out")==0) { break; } strcpy(p->data.name, name); printf("input student score:"); scanf("%d",&score); p->data.score = score; printf("input student phone:"); scanf("%s",phone); strcpy(p->data.phoneNum, phone); p->next = r->next; r->next = p; r = p; } r->next = NULL; return L; } //添加學生信息 DLinkList insertDouLinkListStuent(DLinkList L,int i,char *name, int score,char *phonenum) { DLinkList p,s; p = L->next; int tempi; for(tempi = 1;tempi < i-1; tempi++) p = p->next; s = (Node *)malloc(sizeof(Node)); s->data.score = score; strcpy(s->data.name,name); strcpy(s->data.phoneNum,phonenum); s->next = p->next; p->next->prior = s; s->prior = p; p->next = s; return L; } // 查找學生信息 int findDouLinkListStudent(DLinkList L,char *name) { DLinkList p; p = L->next; int i = 1; while(p != NULL && (strcmp(p->data.name, name)!=0)) { ++i; p = p->next; } if(p == NULL) return 0; else return i; } // 移除一個學生 DLinkList removeDouLinkListStudent(DLinkList L,char *name) { int tempi = 1; DLinkList p; p = L->next; int i =findDouLinkListStudent(L,name); while((tempi++) != i && p != NULL) { p = p->next; } if(p == NULL) printf("no list \n"); else if(p->next == NULL) { p->prior->next = NULL; free(p); } else { p->prior->next = p->next; p->next->prior = p->prior; free(p); } return L; } // 鋪助打印信息 void printfInfo(DLinkList L) { DLinkList p; p = L->next; while (p!=NULL) { printf("student name %s\n",p->data.name); printf("student name %d\n",p->data.score); printf("student name %s\n",p->data.phoneNum); p=p->next; } } void main () { char name2[20]="hanmeimei"; char phone2[14]="13612345678"; DLinkList L =initDouLinkList(); // 2.1 初始化學生雙向鏈表數(shù)據(jù) insertDouLinkListStuent(L,1,name2,99,phone2); printfInfo(L); // 2.2 查找學生zhangsan findDouLinkListStudent(L,'zhangsan'); printfInfo(L); // 2.3 刪除學生zhangsan removeDouLinkListStudent(L,'zhangsan'); printfInfo(L); // 2.4 添加學生zengteng insertDouLinkListStuent(L,9,'zengteng',89,'13643345667'); printfInfo(L); }
以上就是對C語言雙向鏈表的資料整理,后續(xù)繼續(xù)補充相關(guān)資料,謝謝大家對本站的支持!
上一篇:c語言實現(xiàn)詞頻統(tǒng)計的簡單實例
欄 目:C語言
下一篇:排列和組合算法的實現(xiàn)方法_C語言經(jīng)典案例
本文標題:C語言之雙向鏈表詳解及實例代碼
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/2047.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對數(shù)怎么表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實例總結(jié)
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改