目录
愿你熬过万丈孤苦,藏下星辰大海。
一、双向链表的实现
带头、双向、循环
list.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int ltdatatype;
typedef struct listnode
{
ltdatatype data;
struct listnode* next;
struct listnode* prev;
}ltnode;
void listprint(ltnode* phead);
//void listinit(ltnode** pplist);//初始化,二级指针
ltnode* listinit(ltnode* phead);
void listpushback(ltnode* phead, ltdatatype x);//尾插
void listpopback(ltnode* phead);//尾删
void listpushfront(ltnode* phead, ltdatatype x);
void listpopfront(ltnode* phead);
ltnode* listfind(ltnode* phead, ltdatatype x);
//pos之前插入
void listinsert(ltnode* pos, ltdatatype x);
//删除pos位置的节点
void listerase(ltnode* pos);
void listdestory(ltnode* phead);
list.c
#define _crt_secure_no_warnings 1
#include "list.h"
ltnode* buyltnode(ltdatatype x)
{
ltnode* newnode = (ltnode*)malloc(sizeof(ltnode));
if (newnode == null)
{
printf("malloc fail\n");
exit(-1);
}
newnode->prev = null;
newnode->data = x;
newnode->next = null;
}
初始化,二级指针的思路;
//void listinit(ltnode** pphead)//初始化,next指向自己,prev指向自己,才算是循环,头的地址还不清楚//初始化个哨兵位
//{
// assert(pphead);
// *pphead = buyltnode(0);
// (*pphead)->prev = *pphead;
// (*pphead)->next = *pphead;
//}
//初始化,一级指针的思路,返回头即可,既可以改变头的地址,又可以传一级指针
ltnode* listinit(ltnode* phead)
{
phead = buyltnode(0);
phead->prev = phead;
phead->next = phead;
return phead;
}
//打印
void listprint(ltnode* phead)
{
//从head的next = cur->data开始打印,当cur->next = head结束
assert(phead);
ltnode* cur = phead->next;
while (cur != phead)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
void listpushback(ltnode* phead, ltdatatype x)
{
assert(phead);//带头
listinsert(phead, x);
/*ltnode* tail = phead->prev;
ltnode* newnode = buyltnode(x);
tail->next = newnode;
newnode->prev = tail;
newnode->next = phead;
phead->prev = newnode;*/
}
//尾删,尾删到没有节点,也不用处理
void listpopback(ltnode* phead)
{
assert(phead);
//链表为空
assert(phead->next != phead);
/*ltnode* tail = phead->prev;
ltnode* tailprev = tail->prev;
phead->prev = tailprev;
tailprev->next = phead;
free(tail);
tail = null;*/
listerase(phead->prev);
}
//头插
void listpushfront(ltnode* phead, ltdatatype x)
{
assert(phead);
listinsert(phead->next, x);
/*ltnode* newnode = buyltnode(x);
ltnode* next = phead->next;
next->prev = newnode;
newnode->next = next;
phead->next = newnode;
newnode->prev = phead;*/
}
//头删
void listpopfront(ltnode* phead)
{
assert(phead);
listerase(phead->next);
/*assert(phead->next != phead);
ltnode* head = phead->next;
ltnode* next = head->next;
phead->next = next;
next->prev = phead;
free(head);
head = null;*/
}
//查找
ltnode* listfind(ltnode* phead, ltdatatype x)
{
assert(phead);
ltnode* cur = phead->next;
while (cur != phead)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return null;
}
//插入
void listinsert(ltnode* pos, ltdatatype x)
{
assert(pos);
ltnode* newnode = buyltnode(x);
ltnode* prev = pos->prev;
prev->next = newnode;
newnode->prev = prev;
newnode->next = pos;
pos->prev = newnode;
}
//删除
void listerase(ltnode* pos)//在主函数中,pos的实参要置空,否则实参就会成为野指针
{
assert(pos);
ltnode* next = pos->next;
ltnode* prev = pos->prev;
prev->next = next;
next->prev = prev;
free(pos);
pos = null;
}
//传一级指针是为了保持接口一致性
void listdestory(ltnode* phead)//注意,phead的实参要进行free,否则会导致野指针
{
assert(phead);
ltnode* cur = phead->next;
while (cur != phead)
{
ltnode* next = phead->next;
free(cur);
cur = next;
}
free(phead);
phead = null;
}
二、顺序表和带头双向循环链表的区别
这两个结构是相辅相成的结构,
顺序表:优点:(1)物理空间是连续的,方便用下标随机访问。【二分查找、排序】
(2)cpu高速缓存命中率会更高。
缺点:(1)由于需要物理空间连续,空间不够需要扩容。扩容本身有一定的消耗,其次扩容机制还存在一定的空间消耗。(2)头部或者中间的插入删除,需要挪动数据,效率低。o(n)
链表:优点(1)按需申请释放空间(2)任意位置可以o(1)的插入删除数据
缺点:不支持下标的随机访问,有些算法不适合在他上面进行。如:二分查找、排序等
发表评论