gpt4 book ai didi

c++ - 将数组元素添加到链表中

转载 作者:行者123 更新时间:2023-11-30 21:22:39 25 4
gpt4 key购买 nike

我有数组元素ar1[i]我想将该数组的数据添加到链表中

struct node
{
int data;
struct node*next;
};


void create(struct node *head,int ar1[] int i1)
{
struct node *temp, *p, *q;
int i;


head = (struct node*)malloc(sizeof(struct node));
temp = (struct node*)malloc(sizeof(struct node));


for (i=0; i<i1; i++) //lets say i1 is no. of data in array
{
if(head == NULL)
{
head->data = ar1[i];
head->next = NULL;
}
else
{
temp->data = ar1[i];
temp->next = NULL;

p = head;
while(p->next != NULL)
p = p->next;
p->next = temp;
}

}

我做了这个程序,但它不起作用。它接受输入数据,但不显示输出,也不终止程序。

编辑:正如每个人在评论中建议的那样,我尽了最大努力并提出了解决方案。实际上我的程序是从用户那里获取最多10位数字并对其进行算术运算。

这是我的代码:

   /*
WAP to store at most 10 digit integer in a Singly linked list and
perform
arithmetic operations on it.
*/

#include<iostream>
using namespace std;

struct node
{
int data;
struct node*next;
};

void create(struct node *head,int ar1[],int ar2[], int i1, int i2)
{
struct node *newNode, *temp;
int i=0;

head = (struct node *)malloc(sizeof(struct node));

if(head == NULL)
{
cout<<"Unable to allocate memory.";
}
else
{

head->data = ar1[i]; // Link the data field with data
head->next = NULL; // Link the address field to NULL

temp = head;

for(i=1; i<i1; i++) //Create n nodes and adds to linked list
{
newNode = (struct node *)malloc(sizeof(struct node));

if(newNode == NULL)

{
cout<<"Unable to allocate memory.";
break;
}
else
{


newNode->data = ar1[i];
newNode->next = NULL;

temp->next = newNode;
temp = temp->next;
}
}
}

temp = head;

while(temp!=NULL)
{
cout<<temp->data<<" ";
temp= temp->next;
}

}

int main()
{
struct node*head = NULL ;
int n1,n2;
int i1,i2,choice;
int ar1[10],ar2[10];

head = (struct node*)malloc(sizeof(struct node));

cout<<"Enter numbers you want to perform operations on\n1). ";
cin>>n1;
cout<<"2). ";
cin>>n2;

i1=0; i2=0;

while(n1) //getting each digit of given number
{

ar1[i1] = n1 %10;
n1 /= 10;
i1= i1 + 1;
}

while(n2) //getting each digit of given number
{

ar2[i2] = n2 % 10;
n2 /= 10;
i2++;
}

cout<<"\nChoose what operation you want to
perform:\n1.Addition\n2Subtraction\n";
cin>>choice;

create(head,ar1,ar2,i1,i2);
/*if(choice == 1) I comment out this part i.e. not important rn.
{
add(ar1,ar2);
}
else if (choice == 2)
sub();
else
{
cout<<"Please chose valid data\n";
}
*/
return 0;
}

最佳答案

有很多问题。您的 create 函数的整个逻辑是错误的:

void create(struct node *head, int ar1[], int i1)
{
struct node *temp, *p, *q;
int i;

head = (struct node*)malloc(sizeof(struct node)); // Problem 1
temp = (struct node*)malloc(sizeof(struct node));

for (i = 0; i < i1; i++)
{
if (head == NULL) // Problem 2
{
head->data = ar1[i];
head->next = NULL;
}
else
{
temp->data = ar1[i];
temp->next = NULL;

p = head;
while (p->next != NULL)
p = p->next;

p->next = temp; // Problem 3
}
}
}


...
create(myhead, myarray, maysize); // Problem 4
...

问题1

你分配内存太早了,你应该只在真正需要的时候分配它。

问题2

部分是由问题 1 引起的:这里 head 不能为 NULL (假设 malloc 没有失败,但那是另一个故事了),因为你之前已将其分配给非 NULL 值。

问题3

这里 p->next 始终为 NULL,因为上面有 while 循环。

问题4

假设你不能像这样创建:

...
myhead = NULL;
...
create(myhead, myarray, maysize);
调用 create 后,

myhead 仍将为 NULL,您应该阅读 this SO article .

关于c++ - 将数组元素添加到链表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52834806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com