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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

单向循环链表的实现

發布時間:2025/3/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 单向循环链表的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 單向循環鏈表的實現
    • 單向循環鏈表的初始化
    • 向鏈表中插入一個數據
    • 刪除鏈表中的一個數據
    • 查找鏈表中的指定的數據

單向循環鏈表的實現

單向循環鏈表的初始化

//< 初始化單向循環鏈表 void ds_init(node **pNode) {int item;node *temp;node *target;printf("初始化單向循環鏈表\n");while(1){scanf("%d", &item);fflush(stdin);printf("item = %d\n\n", item);if(item == 0)return;if((*pNode) == NULL){ *pNode = (node*)malloc(sizeof(struct CLinkList));if(!(*pNode))exit(0);(*pNode)->data = item;(*pNode)->next = *pNode;}else{//< 指定鏈表為0,也要對鏈表進行初始化for(target = (*pNode); target->next != (*pNode); target = target->next);temp = (node *)malloc(sizeof(struct CLinkList));if(!temp)exit(0);temp->data = item;temp->next = *pNode;target->next = temp;}} }

向鏈表中插入一個數據

void ds_insert(node **pNode , int i) {node *temp;node *target;node *p;int item;int j = 1;printf("向鏈表中插入數據:");scanf("%d", &item);if(i == 1){ temp = (node *)malloc(sizeof(struct CLinkList));if(!temp)exit(0);temp ->data = item;for(target = (*pNode); target->next != (*pNode); target = target->next);temp->next = (*pNode);target->next = temp;*pNode = temp;}else{target = *pNode;for( ; j < (i-1); ++j ){target=target->next;}temp = (node *)malloc(sizeof(struct CLinkList));if(!temp)exit(0);temp ->data = item;p = target->next;target->next = temp;temp->next = p;} }

刪除鏈表中的一個數據

void ds_delete(node **pNode, int i) {node *target;node *temp;int j = 1;if(i == 1){ for(target = *pNode; target->next != *pNode;target = target->next);temp = *pNode;*pNode = (*pNode)->next;target->next = *pNode;free(temp);}else{target = *pNode;for( ; j < i-1; ++j ){target = target->next;}temp = target->next;target->next = temp->next;free(temp);} }

查找鏈表中的指定的數據

//< 查找鏈表中的指定的數據 int ds_search(node *pNode, int elem) {node *target;int i = 1;for(target = pNode; target->data != elem && target->next != pNode; ++i){target = target->next;}if(target->next == pNode) return 0;elsereturn i; } #include <stdio.h> #include <stdlib.h>/**** 單向循環鏈表示例* * */typedef struct CLinkList {int data;struct CLinkList *next; }node;void ds_init(node **pNode);void ds_insert(node **pNode , int i);void ds_delete(node **pNode, int i);int ds_search(node *pNode, int elem);void ds_traverse(node *pNode);//< 初始化單向循環鏈表 void ds_init(node **pNode) {int item;node *temp;node *target;printf("初始化單向循環鏈表\n");while(1){scanf("%d", &item);fflush(stdin);printf("item = %d\n\n", item);if(item == 0)return;if((*pNode) == NULL){ //< 指定的數據為0 也要對鏈表進行初始化*pNode = (node*)malloc(sizeof(struct CLinkList));if(!(*pNode))exit(0);(*pNode)->data = item;(*pNode)->next = *pNode;}else{//< 指定鏈表為0,也要對鏈表進行初始化for(target = (*pNode); target->next != (*pNode); target = target->next);temp = (node *)malloc(sizeof(struct CLinkList));if(!temp)exit(0);temp->data = item;temp->next = *pNode;target->next = temp;}} }void ds_insert(node **pNode , int i) {node *temp;node *target;node *p;int item;int j = 1;printf("向鏈表中插入數據:");scanf("%d", &item);if(i == 1){ temp = (node *)malloc(sizeof(struct CLinkList));if(!temp)exit(0);temp ->data = item;for(target = (*pNode); target->next != (*pNode); target = target->next);temp->next = (*pNode);target->next = temp;*pNode = temp;}else{target = *pNode;for( ; j < (i-1); ++j ){target=target->next;}temp = (node *)malloc(sizeof(struct CLinkList));if(!temp)exit(0);temp ->data = item;p = target->next;target->next = temp;temp->next = p;} }void ds_delete(node **pNode, int i) {node *target;node *temp;int j = 1;if(i == 1){ for(target = *pNode; target->next != *pNode;target = target->next);temp = *pNode;*pNode = (*pNode)->next;target->next = *pNode;free(temp);}else{target = *pNode;for( ; j < i-1; ++j ){target = target->next;}temp = target->next;target->next = temp->next;free(temp);} }//< 查找鏈表中的指定的數據 int ds_search(node *pNode, int elem) {node *target;int i = 1;for(target = pNode; target->data != elem && target->next != pNode; ++i){target = target->next;}if(target->next == pNode) return 0;elsereturn i; }//< 打印處單向鏈表 void ds_traverse(node *pNode) {node *temp;printf("***********鏈表中的數據******************\n");temp = pNode;printf("***********鏈表中的數據******************\n");do{printf("%4d ", temp->data);}while((temp = temp->next) != pNode);printf("\n"); }int main() {node *pHead = NULL;char opp;int find;printf("1.初始化單向循環鏈表 \n\n2.插入數據 \n\n3.刪除一個節點 \n\n4.搜索鏈表中指定的數據 \n\n5.打印鏈表數據 \n\n0.退出 \n\n請輸入你的選項");while(opp != '0'){scanf("%c", &opp);switch(opp){case '1':ds_init(&pHead);printf("\n");//< 如果沒有初始化的鏈表直接使用ds_traverse 函數會造成段錯誤// ds_traverse(pHead);break;case '2':printf("向鏈表中插入數據");scanf("%d", &find);ds_insert(&pHead, find);printf("插入的數據為:%d\n", find);ds_traverse(pHead);printf("\n");break;case '3':printf("刪除指定的鏈表節點");scanf("%d", &find);ds_delete(&pHead, find);printf("刪除的節點為: %d\n", find);ds_traverse(pHead);printf("\n");break;case '4':printf("搜索鏈表中指定的數據");scanf("%d", &find);printf("要查找的數據為%d\n", find, ds_search(pHead, find));//ListTraverse(L);printf("\n");break;case '5':ds_traverse(pHead);printf("\n");break;case '6':exit(0);}}return 0; }

總結

以上是生活随笔為你收集整理的单向循环链表的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

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