single linked list through c++

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  #include<stdio.h>
#include<conio.h>
#include<string.h>
#include<iostream.h>

Struct node{

   int data;
   Struct node *next;
}*head,*temp,*displaytemp;
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;
   }
}
int main(){

   if(count==0){


   }

   int myval=0;
   while(myval!=1){

        cout << "enter ur choice: a. add  b. display  c. exit";

        char choice = NULL;
        cin >> choice;
        switch(choice){

          case 'a':adddata();
            break;
          case 'b':displaydata();
            break;
          case 'c':myval=1;
        }
 }
}

   count=count+1;
  return 0;
  


}




hi all..i am not able to display the data from the list..when i am trying to do that its going into unending loop..plz help
Last edited on
Move line 16 inside adddata() before line 25. temp should be a local variable. date will always contain a garbage value.

Struct is supposed to be struct // Note: lower case .
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..:-)

thanks a ton
The point is that you have just one object: temp.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

#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();
             break
          case '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.
Last edited on
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void insertatend(){

   int enddata;
   cout << "enter data to be inserted at end";
   cin >> enddata;

   node *insertatend_node = new node;
   insertatend_node->date=enddata;

   if(head !=NULL)
   {
        node *iteratenode= head;
        node *endnode;

        while(iteratenode !=NULL){

              endnode = iteratenode;
              iteratenode = iteratenode->next;
        }
        endnode->next = insertatend_node;
   }
   else
        head = endnode;
}
in the below program i am trying to delete the last node..but its not getting deleted.Can u please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

void delatend(){

  node *somenode1_temp = new node;
  node *enddel_temp = new node;
  somenode1_temp = head;
  enddel_temp = head;
  int count=0;
  while(somenode1_temp->next!=NULL){

      somenode1_temp = somenode1_temp->next;
      count=count+1;
  }
    for(int i=0;i<=count-1;i++){

	enddel_temp = enddel_temp->next;

    }
    enddel_temp->next = NULL;
}


also thank u for the previous to previous program ...it worked...:)...i am very happy....:)kindly help me with the previous program...
closed account (D80DSL3A)
You need a pointer to the next to last node so you can assign its next = nullptr, marking the new list end.
Here's one way (warning: untested)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

void delatend(){

  node *somenode1_temp = new node;
  node *enddel_temp = new node;
  somenode1_temp = head;
  enddel_temp = head;
  int count=0;
  while(somenode1_temp->next!=NULL){

      somenode1_temp = somenode1_temp->next;
      count=count+1;
  }
    for(int i=0;i<=count-1;i++){

	enddel_temp = enddel_temp->next;

    }

    delete enddel_temp;
    enddel_temp->next = NULL;

}


closed account (D80DSL3A)
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
 
}
Topic archived. No new replies allowed.