一篇解双链表(0基础看)(C语言)《数据结构与算法》
【摘要】 4. 源代码 #include"List.h" int main(){ printf("初始化\n"); ListNode* plist = ListInit(); printf("头插 1 2 3 并打印\n"); ListPushFront(plist, 1); ListPushFront(plist, 2); ListPushFront(plist, 3); ListPrint(pl...
4. 源代码
#include"List.h"
int main()
{
printf("初始化\n");
ListNode* plist = ListInit();
printf("头插 1 2 3 并打印\n");
ListPushFront(plist, 1);
ListPushFront(plist, 2);
ListPushFront(plist, 3);
ListPrint(plist);
printf("尾插4 5 6 并打印\n");
ListPushBack(plist, 4);
ListPushBack(plist, 5);
ListPushBack(plist, 6);
ListPrint(plist);
printf("头删并打印\n");
ListPopFront(plist);
ListPrint(plist);
printf("尾删并打印\n");
ListPopBack(plist);
ListPrint(plist);
printf("1前插入3并打印\n");
ListNode* pos = ListFind(plist, 1);
ListInsert(plist, pos, 3);
ListPrint(plist);
printf("2前插入8并打印\n");
pos = ListFind(plist, 2);
ListInsert(plist, pos, 8);
ListPrint(plist);
printf("删除4并打印\n");
pos = ListFind(plist, 4);
ListErase(plist, pos);
ListPrint(plist);
printf("把1改成9并打印\n");
ListChange(plist, 1, 9);
ListPrint(plist);
printf("销毁链表\n");
ListDestory(plist);
return 0;
}
List.h
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<malloc.h>
#include<assert.h>
typedef int ListDateType;
typedef struct ListNode
{
struct ListNode* prev;
struct ListNode* next;
ListDateType date;
}ListNode;
//增删查改
//初始化
ListNode* ListInit();
//销毁
void ListDestory(ListNode* phead);
//得到一个节点
ListNode* BuyListNode(ListDateType x);
//打印
void ListPrint(ListNode* phead);
//头插
void ListPushFront(ListNode* phead, ListDateType x);
//尾插
void ListPushBack(ListNode* phead, ListDateType x);
//头删
void ListPopFront(ListNode* phead);
//尾删
void ListPopBack(ListNode* phead);
//查某个值,返回地址
ListNode* ListFind(ListNode* phead, ListDateType x);
//改某个值
void ListChange(ListNode* phead, ListDateType source, ListDateType destination);
//某个位置前插入数据
void ListInsert(ListNode* phead, ListNode* pos, ListDateType x);
//删除某个位置数据
void ListErase(ListNode* phead, ListNode* pos);
List.c
#include"List.h"
//初始化
ListNode* ListInit()
{
ListNode* phead = BuyListNode(0);
phead->prev = phead;
phead->next = phead;
phead->date = 0;
return phead;
}
//得到一个节点
ListNode* BuyListNode(ListDateType x)
{
ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
newnode->prev = NULL;
newnode->next = NULL;
newnode->date = x;
return newnode;
}
//头插
void ListPushFront(ListNode* phead, ListDateType x)
{
assert(phead);
ListNode* newnode = BuyListNode(x);
ListNode* first = phead->next;
phead->next = newnode;
newnode->prev = phead;
newnode->next = first;
first->prev = newnode;
}
//尾插
void ListPushBack(ListNode* phead, ListDateType x)
{
assert(phead);
ListNode* tail = phead->prev;
ListNode* newnode = BuyListNode(x);
phead->prev = newnode;
newnode->next=phead;
tail->next = newnode;
newnode->prev = tail;
}
//打印
void ListPrint(ListNode* phead)
{
ListNode* cur = phead->next;
while (cur != phead)
{
printf("%d ", cur->date);
cur = cur->next;
}
printf("\n");
}
//头删
void ListPopFront(ListNode* phead)
{
assert(phead);
//别把头删了
assert(phead->next != phead);
ListNode* first = phead->next;
phead->next = first->next;
first->next->prev = phead;
free(first);
first = NULL;
}
//尾删
void ListPopBack(ListNode* phead)
{
assert(phead);
//别把头删了
assert(phead->next != phead);
ListNode* tail = phead->prev;
ListNode* prev = tail->prev;
prev->next = phead;
phead->prev = prev;
free(tail);
tail = NULL;
}
//查某个值,返回地址
ListNode* ListFind(ListNode* phead, ListDateType x)
{
assert(phead);
ListNode* cur = phead->next;
while (cur != phead)
{
if (cur->date == x)
{
return cur;
}
else
{
cur = cur->next;
}
}
return NULL;
}
//某个位置前插入数据
void ListInsert(ListNode* phead, ListNode* pos, ListDateType x)
{
assert(phead);
ListNode* newnode = BuyListNode(x);
ListNode* prev = pos->prev;
prev->next = newnode;
newnode->prev = prev;
newnode->next = pos;
pos->prev = newnode;
}
//删除某个位置数据
void ListErase(ListNode* phead, ListNode* pos)
{
assert(phead);
//别把头删了
assert(phead->next != phead);
ListNode* prev = pos->prev;
ListNode* next = pos->next;
prev->next = next;
next->prev = prev;
free(pos);
pos = NULL;
}
//改某个值
void ListChange(ListNode* phead, ListDateType source, ListDateType destination)
{
assert(phead);
ListNode* pos = ListFind(phead, source);
pos->date = destination;
}
//销毁
void ListDestory(ListNode* phead)
{
assert(phead);
ListNode* cur = phead->next;
ListNode* tail = cur->next;
while (tail != phead)
{
free(cur);
cur = NULL;
cur = tail;
tail = tail->next;
}
free(cur);
cur = NULL;
if (cur != tail)
{
free(tail);
tail = NULL;
}
}
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)