最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語言實(shí)現(xiàn)帶頭雙向循環(huán)鏈表的接口

 更新時間:2021年10月20日 11:54:47   作者:__ericZhao  
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)帶頭雙向循環(huán)鏈表的接口,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C語言實(shí)現(xiàn)帶頭雙向循環(huán)鏈表的接口,供大家參考,具體內(nèi)容如下

各函數(shù)功能如下

申請空間

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;
}

元素個數(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);

//申請空間
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);

//元素個數(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.按需申請內(nèi)存,需要存一個數(shù)據(jù),就申請一塊內(nèi)存。不存在空間浪費(fèi)。
2.任意位置O(1)時間內(nèi)插入刪除數(shù)據(jù)

鏈表缺點(diǎn):

1.不支持下標(biāo)的隨機(jī)訪問
2.緩存命中率相對低。

順序表優(yōu)點(diǎn)

1.按下標(biāo)去進(jìn)行隨機(jī)訪問
2.cpu高速緩存命中率比較高

順序表缺點(diǎn)

1.空間不夠需要增容。(一定程序的性能消耗),可能存在一定的空間浪費(fèi)
2.頭部或者中間插入刪除數(shù)據(jù),需要挪動數(shù)據(jù),效率比較低->O(N)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C++移動語義介紹與使用講解

    C++移動語義介紹與使用講解

    首先,移動語義和完美轉(zhuǎn)發(fā)這兩個概念是在C++的模板編程的基礎(chǔ)上,新增的特性,主要是配合模板來使用。本篇會從C++的值類型,到移動拷貝與移動賦值來理解移動語義與完美轉(zhuǎn)發(fā)
    2022-09-09
  • Qt實(shí)現(xiàn)http服務(wù)的示例代碼

    Qt實(shí)現(xiàn)http服務(wù)的示例代碼

    這篇文章將為大家詳細(xì)講解有關(guān)Qt如何實(shí)現(xiàn)http服務(wù),小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲
    2023-04-04
  • C/C++新建注冊表項(xiàng)的代碼示例

    C/C++新建注冊表項(xiàng)的代碼示例

    今天小編就為大家分享一篇關(guān)于C/C++新建注冊表項(xiàng)的代碼示例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • C語言驅(qū)動開發(fā)之內(nèi)核通過PEB獲取進(jìn)程參數(shù)

    C語言驅(qū)動開發(fā)之內(nèi)核通過PEB獲取進(jìn)程參數(shù)

    PEB結(jié)構(gòu)(Process Envirorment Block Structure)其中文名是進(jìn)程環(huán)境塊信息。本文將通過PEB實(shí)現(xiàn)獲取進(jìn)程參數(shù),感興趣的小伙伴可以了解一下
    2022-10-10
  • 簡單介紹C++中變量的引用

    簡單介紹C++中變量的引用

    這篇文章主要簡單介紹了C++中變量的引用,是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-09-09
  • C語言數(shù)據(jù)結(jié)構(gòu)之單鏈表操作詳解

    C語言數(shù)據(jù)結(jié)構(gòu)之單鏈表操作詳解

    鏈表是一種物理存儲結(jié)構(gòu)上非連續(xù)、非順序的存儲結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過鏈表中的指針鏈接次序?qū)崿F(xiàn)的。本文將和大家一起聊聊C語言中單鏈表的常用操作,感興趣的可以學(xué)習(xí)一下
    2022-07-07
  • Visual Studio 2022無法打開源文件的解決方式

    Visual Studio 2022無法打開源文件的解決方式

    這篇文章主要介紹了Visual Studio 2022無法打開源文件的解決方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • C++實(shí)現(xiàn)LeetCode(237.刪除鏈表的節(jié)點(diǎn))

    C++實(shí)現(xiàn)LeetCode(237.刪除鏈表的節(jié)點(diǎn))

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(237.刪除鏈表的節(jié)點(diǎn)),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • 如何寫出優(yōu)美的C語言代碼

    如何寫出優(yōu)美的C語言代碼

    今天小編就為大家分享一篇關(guān)于如何寫出優(yōu)美的C語言代碼,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • C++結(jié)構(gòu)體案例練習(xí)分享

    C++結(jié)構(gòu)體案例練習(xí)分享

    這篇文章主要和大家分享幾個C++?結(jié)構(gòu)體的案例練習(xí),幫助大家更好的理解和學(xué)習(xí)c++,感興趣的朋友可以了解下,希望能夠給你帶來幫助
    2022-04-04

最新評論

名山县| 永清县| 皮山县| 五家渠市| 荔波县| 嵩明县| 镇沅| 濉溪县| 合肥市| 阿拉善盟| 南和县| 宜宾市| 咸丰县| 黑河市| 班玛县| 龙南县| 永年县| 什邡市| 嘉定区| 蒲城县| 北流市| 安新县| 浮山县| 铜鼓县| 区。| 兰溪市| 正安县| 高碑店市| 临沭县| 汽车| 若尔盖县| 怀远县| 昌邑市| 宜州市| 福泉市| 镇安县| 历史| 河西区| 赤城县| 克东县| 新昌县|