当通过引用传递时,通过传递链接列表在第n个位置添加元素不工作 [英] Adding element at nth position by passing linked list when passed by reference not working
问题描述
我是新的链接列表这是我插入元素后的第二个问题。我试图插入元素在第n个位置。
我这样做:
(1)先取终端用户的大小。
(2)第二次从用户连续读取输入,直到大小。
(3)我在LL的开头添加在终端读取的元素。 / p>
(4)我打印LL直到形成。
直到这里一切正常
(5)现在,我试图在LL的第n个位置添加,但它给出 3错误我已经在我的代码中的注释中解释。还请告诉我是否我在第n个位置添加元素的逻辑是否正确?
注意:我不得不通过List节点作为参考
以下是我的完整代码在注释中指向错误。
#include< stdio.h>
#include< stdlib.h>
#include< malloc.h>
#include< string.h>
struct node
{
int freq;
struct node * next;
};
typedef struct node node;
/////////////////////////////功能定义/////////////// /////////////////////////
insert_beginning(int size,node * * head)
{
node *温度
temp =(node *)malloc(sizeof(node));
temp - > freq = size;
temp - > next = * head;
* head = temp;
}
insert_Nth_position(int posn,int varn,node * * head)
{
int count = 0;
do
{
* head = * head - > next; //第一个错误在这里,下一个是不是结构或联合
count ++;
} while(count!= posn - 1); // this loop i do to go to node at nth position
node * temp2 = * head;
temp2 =(node *)malloc(sizeof(node));
temp2 - > freq = varn;
temp2 - > next = * head - >下一个; //第二个错误在这里,下一个不是结构或联合
* head - > next = temp2; //第三个错误在这里,next is something not structure or union
}
//////////////////// ////////////////////////////////////////////////// /////////////////////////////////
main()
{
int size,data,i,pos,
var;
node * head = NULL;
printf(输入node\\\
的大小);
scanf(%d,& size);
printf(开始输入元素的数量,直到你的size\\\
);
for(i = 1; i <= size; i ++)
{
scanf(%d,& data);
insert_beginning(data,& head);
}
node * temp1;
temp1 = head;
while(temp1!= NULL)
{
printf(%d->,temp1 - > freq);
temp1 = temp1 - >下一个;
}
printf(输入posotion在哪里添加node\\\
);
scanf(%d,& pos);
printf(输入变量node\\\
);
scanf(%d,& var);
insert_Nth_position(pos,var,& head); //这里可能有问题
printf(added after addition \\\
);
node * temp3 = head;
while(temp3!= NULL)
{
printf(%d->,temp3 - > freq);
temp3 = temp3 - >下一个;
}
}
实际错误是:
hp @ ubuntu:〜/ Desktop / Internship_Xav / Huf_pointer $ gcc tlt.c -o tlt
tlt.c:在函数'insert_Nth_position' :
tlt.c:26:18:error:请求成员'next'在不是结构或联合的东西
tlt.c:32:28:error:请求成员'next'不是结构或联合
tlt.c:33:9:错误:请求成员'next'在不是结构或联合的东西
欢迎任何语言的c / c ++或算法。
的所有函数都没有返回类型。虽然旧C编译器允许这样做隐式定义返回类型 int
我建议明确指定函数的返回类型。
对于你的错误,你不能像表达式一样使用dereference运算符。
* head = *头 - >下一个;
或
* head - > next = temp2;
这是正确的写
* head =(* head) - > next;
和
(* head)→> next = temp2;
虽然语义上这个代码是无效的。
此外,函数insert_Nth_position也是错误的。例如,假设参数 int posn
的值等于0.在这种情况下,条件 while(count!= posn - 1) code>将无效。
我将定义在列表中指定位置的参数为 unsigned int
或 size_t
函数insert_Nth_position可以采用以下方式:
int insert_Nth_position(node ** head,size_t posn,int varn)
{
if(posn == 0)return insert_beginning varn);
node * tmp = * head;
while(--posn& tmp)tmp = tmp-> next;
if(tmp)
{
node * new_node = malloc(sizeof(node));
new_node-> freq = varn;
new_node-> next = tmp-> next;
tmp-> next = new_node;
}
return(temp!= NULL);
}
如果你的编译器支持C99,那么返回类型可以替换 _Bool
同样函数insert_beginning我将声明为
int insert_beginning(node ** head,int varn);
I am new to linked list this is my second problem after inserting element in LL.Now i am trying to insert element at nth position. I do so like this:
(1) First taking the size of user at terminal.
(2) Second read the input continuously from the user until the size.
(3) I add the element read at terminal at the beginning of the LL.
(4) I print that LL until formed.
Until here everything works fine
(5) Now after that i try to do addition at nth position in LL but it give 3 errors That i have explained in comments in my code. Also please tell me if my logic to add the element at nth position is correct or not ?
Note: I am obliged to pass the List node as a reference only in function call (and the dereference them inside function definition)
Below is my full code pointing errors in comments.
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
struct node
{
int freq;
struct node * next;
};
typedef struct node node;
///////////////////////////// Function definitions ////////////////////////////////////////
insert_beginning(int size, node * * head)
{
node * temp;
temp = (node * ) malloc(sizeof(node));
temp -> freq = size;
temp -> next = * head;
* head = temp;
}
insert_Nth_position(int posn, int varn, node * * head)
{
int count = 0;
do
{
* head = * head -> next;//The first error is here , "next is something not structure or union"
count++;
} while (count != posn - 1); //this loop i do to go to node at nth position
node * temp2 = * head;
temp2 = (node * ) malloc(sizeof(node));
temp2 -> freq = varn;
temp2 -> next = * head -> next; //The seond error is here , "next is something not structure or union"
* head -> next = temp2;//The third error is here , "next is something not structure or union"
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
main()
{
int size, data, i, pos,
var;
node * head = NULL;
printf("enter the size of node\n");
scanf("%d", & size);
printf("start entering the number of elements until your size\n");
for (i = 1; i <= size; i++)
{
scanf("%d", & data);
insert_beginning(data, & head);
}
node * temp1;
temp1 = head;
while (temp1 != NULL)
{
printf("%d-> ", temp1 -> freq);
temp1 = temp1 -> next;
}
printf("enter the posotion where to add the node\n");
scanf("%d", & pos);
printf("enter the variable node\n");
scanf("%d", &var);
insert_Nth_position(pos,var, & head); //There may be problem here
printf("Print after addition\n");
node * temp3 = head;
while (temp3 != NULL)
{
printf("%d-> ", temp3 -> freq);
temp3 = temp3 -> next;
}
}
The actual errors are :
hp@ubuntu:~/Desktop/Internship_Xav/Huf_pointer$ gcc tlt.c -o tlt
tlt.c: In function ‘insert_Nth_position’:
tlt.c:26:18: error: request for member ‘next’ in something not a structure or union
tlt.c:32:28: error: request for member ‘next’ in something not a structure or union
tlt.c:33:9: error: request for member ‘next’ in something not a structure or union
Answer is welcome in any language c/c++ or algorithm even.
First of all your functions have no return type. Though old C compilers allow to do this defining implicitly the return type as int
I advice to specify explicitly the return type of a function.
As for your errors then you shall not use the dereference operator in expressions as you do
* head = * head -> next;
or
* head -> next = temp2;
It would be correct to write
*head = ( *head )->next;
and
(*head )->next = temp2;
Though semantically this code is invalid. You are changing head in the loop while it shall not be changed.
Also function insert_Nth_position is wrong. Imagine for example that the value of parameter int posn
is equal to 0. In this case condition while (count != posn - 1);
will be invalid.
I would define the parameter that specifiers the position in the list as having type unsigned int
or size_t
Function insert_Nth_position could look the following way
int insert_Nth_position( node ** head, size_t posn, int varn )
{
if ( posn == 0 ) return insert_beginning( head, varn );
node *tmp = *head;
while ( --posn && tmp ) tmp = tmp->next;
if ( tmp )
{
node *new_node = malloc( sizeof( node ) );
new_node->freq = varn;
new_node->next = tmp->next;
tmp->next = new_node;
}
return ( temp != NULL );
}
If your compiler supports C99 then the return type could be substituted for _Bool
Also function insert_beginning I would declare as
int insert_beginning( node ** head, int varn );
这篇关于当通过引用传递时,通过传递链接列表在第n个位置添加元素不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!