C++ 将数组元素添加到链表中
我有数组元素C++ 将数组元素添加到链表中,c++,c,C++,C,我有数组元素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
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;
}
}
struct节点
{
int数据;
结构节点*下一步;
};
void create(结构节点*head,int ar1[]int i1)
{
结构节点*temp,*p,*q;
int i;
head=(结构节点*)malloc(sizeof(结构节点));
temp=(结构节点*)malloc(sizeof(结构节点));
对于(i=0;idata=ar1[i];
head->next=NULL;
}
其他的
{
温度->数据=ar1[i];
temp->next=NULL;
p=水头;
while(p->next!=NULL)
p=p->next;
p->next=温度;
}
}
我做了这个程序,但它不工作。它接受输入数据,但不显示输出,也不终止程序
编辑:正如每个人在评论中建议的那样,我尽了最大努力并提出了解决方案。实际上,我的程序是从用户那里获取最多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;
}
/*
WAP在单链接列表中最多存储10位整数,以及
表演
对它进行算术运算。
*/
#包括
使用名称空间std;
结构节点
{
int数据;
结构节点*下一步;
};
void create(结构节点*head,int ar1[],int ar2[],int i1,int i2)
{
结构节点*newNode,*temp;
int i=0;
head=(结构节点*)malloc(sizeof(结构节点));
if(head==NULL)
{
coutnext=NULL;//将地址字段链接到NULL
温度=水头;
对于(i=1;inxt=newNode;
温度=温度->下一步;
}
}
}
温度=水头;
while(temp!=NULL)
{
cout有很多问题。您的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
...
myhead
在调用create
后仍将为NULL
,您应该阅读
用纸和笔来理解逻辑,然后
按顺序优化代码
struct节点
{
int-val;
结构节点*下一步;
};
结构节点*head=NULL,*last=NULL;
无效插入(int值)
{
结构节点*tmp;
tmp=(结构节点*)malloc(sizeof(结构节点));
tmp->val=值;
tmp->next=NULL;
if(head==NULL)
{
水头=tmp;
last=tmp;
}
其他的
{
最后->下一步=tmp;
last=tmp;
}
}
作废打印列表()
{
结构节点*tmp=head;
printf(“列表为:”);
while(tmp!=NULL)
{
printf(“%d”,tmp->val);
tmp=tmp->next;
}
printf(“\n”);
}
无效搜索()
{
printf(“搜索哪个值?”);
int n,k=0;
scanf(“%d”和“&n”);
结构节点*tmp=head;
while(tmp!=NULL)
{
如果(tmp->val==n)
k=1;
tmp=tmp->next;
}
如果(k)
printf(“是\n”);
其他的
printf(“否”);
}
void deletenode()
{
int n,posi;
printf(“在哪里删除?\n”);
printf(“1\t第一位置\n”);
printf(“2\t列表位置\n”);
printf(“3\t任何所需位置\n”);
scanf(“%d”和“&n”);
如果(n==1)
{
结构节点*tmp=head;
tmp=tmp->next;
水头=tmp;
打印列表();
}
else如果(n==2)
{
结构节点*tmp=head;
而(1)
{
如果(tmp->next->next==NULL)
{
tmp->next=NULL;
打破
}
其他的
tmp=tmp->next;
}
打印列表();
}
else如果(n==3)
{
int i=1;
printf(“输入位置:”);
scanf(“%d”和“posi”);
结构节点*tmp=head;
而(tmp->next!=NULL&&i next=tmp->next->next;
打破
}
其他的
{
i++;
tmp=tmp->next;
}
}
打印列表();
}
}
void ins()
{
int n,v,posi;
printf(“在何处插入?\n”);
printf(“1\t第一位置\n”);
printf(“2\t列表位置\n”);
printf(“3\t任何所需位置\n”);
scanf(“%d”和“&n”);
如果(n==1)
{
printf(“输入值:”);
scanf(“%d”和“&v”);
结构节点*tmp;
tmp=(结构节点*)malloc(sizeof(结构节点));
tmp->val=v;
tmp->next=头部;
水头=tmp;
打印列表();
}
else如果(n==2)
{
printf(“输入值:”);
scanf(“%d”和“&v”);
结构节点*tmp;
tmp=(结构节点*)malloc(sizeof(结构节点));
tmp->val=v;
tmp->next=NULL;
最后->下一步=tmp;
last=tmp;
打印列表();
}
else如果(n==3)
{
int i=1;
printf(“输入位置:”);
scanf(“%d”和“posi”);
printf(“\n输入值:”);
scanf(“%d”和“&n”);
结构节点*tmp1,*tmp=head,*tmp2;
tmp1=(结构节点*)malloc(sizeof(结构节点));
tmp1->val=n;
tmp1->next=NULL;
while(tmp->next!=NULL&&i next;
tmp->next=tmp1;
tmp1->next=tmp2;
}
i++;
tmp=tmp->next;
}
打印列表();
}
}
int main()
{
int n;
printf(“输入值并输入-1以退出。\n”);
而(1)
{
scanf(“%d”和“&n”);
if(n<0)
打破
插入(n);
}
打印列表();
printf(“输入选项:\n\n”);
printf(“1\t插入\n”);
printf(“2\tDelete\n”);
printf(“3\t搜索\n”);
printf(“4\tExit\n”);
scanf(“%d”和“&n”);
如果(n==1)
ins();
else如果(n==2)
deletenode();
else如果(n==3)
搜索();
else如果(n==4)
返回0;
返回0;
}
为什么C++中使用<代码> MalCal?你的代码看起来完全像C代码。首先写一个函数,它将一个元素添加到列表中。然后在数组和每个元素调用中重复该函数。“但是它不工作”。不是一个有用的问题描述。它如何不工作?它是否编译?如果没有,是否有错误消息?如果它编译,它是否运行?它的行为是否与您预期的不同?您如何预期我
...
myhead = NULL;
...
create(myhead, myarray, maysize);
struct node
{
int val;
struct node *next;
};
struct node *head=NULL, *last=NULL;
void insert( int value)
{
struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->val=value;
tmp->next=NULL;
if( head == NULL )
{
head=tmp;
last=tmp;
}
else
{
last->next=tmp;
last=tmp;
}
}
void printlist()
{
struct node *tmp=head;
printf("The list is:");
while( tmp != NULL )
{
printf(" %d", tmp->val);
tmp=tmp->next;
}
printf("\n");
}
void search( )
{
printf("Which value to Search?");
int n, k=0;
scanf("%d",&n);
struct node *tmp=head;
while( tmp != NULL )
{
if( tmp->val == n )
k=1;
tmp=tmp->next;
}
if( k )
printf("Yes\n");
else
printf("No\n");
}
void deletenode( )
{
int n, posi;
printf("Where to Delete?\n");
printf("1\tFirst Position\n");
printf("2\tLast Position\n");
printf("3\tAny Desired Position\n");
scanf("%d",&n);
if( n == 1 )
{
struct node *tmp=head;
tmp=tmp->next;
head=tmp;
printlist();
}
else if( n == 2 )
{
struct node *tmp=head ;
while( 1 )
{
if( tmp->next->next == NULL )
{
tmp->next=NULL;
break;
}
else
tmp=tmp->next;
}
printlist();
}
else if( n == 3 )
{
int i=1;
printf("Enter Position: ");
scanf("%d",&posi);
struct node *tmp=head;
while( tmp->next != NULL && i <= posi )
{
if( i+1 == posi )
{
tmp->next=tmp->next->next;
break;
}
else
{
i++;
tmp=tmp->next;
}
}
printlist();
}
}
void ins()
{
int n, v, posi;
printf("Where to insert?\n");
printf("1\tFirst Position\n");
printf("2\tLast Position\n");
printf("3\tAny Desired Position\n");
scanf("%d",&n);
if( n == 1 )
{
printf("Enter value: ");
scanf("%d",&v);
struct node *tmp;
tmp=(struct node *)malloc( sizeof(struct node));
tmp->val=v;
tmp->next=head;
head=tmp;
printlist();
}
else if( n == 2 )
{
printf("Enter value: ");
scanf("%d",&v);
struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->val=v;
tmp->next=NULL;
last->next=tmp;
last=tmp;
printlist();
}
else if(n == 3 )
{
int i=1;
printf("Enter position: ");
scanf("%d",&posi);
printf("\nEnter value: ");
scanf("%d",&n);
struct node *tmp1, *tmp=head, *tmp2;
tmp1=(struct node *)malloc(sizeof(struct node));
tmp1->val=n;
tmp1->next=NULL;
while( tmp->next != NULL && i <= posi)
{
if( i+1 == posi )
{
tmp2=tmp->next;
tmp->next=tmp1;
tmp1->next=tmp2;
}
i++;
tmp=tmp->next;
}
printlist();
}
}
int main()
{
int n;
printf("Enter value & Enter -1 to exit.\n");
while( 1 )
{
scanf("%d",&n);
if( n < 0 )
break;
insert(n);
}
printlist();
printf("Enter choice:\n\n");
printf("1\tInsert\n");
printf("2\tDelete\n");
printf("3\tSearch\n");
printf("4\tExit\n");
scanf("%d",&n);
if( n == 1 )
ins();
else if( n == 2 )
deletenode();
else if( n == 3 )
search();
else if( n == 4 )
return 0;
return 0;
}