In the future, you should make a
minimal program that we can run, preferably without having to figure out what to type, that exactly reproduces your problem. This means removing anything that isn't related to your problem while still reproducing the issue.
For example, especially since your issue is not with reading from the file itself, remove any file reading from your program; it just makes it harder for people to help you, and will dramatically lower the number of replies you get.
Okay, so let's say there's only 1 node on the list, which could mean you've only inserted 1 node, and counter is 1. Both start and last now point to the only node, I assume?
Then, you call the following, I assume:
1 2
|
cdl.delete_pos();
cdl.display();
|
Is the first position 0 or 1? It looks like you want it to be 1.
So then you do the following:
1 2 3 4 5 6 7 8
|
s = start;
counter--;
last->next = s->next; // set to null
s->next->prev = last; // This looks wrong. s->next is null. So you might be dereferencing a null pointer.
start = s->next; // set start to null.
delete(s);
cout << "Element successfully removed!" << endl << endl;
return;
|
* so counter is now 0.
* start is null (0)
* last is not updated (still points to the the deleted data, as far as I can tell).
* You've invoked undefined behavior by deferencing a null next.
On line 277, start is no longer equal to last, so that if-statement is not reached.
Instead, it continues down your display function, and tries to dereference deleted data by deferencing start (s). et viola; you have junk.
Also,
1 2
|
*start, *last;
int counter = 0;
|
AVOID GLOBAL VARIABLES. They will bite you! For example, it's impossible for your program to have more than 1 linked list without causing bugs.