C語(yǔ)言實(shí)現(xiàn)帶頭雙向循環(huán)鏈表的接口
本文實(shí)例為大家分享了C語(yǔ)言實(shí)現(xiàn)帶頭雙向循環(huán)鏈表的接口,供大家參考,具體內(nèi)容如下
各函數(shù)功能如下
申請(qǐng)空間
ListNode* BuyListNode(LTDataType x) { ListNode* node = (ListNode*)malloc(sizeof(ListNode)); node->next = NULL; node->prev = NULL; node->data = x; return node; }
初始化
ListNode* ListInit() { ListNode* phead = BuyListNode(0); phead->next = phead; phead->prev = phead; return phead; }
指定位置插入
void ListInsert(ListNode* pos, LTDataType x) { assert(pos); ListNode* prev = pos->prev; ListNode* newnode = BuyListNode(x); prev->next = newnode; newnode->prev = prev; newnode->next = pos; pos->prev = newnode; }
頭插
void ListPushFront(ListNode* phead, LTDataType x) { //assert(phead); //ListNode* first = phead->next; //ListNode* newnode = BuyListNode(x); phead newnode first //phead->next = newnode; //newnode->prev = phead; //newnode->next = first; //first->prev = newnode; ListInsert(phead->next, x);//實(shí)現(xiàn)了指定位置插入后,可以套用 }
尾插
void ListPushBack(ListNode* phead, LTDataType x) { //assert(phead); //ListNode* tail = phead->prev; //ListNode* newnode = BuyListNode(x); //tail->next = newnode; //newnode->prev = tail; //newnode->next = phead; //phead->prev = newnode; ListInsert(phead, x); }
指定位置刪除
void ListErase(ListNode* pos) { assert(pos); ListNode* prev = pos->prev; ListNode* next = pos->next; prev->next = next; next->prev = prev; free(pos); }
頭刪
void ListPopFront(ListNode* phead) { //assert(phead); //assert(phead->next != phead); //ListNode* first = phead->next; //ListNode* second = first->next; //free(first); //phead->next = second; //second->prev = phead; ListErase(phead->next); }
尾刪
void ListPopBack(ListNode* phead) { //assert(phead); //assert(phead->next != phead); //ListNode* tail = phead->prev; //ListNode* tailPrev = tail->prev; //free(tail); //tailPrev->next = phead; //phead->prev = tailPrev; ListErase(phead->prev); }
查找
ListNode* ListFind(ListNode* phead, LTDataType x) { assert(phead); ListNode* cur = phead->next; while (cur) { if (cur->data == x) { return cur; } cur = cur->next; } return NULL; }
判空
int ListEmpty(ListNode* phead) { assert(phead); return phead->next == phead ? 1 : 0; }
元素個(gè)數(shù)
int ListSize(ListNode* phead) { assert(phead); int size = 0; ListNode* cur = phead->next; while (cur != phead) { size++; cur = cur->next; } return size; }
鏈表銷毀
void ListDestory(ListNode* phead) { assert(phead); ListNode* cur = phead->next; while (cur != phead) { ListNode* next = cur->next; free(cur); cur = next; } free(phead); phead = NULL; }
List.h
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include <stdlib.h> #include <stdio.h> #include <assert.h> typedef int LTDataType; typedef struct ListNode { struct ListNode* next; struct ListNode* prev; LTDataType data; }ListNode; //打印 void ListPrint(ListNode* phead); //申請(qǐng)空間 ListNode* BuyListNode(LTDataType x); //初始化 ListNode* ListInit(); //尾插 void ListPushBack(ListNode* phead, LTDataType x); //頭插 void ListPushFront(ListNode* phead, LTDataType x); //尾刪 void ListPopBack(ListNode* phead); //頭刪 void ListPopFront(ListNode* phead); //查找 ListNode* ListFind(ListNode* phead, LTDataType x); //插入 void ListInsert(ListNode* pos, LTDataType x); //刪除 void ListErase(ListNode* pos); //空返回1,非空返回0 int ListEmpty(ListNode* phead); //元素個(gè)數(shù) int ListSize(ListNode* phead); //鏈表銷毀 void ListDestory(ListNode* phead);
List.c
#include "List.h" ListNode* BuyListNode(LTDataType x) { ListNode* node = (ListNode*)malloc(sizeof(ListNode)); node->next = NULL; node->prev = NULL; node->data = x; return node; } ListNode* ListInit() { ListNode* phead = BuyListNode(0); phead->next = phead; phead->prev = phead; return phead; } //打印 void ListPrint(ListNode* phead) { ListNode* cur = phead->next; while (cur != phead) { printf("%d ", cur->data); cur = cur->next; } puts("\n------------------------------------------------\n"); } void ListPushBack(ListNode* phead, LTDataType x) { //assert(phead); //ListNode* tail = phead->prev; //ListNode* newnode = BuyListNode(x); //tail->next = newnode; //newnode->prev = tail; //newnode->next = phead; //phead->prev = newnode; ListInsert(phead, x); } //頭插 void ListPushFront(ListNode* phead, LTDataType x) { //assert(phead); //ListNode* first = phead->next; //ListNode* newnode = BuyListNode(x); phead newnode first //phead->next = newnode; //newnode->prev = phead; //newnode->next = first; //first->prev = newnode; ListInsert(phead->next, x); } //尾刪 void ListPopBack(ListNode* phead) { //assert(phead); //assert(phead->next != phead); //ListNode* tail = phead->prev; //ListNode* tailPrev = tail->prev; //free(tail); //tailPrev->next = phead; //phead->prev = tailPrev; ListErase(phead->prev); } //頭刪 void ListPopFront(ListNode* phead) { //assert(phead); //assert(phead->next != phead); //ListNode* first = phead->next; //ListNode* second = first->next; //free(first); //phead->next = second; //second->prev = phead; ListErase(phead->next); } //查找 ListNode* ListFind(ListNode* phead, LTDataType x) { assert(phead); ListNode* cur = phead->next; while (cur) { if (cur->data == x) { return cur; } cur = cur->next; } return NULL; } //插入 void ListInsert(ListNode* pos, LTDataType x) { assert(pos); ListNode* prev = pos->prev; ListNode* newnode = BuyListNode(x); prev->next = newnode; newnode->prev = prev; newnode->next = pos; pos->prev = newnode; } //刪除 void ListErase(ListNode* pos) { assert(pos); ListNode* prev = pos->prev; ListNode* next = pos->next; prev->next = next; next->prev = prev; free(pos); } //空返回1,非空返回0 int ListEmpty(ListNode* phead) { assert(phead); return phead->next == phead ? 1 : 0; } int ListSize(ListNode* phead) { assert(phead); int size = 0; ListNode* cur = phead->next; while (cur != phead) { size++; cur = cur->next; } return size; } void ListDestory(ListNode* phead) { assert(phead); ListNode* cur = phead->next; while (cur != phead) { ListNode* next = cur->next; free(cur); cur = next; } free(phead); phead = NULL; }
test.c
#include "List.h" void TestList1() { ListNode* plist = ListInit(); ListPushBack(plist, 1); ListPushBack(plist, 2); ListPushBack(plist, 3); ListPushBack(plist, 4); ListPrint(plist); ListPushFront(plist, 0); ListPushFront(plist, -1); ListPushFront(plist, -2); ListPrint(plist); ListPopFront(plist); ListPopFront(plist); ListPopFront(plist); ListPrint(plist); ListDestory(plist); plist = NULL; } int main() { TestList1(); return 0; }
總結(jié)
鏈表優(yōu)點(diǎn):
1.按需申請(qǐng)內(nèi)存,需要存一個(gè)數(shù)據(jù),就申請(qǐng)一塊內(nèi)存。不存在空間浪費(fèi)。
2.任意位置O(1)時(shí)間內(nèi)插入刪除數(shù)據(jù)
鏈表缺點(diǎn):
1.不支持下標(biāo)的隨機(jī)訪問
2.緩存命中率相對(duì)低。
順序表優(yōu)點(diǎn)
1.按下標(biāo)去進(jìn)行隨機(jī)訪問
2.cpu高速緩存命中率比較高
順序表缺點(diǎn)
1.空間不夠需要增容。(一定程序的性能消耗),可能存在一定的空間浪費(fèi)
2.頭部或者中間插入刪除數(shù)據(jù),需要挪動(dòng)數(shù)據(jù),效率比較低->O(N)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C語(yǔ)言中帶頭雙向循環(huán)鏈表基本操作的實(shí)現(xiàn)詳解
- C語(yǔ)言帶頭雙向循環(huán)鏈表的示例代碼
- 詳解C語(yǔ)言如何實(shí)現(xiàn)雙向帶頭循環(huán)鏈表
- C語(yǔ)言詳解如何實(shí)現(xiàn)帶頭雙向循環(huán)鏈表
- C語(yǔ)言手把手帶你掌握帶頭雙向循環(huán)鏈表
- C語(yǔ)言超詳細(xì)講解數(shù)據(jù)結(jié)構(gòu)中雙向帶頭循環(huán)鏈表
- C語(yǔ)言實(shí)現(xiàn)帶頭雙向循環(huán)鏈表
- C語(yǔ)言編程數(shù)據(jù)結(jié)構(gòu)帶頭雙向循環(huán)鏈表全面詳解
- C語(yǔ)言超詳細(xì)講解雙向帶頭循環(huán)鏈表
相關(guān)文章
算法學(xué)習(xí)入門之使用C語(yǔ)言實(shí)現(xiàn)各大基本的排序算法
這篇文章主要介紹了使用C語(yǔ)言實(shí)現(xiàn)各大基本的排序算法的方法,同時(shí)也對(duì)算法的選擇問題上給出了一些建議,的朋友可以參考下2015-12-12基于C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單學(xué)生成績(jī)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了基于C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單學(xué)生成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08C語(yǔ)言實(shí)現(xiàn)宿舍管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)宿舍管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06C++深入講解類與對(duì)象之OOP面向?qū)ο缶幊膛c封裝
學(xué)習(xí)過(guò)C語(yǔ)言的小伙伴知道:C語(yǔ)言是面向過(guò)程的,關(guān)注的是過(guò)程,分析出求解問題的步驟,通過(guò)函數(shù)調(diào)用逐步解決問題,接下來(lái)讓我們?cè)敿?xì)的了解2022-05-05Objective-C中常用的結(jié)構(gòu)體NSRange,NSPoint,NSSize(CGSize),NSRect實(shí)例分析
這篇文章主要介紹了Objective-C中常用的結(jié)構(gòu)體NSRange,NSPoint,NSSize(CGSize),NSRect實(shí)例分析,有助于更加直觀的理解Object-C常用的結(jié)構(gòu)體,需要的朋友可以參考下2014-07-07