Help with Linked List.

Hi. So here's the deal, I'm making a linked list program and everything went smoothly, until I added a if else statement in my remove method to try to catch whether a user entered value is in the list and deletes it, or outputs "Value not found" if not found.

The problem is that when I enter a string not found in the list, the program just terminates. I would appreciate if anyone could help.

Here's the program:
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include <string>

using namespace std;

void instructions();
string userInput();

class Node
{
    public:
        string data;
        Node *next;

        Node(string d)
        {
            data = d;
            next = NULL;
        }
        Node(string d, Node *n)
        {
            data = d;
            next = n;
        }


};

class linked
{
    Node *head;


    public:
        int counter;
        linked()
        {
            head = NULL;
            counter = 0;
        }

        bool isEmpty()
        {
            return head == NULL;
        }

        int size()
        {
            return counter;
        }

        void insert()
        {
            string d = userInput();
            Node *temp;
            if(head == NULL)
            {
                head = new Node(d, NULL);
                counter++;
                cout<<"Inserted: "<<head->data<<endl;
            }

            else
            {
                 temp = head;
                 while(temp->next !=NULL)
                    temp = temp->next;
                 temp->next = new Node(d, NULL);
                 counter++;
                 cout<<"Inserted: "<<temp->next->data<<endl;
            }

        }

        string delhead()
        {
            Node *temp = head;
            head = head->next;
            return temp->data;
        }

        void remove()
        {
            int x;
            string d = userInput();
            if(head == NULL)
                cout<<"The list is Empty."<<endl;
            else if(head->data.compare(d) == 0)
                cout<<"Removed: "<<delhead()<<endl;
            else
            {
                Node *temp = head;
                for(x=0; x<=counter; x++)
                {
                    if(temp->next->data.compare(d) == 0)
                        break;
                    else
                        temp = temp->next;
                }

                if(x>=0)
                {
                    Node *rem = temp->next;
                    temp->next = temp->next->next;
                    counter--;
                    cout<<"Removed: "<<rem->data<<endl;
                }
                else
                    cout<<"Value entered not found."<<endl;
            }


        }

        void display()
        {
            Node *temp;
            temp = head;
            while(temp != NULL)
            {
                cout<<temp->data<<endl;
                temp = temp->next;
            }

        }

};

int main(void)
{
    linked ln;
    int choice;

    do
    {
        instructions();
        cout<<"? ";
        cin>>choice;
        cin.ignore();

        switch(choice)
        {
            case 1:
                ln.insert();
                break;

            case 2:
                ln.remove();
                break;
            case 3:
                ln.display();
                break;
            case 4:
                cout<<"Goodbye!"<<endl;
                break;
            default:
                cout<<"Invalid Choice! Try Again..."<<endl;
                break;
        }

    }while(choice != 4);

    cin.get();
    return 0;
}

void instructions()
{
    cout<<"1. Insert\n2. Delete\n3. Display\n4. Exit"<<endl;
}

string userInput()
{
    string value;
    cout<<"Enter Value: ";
    getline (cin, value);

    return value;
}


Here's the problematic output:

1. Insert
2. Delete
3. Display
4. Exit
? 1
Enter Value: asd
Inserted: asd
1. Insert
2. Delete
3. Display
4. Exit
? 2
Enter value: d

Process returned -1 (0xFFFFFFFF) execution time: 9.982 s
Press Enter to Continue


I've been at this for the past hour and to no avail, I can't figure out what's wrong.
Last edited on
Suppose that you've got 1 element in your list. counter = 0
1
2
3
4
5
6
7
8
               Node *temp = head;
                for(x=0; x<=counter; x++) //¿how many times is the loop executed?
                {
                    if(temp->next->data.compare(d) == 0) //¿which element are you looking at?
                        break;
                    else
                        temp = temp->next;
                }
@ne555 Ah, so you mean when it accesses the the temp->next-data when there is really no temp->next->data then it exit abnormally??
Got it sorted out. Thanks for the help ne555. I did not think that it would terminate incorrectly if it accesses something not there. Thanks again!
Topic archived. No new replies allowed.