2,双向链表
2.1双向链表基本框架
typedef int Data;
typedef struct _Node
{
Data data;//数据域
struct _Node* next;//后继指针--指向下一个结点
struct _Node* prev;//前驱指针--指向上一个结点
}Node;//结点
typedef struct DoubleList
{
int size;//当前结点数量
Node* front;//指向头结点的指针
Node* tail;//指向尾结点的指针
}DuList;//双向链表
2.2创建一个双向链表
DuList* createDuList()
{
DuList* list = calloc(1, sizeof(DuList));
assert(list != NULL);
//创建头结点
list->front = calloc(1, sizeof(Node));
assert(list->front != NULL);
list->tail = list->front;//让头指针和尾指针都指向头结点
return list;
}
2.3创建一个结点
//创建一个结点
Node* createNode(Data data)
{
Node* newNode = calloc(1, sizeof(Node));
if (newNode==NULL)
{
return NULL;
}
newNode->data = data;
return newNode;
}
2.4尾插
//尾插
void push_back(DuList* list, Data data)
{
Node* newNode = createNode(data);
newNode->next = list->tail->next;//新结点的后继指针指向NULL
newNode->prev = list->tail;//新结点的前驱指针指向原先的最后一个
list->tail->next = newNode;//让原先的尾结点指向新结点
list->tail = newNode;//让尾指针指向新结点
list->size++;
}
2.5头插
//头插
void push_front(DuList* list, Data data)
{
Node* newNode = createNode(data);
newNode->next = list->front->next;
newNode->prev = list->front;
list->front->next = newNode;
list->size++;
}
2.6查看
//输出
void show_list(DuList* list)
{
if (empty(list))
{
return;
}
Node* curNode = list->front->next;
while (curNode!=NULL)
{
printf("%d ", curNode->data);
curNode = curNode->next;
}
printf("\n");
}
2.7判断链表是否为空
//判断链表是否为空
#include<stdbool.h>
bool empty(DuList* list)
{
return list->size == 0;
}
2.8查找指定元素
//查找指定元素,找到返回结点所在的位置,没有则放回NULL
Node* find(DuList* list, Data data)
{
if (empty(list))
{
return NULL;
}
Node* curNode = list->front->next;
while (curNode!=NULL)
{
if (curNode->data==data)
{
return curNode;
}
curNode = curNode->next;
}
return NULL;
}
2.9插入
//插入
void insert(DuList* list, Node* iterator, Data data)
{
Node* newNode = createNode(data);
Node* prevNode = iterator->prev;//保存要插入的位置的上一个结点
newNode->next = iterator;//让新结点连上去
newNode->prev = prevNode;
iterator->prev = newNode;//让前后结点连上新结点
prevNode->next = newNode;
list->size++;
}
2.10尾部删除
//尾部删除
void pop_back(DuList* list)
{
if (empty(list))
{
return;
}
list->tail = list->tail->prev;//让尾指针前移一位
free(list->tail->next);
list->tail->next = NULL;
list->size--;
}
2.11头部删除
//头部删除
void pop_front(DuList* list)
{
if (empty(list))
{
return;
}
Node* delNode = list->front->next;//保存删除的结点
list->front->next = delNode->next;
//判断是不是只有一个结点,如果只有一个结点,需要对尾结点进行处理
if (delNode->next!=NULL)
{
delNode->next->prev = list->front;
}
else
{
list->tail = list->front;
}
free(delNode);
list->size--;
}
2.12指定元素删除
//指定元素删除
void erase_val(DuList* list, Data data)
{
Node* delNode = find(list, data);
if (delNode==NULL)
{
return;
}
Node* prevNode = delNode->prev;//保存上一个结点
prevNode->next = delNode->next;
delNode->next->prev = prevNode;
free(delNode);
list->size--;
}
2.13排序
//排序
void sort(DuList* list)
{
if (empty(list))
{
return;
}
for (Node* i = list->front->next; i->next!= NULL; i=i->next)
{
for (Node* k = list->front->next; k->next != NULL; k = k->next)
{
if (k->data>k->next->data)
{
Data t = k->data;
k->data = k->next->data;
k->next->data = t;
}
}
}
}
2.14清除
//清除
void clear(DuList* list)
{
if (empty(list))
{
return;
}
while (list->front->next!=NULL)
{
pop_front(list);
}
list->size = 0;
}
2.15销毁
//销毁
void destory(DuList** list)
{
clear(*list);
free((*list)->front);
free(*list);
*list = NULL;
}
2.16反转
//反转
void reverse(DuList* list)
{
if (size(list) <= 1)
{
return;
}
Node* curNode = list->front->next;
Node* prevNode = NULL;
Node* nextNode = NULL;
list->tail = curNode;
while (curNode!=NULL)
{
nextNode = curNode->next;
curNode->next = prevNode;
curNode->prev = nextNode;
prevNode = curNode;
curNode = nextNode;//移动当前指针
}
list->front->next = prevNode;
prevNode->prev = list->front;
}
2.17求大小
int size(DuList* list)
{
return list->size;
}