建立文件
新建三个文件:
List.h:函数声明
List.c:函数实现
main.c:函数调用
定义链表
定义结构体,成员变量为前驱指针、数据和后继指针
List.h中写入:
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
typedef int LTDataType;
typedef struct ListNode
{
struct ListNode* prev;//前驱
LTDataType data;//存放数据
struct ListNode* next;//后继
}ListNode;
与单向链表相比,这种结构操作更加方便
函数调用的实现
主函数依旧使用do-while、switch语句
流程
进入程序 --> 定义结构体指针(同时初始化哨兵结点)(第一个函数) --> 进入循环 --> 打印菜单(第二个函数) --> 进行选择 --> 进入switch -->
case 1:头插 – PushFront(第三个函数)
case 2:尾插 – PushBack(第四个函数)
case 3:头删 – PopFront(第五个函数)
case 4:尾删 – PopBack(第六个函数)
case 5:指定位置插入 – Insert(第七个函数)
case 6:指定位置删除 – Erase(第八个函数)
case 7:统计个数 – Size(第九个函数)
case 8:更改数据 – Modify(第十个函数)
case 9:打印链表 – Print(第十一个函数)
case 0:退出程序,销毁链表(第十二个函数)
进行函数声明
List.h中写入:
ListNode* ListInit();//初始化
void menu();//菜单
void ListPushFront(ListNode* phead);//头插
void ListPushBack(ListNode* phead);//尾插
void ListPopFront(ListNode* phead);//头删
void ListPopBack(ListNode* phead);//尾删
void ListInsert(ListNode* phead);//指定位置插入
void ListErase(ListNode* phead);//指定位置删除
void ListSize(ListNode* phead);//统计个数
void ListModify(ListNode* phead);//更改数据
void ListPrint(ListNode* phead);//打印链表
void ListDestory(ListNode* phead);//销毁链表
完成主函数
为了直观看到每个case的功能,创建枚举常量
List.h中写入:
enum Option
{
Exit,//0
PushFront,//1
PushBack,//2
PopFront,//3
PopBack,//4
Insert,//5
Erase,//6
Size,//7
Modify,//8
Print//9
};
完成主函数
main.c中写入:
#include "List.h"
int main()
{
int input = 0;
ListNode* pList = ListInit();
do
{
menu();
scanf("%d", &input);
switch (input)
{
case PushFront:
ListPushFront(pList);
break;
case PushBack:
ListPushBack(pList);
break;
case PopFront:
ListPopFront(pList);
break;
case PopBack:
ListPopBack(pList);
break;
case Insert:
ListInsert(pList);
break;
case Erase:
ListErase(pList);
break;
case Size:
ListSize(pList);
break;
case Modify:
ListModify(pList);
break;
case Print:
ListPrint(pList);
break;
case Exit:
ListDestory(pList);
printf("bye bye!\n");
break;
default:
printf("Seleect error, Please Select Again!\n");
break;
}
} while (input);
return 0;
}
ListInit的实现
ListInit的实现
工作:开辟一个哨兵结点,前驱和后继暂时都指向自己
List.c中写入:
ListNode* ListInit()
{
ListNode* phead = BuyListNode(0);
phead->prev = phead;
phead->next = phead;
return phead;
}
用到了新函数 - BuyListNode
BuyListNode的实现
工作:开辟一个新结点,data 赋值,前驱和后继都置空,返回结点地址
函数声明
List.h中写入:
ListNode* BuyListNode(LTDataType x);
函数实现
List.c中写入:
ListNode* BuyListNode(LTDataType x)
{
ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
if (newnode == NULL)//空间开辟失败
{
perror("malloc");
exit(-1);
}
newnode->prev = NULL;
newnode->data = x;
newnode->next = NULL;
return newnode;
}
menu的实现
List.c中写入:
#include "List.h"
void menu()
{
printf("----------------------------------------------\n");
printf("********** 1、PushFront 2、PushBack **********\n");
printf("********** 3、PopFront 4、PopBack **********\n");
printf("********** 5、Insert 6、Erase **********\n");
printf("********** 7、Size 8、Modify **********\n");
printf("********** 9、Print 0、Exit **********\n");
printf("----------------------------------------------\n");
printf("Please Select:");
}
ListPushFront的实现
工作:将新结点 newnode 放到 phead 和 phead->next 之间
List.c中写入:
void ListPushFront(ListNode* phead)
{
assert(phead);
int x = 0;
printf("请输入要插入的数值:");
scanf("%d", &x);
ListNode* newnode = BuyListNode(x);
ListNode* first = phead->next;
phead->next = newnode;
newnode->prev = phead;//链接phead和newnode
newnode->next = first;
first->prev = newnode;//链接newnode和first
printf("插入成功!\n");
}
/*
写法二:
void ListPushFront(ListNode* phead)
{
assert(phead);
int x = 0;
printf("请输入要插入的数值:");
scanf("%d", &x);
ListNode* newnode = BuyListNode(x);
newnode->next = phead->next;
phead->next->prev = newnode;
phead->next = newnode;
newnode->prev = phead;
}
如果这样写,那就一定要先链接后面的结点,再链接哨兵结点,否则phead->next会遗失
*/
ListPushBack的实现
工作:将新结点 newnode 放到末节点和哨兵结点之间
List.c中写入:
void ListPushBack(ListNode* phead)
{
assert(phead);
int x = 0;
printf("请输入要插入的数值:");
scanf("%d", &x);
ListNode* newnode = BuyListNode(x);
ListNode* tail = phead->prev;
tail->next = newnode;
newnode->prev = tail;//链接tail和newnode
newnode->next = phead;
phead->prev = newnode;//链接newnode和phead
printf("插入成功!\n");
}
ListPopFront的实现
工作:链接 phead 和 phead->next->next,free ( phead->next )
List.c中写入:
void ListPopFront(ListNode* phead)
{
assert(phead);
assert(phead->next != phead);
ListNode* first = phead->next;
ListNode* second = first->next;
free(first);
first = NULL;
phead->next = second;
second->prev = phead;//链接phead和second
printf("删除成功!\n");
}
ListPopBack的实现
工作:链接 phead->prev->prev 和 phead,free ( phead->prev )
List.c中写入:
void ListPopBack(ListNode* phead)
{
assert(phead);
assert(phead->next != phead);
ListNode* tail = phead->prev;
ListNode* prev = tail->prev;
free(tail);
tail = NULL;
prev->next = phead;
phead->prev = prev;//链接prev和phead
printf("删除成功!\n");
}
ListInsert的实现
ListInsert的实现
工作:在指定位置的前面插入数据
List.c中写入:
void ListInsert(ListNode* phead)
{
assert(phead);
LTDataType x = 0;
printf("请输入要插入的位置(数据):");
scanf("%d", &x);
ListNode* pos = ListFind(phead, x);//找到x所在位置pos
if (pos == NULL)
{
printf("该位置不存在!\n");
return;
}
else
{
LTDataType y = 0;
printf("请输入要插入的数据:");
scanf("%d", &y);
ListNode* newnode = BuyListNode(y);
ListNode* prev = pos->prev;
prev->next = newnode;
newnode->prev = prev;
newnode->next = pos;
pos->prev = newnode;//将新结点插入到prev和pos之间
printf("插入成功!\n");
}
}
用到了新函数 - ListFind
ListFind的实现
工作:找到某个数据所在位置,返回结点地址
函数声明
List.h中写入:
ListNode* ListFind(ListNode* phead, LTDataType x);
函数实现
List.c中写入:
ListNode* ListFind(ListNode* phead, LTDataType x)
{
assert(phead);
ListNode* cur = phead->next;
while (cur != phead)//遍历
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;//找不到返回空指针
}
ListErase的实现
工作:利用 ListFind 找到数据所在结点 pos,链接 pos->prev 和 pos->next,再 free ( pos )
List.c中写入:
void ListErase(ListNode* phead)
{
assert(phead);
LTDataType x = 0;
printf("请输入要删除的数据:");
scanf("%d", &x);
ListNode* pos = ListFind(phead, x);
if (pos == NULL)
{
printf("该数据不存在!\n");
return;
}
else
{
ListNode* prev = pos->prev;
ListNode* next = pos->next;
prev->next = next;
next->prev = prev;//链接prev和next
free(pos);
pos = NULL;
printf("删除成功!\n");
}
}
ListSize的实现
思路:遍历链表,使用一个计数器 count
List.c中写入:
void ListSize(ListNode* phead)
{
assert(phead);
ListNode* cur = phead->next;
int count = 0;
while (cur != phead)
{
count++;
cur = cur->next;
}
printf("数据个数为<%d>\n", count);
}
ListModify的实现
工作:利用 ListFind 找到要更改的位置 pos,修改 pos->data
List.c中写入:
void ListModify(ListNode* phead)
{
assert(phead);
LTDataType x = 0;
printf("请输入要修改的数据:");
scanf("%d", &x);
ListNode* pos = ListFind(phead, x);
if (pos == NULL)
{
printf("该数据不存在!\n");
return;
}
else
{
LTDataType y = 0;
printf("请输入修改后的数据:");
scanf("%d", &y);
pos->data = y;
printf("修改成功!\n");
}
}
ListDestory的实现
思路:遍历链表,free 结点
List.c中写入:
void ListDestory(ListNode* phead)
{
assert(phead);
ListNode* cur = phead->next;
while (cur != phead)
{
ListNode* next = cur->next;
free(cur);
cur = NULL;
cur = next;
}
free(phead);
phead = NULL;
}
ListPrint的实现
思路:遍历
List.c中写入:
void ListPrint(ListNode* phead)
{
assert(phead);
ListNode* cur = phead->next;
while (cur != phead)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}