国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

循環(huán)單鏈表的基本操作

2019-11-14 10:14:58
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

代碼示例

/* function:循環(huán)單鏈表的基本操作 created by : xilong date: 2017.2.4*/#include "iostream"using namespace std;#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0typedef int Elemtype;typedef int Status;typedef struct Node{ Elemtype data; struct Node *next;} Node;typedef struct Node *CLinkList;/* 功能:初始化一個(gè)循環(huán)空鏈表*/CLinkList CLinkList_Init(){ CLinkList head; head = (CLinkList)malloc(sizeof(CLinkList)); head->next = head; return head;}/* 功能:創(chuàng)建循環(huán)鏈表*/void CreateCLinkList(CLinkList *head){ CLinkList p, s; p = *head; int flag = 1; double c; while (flag) { cin >> c; if (c != -99999) { s = (CLinkList)malloc(sizeof(CLinkList)); s->data = c; s->next = *head; // 因?yàn)槭俏膊宸ǎ陨暾?qǐng)結(jié)點(diǎn)的next指向鏈表頭,構(gòu)成循環(huán) p->next = s; p = s; } else { flag = 0; } }}/* 功能:循環(huán)鏈表中元素的個(gè)數(shù)*/int CLinkList_Length(CLinkList *head){ CLinkList p; p = *head; int count = 0; while (p->next != *head) { count++; p = p->next; } return count;}/* 功能:在第 i 個(gè)位置插入一個(gè)元素*/Status CLinkList_Insert(CLinkList *head, int i, Elemtype e){ CLinkList PRe, s; pre = *head; int k = 1; while (pre && k < i) // 找到第 i-1 個(gè)元素 { pre = pre->next; k++; } if (!pre || k > i) { cout << "插入位置錯(cuò)誤!" << endl; return ERROR; } if (i > CLinkList_Length(head) + 1) { cout << "插入位置錯(cuò)誤!" << endl; return ERROR; } else { s = (CLinkList)malloc(sizeof(CLinkList)); s->data = e; s->next = pre->next; pre->next = s; } return OK;}/* 功能:刪除第 i 個(gè)元素,并將其值賦給*e*/Status CLinkList_Delete(CLinkList *head, int i, Elemtype *e){ CLinkList pre, r; pre = *head; int k = 1; while (pre && k < i) // 找到第 i-1 個(gè)元素 { pre = pre->next; k++; } if (!pre || k > i) { cout << "刪除位置錯(cuò)誤!" << endl; return ERROR; } r = pre->next; if (i > CLinkList_Length(head)) { cout << "刪除位置錯(cuò)誤!" << endl; return ERROR; } else pre->next = pre->next->next; *e = r->data; //free(r); return OK;}/* 功能:查找第 i 個(gè)元素,并將查找到的元素放入 *e 中*/Status CLinkList_GetData(CLinkList *head, int i, Elemtype *e){ CLinkList p; p = *head; int k = 0; while (p && k < i) // 找到第 i 個(gè)元素 { p = p->next; k++; } if (!p || k > i) { cout << "查找位置錯(cuò)誤!" << endl; return ERROR; } if (i > CLinkList_Length(head) || i <= 0) { cout << "查找位置錯(cuò)誤!" << endl; return ERROR; }else { *e = p->data; } return OK;}/* 功能:打印整個(gè)鏈表*/Status PrintList(CLinkList *head){ CLinkList p; p = (*head)->next; if (p != NULL) { while (p != *head) { cout << p->data << " "; p = p->next; } } else { cout << "沒(méi)有元素!" << endl; return ERROR; } cout << endl; return OK;}void main(){ CLinkList head; Elemtype e; cout << "開(kāi)始初始化..............................................." << endl; head = CLinkList_Init(); cout << "初始化操作完畢!" << endl; cout << "開(kāi)始建表(這里是尾插法建表,輸入-99999結(jié)束建表)..........." << endl; CreateCLinkList(&head); cout << "建表操作完畢!" << endl; cout << "打印線性表中的所有數(shù)據(jù):"; PrintList(&head); cout << "打印線性表的長(zhǎng)度:"; int count = CLinkList_Length(&head); cout << count << endl; cout << "-------------------------------------------------" << endl; cout << "開(kāi)始插入(在第6個(gè)位置插入81)............................" << endl; CLinkList_Insert(&head, 6, 81); cout << "插入操作完畢!" << endl; cout << "打印線性表中的所有數(shù)據(jù):"; PrintList(&head); cout << "打印線性表的長(zhǎng)度:"; int count2 = CLinkList_Length(&head); cout << count2 << endl; cout << "-------------------------------------------------" << endl; cout << "開(kāi)始刪除(這里刪除第2個(gè)元素)............................" << endl; CLinkList_Delete(&head, 2, &e); cout << "刪除操作完畢!" << endl; cout << "刪除后打印線性表中的所有數(shù)據(jù):"; PrintList(&head); cout << "-------------------------------------------------" << endl; cout << "開(kāi)始查找(這里查找第5個(gè)元素)............................." << endl; if (CLinkList_GetData(&head, 5, &e)) { cout << "查找操作完畢!" << endl; cout << "打印查找到的數(shù)據(jù):"; cout << e << endl; } else { cout << "查找位置錯(cuò)誤!" << endl; } system("pause");}

程序截圖

這里寫(xiě)圖片描述


說(shuō)明:

  程序中不管是插入,刪除和查找函數(shù),都做了判斷輸入的位置是否合法,比如查找第0號(hào)元素,會(huì)返回查找位置錯(cuò)誤,或者是查找的位置大于鏈表長(zhǎng)度,也會(huì)返回查找位置錯(cuò)誤,刪除和插入做了同樣的處理,判斷輸入的位置是否合法。


上一篇:sql總結(jié)

下一篇:代碼筆記(1)

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 洪洞县| 高邑县| 双桥区| 巴林左旗| 修文县| 江源县| 桂林市| 中宁县| 璧山县| 洪湖市| 东源县| 乐平市| 曲周县| 菏泽市| 宜城市| 措美县| 阿荣旗| 防城港市| 赤峰市| 绥化市| 南川市| 临朐县| 许昌县| 思南县| 巴青县| 新龙县| 新邵县| 汾西县| 崇仁县| 宜春市| 迁安市| 平远县| 修文县| 广灵县| 洛宁县| 昌宁县| 安康市| 青河县| 清河县| 福泉市| 大新县|