Adding element at nth position by passing linked list when passed by reference not working

你离开我真会死。 提交于 2019-12-12 05:28:07

问题


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.


回答1:


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



回答2:


* head = * head -> next;

Here, the arrow operator -> has a higher precedence than address-of operator *, you need parenthesis to make it compile:

*head = (*head)-> next;


来源:https://stackoverflow.com/questions/22114872/adding-element-at-nth-position-by-passing-linked-list-when-passed-by-reference-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!