Singly linked list problem

I am new to c++ , trying to write singly linked list in c++ , and it's various operations like , print, insert at the end, insert the end, delete etc. I am doing step by step. I have written my first insert method. It is working fine no error , but the first time it goes to the if condition of insert i.e the list is empty and create the first node here the problem is coming is after entering the first node the output screen is closing saying "project has stopped working" . what I am trying to do is after entering one node it should ask "do u wish to add another node?" if users enter "y" it should continue otherwise should go back to main function : Here is my code :

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  #include<iostream>

using namespace std;

struct node
{
  int num;
  node *next;
} ;

class link_list
{
  node *head;
  node *list;
  public:
         link_list()
         {
               head = NULL;
               list = NULL;
               //cout << list ;
         }
        // void display_options(link_list &b);
         void print_list();
         void insert();
         //void insert_at_beg();
        // void insert_at_middle();
};  

void link_list :: print_list()
{
 if (list==NULL)
 {
                cout<< "Empty list"<< endl;
                //return;
 }
 else
 {
  int count =0;      
  while(list->next!=NULL)
  {
     list=list->next;
     count++;    
     cout << "Node        " << "value " << endl;
     cout << count << "         " << list->num <<endl ;       
  }
 }
}  

void link_list :: insert ()
{

 // head = new node;
  //list = head;
  int y;
  char a;

  do
  {
  if(head==NULL)
  {

                head = new node;
                cout << "enter the first node" << endl;
                cin >> y;
                list->num=y;
                list->next=NULL;

  }
  else
  {
       node * newNode;
       list==head;
       while(list->next!=NULL)
       {
           list->next=list;
       }
           newNode = new node;
           cin >> y;
           newNode->num=y;
           list->next=newNode;
           newNode->next=NULL;


  }
  cout<< "do u wish to add another node?" << endl;
  cin >> a;
  } while(a=='y'||a=='Y');


}


int main()
{  
link_list ll;
char choice;
do{
cout << "select one"<< endl;
cout <<  "press 1 for insert ." << endl;
cout << "press 2 for insert at beginning ." << endl;
cout << "press 3 for insert at the middle ." << endl;
cout << "press 4 delete ." << endl;
cout << "print 5 print the linked list :" << endl;
cout << "print 6 exit :" << endl;

     int no;
     cin >> no;
     switch (no)
     {
            case 1:
                 ll.insert();
                 break;
            case 5:
                 ll.print_list();
            case 6:
                 return 0;

            default:
                    cout <<"oops wrong choice"<< endl;

     }
     
 cout << "Do u wanna make another choice?" << endl;
 cin >> choice;
 cout<< choice << endl;
 }while(choice=='Y'||choice=='y');
cout<<"Thanks!"<< endl;
system("pause");
return 0;
}  
@65
1
2
   list->num=y;
   list->next=NULL;


should be
1
2
   head->num=y;
   head->next=NULL;


@72 == is a comparison, you need =

@75 try
1
2
list = list->next;
 


other than that it's looking nice :)

edit...
Your prompt will work better if you move lines 63 & 64 and put them between line 58 & 59.

thats still a bit messy though, really you should ask the user before calling insert and pass the users value to insert()
1
2
3
   cout << "enter the value " << endl;
   cin >> y;
   insert(y);
Last edited on
@Jaybob66

thanks a lot . It's working fine now... :) but now having another problem with print after inserting if I enter a choice to print, no output screen is coming.
in print_list()

if (list==NULL)
should be
if (head==NULL)

and just before
while(list->next!=NULL)
add
list = head;

remember:
1) head is your anchor, only ever assign it when the list becomes empty (head=null) or when the very first item is inserted.

2)list is your pointing finger that scans the list, always set it to head before looping.
Last edited on
@Jaybob66

thanks again :) but even after modifying the code in print, the result is same. i am not sure if it's printing anything or not but the output screen is not staying, it's disappearing :(
if your application is a command line one, then the shell will close when the app finishes.

I usually set a breakpoint at the end of main() to stop that, but i'm sure there are other ways too.
@jaybob66
I got my solution , it was happening due to a silly mistake of mine... i forgot to put a break in switch function in main after calling the print function... thanks a lot for your help :)
Topic archived. No new replies allowed.