各函式功能如下
申請空間
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);//實作了指定位置插入后,可以套用
}
尾插
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;
}
元素個數
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);
//元素個數
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;
}
總結
鏈表優點:
- 按需申請記憶體,需要存一個資料,就申請一塊記憶體,不存在空間浪費,
- 任意位置O(1)時間內插入洗掉資料
鏈表缺點:
- 不支持下標的隨機訪問
- 快取命中率相對低,
順序表優點
- 按下標去進行隨機訪問
- cpu高速快取命中率比較高
順序表缺點
- 空間不夠需要增容,(一定程式的性能消耗),可能存在一定的空間浪費
- 頭部或者中間插入洗掉資料,需要挪動資料,效率比較低->O(N)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/325581.html
標籤:其他
