數(shù)據(jù)結(jié)構(gòu) C語(yǔ)言實(shí)現(xiàn)循環(huán)單鏈表的實(shí)例
更新時(shí)間:2017年05月10日 11:39:11 投稿:lqh
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu) C語(yǔ)言實(shí)現(xiàn)循環(huán)單鏈表的實(shí)例的相關(guān)資料,需要的朋友可以參考下
數(shù)據(jù)結(jié)構(gòu) C語(yǔ)言實(shí)現(xiàn)循環(huán)單鏈表的實(shí)例
實(shí)例代碼:
//=========楊鑫========================// //循環(huán)單鏈表的實(shí)現(xiàn) #include <stdio.h> #include <stdlib.h> typedef int ElemType; //定義結(jié)點(diǎn)類型 typedef struct Node { ElemType data; struct Node *next; }Node,*LinkedList; int count = 0; //1、單循環(huán)鏈表的初始化 LinkedList init_circular_linkedlist() { Node *L; L = (Node *)malloc(sizeof(Node)); if(L == NULL) printf("申請(qǐng)內(nèi)存空間失敗\n"); L->next = L; } //2、循環(huán)單鏈表的建立 LinkedList creat_circular_linkedlist() { Node *L; L = (Node *)malloc(sizeof(Node)); L->next = L; Node *r; r = L; ElemType x; while(scanf("%d",&x)) { if(x == 0) break; count++; Node *p; p = (Node *)malloc(sizeof(Node)); p->data = x; r->next = p; r = p; } r->next = L; return L; } //4、循環(huán)單鏈表的插入,在循環(huán)鏈表的第i個(gè)位置插入x的元素 LinkedList insert_circuler_linkedlist(LinkedList L,int i,ElemType x) { Node *pre; pre = L; int tempi = 0; for (tempi = 1; tempi < i; tempi++) pre = pre->next; Node *p; p = (Node *)malloc(sizeof(Node)); p->data = x; p->next = pre->next; pre->next = p; return L; } //5、循環(huán)單鏈表的刪除,在循環(huán)鏈表中刪除值為x的元素 LinkedList delete_circular_linkedlist(LinkedList L,ElemType x) { Node *p,*pre; p = L->next; while(p->data != x) { pre = p; p = p->next; } pre->next = p->next; free(p); return L; } int main() { int i; LinkedList list, start; printf("請(qǐng)輸入循環(huán)單鏈表的數(shù)據(jù), 以0結(jié)束!\n"); list = creat_circular_linkedlist(); printf("循環(huán)單鏈表的元素有:\n"); for(start = list->next; start != NULL; start = start->next) { if(count== 0) { break; } printf("%d ", start->data); count--; } printf("\n"); return 0; }
如圖:
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
C語(yǔ)言動(dòng)態(tài)內(nèi)存的分配實(shí)例詳解
動(dòng)態(tài)內(nèi)存管理同時(shí)還具有一個(gè)優(yōu)點(diǎn),當(dāng)程序在具有更多內(nèi)存的系統(tǒng)上需要處理更多數(shù)據(jù)時(shí),不需要重寫程序,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言動(dòng)態(tài)內(nèi)存分配的相關(guān)資料,需要的朋友可以參考下2022-06-06OpenCV實(shí)現(xiàn)拼接圖像的簡(jiǎn)單方法
這篇文章主要為大家詳細(xì)介紹了OpenCV實(shí)現(xiàn)拼接圖像的簡(jiǎn)單方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05Vscode Remote Development遠(yuǎn)程開發(fā)調(diào)試的實(shí)現(xiàn)思路
這篇文章主要介紹了Vscode Remote Development遠(yuǎn)程開發(fā)調(diào)試的相關(guān)資料,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04C語(yǔ)言之函數(shù)返回值與參數(shù)傳遞案例教程
這篇文章主要介紹了C語(yǔ)言之函數(shù)返回值與參數(shù)傳遞案例教程,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C/C++?Qt?給ListWidget組件增加右鍵菜單功能
本篇文章給大家介紹ListWidget組件增加一個(gè)右鍵菜單,當(dāng)用戶在ListWidget組件中的任意一個(gè)子項(xiàng)下右鍵,我們讓其彈出這個(gè)菜單,并根據(jù)選擇提供不同的功能,感興趣的朋友跟隨小編一起看看吧2021-11-11C++ EnterCriticalSection簡(jiǎn)單使用
線程鎖在多線程中可以控制線程的執(zhí)行順序,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08