双向循环链表,主要用于STL中的list实现,存储数据
单向不循环链表,主要用于oj题
顺序表(数组)与链表的区别:
顺序表(数组)扩容有消耗,且存在空间的浪费,头插头删中间插入中间删除效率低O(n)
链表不支持随机访问,头插头删中间插入中间删除效率高O(1)
list.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int LTDataType;
typedef struct ListNode ListNode;
struct ListNode {
struct ListNode* next;
struct ListNode* prev;
LTDataType data;
};
void ListInite(ListNode** pphead);
void ListDestory(ListNode** pphead);
void ListPushBack(ListNode* phead,LTDataType val);
void ListPopBack(ListNode* phead);
void ListPushFront(ListNode* phead, LTDataType val);
void ListPopFront(ListNode* phead);
void ListPrint(ListNode* phead);
void ListInsert(ListNode* pos, LTDataType val);
void ListErase(ListNode* pos);
ListNode* ListFind(ListNode* phead, LTDataType val);
list.c
#include "List.h"
ListNode* BuyNode(LTDataType val) {
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
if (node == NULL) {
printf("fail\n");
exit(-1);
}
node->prev = NULL;
node->next = NULL;
node->data = val;
return node;
}
void ListInite(ListNode** pphead) {
*pphead= BuyNode(0);
(*pphead)->next = *pphead;
(*pphead)->prev = *pphead;
}
void ListPushBack(ListNode* phead, LTDataType val) {
assert(phead);
/*ListNode* newnode = BuyNode(val);
ListNode* tail = phead->prev;
tail->next = newnode;
newnode->prev = tail;
phead->prev = newnode;
newnode->next = phead;*/
ListInsert(phead->prev, val);
}
void ListPrint(ListNode* phead) {
assert(phead);
ListNode* cur= phead->next;
while (cur != phead) {
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
void ListClear(ListNode* phead) {
assert(phead);
ListNode* cur = phead->next;
while (cur != phead) {
ListNode* del = cur;
cur = cur->next;
free(del);
}
phead->next = NULL;
phead->prev = NULL;
}
void ListDestory(ListNode** pphead) {
assert(*pphead);
ListClear(*pphead);
free(*pphead);
*pphead = NULL;
}
void ListPushFront(ListNode* phead, LTDataType val) {
assert(phead);
/*ListNode* first = phead->next;
ListNode* newnode = BuyNode(val);
phead->next = newnode;
newnode->next = first;
newnode->prev = phead;
first->prev = newnode;*/
ListInsert(phead->next, val);
}
void ListPopBack(ListNode* phead) {
assert(phead);
/*if (phead == phead->next)return;
ListNode* tail = phead->prev;
ListNode* tailPrev = tail->prev;
tailPrev->next = phead;
phead->prev = tailPrev;
free(tail);*/
ListErase(phead->prev);
}
void ListPopFront(ListNode* phead) {
assert(phead);
/*if (phead == phead->next)return;
ListNode* first = phead->next;
ListNode* second = first->next;
phead->next = second;
second->prev = phead;
free(first);*/
ListErase(phead->next);
}
void ListInsert(ListNode* pos, LTDataType val) {
assert(pos);
ListNode* posPrev = pos->prev;
ListNode* newnode = BuyNode(val);
newnode->next = pos;
newnode->prev = posPrev;
posPrev->next = newnode;
pos->prev = newnode;
}
void ListErase(ListNode* pos) {
assert(pos);
if (pos->next == pos)return;
ListNode* posPrev = pos->prev;
ListNode* posNext = pos->next;
posPrev->next = posNext;
posNext->prev = posPrev;
free(pos);
}
ListNode* ListFind(ListNode* phead, LTDataType val) {
assert(phead);
ListNode* cur = phead->next;
while (cur != phead) {
if (cur->data == val)return cur;
cur = cur->next;
}
return NULL;
}