Thread: Problem in output of code(Inserting node in linked list)

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    2

    Problem in output of code(Inserting node in linked list)

    It only prints the first element
    After that it shows the same output everytime.
    Any help will be appreciated.


    Code:
    #include<conio.h>
    #include<stdio.h>
    #include<stdlib.h>
    struct node
    {
    int data;
    struct node *next;
    };
    struct node *start;
    void insert_n(int val,int x)
    {
    int c=0;
    int i;
    struct node *preptr;
    struct node *ptr=(node *)malloc(sizeof(struct node));
    if((start==NULL)&&(x>1))
    {
    printf("\nEmpty list.Cannot insert at given position.");
    }
    else if((start==NULL)&&(x==1))
    {
    ptr->data=val;
    ptr->next=start;
    start=ptr;
    }
    else if((start!=NULL)&&(c<x))
    {
    for(i=0;i<x-2;i++)
    {
    preptr=preptr->next;
    }
    ptr->data=val;
    ptr->next=preptr->next;
    preptr->next=ptr;
    c++;
    }
    
    
    }
    void print()
    {
    struct node *temp;
    temp=start;
    while(temp->next!=NULL)
    {
    printf("%d",temp->data);
    temp=temp->next;
    }
    printf("%d",temp->data);
    }
    void main()
    {
    int i,n,x,val;
    clrscr();
    start=NULL;
    printf("\nhow many elements: ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    printf("\nEnter new alement: ");
    scanf("%d",&val);
    printf("\nEnter position: ");
    scanf("%d",&x);
    insert_n(val,x);
    print();
    }
    getch();
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The first step would be posting code in an indent style other than "none".
    Indent style - Wikipedia, the free encyclopedia

    Other things which stand out.
    - Using the obsolete header file conio.h
    - Using void main, where int main is standard.
    - Using unnecessary global variables; start should be passed as a parameter / returned as a result as appropriate.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2016
    Posts
    2
    But do you see whats wrong with the code..??

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Maybe, but I'd rather you post formatted code first.
    It would make it easier to see what the problems are.

    Sure, I could reformat the code, but why should I bother, if you can't be bothered.

    Or to put it simply, you attract more interest with honey than you do with vinegar.
    What you posted is vinegar (on most other forums, it would simply be ignored).
    Formatted code is honey.
    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. Two Question About Inserting Node In the Linked List
    By hefese in forum C Programming
    Replies: 10
    Last Post: 08-07-2012, 12:24 PM
  2. problem inserting node in middle of list
    By c_weed in forum C++ Programming
    Replies: 3
    Last Post: 12-22-2010, 09:14 PM
  3. Replies: 7
    Last Post: 11-04-2010, 01:18 PM
  4. Linked List Node Problem
    By cpazurek in forum C Programming
    Replies: 5
    Last Post: 07-22-2009, 01:37 PM
  5. Problem With Inserting into Linked List
    By Stunner in forum C++ Programming
    Replies: 6
    Last Post: 07-19-2007, 08:28 PM

Tags for this Thread