I have a problem in the linklist please help me?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AnagJohari
    New Member
    • May 2010
    • 96

    I have a problem in the linklist please help me?

    hello guys
    i succesfully create a linklist but have a problem to insert a perticualt node from beg in the link list i try so much but not getting the reason why my code is not running

    i send you a code then i explain
    Code:
    #include<conio.h>
    #include<stdio.h>
    #include<malloc.h>
    struct node
    {
    int no;
    struct node *next;
    } *start,*current,*temp;
    void CreateLinkList();
    void DisplayLinklist();
    void InsertBefore();
    void main()
    {
    struct node *p;
     CreateLinkList();
     DisplayLinklist();
     InsertBefore();
     DisplayLinklist();
    getch();
    }
    void CreateLinkList()
    {
    int ch;
    clrscr();
    start=(node*)malloc(sizeof(node));
    start=NULL;
    printf("enter the value(s)");
    scanf("%d",&start->no);
    start->next=0;
    temp=(node*)malloc(sizeof(node));
    temp=start;
    printf("Do you want to add the next node press 1");
    scanf("%d",&ch);
    while(ch==1)
    {
    current=(node*)malloc(sizeof(node));
    printf("eneter the value(e)");
    scanf("%d",&current->no);
    temp->next=current;
    temp=current;
    printf("Do you want to add the next node press 1");
    scanf("%d",&ch);
    }
    current->next=NULL;
    }
    
    void DisplayLinklist()
    {    current=start;
    
    
    int i=(int)start->next;
    printf("int= %d",i);
    
    printf("The Link List=");
    while(current->next!=NULL)
    {
    printf("%d->",current->no);
    current=current->next;
    }
    printf("%d",current->no);
    }
     void InsertBefore()
     {
     struct node *newnode;
     int ch=1;
     while(ch==1)
     {
    
     newnode=(node*)malloc(sizeof(node));
     printf("\nenter the value in new node");
     scanf("%d",&newnode->no);
     newnode->next=start;
     start->next=temp->next;
     start=newnode;
     printf("\ndo u want to add the newnode again press 1 ");
     scanf("%d",&ch);
     }
    }
    create linklist & displaylinklist is worked properly.
    when i use insertbefore function its not working not linked the value to my linklist which i create
    suppose i input the values 1 2 3 4 5
    display function work correctly
    by displaying my linklist like this
    1->2->3->4->5
    ------------------
    now from here the problem is occured
    when i try to insert value before "1" suupose that is "0"
    after that i again insert a value before "0" suppose that is"8"
    now display function display my link list in the following manner
    8->1
    ------
    thats it
    nots displaying other values
    please correct my code & tell me where i was doing wrong so i can know what is problem actually is?
    thank you
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:

    Code:
    newnode->next=start; 
     start->next=temp->next; 
     start=newnode;
    from the InsertBefore function doesn't work.

    True: newnode->next = start
    That's because the new node is the new start of the list so the newnode->next has to be the current start of the list.

    True:start = newnode
    This identifies the newnode as the current start of the list.

    False:start->next=temp->next
    a) start->next is already correct. It points to the next node of the list.
    b) what is temp->next? temp is not referred to in this function.

    It think you need to delete this line of code.

    Comment

    • AnagJohari
      New Member
      • May 2010
      • 96

      #3
      tnx its work
      you are great man

      Comment

      Working...