新学链表,
题目:已知有两个递增的正整数序列A和B,序列中元素个数未知,同一序列中不会有重复元素出现,有可能某个序列为空。现在将AB所有的元素归并到一个链表,A和B相同的元素放到另一个链表里。
当输入为 -1
1 3 5 -1的时候,输出会出现地址,
例如:The new list A:1 3 5 2037056 2031952 2054656
There is no item in B list.
希望能帮忙解答一下。
typedef struct node
{
int number;
struct node* nextPtr;
}Node;
int input(Node* head)
{
int n,cnt=0;
scanf("%d",&n);
struct node* buf=head;
while (n!=-1)
{
struct node* temp=(struct node*)malloc(sizeof(struct node*));
temp->number=n;
temp->nextPtr=NULL;
buf->nextPtr=temp;
buf=buf->nextPtr;
scanf("%d",&n);
cnt+=1;
}
return cnt;
}
void output(Node* head)
{
Node* tempa=head->nextPtr;
while (tempa!=NULL)
{
if (tempa->nextPtr==NULL)
{
printf("%d",tempa->number);
tempa=tempa->nextPtr;
}
else
{
printf("%d ",tempa->number);
tempa=tempa->nextPtr;
}
}
}
//1 3 4 5 6 7 -1
//2 3 6 8 9 10 11-1
int main()
{
Node* headA=(Node*)malloc(sizeof(Node));
Node* headB=(Node*)malloc(sizeof(Node));
int nA=input(headA),nB=input(headB);
Node* temp1=headA->nextPtr,*temp2=headB->nextPtr;
Node* ans1=(Node*)malloc(sizeof(Node)),*ans2=(Node*)malloc(sizeof(Node));
Node* tick1=ans1,*tick2=ans2;
int a=0;
if ((!nA)&&(!nB))
{
printf("There is no item in A list.\nThere is no item in B list.");
return 0;
}
while (1)
{
if (temp1->number==temp2->number)
{
Node *buf=(Node*)malloc(sizeof(Node));
buf->number=temp2->number;
buf->nextPtr=NULL;
Node *buf1=(Node*)malloc(sizeof(Node));
buf1->number=temp2->number;
buf1->nextPtr=NULL;
tick2->nextPtr=buf;
tick2=tick2->nextPtr;
tick1->nextPtr=buf1;
tick1=tick1->nextPtr;
temp1=temp1->nextPtr;
temp2=temp2->nextPtr;
a++;
}
else if (temp1->number>temp2->number)
{
Node *buf=(Node*)malloc(sizeof(Node));
buf->number=temp2->number;
buf->nextPtr=NULL;
tick1->nextPtr=buf;
tick1=tick1->nextPtr;
temp2=temp2->nextPtr;
}
else
{
Node *buf=(Node*)malloc(sizeof(Node));
buf->number=temp1->number;
buf->nextPtr=NULL;
tick1->nextPtr=buf;
tick1=tick1->nextPtr;
temp1=temp1->nextPtr;
}
if (temp1==NULL)
{
while (temp2!=NULL)
{
Node *buf=(Node*)malloc(sizeof(Node));
buf->number=temp2->number;
buf->nextPtr=NULL;
tick1->nextPtr=buf;
tick1=tick1->nextPtr;
temp2=temp2->nextPtr;
}
break;
}
if (temp2==NULL)
{
while (temp1!=NULL)
{
Node *buf=(Node*)malloc(sizeof(Node));
buf->number=temp1->number;
buf->nextPtr=NULL;
tick1->nextPtr=buf;
tick1=tick1->nextPtr;
temp1=temp1->nextPtr;
}
break;
}
}
if (!a)
{
printf("The new list A:");
output(ans1);
printf("\n");
printf("There is no item in B list.");
}
else
{
printf("The new list A:");
output(ans1);
printf("\n");
printf("The new list B:");
output(ans2);
}
printf("\n%d %d",nA,nB);
}
代码有4个问题:
(1)input函数中,sizeof里面应该是Node,你写成指针了。
(2)main函数中,headA和headB的nextPtr你没有初始化,需要设置为0;
(3)while(1)循环中,没有判断temp1和temp2是否为空,最好改成while(temp1 && temp2),同时,把最下面的两个if语句拿到while循环外面。
修改后的运行结果:
修改后的代码(修改的地方有注释):
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int number;
struct node* nextPtr;
}Node;
int input(Node* head)
{
int n,cnt=0;
scanf("%d",&n);
struct node* buf=head;
while (n!=-1)
{
struct node* temp=(struct node*)malloc(sizeof(struct node)); //修改1,sizeof里面不应该用指针
temp->number=n;
temp->nextPtr=NULL;
buf->nextPtr=temp;
buf=buf->nextPtr;
scanf("%d",&n);
cnt+=1;
}
return cnt;
}
void output(Node* head)
{
Node* tempa=head->nextPtr;
while (tempa!=NULL)
{
if (tempa->nextPtr==NULL)
{
printf("%d",tempa->number);
tempa=tempa->nextPtr;
}
else
{
printf("%d ",tempa->number);
tempa=tempa->nextPtr;
}
}
}
//1 3 4 5 6 7 -1
//2 3 6 8 9 10 11-1
int main()
{
Node* headA=(Node*)malloc(sizeof(Node));
Node* headB=(Node*)malloc(sizeof(Node));
headA->nextPtr = 0; //修改2:设置headA和headB的nextptr
headB->nextPtr = 0;
int nA=input(headA),nB=input(headB);
Node* temp1=headA->nextPtr,*temp2=headB->nextPtr;
Node* ans1=(Node*)malloc(sizeof(Node)),*ans2=(Node*)malloc(sizeof(Node));
Node* tick1=ans1,*tick2=ans2;
int a=0;
if ((!nA)&&(!nB))
{
printf("There is no item in A list.\nThere is no item in B list.");
return 0;
}
while (temp1 && temp2) //修改3:判断temp1和temp2是否为空指针
{
if (temp1->number==temp2->number)
{
Node *buf=(Node*)malloc(sizeof(Node));
buf->number=temp2->number;
buf->nextPtr=NULL;
Node *buf1=(Node*)malloc(sizeof(Node));
buf1->number=temp2->number;
buf1->nextPtr=NULL;
tick2->nextPtr=buf;
tick2=tick2->nextPtr;
tick1->nextPtr=buf1;
tick1=tick1->nextPtr;
temp1=temp1->nextPtr;
temp2=temp2->nextPtr;
a++;
}
else if (temp1->number>temp2->number)
{
Node *buf=(Node*)malloc(sizeof(Node));
buf->number=temp2->number;
buf->nextPtr=NULL;
tick1->nextPtr=buf;
tick1=tick1->nextPtr;
temp2=temp2->nextPtr;
}
else
{
Node *buf=(Node*)malloc(sizeof(Node));
buf->number=temp1->number;
buf->nextPtr=NULL;
tick1->nextPtr=buf;
tick1=tick1->nextPtr;
temp1=temp1->nextPtr;
}
}
//修改4:从while(1)中拿出来
if (temp1==NULL)
{
while (temp2!=NULL)
{
Node *buf=(Node*)malloc(sizeof(Node));
buf->number=temp2->number;
buf->nextPtr=NULL;
tick1->nextPtr=buf;
tick1=tick1->nextPtr;
temp2=temp2->nextPtr;
}
//break;
}
if (temp2==NULL)
{
while (temp1!=NULL)
{
Node *buf=(Node*)malloc(sizeof(Node));
buf->number=temp1->number;
buf->nextPtr=NULL;
tick1->nextPtr=buf;
tick1=tick1->nextPtr;
temp1=temp1->nextPtr;
}
//break;
}
if (!a)
{
printf("The new list A:");
output(ans1);
printf("\n");
printf("There is no item in B list.");
}
else
{
printf("The new list A:");
output(ans1);
printf("\n");
printf("The new list B:");
output(ans2);
}
printf("\n%d %d",nA,nB);
}
#include<bits/stdc++.h>
using namespace std;
typedef struct node
{
int number;
struct node* nextPtr;
}Node;
int input(Node* head)
{
int n,cnt=0;
scanf("%d",&n);
struct node* buf=head;
while (n!=-1)
{
struct node* temp=(struct node*)malloc(sizeof(struct node*));
temp->number=n;
temp->nextPtr=NULL;
buf->nextPtr=temp;
buf=buf->nextPtr;
scanf("%d",&n);
cnt+=1;
}
return cnt;
}
void output(Node* head)
{
Node* tempa=head->nextPtr;
while (tempa!=NULL)
{
if (tempa->nextPtr==NULL)
{
printf("%d",tempa->number);
tempa=tempa->nextPtr;
}
else
{
printf("%d ",tempa->number);
tempa=tempa->nextPtr;
}
}
}
//1 3 4 5 6 7 -1
//2 3 6 8 9 10 11-1
int main()
{
Node* headA=(Node*)malloc(sizeof(Node));
Node* headB=(Node*)malloc(sizeof(Node));
int nA=input(headA),nB=input(headB);
Node* temp1=headA->nextPtr,*temp2=headB->nextPtr;
Node* ans1=(Node*)malloc(sizeof(Node)),*ans2=(Node*)malloc(sizeof(Node));
Node* tick1=ans1,*tick2=ans2;
int a=0;
if ((!nA)&&(!nB))
{
printf("There is no item in A list.\nThere is no item in B list.");
return 0;
}
while (1)
{
if (temp1->number==temp2->number)
{
Node *buf=(Node*)malloc(sizeof(Node));
buf->number=temp2->number;
buf->nextPtr=NULL;
Node *buf1=(Node*)malloc(sizeof(Node));
buf1->number=temp2->number;
buf1->nextPtr=NULL;
tick2->nextPtr=buf;
tick2=tick2->nextPtr;
tick1->nextPtr=buf1;
tick1=tick1->nextPtr;
temp1=temp1->nextPtr;
temp2=temp2->nextPtr;
a++;
}
else if (temp1->number>temp2->number)
{
Node *buf=(Node*)malloc(sizeof(Node));
buf->number=temp2->number;
buf->nextPtr=NULL;
tick1->nextPtr=buf;
tick1=tick1->nextPtr;
temp2=temp2->nextPtr;
}
else
{
Node *buf=(Node*)malloc(sizeof(Node));
buf->number=temp1->number;
buf->nextPtr=NULL;
tick1->nextPtr=buf;
tick1=tick1->nextPtr;
temp1=temp1->nextPtr;
}
if (temp1==NULL)
{
while (temp2!=NULL)
{
Node *buf=(Node*)malloc(sizeof(Node));
buf->number=temp2->number;
buf->nextPtr=NULL;
tick1->nextPtr=buf;
tick1=tick1->nextPtr;
temp2=temp2->nextPtr;
}
break;
}
if (temp2==NULL)
{
while (temp1!=NULL)
{
Node *buf=(Node*)malloc(sizeof(Node));
buf->number=temp1->number;
buf->nextPtr=NULL;
tick1->nextPtr=buf;
tick1=tick1->nextPtr;
temp1=temp1->nextPtr;
}
break;
}
}
if (!a)
{
printf("The new list A:");
output(ans1);
printf("\n");
printf("There is no item in B list.");
}
else
{
printf("The new list A:");
output(ans1);
printf("\n");
printf("The new list B:");
output(ans2);
}
printf("\n%d %d",nA,nB);
}
struct node* temp=(struct node*)malloc(sizeof(struct node*));
改为
struct node* temp=(struct node*)malloc(sizeof(struct node));
该回答引用GPTᴼᴾᴱᴺᴬᴵ
在输出链表的时候出现地址的问题是因为在创建新节点时,使用了不正确的 malloc 调用。在这里,应该为节点分配一个 struct node 的大小而不是一个 struct node* 的大小。因此,需要修改为 malloc(sizeof(struct node))。
·
另外,在输出链表时,tempa->nextPtr 和 tempa 的更新顺序不正确,导致最后一个元素无法输出。应该先将 tempa 移动到下一个节点,然后再判断是否为 NULL。