日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构-单循环链表(C语言代码)

發布時間:2025/3/21 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构-单循环链表(C语言代码) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

數據結構純屬新手,小白一枚,歡迎批評指正!
下面這個圖是接下來要實現的單循環鏈表!

直接上代碼OVO!

定義結構體

typedef struct Node {int data; //數據域struct Node* next; //指針域 }Node;

單循環鏈表跟單鏈表類似只有數據域和指向下一個結點的指針域,不過尾結點的指針域指向第一個結點。

創建鏈表

//創建單循環鏈表 Node* initList() {Node* L = (Node*)malloc(sizeof(Node));L->data = 0;L->next = L;return L; }

頭插法

//頭插法 void headInsert(Node* L, int data) {Node* node = (Node*)malloc(sizeof(Node));node->data = data;node->next = L->next;L->next = node;L->data++; }

尾插法

//尾插法 void tailInsert(Node* L,int data) {Node* n = L;Node* node = (Node*)malloc(sizeof(Node));node->data = data;n = L->next;while (n->next != L) {n = n->next;}node->next = n->next;n->next = node;L->data++;}

刪除結點

//刪除結點 int deleteList(Node* L, int data) {Node* node = L->next;Node* preNode = L;while (node != L) {if (node->data == data) {preNode->next = node->next;L->data--;free(node);return TRUE;}preNode = node;node = node->next;}return FALSE; }

遍歷鏈表

//遍歷鏈表 void printList(Node* L) {Node* node = L->next;while (node != L) {printf("%d->", node->data);node = node->next;}printf("NULL\n"); }

全部代碼:

#include <stdio.h> #include <stdlib.h>#define TRUE 1 #define FALSE 0typedef struct Node {int data;struct Node* next; }Node;//創建單循環鏈表 Node* initList() {Node* L = (Node*)malloc(sizeof(Node));L->data = 0;L->next = L;return L; }//頭插法 void headInsert(Node* L, int data) {Node* node = (Node*)malloc(sizeof(Node));node->data = data;node->next = L->next;L->next = node;L->data++; }//尾插法 void tailInsert(Node* L,int data) {Node* n = L;Node* node = (Node*)malloc(sizeof(Node));node->data = data;n = L->next;while (n->next != L) {n = n->next;}node->next = n->next;n->next = node;L->data++;}//刪除結點 int deleteList(Node* L, int data) {Node* node = L->next;Node* preNode = L;while (node != L) {if (node->data == data) {preNode->next = node->next;L->data--;free(node);return TRUE;}preNode = node;node = node->next;}return FALSE; }//遍歷鏈表 void printList(Node* L) {Node* node = L->next;while (node != L) {printf("%d->", node->data);node = node->next;}printf("NULL\n"); }int main(void) {Node* L=initList();headInsert(L,1);headInsert(L,2);headInsert(L,3);headInsert(L,4);headInsert(L,5);tailInsert(L,6);tailInsert(L,7);deleteList(L,1);printList(L);return 0; }

運行截圖:

總結

單循環鏈表是單鏈表的另一種形式,其結構特點鏈表中最后一個結點的指針域不再是結束標記,而是指向整個鏈表的第一個結點,從而使鏈表形成一個環。.
和單鏈表相同,單循環鏈表也有帶頭結點結構和不帶頭結點結構兩種,帶頭結點的循環單鏈表實現插入和刪除操作較為方便。.

總結

以上是生活随笔為你收集整理的数据结构-单循环链表(C语言代码)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。