Thread: Insertion in linked list

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    12

    Insertion in linked list

    I am unable to insert data in a linked lists.
    Show function is not working.
    insert
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
       typedef struct node{
       int data;
       struct node *next;
       }
       Node;
       Node *sptr;
       Node *head=NULL;
       
       void show();
       void ins(int key);
       
       int main()
       {
       int i,item;
       for(i=0;i<=5;i++)
          {
       printf("Enter number\n"); // Inserting 6 elements in list
       scanf("%d",&item);
       ins(item);                    
          }
       show();                       //To show the data in linked list
       return 0; 
       }
       void ins(int key)
        {
        Node *ptr=NULL;
        ptr=(Node*)malloc(sizeof(Node));
        ptr->data=key;
        if(head==NULL)
            {
        head=ptr;
        ptr->next=NULL;
            }
        else
            {
        ptr->next=head;
        head=ptr;
            }
        }
        
        void show()
        {
        while(sptr->next!=NULL)
          {
        printf("%d\n",sptr->data);
          }
        }
    Last edited by dumb09; 02-09-2015 at 06:35 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please indent your code properly. You should also explain how does the code not work.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    12
    Show function is not working in the code.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, you did not assign to sptr in your show function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    12
    I have declared it globally.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That makes it even worse. Your show function should look like this:
    Code:
    void show(const Node *node)
    {
        while (node != NULL)
        {
            printf("%d\n", node->data);
            node = node->next;
        }
    }
    Notice that I declared a parameter named node of type const Node*. The const is there because when you're displaying something, you typically don't need to change it, i.e., it is constant in that context. Then, I loop over the nodes starting from that node. Notice that I assign to node within the loop: that's how we get from the current node to the next node. This function could be called like this:
    Code:
    show(head);
    head should be a local variable too.

    Oh, and notice that I indented the code properly: the body of the function is indented by one level, then the body of the while loop indented by yet another level. This way, we can easily see where they start and end.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2014
    Posts
    12
    Still, It is not working

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'm so sorry to hear that. Perhaps you should post your current code and explain how does it not work. Otherwise, I will only be able to help you after I have acquired mind reading powers over the vast distances that separate us, and that could very well be never.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP! Linked List Insertion
    By divisionbyzero in forum C Programming
    Replies: 1
    Last Post: 04-08-2010, 03:43 PM
  2. Alphabetical Linked List Insertion
    By serg_yegi in forum C Programming
    Replies: 6
    Last Post: 03-27-2010, 11:11 PM
  3. Linked List Insertion Sort
    By The Brain in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2005, 07:20 PM
  4. Linked List alpha insertion
    By mouse163 in forum C++ Programming
    Replies: 9
    Last Post: 02-21-2005, 02:24 PM
  5. Insertion Linked List Help
    By 0rion in forum C Programming
    Replies: 4
    Last Post: 05-12-2004, 07:38 AM

Tags for this Thread