here below is code written in turboo c++ compiler, when i run first time below code on turbo c i get correct output but when i run it again, i get some infinite loop printed output… i stuck in this problem from very long time… i am getting such problem when i am calling deleteFirstNode(…) function, before that function i think everyThing is allright… plese someONe help me
in Short checkOut deleteFirstNode(…) implementation
Thanku IN Advance
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct Node{
int value;
struct Node *next;
};
void addFirst(int value,struct Node **head){
struct Node *node=(struct Node*)malloc(sizeof(struct Node));
(*node).value=value;
if(node==NULL){
printf("%s","Memory Not allocated by malloc");
return;
}
(*node).next=*head;
*head=node;
}
void traverseNode(struct Node *temp){
if(temp==NULL){
printf("%s\n","LinkedeList Is Empty");
return;
}
while(temp!=NULL){
printf("%d\n",(*temp).value);
temp=(*temp).next;
}
printf("\n\n");
}
void traverseNodeUsingp2p(struct Node **temp){
struct Node *temp1=*temp;
//if(*temp==NULL){
//printf("%s\n","p2p LinkedList is empty");
//return;
//}
//while(*temp!=NULL){
//printf("%d",(**temp).value);
//*temp=(**temp).next;
//}
if(*temp==NULL){
printf("%s\n","LinkedList is Empty");
return;
}
while(temp1!=NULL){
printf("%d",(*temp1).value);
temp1=(*temp1).next;
}
}
void insertAfter(struct Node *temp,int jiskeAageAddKarnaH,int jisNodeKoAddkarnaH){
struct Node *node=(struct Node*)malloc(sizeof(struct Node));
(*node).value=jisNodeKoAddkarnaH;
if(temp==NULL){
printf("%s\n","LinkedList is Empty");
return;
}
while(temp!=NULL){
if((*temp).value==jiskeAageAddKarnaH){
break;
}
temp=temp->next;
}
if(temp!=NULL){
(*node).next=(*temp).next;
(*temp).next=node;
}
}
void append(int value,struct Node **temp){
struct Node *node=(struct Node*)malloc(sizeof(struct Node));
struct Node *temp1=*temp;
(*node).value=value;
if(*temp==NULL){
*temp=node;
return;
}
while((*temp1).next!=NULL){ //HERE we will not use(temp1!=null) because fir change ni kar payenge
temp1=(*temp1).next;
}
(*temp1).next=node;
}
void deleteFirstNode(struct Node **temp){
struct Node *temp1=*temp;
if(*temp==NULL){
printf("%s\n","LinkedList is empty");
return;
}
//free(*temp);
*temp=(*temp1).next;
}
void main(){
struct Node *head=NULL;
clrscr();
addFirst(5,&head);
//traverseNodeUsingp2p(&head);
//traverseNode(head);
addFirst(10,&head);
//traverseNodeUsingp2p(&head);
traverseNodeUsingp2p(&head);
//traverseNode(head);
insertAfter(head,5,15);
traverseNodeUsingp2p(&head);
insertAfter(head,5,20);
traverseNodeUsingp2p(&head);
append(25,&head);
append(30,&head);
traverseNodeUsingp2p(&head);
deleteFirstNode(&head);
//deleteFirstNode(&head);
//traverseNode(head);
traverseNodeUsingp2p(&head);
getch();
}