and I've also obsered that always that below statements
1 2
elseif(f==r)
cout<<f->data;
in the program(line no. 66,67) are getting executed irrespective of the no. of elements in the queue, where 'f' and 'r' are the pointers to front and rear elements of the queue.
Thank you, for replying but I didn't understand. newn is declared inline 21 and what happens if they are in public mode newn is a data member of que which can be used anywhere in the class and are objects of dll.
1) Making newn and temp public members of que is sloppy coding. There is no reason they should be public members of que. Variables should be as local as possible. Since newn and temp are not used across calls, they should be local where they are used and not public members of que.
2) Regarding line 33, where is newn initialized? This is what I mean by sloppy coding. Had you made newn local to insert, you probably would have recognized sooner that it was never initialized. Because newn is uninitialized, it is going to cause undefined behavior. Probably an address violation.
Like AbstractionAnon said, maybe something is not initialised. Set a break point at the lines where the error occurs, run debug build and have a look at the values of the variables.
1 2
elseif(f==r)
cout<<f->data;
My guess is that the variable f is pointing to something invalid.
Here I've observed that I am not able to insert an element, what I am getting is
1.Insert
2.Remove
3.Display
4.search
5.get
6.exit
choose an option: 1
what element you want to insert :1
Segmentation fault (core dumped)
Similar to what I was getting when I tried to remove an element before, and I've also observed that I'm not getting this error for inserting an element if I don't initialize newn value to NULL in the constructor in line 23 as,
I've also observed that if I add a new element to the queue the previous element is automatically getting removed, as if I didn't use break; in the switch block. And I've checked the values in the data in f and r after inserting a new element and both are pointing towards the new element instead of f pointing the first element, if I try to print a NULL value I'm getting
Always 0 is getting displayed, irrespective of the no. of elements in queue whereas remove is working fine if we take no. of elements into consideration. If I try to print the data in f and r, 0 is getting displayed.(may be I've done more blunders than before)
1.Insert
2.Remove
3.Display
4.search
5.get
6.exit
choose an option: 1
what element you want to insert :9
Do you want to continue[y/n] :y
1.Insert
2.Remove
3.Display
4.search
5.get
6.exit
choose an option: 1
what element you want to insert :8
Do you want to continue[y/n] :y
1.Insert
2.Remove
3.Display
4.search
5.get
6.exit
choose an option: 3
Segmentation fault (core dumped)
Thank you very much, no node's 'n' pointer will be NULL if next element actually exists, I shouldn't have done it so blindly. Thank you once again for helping me complete the program, I've just inserted another cout<<newn->data<<endl; after removing ->p.