设计内容
设计并编程实现一个双向链表,首先显示一个菜单并且实现以下功能
- 创建一个双向链表
- 增加结点(能在任意位置增加)
- 删除结点(能删除任意位置结点)
- 修改结点
- 查询结点(根据位置查内容或根据内容查位置)
- 显示所有结点内容(正向显示及方向显示)
- 退出
运行代码
1.main.c代码
#include<stdio.h>
#pragma warning(disable:4996)
#include"func.h"
void show_menu()
{
printf("1、创建一个双向循环链表\n");
printf("2、增加结点\n");
printf("3、删除结点\n");
printf("4、修改结点内容\n");
printf("5、查询结点内容\n");
printf("6、显示所有结点内容\n");
printf("7、退出\n");
}
int main()
{
node* head = creatlist();
int listnum;
//初始化一个头节点
int data;
int position;
while (1)
{
show_menu();
int slect;
scanf_s("%d", &slect);
switch (slect)
{
case 1:
printf("输入链表中的结点数: ");
int n;
scanf_s("%d", &n);
for (int i = 0; i < n; i++) {
printf("输入第 %d 个结点的数据: ", i + 1);
scanf_s("%d", &data);
append(head, data);
}
printf("链表创建成功\n");
break;
case 2://任意位置插入数据
printf("输入要添加的数据:");
scanf_s("%d", &data);
printf("输入要添加的位置:");
scanf_s("%d", &position);
listnum = sizelist(head);
//printf("%d", listnum);
if (position <= listnum + 1)
{
listinsert(head, data, position);
}
else
{
printf("超出链表位置的大小\n");
break;
}
break;
case 3://删除
printf("输入要删除的位置");
scanf_s("%d", &position);
listnum = sizelist(head);
//printf("%d", listnum);
if (position <= listnum + 1)
{
deletelist(head, position);
}
else
{
printf("超出链表位置的大小\n");
break;
}
break;
case 4://修改
printf("输入要修改的值:");
scanf_s("%d", &data);
printf("输入要修改的位置");
scanf_s("%d", &position);
listnum = sizelist(head);
if (position <= listnum)
{
modify(head, position, data);
printf("修改成功\n");
}
else
{
printf("输入的值大于链表位置的大小\n");
break;
}
break;
case 5://查询
printf("请输入要查找的方式:按位置查找:1,按值查找:2\n");
scanf_s("%d", &slect);
listnum = sizelist(head);
if (slect == 1)
{
printf("请输入要查询的位置:\n");
scanf_s("%d", &position);
if (position <= listnum)
{
int num = search(head, position);
printf("查询第个%d位置的是值为:%d\n", position, num);
}
else
{
printf("输入的值大于链表位置的大小\n");
break;
}
}
else if (slect == 2)
{
printf("请输入要查找的值\n");
scanf_s("%d", &data);
position = searchdata(head, data);
if (position == NULL)
{
printf("查无此值\n");
break;
}
else
{
printf("查找的%d值的位置为:%d\n", data, position);
}
}
else
{
printf("无法查找结点,请重新输入:\n");
break;
}
break;
case 6://显示结点内容
printf("请输入打印方式:正序为:1,反序为:2\n");
scanf_s("%d", &slect);
if (slect == 1)
{
showlistnode(head);
}
else if (slect == 2)
{
showlistnodefor(head);
printf("\n");
}
else
{
printf("无法打印结点,请重新输入:\n");
break;
}
break;
case 7://退出
printf("退出系统成功");
exit(0);
break;
default:
printf("输入的数字有误,请重新输入\n");
break;
}
/*system("pause");
system("cls");*/
}
}
2.func.c代码
#include<stdlib.h>
#include"func.h"
#include<string.h>
#include<stdio.h>
//初始化头结点
node* creatlist()
{
node* head = (node*)malloc(sizeof(node));
if (head == NULL)
{
printf("malloc error");
return NULL;
}
else
{
head->next = head;
head->pre = head;
return head;
}
}
//添加结点---本质就是顺序添加
void append(node* head, int data)
{
node* newnode = (node*)malloc(sizeof(node));
if (newnode == NULL)
{
printf("malloc error");
return;
}
else
{
newnode->data = data;
if (head->next==head&&head->pre==head)
{
head->next = newnode;
newnode->pre = head;
newnode->next = head;
head->pre = newnode;
}
else
{
node* lastnode = head->pre;
lastnode->next = newnode;
newnode->pre = lastnode;
newnode->next = head;
head->pre = newnode;
//return head;
///*node* last = head->pre;
//last->next = newnode;
//newnode->pre = last;
//newnode->next = head;
//head->pre = newnode;*/
}
}
}
//任意位置插入
void listinsert(node* head, int data, int position)
{
int count = 0;
if (head->next==head&&head->pre==head)
{
append(head, data);
return;
}
else
{
//如果为空,直接添加
node* newnode = (node*)malloc(sizeof(node));
if (newnode == NULL)
{
printf("malloc error");
}
else
{
newnode->data = data;
//头部插入数据
if (position == 0 || position == 1)
{
node* nextnode = head->next;
head->next = newnode;
newnode->pre = head;
newnode->next = nextnode;
nextnode->pre = newnode;
}
else
{
node* current = head;
int count = 0;
//查找到目标位置的前一个结点
while (count < position - 1)
{
current = current->next;
count++;
}
node* nextnode = current->next;
current->next = newnode;
newnode->pre = current;
newnode->next = nextnode;
nextnode->pre = newnode;
}
}
}
}
//修改这个值
void modify(node* head, int position, int data)
{
if (head->next==head&&head->pre==head)
{
printf("链表为空,无法删除结点\n");
return;
}
node* currrnt = head;
int count = 0;
while (count<position)
{
currrnt = currrnt->next;
count++;
}
currrnt->data = data;
}
//删除结点
void deletelist(node* head, int positiom)
{
if (head ->next==head&&head->pre==head)
{
printf("链表为空,无法删除\n");
return;
}
if (positiom==1||positiom==0)
{
node* current = head->next;
if (current->next ==head)//说明只有一个结点
{
head->next= head;
head->pre = head;
free(current);
}
else//不是一个结点
{
node* nextnode = current->next;
head->next = nextnode;
nextnode->pre = head;
free(current);
}
}
else
{
node* current = head;
int count = 0;
while (count < positiom)
{
current = current->next;
count++;
}//current为需要删除的位置
if (current->next ==head)
{
node* fornode = current->pre;
fornode->next = head;
head->pre = fornode;
free(current);
}
else
{
node* fornode = current->pre;
node* nextnode = current->next;
fornode->next = nextnode;
nextnode->pre = fornode;
free(current);
}
}
}
//顺序显示显示链表内容
void showlistnode(node* phead)
{
if (phead->next&&phead->pre==phead)
{
printf("链表为空\n");
return;
}
else
{
node* cur = phead->next;
do
{
printf("%d ",cur->data);
cur = cur->next;
} while (cur!=phead);
printf("\n");
}
}
//倒序显示
void showlistnodefor(node* phead)
{
if (phead->next == phead && phead->pre == phead)
{
printf("链表为空");
return NULL;
}
else
{
node* cur = phead->pre;
do
{
printf("%d ", cur->data);
cur = cur->pre;
} while (cur!=phead);
}
}
//按位置查找结点
int search(node* head, int position)
{
if (head ->next==head&&head->pre==head)
{
printf("链表为空\n");
return NULL;
}
node* current = head;
int count = 0;
while (count < position)
{
current = current->next;
count++;
}
return current->data;
}
//按值查找
int searchdata(node* head, int data)
{
if (head->next == head && head->pre == head)
{
printf("链表为空\n");
return NULL;
}
node* current = head->next;
int position = 1;
while (current->data!=data)
{
current = current->next;
position++;
if (current == head)
{
return NULL;
}
}
return position;
}
//计算节点的大小
int sizelist(node* head)
{
node* current = head->next;
int count = 0;
while (current!=head)
{
count++;
current = current->next;
}
return count;
}
3.func.h代码
#pragma once
#ifndef FUNC_H
#define FUNC_H
#include<assert.h>
typedef struct node
{
struct node* pre;
int data;
struct node* next;
}node;
//申请节点
node* buynode(int data);
//初始化头
node* creatlist();
//添加结点
void append(node* head, int data);
//打印结点
void showlistnode(node* phead);
//倒序打印
void showlistnodefor(node* phead);
//插入结点
void listinsert(node* head, int data, int position);
//修改结点内容
void modify(node* head, int position, int data);
//删除结点
void deletelist(node* head, int positiom);
//查找结点
int search(node* head, int position);
//按值查找
int searchdata(node* head, int data);
//计算节点的大小;
int sizelist(node* head);
#endif // !FUNC_H