Thread: Can any one help me to add number in a singly linked list?

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

    Can any one help me to add number in a singly linked list?

    Can any one help me to add number in a singly linked list?
    here is my code...

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    struct node {
    int number;
    struct node *next;
    }*head;
    
    
    int Insert(int num)
    {
        struct node *temp;
        temp=(struct node*)malloc(sizeof(struct node));
        temp=head;
        while(temp->next!=NULL){
            temp=temp->next;
        }
        temp->next=(struct node*)malloc(sizeof(struct node));
        temp->number=num;
    
    
        temp->next=NULL;
        head=temp;
        //struct node *temp;
        //temp=(struct node*)malloc(sizeof(struct node));
        //temp->number=num;
       // temp->next=head;
       // head=temp;//
    
    
    
    
    }
    void display(){
        struct node *temp;
        temp=head;
        while(temp!=NULL){
            printf("%d ",temp->number);
            temp=temp->next;
        }
    }
    
    
    int main()
    {
        int n,num,i=0;
        printf("How many number do you want to input??\n");
        scanf("%d",&n);
        for(i=0;i<n;i++){
            printf("Enter Your number- %d - ",i+1);
            scanf("%d",&num);
            Insert(num);
    
    
        }
        printf("Your number are--");
        display();
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Before you can implement the Insert function, you need to be clear on what it does. So, what does the Insert function do? Describe in detail.
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Code:
    temp=(struct node*)malloc(sizeof(struct node));
    temp=head;
    while(temp->next!=NULL)
    1. What happens to the memory allocated on line 1, when the assignment on line 2 happens.
    2. What happens on line 3, when head is NULL the first time this is called.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly linked list c
    By Creotik in forum C Programming
    Replies: 5
    Last Post: 01-15-2014, 08:42 PM
  2. Need help with Singly Linked List C++
    By luckyali444 in forum C++ Programming
    Replies: 3
    Last Post: 10-18-2011, 11:30 PM
  3. singly linked list
    By aextine in forum C Programming
    Replies: 2
    Last Post: 06-08-2010, 02:20 PM
  4. singly linked list
    By right2001 in forum C Programming
    Replies: 3
    Last Post: 08-20-2009, 10:21 AM
  5. singly linked list
    By Luigi in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2002, 11:19 AM