题干
主函数中实现下列操作,绿色部分要求用函数实现。
- 键盘依次接收正整数,以0结束,尾插法创建单链表
- 计算单链表的长度
- 输入要查找的值e,调用按值查找单链表函数,输出它的位序(可能多个)
- 输入要删除的值e,按值删除函数,输出更新后的链表(可能多个)
- 单链表的就地逆置(不另外开辟存储空间)
参考资料
【数据结构】单链表查找和删除:多位序查找和删除_Granitte的博客-CSDN博客https://blog.csdn.net/2301_76172693/article/details/133552997056单链表就地逆置C语言_单链表逆置原地逆置图解析资源-CSDN文库
https://download.csdn.net/download/qq_22471349/85514202?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522169669020616800180675602%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=169669020616800180675602&biz_id=1&utm_medium=distribute.pc_search_result.none-task-download-2~all~first_rank_ecpm_v1~rank_v31_ecpm-1-85514202-null-null.142%5Ev95%5EchatgptT3_1&utm_term=056%E5%B0%B1%E5%9C%B0%E9%80%86%E7%BD%AE&spm=1018.2226.3001.4187.2【数据结构】单链表尾插法-CSDN博客
https://blog.csdn.net/2301_76172693/article/details/133589700?spm=1001.2014.3001.5501
代码
#include <stdio.h>
#include <stdlib.h>
constexpr auto ERROR = 0;
constexpr auto OK = 1;
typedef int ElemType;
typedef int Status;
typedef struct Node {
ElemType data;
struct Node* next;
}LNode, * LinkList;
void CreateList(LinkList& L) //尾插法
{
LinkList p, q;
L = (LinkList)malloc(sizeof(LNode));
q = L;
int j = 1;
while (1)
{
scanf_s("%d", &j);
if (j == 0)
break;
p = (LinkList)malloc(sizeof(LNode));
p->data = j;
q->next = p;
q = p;
}
q->next = NULL;
}
int ListLength(LinkList L)//求表长
{
LinkList p;
p = L; int i = 0;
while (p->next != NULL)
{
p = p->next;
i++;
}
return i;
}
void DisplayLink(LinkList L)//输出
{
LinkList p;
p = L->next;
while (p != NULL)
{
printf("%d\n", p->data);
p = p->next;
}
}
Status QueryNode(LinkList L, ElemType x, LinkList B)//查找
{
LinkList p;
/*B的尾插指针*/LinkList s, r;
/*B的尾插操作*/B = (LinkList)malloc(sizeof(LNode)); r = B;
p = L->next;
int i = 1;
while (p != NULL)
{
if (p->data == x)
{
/*B的尾插操作*/s = (LinkList)malloc(sizeof(LNode));
s->data = i;
r->next = s;
r = s;
}
p = p->next;
i++;
}
/*B的尾插操作*/r->next = NULL;
if (B->data == 0) return ERROR;
printf("元素%d在如下位置:\n", x);
DisplayLink(B);
return OK;
}
Status ListDelete(LinkList& L, int x)//删除
{
LinkList j, k;
k = L;//避免因k未初始化而报错
j = L;
while (j->next != NULL)
{
k = j->next;//每次循环都重新对k赋值
if (k->data == x)
{
j->next = k->next;
free(k);
}
else j = j->next;
}
if (k == L) return ERROR;
return OK;
}
void Reverse(LinkList& L)//就地逆置
{
LinkList p, q, r;
q = L->next;
p = q->next;
r = p->next;
q->next = NULL;
while (r != NULL)
{
p->next = q;
q = p;
p = r;
if (r->next != NULL) //r后面还有结点,则逆置继续
r = r->next;
else
break;
}
r->next = q;
L->next = r; //头结点指向最后一个结点
q = L; //输入整个链表前,先将q移到链表头,L一般不动
while (q->next != NULL) //若q所指向的元素后面还有其它元素,则将该元素的数据输出
{
printf("%d\n", q->next->data); //q->next->data表示q所指向的下一个元素的数据
q = q->next; //完成该元素的输出后,q移至下一个元素重复输出操作
}
printf("\n");
}
int main(void)
{
LinkList A;
printf("请输入正整数,以0结束:\n");
CreateList(A);
printf("您已创建链表如下:\n");
DisplayLink(A);
printf("该表表长为%d\n", ListLength(A));
printf("查找请按1\n删除请按2\n就地逆置请按3\n退出请按0\n");
int x = 6;
while (x)
{
scanf_s("%d", &x);
if (x == 1)
{
int e; LinkList B; B = (LinkList)malloc(sizeof(LNode)); B->data = 0; B->next = NULL;
printf("要查找的值为\n");
scanf_s("%d", &e);
QueryNode(A, e, B);
DisplayLink(B);
printf("查找请按1\n删除请按2\n就地逆置请按3\n退出请按0\n");
}
else if (x == 2)
{
int g;
printf("要删除的值为\n");
scanf_s("%d", &g);
ListDelete(A, g);
printf("更新后的链表如下:\n");
DisplayLink(A);
printf("查找请按1\n删除请按2\n就地逆置请按3\n退出请按0\n");
}
else if (x == 3)
{
printf("逆置结果如下\n");
Reverse(A);
printf("查找请按1\n删除请按2\n就地逆置请按3\n退出请按0\n");
}
else printf("重新输入\n");
}
printf("循环结束,退出函数\n");
return 0;
}