linklist.h
創(chuàng)新互聯(lián)自2013年起,先為柳江等服務(wù)建站,柳江等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為柳江企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。#ifndef __LINKLIST_H__ #define __LINKLIST_H__ #include<stdio.h> #include<stdlib.h> #include<assert.h> typedef int DataType; typedef struct LinkNode { DataType data; struct LinkNode* next; }LinkNode,*pLinkNode; typedef struct LinkList { LinkNode* pHead; }LinkList,*pLinkList; void InitLinkList(pLinkList list); void DestoryList(pLinkList list); void PushBack(pLinkList list , DataType x); void PopBack(pLinkList list); void PushFront(pLinkList list , DataType x); void PopFront(pLinkList list); void PrintList(pLinkList list); pLinkNode Find(pLinkList list,DataType x); void Insert(pLinkList list, pLinkNode pos, DataType x); void Remove(pLinkList list, DataType x); void RemoveAll(pLinkList list, DataType x); void Erase(pLinkList list,pLinkNode pos); void BubbleSort(pLinkList list); void SelectSort(pLinkList list); void InsertSort(pLinkList list); #endif //__LINKLIST_H__
linkllist.c
#include"linklist.h" void CreateNode(pLinkNode *newNode, DataType x) //創(chuàng)建節(jié)點(diǎn) { *newNode = (pLinkNode)malloc(sizeof(LinkNode)); if (NULL == *newNode) { printf("out of memory\n"); exit(EXIT_FAILURE); } (*newNode)->data = x; (*newNode)->next = NULL; } void InitLinkList(pLinkList list) //初始化 { list->pHead = NULL; assert(list); } void DestoryList(pLinkList list) { assert(list); if (NULL==list->pHead) //鏈表為空直接返回 { return; } else { pLinkNode cur = list->pHead; //cur指向第一個(gè)結(jié)點(diǎn) while (cur != NULL) { list->pHead = cur->next; //pHead指向cur的下一個(gè)結(jié)點(diǎn),當(dāng)cur是最后一個(gè)結(jié)點(diǎn)時(shí),pHead指向空 free(cur); cur = list->pHead; //cur指向當(dāng)前第一個(gè)結(jié)點(diǎn),當(dāng)鏈表為空時(shí),cur指向空 } } } void PushBack(pLinkList list, DataType x) { pLinkNode newNode = NULL; assert(list); CreateNode(&newNode,x); if (NULL == (list->pHead)) //如果是空鏈表,直接插入頭指針之后 { list->pHead = newNode; } else { pLinkNode cur = list->pHead; while (NULL != (cur->next)) //找到最后一個(gè)結(jié)點(diǎn)cur { cur = cur->next; } cur->next = newNode; } } void PopBack(pLinkList list) { assert(list); if (NULL == list->pHead) { return; } else { pLinkNode cur = list->pHead; if (NULL == cur->next) //如果只有一個(gè)結(jié)點(diǎn) { free(cur); list->pHead= NULL; } else { while (NULL != cur->next->next) //大于一個(gè)結(jié)點(diǎn),先找到倒數(shù)第二個(gè)結(jié)點(diǎn) { cur = cur->next; } free(cur->next); cur->next= NULL; } } } void PushFront(pLinkList list, DataType x) { pLinkNode newNode = NULL; assert(list); CreateNode(&newNode, x); newNode->next =list->pHead; //newNode的指針域先指向第一個(gè)結(jié)點(diǎn) list->pHead= newNode; //頭指針指向newNode } void PopFront(pLinkList list) { pLinkNode cur = list->pHead; //cur指向第一個(gè)結(jié)點(diǎn) assert(list); if (NULL == list->pHead) //空鏈表 { return; } list->pHead = cur->next; //指向第一個(gè)結(jié)點(diǎn)的指針域 free(cur); cur = NULL; } void PrintList(pLinkList list) { pLinkNode cur = list->pHead; assert(list); while (NULL != cur) { printf("%d->", cur->data); cur = cur->next; } printf("over\n"); } pLinkNode Find(pLinkList list, DataType x) { pLinkNode cur = list->pHead; assert(list); while (NULL != cur) { if (cur->data == x) { break; } cur = cur->next; } return cur; } void Insert(pLinkList list, pLinkNode pos, DataType x) //在pos后面插入元素 { pLinkNode cur = list->pHead; pLinkNode newNode = NULL; assert(list); CreateNode(&newNode, x); while (NULL != cur) //先找到這個(gè)位置 { if (cur == pos) { break; } cur = cur->next; } if (NULL != cur) { newNode->next=cur->next; cur->next = newNode; } else { printf("沒有這個(gè)結(jié)點(diǎn)\n"); } } void Remove(pLinkList list, DataType x) { pLinkNode cur = list->pHead; pLinkNode p = list->pHead; assert(list); if (NULL == list->pHead) //空鏈表直接返回 { return; } if (NULL == cur->next) //如果只有一個(gè)結(jié)點(diǎn) { if (cur->data == x) { list->pHead = cur->next; free(cur); return; } } else { if (cur->data == x) //先判斷第一個(gè)結(jié)點(diǎn)是不是要?jiǎng)h除的結(jié)點(diǎn) { list->pHead = cur->next; free(cur); return; } cur = cur->next; while (NULL != cur) { if (cur->data == x) { p->next = cur->next; //p結(jié)點(diǎn)的指針域指向要?jiǎng)h除結(jié)點(diǎn)的指針域 free(cur); return; } p = cur; cur = cur->next; } } } void RemoveAll(pLinkList list, DataType x) { pLinkNode cur = list->pHead; pLinkNode p = NULL; assert(list); if (NULL == list->pHead) { return; } while (NULL != cur) { if (NULL == list->pHead->next) //如果要只有一個(gè)結(jié)點(diǎn) { if (cur->data == x) //如果是則刪除 { list->pHead = cur->next; free(cur); return; } } else if (list->pHead->data == x) //判斷是不是第一個(gè)結(jié)點(diǎn),是則刪除,繼續(xù)判斷 { list->pHead = cur->next; free(cur); cur = list->pHead; } else { break; } } //要?jiǎng)h除的結(jié)點(diǎn)在第一個(gè)結(jié)點(diǎn)之后 cur = cur->next; p = list->pHead ; while (NULL != cur) { if (cur->data == x) { p->next = cur->next; //p結(jié)點(diǎn)的指針域指向要?jiǎng)h除結(jié)點(diǎn)的指針域 free(cur); cur = p; } p = cur; cur = cur->next; } } void Erase(pLinkList list, pLinkNode pos) //刪除pos后面的結(jié)點(diǎn) { pLinkNode cur = list->pHead; pLinkNode p = list->pHead; assert(list); if (NULL == cur) { return; } if (NULL == cur->next) //如果只有一個(gè)結(jié)點(diǎn) { if (cur == pos) { free(cur); list->pHead = NULL; } } else { if (cur == pos) //如果是第一個(gè)結(jié)點(diǎn) { list->pHead = cur->next; free(cur); return; } cur = cur->next; while (NULL != cur) { if (cur == pos) { p->next = cur->next; free(cur); return; } p= cur; cur = cur->next; } } } void BubbleSort(pLinkList list) { pLinkNode cur = list->pHead; pLinkNode p1= list->pHead; int flag = 0; DataType tmp=0; pLinkNode p2 = list->pHead->next; assert(list); if (NULL == list->pHead) { return; } if (NULL == cur->next) { return; } cur = cur->next; while (NULL!=cur) { flag = 1; while (NULL != p2) { if (p1->data > p2->data) { tmp = p1->data; p1->data = p2->data; p2->data = tmp; flag = 0; } p2 = p2->next; p1 = p1->next; } if (flag) { break; } p1 = list->pHead; p2 = list->pHead->next; cur = cur->next; } }
test.c
#include"linklist.h" void Menu() { printf("**********************************\n"); printf("*0.Quit 1.InitLinkList *\n"); printf("*2.PushBack 3.PopBack *\n"); printf("*4.PushFront 5.PopFront *\n"); printf("*6.PrintList 7.Find *\n"); printf("*8.Insert 9.Remove *\n"); printf("*10.RemoveAll 11.Erase *\n"); printf("*12.BubbleSort \n"); printf("**********************************\n\n"); printf("請(qǐng)選擇: "); } void test() { LinkList list; DataType x = 0; pLinkNode pos = NULL; int n = -1; while (1) { Menu(); scanf("%d", &n); switch (n) { case 0: DestoryList(&list); exit(1); break; case 1: InitLinkList(&list); break; case 2: printf("請(qǐng)輸入:"); scanf("%d",&x); PushBack(&list,x); break; case 3: PopBack(&list); break; case 4: printf("請(qǐng)輸入:"); scanf("%d", &x); PushFront(&list,x); break; case 5: PopFront(&list); break; case 6: PrintList(&list); break; case 7: printf("請(qǐng)輸入:"); scanf("%d", &x); pos=Find(&list,x); printf("查找成功\n"); break; case 8: printf("請(qǐng)輸入元素:"); scanf("%d", &x); Insert(&list,pos,x); break; case 9: printf("請(qǐng)輸入:"); scanf("%d", &x); Remove(&list,x); break; case 10: printf("請(qǐng)輸入:"); scanf("%d", &x); RemoveAll(&list,x); break; case 11: Erase(&list,pos); break; case 12: BubbleSort(&list); break; default: printf("選擇無效,請(qǐng)重新選擇\n"); break; } } } int main() { test(); system("pause"); return 0; }
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國(guó)云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開啟,新人活動(dòng)云服務(wù)器買多久送多久。
網(wǎng)站欄目:鏈表的代碼實(shí)現(xiàn)-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://vcdvsql.cn/article34/cdigpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、云服務(wù)器、網(wǎng)站內(nèi)鏈、小程序開發(fā)、面包屑導(dǎo)航、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容