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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

双向链表操作c语言 函数,c语言 双向链表的基础操作

發布時間:2023/12/19 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 双向链表操作c语言 函数,c语言 双向链表的基础操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

//頭文件

#pragma?once

typedef?int?DataType;

typedef?struct?LinkList

{

DataType?_data;

struct?LinkList*?_next;

struct?LinkList*?_prev;

}LinkList,*pLinkList;

void?InitList(pLinkList?pNode);

pLinkList?_BuyNode(pLinkList&?pNode,DataType?x);

void?PrintList(pLinkList?pHead);

void?PushBack(pLinkList&?pHead,DataType?x);

void?PopBack(pLinkList&?pHead);

void?PushFront(pLinkList&?pHead,DataType?x);

void?PopFront(pLinkList&?pHead);

pLinkList?Find(pLinkList?pHead,?DataType?x);

void?Insert(pLinkList?pos,DataType?x);

void?Erase(pLinkList&?pHead,?pLinkList?pos);

void?Reverse(pLinkList&?pHead);

void?DestroyList(pLinkList&?pHead);

//函數文件

#include

#include

#include"LinkList.h"

#include

//初始化

void?InitList(pLinkList?pNode)

{

assert(pNode);

if?(pNode->_next?==?NULL)

{

pNode->_prev?=?NULL;

}

}

//創建節點

pLinkList?_BuyNode(pLinkList&?pNode,?DataType?x)

{

pNode?=?(pLinkList)malloc(sizeof(LinkList));

pNode->_data?=?x;

pNode->_next?=?NULL;

pNode->_prev?=?NULL;

return?pNode;

}

//遍歷輸出鏈表數據域

void?PrintList(pLinkList?pHead)

{

pLinkList?head?=?pHead;

if(pHead?==?NULL)

{

printf("鏈表為空!\n");

return;

}

while?(head)

{

printf("%d?",head->_data);

head?=?head->_next;

}

printf("\n");

}

//尾插

void?PushBack(pLinkList&?pHead,?DataType?x)

{

pLinkList?head?=?pHead;

if?(head?==?NULL)

{

_BuyNode(pHead,?x);

return;

}

while?(head->_next)

{

head?=?head->_next;

}

head->_next?=?_BuyNode(head->_next,x);

head->_next->_prev?=?head;

}

//尾刪

void?PopBack(pLinkList&?pHead)

{

pLinkList?head?=?pHead;

pLinkList?tmp?=?NULL;

if?(pHead?==?NULL)

{

printf("鏈表已空!\n");

return;

}

if?(head->_next?==?NULL)

{

free(head);

pHead?=?NULL;

return;

}

while?(head->_next)

{

head?=?head->_next;

}

tmp?=?head->_prev;

tmp->_next?=?NULL;

free(head);

}

//頭插

void?PushFront(pLinkList&?pHead,?DataType?x)

{

pLinkList?head?=?pHead;

pLinkList?tmp?=?pHead;

if?(pHead?==?NULL)

{

_BuyNode(pHead,?x);

return;

}

pHead=_BuyNode(head->_prev,?x);

pHead->_next?=?tmp;

}

//頭刪

void?PopFront(pLinkList&?pHead)

{

pLinkList?tmp?=?pHead;

if?(pHead?==?NULL)

{

printf("鏈表已空!\n");

return;

}

pHead?=?pHead->_next;

if?(pHead)

{

pHead->_prev?=?NULL;

}

free(tmp);

}

//查找

pLinkList?Find(pLinkList?pHead,?DataType?x)

{

pLinkList?head?=?pHead;

assert(pHead);

while?(head)

{

if?(head->_data?==?x)

return?head;

head?=?head->_next;

}

return?NULL;

}

//中插_之后

void?Insert(pLinkList?pos,?DataType?x)

{

pLinkList?tmp?=?pos->_next;

assert(pos);

_BuyNode(pos->_next,?x);

pos->_next->_prev?=?pos;

pos->_next->_next?=?tmp;

}

//中刪

void?Erase(pLinkList&?pHead,?pLinkList?pos)

{

pLinkList?tmp?=?pos;

assert(pos);

if?(pos->_next?==?NULL)

{

pos->_prev->_next?=?NULL;

free(tmp);

return;

}

if?(pos==pHead)

{

pHead?=?pHead->_next;

pHead->_prev?=?NULL;

free(pos);

return;

}

tmp->_prev->_next?=?tmp->_next;

tmp->_next->_prev?=?tmp->_prev;

free(pos);

}

//逆置

void?Reverse(pLinkList&?pHead)

{

pLinkList?head?=?pHead;

pLinkList?tmp?=?pHead;

if?(pHead?==?NULL&&pHead->_next?==?NULL)

return;

while?(head)

{

tmp?=?head->_next;

head->_next?=?head->_prev;

head->_prev?=?tmp;

if?(head->_prev?==?NULL)

pHead?=?head;

head?=?tmp;

}

}

//銷毀

void?DestroyList(pLinkList&?pHead)

{

while?(pHead)

{

PopFront(pHead);

}

}

//測試用例??主函數

#include"LinkList.h"

#include

void?test1()

{

pLinkList?pHead=NULL;

//PushBack(pHead,?1);

//PushBack(pHead,?2);

//PushBack(pHead,?3);

//???PushBack(pHead,?4);

//PushBack(pHead,?5);

//PrintList(pHead);

//PopBack(pHead);

//PopBack(pHead);

//PopBack(pHead);

//PopBack(pHead);

//PopBack(pHead);

//PopBack(pHead);

PushFront(pHead,?1);

PushFront(pHead,?2);

PushFront(pHead,?3);

PushFront(pHead,?4);

PushFront(pHead,?5);

PrintList(pHead);

//PopFront(pHead);

//PopFront(pHead);

//PopFront(pHead);

//PopFront(pHead);

//PopFront(pHead);

//PopFront(pHead);

//printf("%d\n",Find(pHead,?6));

Insert(Find(pHead,?1),?6);

Erase(pHead,Find(pHead,2));

PrintList(pHead);

Reverse(pHead);

PrintList(pHead);

DestroyList(pHead);

PrintList(pHead);

}

int?main()

{

test1();

return?0;

}

總結

以上是生活随笔為你收集整理的双向链表操作c语言 函数,c语言 双向链表的基础操作的全部內容,希望文章能夠幫你解決所遇到的問題。

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