通过引用传递链接列表时,在第n个位置添加元素

我是新链接列表这是我在LL中插入元素后的第二个问题。现在我试图在第n个位置插入元素。 我这样做:

(1)首先在终端上取用户的大小。

(2)第二次从用户连续读取输入直到大小。

(3)我在LL的开头添加了在终端读取的元素。

(4)我打印出LL直到形成。

直到这里一切正常

(5)之后我尝试在LL的第n个位置添加,但是它给出了3个错误我已经在我的代码中的注释中解释了。 还请告诉我,我在第n个位置添加元素的逻辑是否正确?

注意: 我有义务仅在函数调用中传递List节点作为引用(并在函数定义中取消引用它们)

下面是我的完整代码,指出评论中的错误。

#include  #include  #include  #include  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  ", 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; } } 

实际错误是:

 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 

欢迎使用任何语言c / c ++或算法回答答案。

首先,您的函数没有返回类型。 虽然旧的C编译器允许隐式地将返回类型定义为int我建议明确指定函数的返回类型。

至于你的错误,你不应该像你一样在表达式中使用dereference运算符

 * head = * head -> next; 

要么

 * head -> next = temp2; 

写这是正确的

 *head = ( *head )->next; 

 (*head )->next = temp2; 

虽然在语义上这个代码是无效的。 你正在改变循环中的头部,而它不会被改变。

函数insert_Nth_position也是错误的。 想象一下,例如参数int posn值等于0.在这种情况下条件while (count != posn - 1); 将无效。

我将定义参数,该参数指定列表中的位置为unsigned intsize_t类型

函数insert_Nth_position可以采用以下方式

 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 ); } 

如果您的编译器支持C99,则返回类型可以替换为_Bool

函数insert_beginning我也会声明为

 int insert_beginning( node ** head, int varn ); 
 * head = * head -> next; 

这里,箭头操作符->的优先级高于address-of operator * ,需要括号才能使其编译:

 *head = (*head)-> next;