Thread: linked list

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    23

    linked list

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    char str1[10];
    typedef struct Node 
    {
            char data[10];
            struct Node *next;
    }node;
    node *head,*temp;
    head=NULL;
    temp=(node*)malloc(sizeof(node));
    temp->next=NULL;
    head=temp;
    /*printf("enter your name");
    scanf("%s",temp->data);*/
    printf("enter the name:");
    scanf("%s",str1);
    insertnode(str1,head);
    while(temp->next!=NULL)
    printf("%s\n",temp->data);
    return(0);
    }
    void insertnode(char str1,node head)
    {
    node *temp,*ptr;
    temp=head;
    while(temp->next!=NULL)
    temp=temp->next;
    ptr=(node*)malloc(sizeof(node));
    temp->next=ptr;
    ptr->data=data;
    ptr->next=NULL;
    
    }
    what is the problem in the above code its giving unknown type name node on compilation...kindly help
    Last edited by lovelycse; 08-30-2012 at 02:13 PM.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Problem 1: No indentation. You want us to help you so try to make it as easy as possible for us.
    Problem 2: You typedef node inside main(), thus it is only visible inside main.
    Problem 3: You use insertnode before you declare it. If you want to call it from main you either have to use a prototype before main or move the whole function before main.

    Bye, Andreas

    EDIT: After fixing these problems you will get some new problems to fix :-).
    Last edited by AndiPersti; 08-30-2012 at 02:20 PM.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm pretty sure we've helped you with this issue before. If you declare node in main(), then the type name "node" is only known in main().

    Preferably you should declare types in a separate h header file, and use a header file to expose the type to other source files.

    Code:
    /* in header file node.h */
    #ifndef INCLUDED_NODE_H
    #define INCLUDED_NODE_H
    typedef struct Node 
    {
            char data[10];
            struct Node *next;
    }node;
       
    void insertnode(char str1,node head);
    #endif
    /* end node.h */
    You would then implement insertnode() in yet another source file.

    Code:
    /* in c source file node.c */
    #include "node.h"
    void insertnode(char str1,node head)
    {
    node *temp,*ptr;
    temp=head;
    while(temp->next!=NULL)
    temp=temp->next;
    ptr=(node*)malloc(sizeof(node));
    temp->next=ptr;
    ptr->data=data;
    ptr->next=NULL;
    
    }
    /* end node.c */
    The least you could do is declare "node" before main().

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    23
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    typedef struct Node
    {
            char data[10];
            struct Node *next;
    }node;
    
    void insertnode(char str1,node *head);
    int main()
    {
    char str1[10];
    node *temp,*head;
    temp=head;
    temp->next=NULL;
    printf("enter the name:");
    scanf("%s",str1);
    /*printf("enter your name");
    scanf("%s",temp->data);*/
    
    insertnode(str1,head);
    while(temp->next!=NULL)
    printf("%s\n",temp->data);
    return(0);
    }
    void insertnode(char str1,node *head)
    {
    node *temp,*ptr;
    temp=head;
    while(temp->next!=NULL)
    temp=temp->next;
    ptr=(node*)malloc(sizeof(node));
    temp->next=ptr;
    ptr->data=str1;
    ptr->next=NULL;
     
    }
    what is wrong int this code..
    again finding compilation errors....help me guys

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're still lacking indentation.

    Kindly let us know what error(s) you get while compiling.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    errors(in main)
    Code:
    t.c:22: warning: passing argument 1 of 'insertnode' makes integer from pointer without a cast
    t.c:22: error: incompatible type for argument 2 of 'insertnode'
    The prototype of the insertnode(which i recommend to be insertNode or insert_node) has as 1st argument the char str1,it should be char* str1,because you want to pass an array through it.Change this in prototype and in declaration.

    Some logic faults i want to point out:
    • line 22 you check if the next is null...in this way you will not have access in the last node of your list because,because next of last node is NULL,so the while body will not be executed for this node
    • At line 22 again,in the body this time,you do not go to the next node,but you stay on the very same node for ever,thus an infinite loop is waiting you and your pc,and unfortunately your stack
    • You create a node in main ,before insertnode is called.You have the temp point for this.Well you create the node,but you have left the data of it initialized,so data contains garbage!As a result when going to print out the list(you have two nodes),the first node has garbage and only the second has real info.


    Advice : Do not use more things that you need.You do not need so many pointers

    Tip:It is very helpful if you post your errors by yourself Imagine a patient going to a doctor,and when the doctor ask the patient what is the problem,then the patient gives an incomplete statement which lacks of meaningful data (like the errors for us). That way the doctor is not helped and either he has to examine by your self(i had to set up a new project for this in order to find out the errors) or give a less specific solution as the one he would have given if he knew all the details of the problem

    EDIT->You allocate memory for ptr with malloc but you do not free it.Wrong!Use free to de - allocate.Unless this happened because you have this to be done in the delete function that you are going to write later
    Also use strncpy to copy the str1 inside the function.
    Last edited by std10093; 08-30-2012 at 06:22 PM.

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by lovelycse View Post
    what is wrong int this code..
    again finding compilation errors....help me guys
    Please learn to ask smart questions.
    Thanks.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring linked list inside linked list
    By blueboyz in forum C Programming
    Replies: 33
    Last Post: 04-20-2012, 10:13 AM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM