目录
三、链表的分类:
实际中链表的结构非常多样,主要按以下方式区分:
1.带头或不带头(头结点、哨兵位)
2.单向或双向(是否有前驱指针)
3.循环或不循环(尾结点是否指向第一个结点)
两两组合一共有8种链表,虽然有这么多的链表的结构,但是我们实际中最常用还是两种结构:
1. 无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。
2.带头双向循环链表:结构最复杂,一般用在单独存储数据。实际中使用的链表数据结构,都是带头双向循环链表。这个结构虽然结构复杂,但是使用代码实现反而比单链表简单。
四、代码+详细注释(C语言实现)
List.h
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
//定义双向链表的结点
typedef int ListDatatype;//重定义链表存储数据的数据类型,便于修改
typedef struct ListNode
{
ListDatatype data;
struct ListNode* prev;//记录上一个结点;
struct ListNode* next;//记录下一个结点;
}ListNode;
ListNode* BuyListNode(ListDatatype x);//创建结点函数
ListNode* ListNodeInit();//初始化函数(创建双向链表的哨兵位的头节点)
void ListPrint(ListNode* phead);//打印函数
ListNode* ListFind(ListNode* phead, ListDatatype x);//查找函数
int ListSize(ListNode* phead);//计算链表结点个数
void ListDestory(ListNode* phead);//销毁函数
void ListPushBack(ListNode* phead, ListDatatype x);//尾插
void ListPushFront(ListNode* phead, ListDatatype x);//头插
void ListPopBack(ListNode* phead);//尾删
void ListPopFront(ListNode* phead);//头删
void ListInsert(ListNode* pos, ListDatatype x);//在pos结点之前插入新结点
void ListErase(ListNode* pos);//删除pos结点
List.c
#include"List.h"
ListNode* BuyListNode(ListDatatype x)//创建结点函数
{
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
node->data = x;
node->next = NULL;
node->prev = NULL;
return node;
}
ListNode* ListNodeInit()//初始化函数(创建双向链表的哨兵位的头节点)
{
ListNode* head = BuyListNode(0);
head->prev = head;
head->next = head;
return head;
}
void ListPrint(ListNode* phead)//打印函数
{
//有哨兵位传入的结点一定不为空
assert(phead);
ListNode* cur = phead->next;//从哨兵位的头结点的下一位开始
while (cur != phead)//等于哨兵位时结束
{
printf("%d ", cur->data);
cur = cur->next;
}
putchar(10);
}
ListNode* ListFind(ListNode* phead, ListDatatype x)//查找函数
{
//有哨兵位传入的结点一定不为空
assert(phead);
ListNode* cur = phead->next;
while (cur != phead)
{
if (cur->data == x)
return cur;
else
cur = cur->next;
}
return NULL;
}
int ListSize(ListNode* phead)//计算链表结点个数
{
//有哨兵位传入的结点一定不为空
assert(phead);
ListNode* cur = phead->next;
int size = 0;
while (cur != phead)
{
++size;
cur = cur->next;
}
return size;
}
void ListDestory(ListNode* phead)//销毁函数
{
//有哨兵位传入的结点一定不为空
assert(phead);
ListNode* cur = phead->next;
while (cur != phead)//循环调用ListErase()函数,直到只剩下哨兵位
{
ListNode* next = cur->next;
ListErase(cur);
cur = next;
}
free(phead);//删除哨兵位;
cur = NULL;
}
void ListPushBack(ListNode* phead, ListDatatype x)//尾插
{
//有哨兵位传入的结点一定不为空
assert(phead);
////1.创建节点
//ListNode* newnode = BuyListNode(x);
////2.插入
// //找到尾节点
//ListNode* tail = phead->prev;
// //插入
//tail->next = newnode;
//newnode->prev = tail;
//newnode->next = phead;
//phead->prev = newnode;
ListInsert(phead, x);//功能相同复用ListInsert函数
}
void ListPushFront(ListNode* phead, ListDatatype x)//头插
{
//有哨兵位传入的结点一定不为空
assert(phead);
//
////1.创建节点
//ListNode* newnode = BuyListNode(x);
////2.插入
// //.记录原头结点
//ListNode* next = phead->next;
// //.链接
//phead->next = newnode;
//newnode->prev = phead;
//newnode->next = next;
//next->prev = newnode;
ListInsert(phead->next, x);//功能相同复用ListInsert函数
}
void ListPopBack(ListNode* phead)//尾删
{
////有哨兵位传入的结点一定不为空
//assert(phead);
////防止删除掉哨兵位
//assert(phead != phead->next);
////1.找到尾结点和尾结点的前一个结点
//ListNode* tail = phead->prev;
//ListNode* tailprev = tail->prev;
////2.删除
//free(tail);
////3.链接哨兵位和新的尾结点
//tailprev->next = phead;
//phead->prev = tailprev;
ListErase(phead->prev);
}
void ListPopFront(ListNode* phead)//头删
{
////有哨兵位传入的结点一定不为空
//assert(phead);
////防止删除掉哨兵位
//assert(phead != phead->next);
////1.找到头结点和头结点的下一个结点
//ListNode* head = phead->next;
//ListNode* headnext = head->next;
////2.删除头结点
//free(head);
////3.链接哨兵位和新的头结点
//phead->next = headnext;
//headnext->prev = phead;
ListErase(phead->next);
}
void ListInsert(ListNode* pos, ListDatatype x)//在pos结点之前插入新结点
{
//传入的结点一定不为空
assert(pos);
//记录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)//删除pos结点
{
//防御性检查
assert(pos);
//记录pos的前后结点
ListNode* prev = pos->prev;
ListNode* next = pos->next;
//删除
free(pos);
//链接
prev->next = next;
next->prev = prev;
}
ListTest.c(测试用例)
#include"List.h"
int main()
{
ListNode* S = ListNodeInit();
ListPushBack(S, 1);
ListPushBack(S, 2);
ListPushFront(S, 0);
ListPushFront(S, -1);
ListPrint(S);
int len = ListSize(S);
printf("The length of List is %d\n", len);
ListPopBack(S);
ListPopFront(S);
ListPrint(S);
ListPushFront(S, -1);
ListPrint(S);
ListErase(ListFind(S, 0));
ListPrint(S);
ListDestory(S);
S = NULL;
return 0;
}





1586

被折叠的 条评论
为什么被折叠?



