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
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
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",¤t->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); } }
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
Comment