#include <stdio.h>
#include <stdlib.h>
// 双向循环链表的节点结构
typedef struct Node {
int val;
struct Node* prev;
struct Node* next;
} ListNode;
// 显示菜单
void DisplayMenu()
{
printf("*************************\n");
printf("*[1]创建一个双向循环链表*\n");
printf("*[2]增加节点 *\n");
printf("*[3]删除节点 *\n");
printf("*[4]查询数据 *\n");
printf("*[5]修改数据 *\n");
printf("*[6]显示所有数据 *\n");
printf("*[7]退出 *\n");
printf("*************************\n");
printf(" \n");
printf("请输入您的选择(数字1-7):");
}
// 创建一个双向循环链表(带头节点)
ListNode* CreateList()
{
int i;
int length = 0;
int data = 0;
ListNode* pNew = NULL;
ListNode* pTail = NULL;
ListNode* pHead = (ListNode*)malloc(sizeof(ListNode));
if (pHead == NULL)
{
printf("内存分配失败!\n");
exit(0);
}
pHead->val = 0;
pHead->prev = pHead;
pHead->next = pHead;
pTail = pHead;
printf("请输入链表的长度:");
scanf_s("%d", &length);
for (i = 0; i < length; ++i)
{
pNew = (ListNode*)malloc(sizeof(ListNode));
if (pNew == NULL)
{
printf("内存分配失败!\n");
exit(0);
}
printf("请输入第%d个节点的数据值:", (i + 1));
scanf_s("%d", &data);
pNew->val = data;
pNew->prev = pTail;
pNew->next = pHead;
pTail->next = pNew;
pHead->prev = pNew;
pTail = pNew;
}
printf("链表创建成功!数据如下:\n");
return pHead;
}
// 获取链表的长度
int GetLength(ListNode* pHead)
{
int length = 0;
ListNode* pNode = pHead->next;
while (pNode != pHead)
{
length++;
pNode = pNode->next;
}
return length;
}
// 增加节点(在pos所在的元素之前添加)
int AddNode(ListNode* pHead, int pos, int data)
{
ListNode* pNew = NULL;
if (pos > 0 && pos < (GetLength(pHead) + 2))
{
pNew = (ListNode*)malloc(sizeof(ListNode));
if (pNew == NULL)
{
printf("内存分配失败!\n");
exit(0);
}
while (--pos)
{
pHead = pHead->next;
}
pNew->val = data;
pNew->prev = pHead;
pNew->next = pHead->next;
pHead->next->prev = pNew;
pHead->next = pNew;
return 1;
}
return 0;
}
// 删除数据
int DelNode(ListNode* pHead, int pos)
{
if (pos > 0 && pos <= GetLength(pHead))
{
while (pos--)
{
pHead = pHead->next;
}
pHead->prev->next = pHead->next;
pHead->next->prev = pHead->prev;
return 1;
}
return 0;
}
// 修改数据
int ModNode(ListNode* pHead, int pos, int data)
{
ListNode* pNode = pHead;
if (pos > 0 && pos <= GetLength(pHead))
{
while (pos--)
{
pNode = pNode->next;
}
pNode->val = data;
return 1;
}
return 0;
}
// 查找数据
int FindNode(ListNode* pHead, int data)
{
ListNode* pNode = pHead->next;
int pos = 0;
while (pNode != pHead)
{
pos++;
if (pNode->val == data)
{
printf("数据%d所在位置为:%d\n", data, pos);
return 1;
}
pNode = pNode->next;
}
return 0;
}
// 显示所有数据
void DisplayList(ListNode* pHead)
{
ListNode* pNode = pHead->next;
printf("链表中的数据如下:");
while (pNode != pHead)
{
printf("%d ", pNode->val);
pNode = pNode->next;
}
printf("\n");
}
// 删除整个链表,并释放内存空间
void FreeMemory(ListNode** pHead)
{
ListNode* pNode = NULL;
while (*pHead != NULL)
{
pNode = (*pHead)->next->next;
if ((*pHead)->next == *pHead)
{
free(*pHead);
*pHead = NULL;
}
else
{
free((*pHead)->next);
(*pHead)->next = NULL;
(*pHead)->next = pNode;
pNode->prev = (*pHead);
}
}
}
int main()
{
int opt;
int pos;
int data;
bool flat = 0;
DisplayMenu();
ListNode* pHead = NULL;
while (1)
{
if (flat == 1)
break;
scanf_s("%d", &opt);
switch (opt)
{
case 1:
pHead = CreateList();
DisplayList(pHead);
break;
case 2:
printf("请输入您想增加的节点的位置:\n");
scanf_s("%d", &pos);
printf("请输入您想增加的节点的值:\n");
scanf_s("%d", &data);
if (AddNode(pHead, pos, data) == 1)
{
printf("添加成功!新链表如下:\n");
DisplayList(pHead);
}
else
{
printf("添加失败!请再次操作!\n");
}
break;
case 3:
printf("请输入您想删除的节点的位置:\n");
scanf_s("%d", &pos);
if (DelNode(pHead, pos) == 1)
{
printf("删除成功!新链表如下:\n");
DisplayList(pHead);
}
else
{
printf("删除失败!请再次操作!\n");
}
break;
case 4:
printf("请输入您想查找的节点的值:\n");
scanf_s("%d", &data);
if (FindNode(pHead, data) == 0)
{
printf("查找不到该数据!请再次操作!\n");
}
break;
case 5:
printf("请输入您想修改的节点的位置:\n");
scanf_s("%d", &pos);
printf("请输入您想修改节点的值为:\n");
scanf_s("%d", &data);
if (ModNode(pHead, pos, data) == 1)
{
printf("修改成功!新链表如下:\n");
DisplayList(pHead);
}
else
{
printf("修改失败!请再次操作!\n");
}
break;
case 6:
DisplayList(pHead);
break;
case 7:
FreeMemory(&pHead);
flat = 1;
break;
default:
printf("您的输入有误!请重新选择!\n");
break;
}
}
return 0;
}
《数据结构》C语言版 双向循环链表的基本操作实现
最新推荐文章于 2021-07-14 13:37:22 发布