#include
using namespace std;
typedef struct Lnode
{
int data;
struct Lnode *next;
}Lnode, *Linklist;
int creat ( Linklist &L )
{
L = new Lnode;
L->next = NULL;
Linklist p, q = L;
while (1)
{
p = new Lnode;
cin >> p->data;
if ( p->data == -1 )
{
return 0;
}
p->next = NULL;
q->next = p;
q = p;
}
}
void cmp ( Linklist &L, int e )
{
Linklist p, q;
p = L;
if ( e >= p->next->data )
{
q = new Lnode;
q->data = e;
q->next =
p->next = q;
p = q;
}
}
void Print (Linklist L)
{
Linklist p = L->next;
while ( p != NULL )
{
cout << p->data;
if ( p->next != NULL)
{
cout << " ";
}
p = p->next;
}
}
int main ()
{
int t;
Linklist L;
creat (L);
while ( 1 )
{
cin >> t;
if ( t == -1 )
{
return 0;
}
else
{
cmp (L, t);
}
}
Print (L);
}
该回答引用ChatGPT
在函数 cmp 中,当链表 L 为空或 e 值小于 L 中第一个节点的值时,没有插入新的节点,导致最终输出的链表仍然是原始的输入链表。因此,需要增加一个判断链表 L 是否为空的条件,以及在需要插入新节点时,要修改新节点的 next 指针和前一个节点的 next 指针。同时,在 main 函数中需要将 Print(L) 函数调用语句放在 while 循环外面,否则在第一次输入 -1 后就会直接返回,无法输出链表。
以下是修正后的代码:
#include <iostream>
using namespace std;
typedef struct Lnode
{
int data;
struct Lnode *next;
} Lnode, *Linklist;
int creat(Linklist &L)
{
L = new Lnode;
L->next = NULL;
Linklist p, q = L;
while (1)
{
p = new Lnode;
cin >> p->data;
if (p->data == -1)
{
return 0;
}
p->next = NULL;
q->next = p;
q = p;
}
}
void cmp(Linklist &L, int e)
{
Linklist p = L;
if (p->next == NULL || e < p->next->data)
{
Linklist q = new Lnode;
q->data = e;
q->next = p->next;
p->next = q;
}
else
{
while (p->next != NULL && e >= p->next->data)
{
p = p->next;
}
Linklist q = new Lnode;
q->data = e;
q->next = p->next;
p->next = q;
}
}
void Print(Linklist L)
{
Linklist p = L->next;
while (p != NULL)
{
cout << p->data;
if (p->next != NULL)
{
cout << " ";
}
p = p->next;
}
}
int main()
{
int t;
Linklist L;
creat(L);
while (1)
{
cin >> t;
if (t == -1)
{
break;
}
cmp(L, t);
}
Print(L);
return 0;
}
cmp函数如果是插入,你咋没有循环比较判断呢?if就能决定插入位置?
至于不输出,是因为main函数中,当输入-1时,你return 0直接结束程序了啊,将return 0改为break跳出while循环就行了
#include <iostream>
using namespace std;
typedef struct Lnode
{
int data;
struct Lnode *next;
}Lnode, *Linklist;
int creat ( Linklist &L )
{
L = new Lnode;
L->next = NULL;
Linklist p, q = L;
while (1)
{
p = new Lnode;
cin >> p->data;
if ( p->data == -1 )
{
return 0;
}
p->next = NULL;
q->next = p;
q = p;
}
}
void cmp ( Linklist &L, int e )
{
Linklist p, q;
p = L;
if ( e >= p->next->data )
{
q = new Lnode;
q->data = e;
q->next = p->next;
p->next = q;
p = q;
}
}
void Print (Linklist L)
{
Linklist p = L->next;
while ( p != NULL )
{
cout << p->data;
if ( p->next != NULL)
{
cout << " ";
}
p = p->next;
}
}
int main ()
{
int t;
Linklist L;
creat (L);
while ( 1 )
{
cin >> t;
if ( t == -1 )
{
break;
}
else
{
cmp (L, t);
}
}
Print (L);
}
这样就能运行了
该回答引用GPTᴼᴾᴱᴺᴬᴵ
在 cmp() 函数中,当链表为空时,需要将新节点作为头节点插入链表中,但是当前的代码没有处理这种情况,导致输出链表为空。
可以在 cmp() 函数中加上链表为空时的处理,即:
void cmp(Linklist& L, int e) {
Linklist p, q;
p = L;
if (p->next == NULL || e >= p->next->data) { // 链表为空或插入位置在头节点之后
q = new Lnode;
q->data = e;
q->next = p->next;
p->next = q;
} else { // 插入位置在头节点之前
while (p->next != NULL && e < p->next->data) {
p = p->next;
}
q = new Lnode;
q->data = e;
q->next = p->next;
p->next = q;
}
}
在插入时,需要将新节点 q 的 next 指向 p 的 next,然后再将 p 的 next 指向 q,具体实现可以参考上述代码。
此外,在主函数中,需要将 Print(L) 放在 while 循环之外,这样才能在插入完数据后输出链表。修改后的 main() 函数为:
int main() {
int t;
Linklist L;
creat(L);
while (1) {
cin >> t;
if (t == -1) {
break;
} else {
cmp(L, t);
}
}
Print(L);
return 0;
}
这样就可以正确输出链表了。