头文件:
#ifndef LIST_H
#define LIST_H
#include <myhead.h>
//#include <stdio.h>
//#include <stdlib.h>
#include <assert.h>
typedef int LTDataType;
typedef struct ListNode
{
LTDataType data;
struct ListNode *next;
struct ListNode *prev;
} Dlist, *pnode;
pnode ListInit(); // 初始化链表
pnode BuyListNode(LTDataType x); // 新建链表单元
void ListPrint(pnode phead); // 打印整个链表
void ListPushBack(pnode phead, LTDataType x); // 尾部插入
void ListPushFront(pnode phead, LTDataType x); // 头部插入
void ListInsert(pnode pos, LTDataType x); // 插入
pnode ListFind(pnode phead, LTDataType x); // 查找
void ListErase(pnode pos); // 删除对应位置
void ListPopBack(pnode phead); // 尾删
void ListDestroy(pnode phead); // 删除整个链表(除了头)
#endif
相关函数:
#include "List.h"
pnode ListInit() // 初始化链表
{
// 哨兵位头结点
pnode head = (pnode)malloc(sizeof(Dlist));
head->next = head;
head->prev = head;
head->data = 0;
printf("创建成功\n");
return head;
}
pnode BuyListNode(LTDataType x) // 新建链表单元
{
pnode newnode = (pnode)malloc(sizeof(Dlist));
newnode->data = x;
newnode->next = NULL;
newnode->prev = NULL;
return newnode;
}
void ListPrint(pnode head) // 打印整个链表
{
assert(head);
pnode cur = head->next;
while (cur != head)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
void ListPushBack(pnode head, LTDataType x) // 尾部插入
{
assert(head);
pnode tail = head->prev;
pnode newnode = BuyListNode(x); // 建立新的节点
newnode->prev = tail;
newnode->next = head;
tail->next = newnode;
head->prev = newnode;
head->data++;
}
void ListPushFront(pnode head, LTDataType x) // 头部插入
{
assert(head);
pnode newnode = BuyListNode(x); // 建立新的节点
newnode->prev = head;
newnode->next = head->next;
head->next->prev = newnode;
head->next = newnode;
head->data++;
}
void ListInsert(pnode pos, LTDataType x) // 插入
{
assert(pos);
pnode newnode = BuyListNode(x);
newnode->prev = pos->prev;
newnode->next = pos;
pos->prev->next = newnode;
pos->prev = newnode;
}
pnode ListFind(pnode head, LTDataType x) // 查找
{
assert(head);
pnode find = head->next;
while (find != head)
{
if (find->data == x)
return (find);
find = find->next;
}
return NULL;
}
void ListErase(pnode pos) // 删除对应位置
{
assert(pos);
pos->prev->next = pos->next;
pos->next->prev = pos->prev;
free(pos);
}
void ListPopFront(pnode head) // 头删
{
assert(head);
assert(head->next != head);
pnode first = head->next;
head->next = first->next;
first->next->prev = head;
free(first);
}
void ListPopBack(pnode head) // 尾删
{
assert(head);
assert(head->next != head); // 保留链表的头
pnode tail = head->prev;
head->prev = tail->prev;
tail->prev->next = head;
head->data --;
free(tail);
}
void ListDestroy(pnode head) // 删除整个链表(除了头)
{
pnode cur = head->next;
pnode destroy = cur;
while (cur != head)
{
destroy = cur;
cur = cur->next;
free(destroy);
}
head->data = 0;
}
// 0 1 2 3 4 5 7 9
// cur x x x x x x x
// des
测试函数:
#include "List.h"
//创建链表
int main(int argc, char const *argv[])
{
pnode head = ListInit();
if (NULL == head)
return -1;
ListPushBack(head, 1);
ListPushBack(head, 2);
ListPushBack(head, 3);
ListPrint(head);
ListPushFront(head, 5);
ListPushFront(head, 6);
ListPrint(head);
pnode p = ListFind(head, 3);
ListErase(p);
head->data--;
ListPrint(head);
p = ListFind(head, 6);
ListInsert(p, 7);
head->data++;
ListPrint(head);
ListPopBack(head);
ListPopBack(head);
ListPrint(head);
return 0;
}