无头单向非循环链表:
结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。
带头双向循环链表:
结构最复杂,一般用在单独存储数据。实际中使用的链表数据结构,都是带头双向循环鲜表。另外这个结构虽然结构复杂,但是使用代码实现以后会发现结构会带来很多优势,实现反而简单了,后商我们代码实现了就知道了。
双向带头循环链表实现
DList.h
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<windows.system.h>
typedef int DLTdatetype;
typedef struct DList
{
DLTdatetype data;
struct DList* next;
struct Dlist* prev;
}DLnode;
//show一下
void Showlist(DLnode* pphead);
//初始化
DLnode* Initial();
//尾插
void PushBack(DLnode* head,DLTdatetype data);
//头插
void PushFront(DLnode* head, DLTdatetype data);
//尾删
void PopBack(DLnode* head);
//头删
void PopFront(DLnode* head);
//查找
DLnode* Find(DLnode* head, DLTdatetype data);
//插入
DLnode* Insert(DLnode* pos, DLTdatetype data);
//删除
DLnode* Erase(DLnode* pos);
//销毁
void Distory(DLnode* head);
DList.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"DList.h"
//show一下
void Showlist(DLnode* phead)
{
assert(phead);
DLnode* cur = phead->next;
while (cur!=phead)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("\n");
}
//初始化
DLnode* Initial()
{
DLnode* newnode = (DLnode*)malloc(sizeof(DLnode));
newnode->next = newnode;
newnode->prev = newnode;
return newnode;
}
//尾插
void PushBack(DLnode* phead, DLTdatetype data)
{
assert(phead);
DLnode* tail = phead->prev;
DLnode* newnode = (DLnode*)malloc(sizeof(DLnode));
newnode->data = data;
tail->next = newnode;
newnode->prev = tail;
newnode->next = phead;
phead->prev = newnode;
}
//尾删
void PopBack(DLnode* head)
{
assert(head);
assert(head->next != head);
DLnode* tail = head->prev;
head->prev = tail->prev;
DLnode* newtail = tail->prev;
newtail->next = head;
tail->next = NULL;
tail->prev = NULL;
free(tail);
tail = NULL;
}
//头插
void PushFront(DLnode* phead, DLTdatetype data)
{
assert(phead);
DLnode* newnode = (DLnode*)malloc(sizeof(DLnode));
newnode->next = phead->next;
phead->next->prev = newnode;
newnode->data = data;
newnode->prev = phead;
phead->next = newnode;
}
//头删
void PopFront(DLnode* head)
{
assert(head);
assert(head->next != head);
DLnode* nextnode = head->next;
head->next = head->next->next;
head->next->prev = head;
nextnode->next = NULL;
nextnode->prev = NULL;
free(nextnode);
nextnode = NULL;
}
//查找
DLnode* Find(DLnode* head, DLTdatetype data)
{
assert(head);
DLnode* cur = head;
while (cur->next != head)
{
if (cur->data==data)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
//插入
DLnode* Insert(DLnode* pos, DLTdatetype data)
{
assert(pos);
DLnode* newnode = (DLnode*)malloc(sizeof(DLnode));
DLnode* nextnode = pos->next;
newnode->data = data;
pos->next = newnode;
newnode->prev = pos;
nextnode->prev = newnode;
newnode->next = nextnode;
}
//删除
DLnode* Erase(DLnode* pos)
{
assert(pos);
DLnode* next = pos->next;
DLnode* prev = pos->prev;
next->prev = prev;
prev->next = next;
pos->next = NULL;
pos->prev = NULL;
pos->data = NULL;
free(pos);
pos = NULL;
}
//销毁
void Distory(DLnode* head)
{
assert(head);
DLnode* cur = (head)->next;
while (cur!=(head))
{
DLnode* next = cur->next;
free(cur);
cur = next;
}
free(head);
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"DList.h"
void test1()
{
DLnode* data = Initial();
PushBack(data, 0);
PushBack(data, 1);
PushBack(data, 2);
Showlist(data);
PopBack(data);
Showlist(data);
}
void test2()
{
DLnode* data = Initial();
PushFront(data, 0);
PushFront(data, 1);
PushFront(data, 2);
Showlist(data);
PushFront(data, -1);
Showlist(data);
PopFront(data);
Showlist(data);
}
void test3()
{
DLnode* data = Initial();
PushFront(data, 0);
PushFront(data, 1);
PushFront(data, 2);
Showlist(data);
Erase(Find(data, 1));
Distory(&data);
free(data);
Sleep(1000);
Showlist(data);
}
int main()
{
//test1();
//test2();
test3();
return 0;
}
此处双向链表相对于上次写的单链表除了双向的区别,最大的不同在于此处传参是传的一级指针,而上次的单链表是传的二级指针。其根本原因是此处双向链表存在头指针哨兵位,而上次的单链表没有。
所以说还是有哨兵位的链表更好用。