在vs2022中运行,到下面那段代码就报错了,不清楚怎么回事,希望有人可以解释一下,顺便优化一下代码,还请注释一下,毕竟你修改之后我未必能看懂。
//设有两个按元素值递增有序的单链表A和B(单链表A和B),
//编一程序将单链表A和B归并成一个新的递增有序的单链表C
//(值相同的元素均保留在单链表C中)。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef int ElemType;
typedef struct node
{
ElemType data;
struct node* next;
}LNode, * LinkList;
void CreLinkListTail(LinkList L, int n)
{
LNode* s, * r;
ElemType x;
int i;
r = L;
printf("输入结点:\n");
for (i = n; i > 0; i--)
{
scanf_s("%d", &x);
s = (LNode*)malloc(sizeof(LNode));
s->next = NULL;
s->data = x;
r->next = s;
r = s;
}
}
void OutPut(LNode* L)
{
LNode* p;
p = L;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
LNode* guibing(LNode* La, LNode* Lb)
{
LNode* pc = (LNode*)malloc(sizeof(LNode));
LNode* p, * linshit = pc;
p = La->next;
while (p != NULL)
{
LNode* r = (LNode*)malloc(sizeof(LNode));
r->data = p->data;
p = p->next;
linshit->next = r;
linshit = r;
}
linshit = pc;
p = Lb->next;
while (linshit->next != NULL)
{
linshit = linshit->next;
}
while (p != NULL)
{
LNode* s = (LNode*)malloc(sizeof(LNode));
s->data = p->data;
p = p->next;
linshit->next = s;
linshit = s;
}
return pc;
}
int list_count(LinkList L) {
LNode* p = L->next;
int len = 0;
while (p) {
p = p->next;
len++;
}
return len;
}
void list_bubble_sort(LinkList L)
{
LNode* p, * q;
int len = list_count(L);
for (int i = 0; i < len; i++)
{
p = L;
for (int j = 0; j < len - i - 1; j++)
{
if (!p->next) break;
if (!p->next->next) break;
if (p->next->data > p->next->next->data)
{
q = p->next;
p->next = q->next;
q->next = q->next->next;
p->next->next = q;
}
p = p->next;
}
}
}
void main()
{
int n, m;
printf("请输入La的结点个数:\n");
scanf_s("%d", &n);
LinkList La = (LinkList)malloc(sizeof(LNode));
La->next = NULL;
CreLinkListTail(La, n);
printf("带头结点头插法--输出建立后的La单链表:\n");
OutPut(La->next);
printf("请输入Lb的结点个数:\n");
scanf_s("%d", &m);
LinkList Lb = (LinkList)malloc(sizeof(LNode));
Lb->next = NULL;
CreLinkListTail(Lb, n);
printf("带头结点头插法--输出建立后的Lb单链表:\n");
OutPut(Lb->next);
LinkList Lc = (LinkList)malloc(sizeof(LNode));
Lc->next = NULL;
printf("输出归并后的Lc单链表:\n");
Lc = guibing(La, Lb);
list_bubble_sort(Lc);
OutPut(Lc);
}
//在vs2022中运行,到这里就报错了
while (linshit->next != NULL)
{
linshit = linshit->next;
}
//引发了异常: 读取访问权限冲突。
//**linshit** 是 0xFFFFFFFFFFFFFFF7。