thankuuuuuuuuuuuuuuuuuuuuuuu sooooooooooo much...!!!!!!!!!it worked....but i will be very pleased and happy if u could kindly tell me why the code went into unending loop when i placed the temp variable outside the method?
because i did not understand y it happened..its imp for me to know so that i will repeat the mistake..:-)
In the first call of adddata() you set head=temp; on line 27.
In the second call of adddata() you set temp->next=head; on line 26. Due to the previous head=temp; and since temp is always the same object temp->next points to itself.
On line 38 displaytemp will be never NULL but always temp which leads to an infinite loop.
To avoid that temp needs to be different within each call of adddata().
By the way: It doesn't make sense to assign something twice like on the lines 15/17 or 32/33
I am not able to insert data at the end of the node..can u please help me...i am not understanding the problem this time.
the problem is when i am inserting data from the insertatend() function after using adddata() ,and then when i am clicking display() function the new data is not getting displayed.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<iostream.h>
struct node{
int data;
struct node *next;
}*head,*temp,*displaytemp,*temp2,*temp3,*temp6;
int count=0;
void adddata(){
cout << "welcome to add data,enetr any number";
temp = new (Struct node);
int num;
temp->date=num;
temp->next=head;
head=temp;
}
void displaydata(){
cout << "welcome to display data func.";
displaytemp = new (Struct node);
displaytemp = head;
while(displaytemp !=NULL){
cout << displaytemp->data;
displaytemp = displaytemp->next;
}
void insertatend(){
int enddata;
cout << "enter data to be inserted at end";
cin >> enddata;
temp2 = new (struct node);
temp3 = new (struct node);
temp6 = new (Struct node);
temp3 = head;
int count=0;
temp2->data = enddata;
while( temp3 != NULL){
if(count==0){
temp6->next=temp3;
}
temp3 = temp3->next;
count=count+1;
}
temp3->next = temp2;
head=temp6;
}
int main(){
if(count==0){
}
int myval=0;
while(myval!=1){
cout << "enter ur choice: a. add b. display c. insert at end d.exit";
char choice = NULL;
cin >> choice;
switch(choice){
case'a':adddata();
break;
case'b':displaydata();
break;
case'c':insertatend();
breakcase'd':myval=1;
}
}
}
count=count+1;
return 0;
}
thanks for the previois program explananation.Kindly help me with this program.Its the same as previous program except that i have included a new function to insert the data at the end.
Again: this double assignment on line 28/29 doesn't make sense. Remove line 28.
Lines 42/43/44: Since you want to add one node it does not make sense to create three nodes.
insertatend() will be similar to displaydata() because you do similar things:
void delatend(){
if( !head )
return;// list is empty
if( !head->next )// only one node in list
{
delete head;
head = nullptr;
return;
}
// at this point we can safely assume there are at least 2 nodes in the list
node* iter = head;
while( iter->next->next )// ok to dereference twice here
iter = iter->next;
// iter should now be pointing to the next to last node
delete iter->next;// delete last node
iter->next = nullptr;// mark new list end
}
i modified the program regarding the last line.,,its deleteing the last element but its displaying some garbage value in place of the last node...i am not understanding where the problem is...
for ex:
if i am adding 2 numbers to the list
2
1.
and now i call the delatend() function..values are displaying in the below manner
2
3925
please help
I tested the function I posted and it works. Why did you change it?
Perhaps my use of nullptr caused a compiler error for you.
Here it is again with NULL instead:
void delatend(){
if( !head )
return;// list is empty
if( !head->next )// only one node in list
{
delete head;
head = NULL;
return;
}
// at this point we can safely assume there are at least 2 nodes in the list
node* iter = head;
while( iter->next->next )// ok to dereference twice here
iter = iter->next;
// iter should now be pointing to the next to last node
delete iter->next;// delete last node
iter->next = NULL;// mark new list end
}