Circular Linked List

Context:Create a circular, doubly linked list from a file (input.txt), containing a list of names. The number of names is unknown.

Prompt the user for the number of nodes to delete and then delete accordingly from the list.

Assumption: The number inputted by the user will not exceed the actual number of nodes in the list.

Question: My delete function does not work. Whenever I output the data that is in the supposedly deleted nodes, it is still there. Any 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
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
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include<cstdlib>
#include <ctype.h>
#include <queue>


using namespace std;

  struct node {
     string data;
     node *next;
     node *prev;
};



    void deleteNode(struct node *&head)
{
            struct node *current=head; //copy of head.

        {


            current->prev->next=current->next;//set next value of next for the "first" node to the node prior to head.
            current->next->prev=current->prev;//set the value of the node that is pointing to head, to point to "first" node instead.
            head=current->next; //new head.

            delete current; //get rid of node that needs to be deleted

        }
        cout << head->data << endl;

}



void insertNode (string item, struct node *&head,int counter,int x)
    {
      node * newNode;
        newNode= new node;//creates new node.
        newNode->data=item;//stores value from queue in node.


        if (!head) { //nothing in the list, everything points to itself.


            newNode->prev=newNode;
            newNode->next=newNode;
            head = newNode;

        }
        else { //something in list. Connect old end and old head

            newNode->prev=head->prev; //connect newNode to end.
            newNode->next=head; //connect NewNode to head.

            head->prev=newNode;//connect old head to the one before it which is the new node.
            newNode->prev->next=newNode;//connect the end to the new node.
            head = newNode; //head becomes newNode.

        }


            if (counter < 2)//after node creation is completed.
                {
                    cout << " Please enter the number of nodes to be deleted. " << endl;
                    cin >> x;

                        if (x > 0)
                            {
                                cout << "Delete function started." << endl;
                                    while (x != 0)
                                        {
                                            deleteNode(head);//function call to deleteNode.
                                            x--;
                                        }

                            }

                }
counter--;

}


int main()
{
    struct node *head=NULL;
    int counter = 0;
    int x = 0;
    string number;
    ifstream myfile;
    queue <string> storage;

  myfile.open ("input.txt");

  while (true)
    {
       getline (myfile, number);
       storage.push (number); //storing values from file in queue.
       counter++;
       cout << " Iterations: " << counter << endl;
       if (myfile.eof() ) break;
    }

    while (counter != 0)
        {
            insertNode(storage.front(),head,counter,x); //passing values from queue.
            cout <<" Input: " << storage.front() << endl;
            storage.pop();
            counter--;
        }



    return 0;
}
Last edited on
Can you be more specific about what you're doing? Where are you printing out data that you believe to have been deleted?

One thing to bear in mind - just because you delete an object, it doesn't mean that the memory at that location will instantly be filled with new values. It's entirely possible that you're looking at a memory location of an object you've deleted, and simply seeing old data that hasn't yet been overwritten.

Something else to consider: in your deleteNode function, what happens when head is the only node in the list?

Also, your code would be easier for you and us to read if you used a sensible, consistent indentation style. That will allow you to see the flow of logic and control much more easily, and spot more easily where you've made a mistake.
My apologies, I have been at this for quite some time now and have been trying to get it to work, rather than focusing on the formatting, but you are correct, it would be easier on the eyes. I don't know what you mean by being more specific about what I am doing, I thought I mentioned that in the context above the code. But anyway, I fixed the problem. Thanks

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
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include<cstdlib>
#include <ctype.h>
#include <queue>


using namespace std;

  struct node {
     string data;
     node *next;
     node *prev;
};



    void deleteNode(struct node *&head)
{
            struct node *current=head; //copy of head.




            current->prev->next=current->next;//set next value of next for the "first" node to the node prior to head.
            current->next->prev=current->prev;//set the value of the node that is pointing to head, to point to "first" node instead.
            head=current->next; //new head.

            delete current; //get rid of node that needs to be deleted



}



void writeHead(struct node* curr, int counter2, int y)
{
    int i = 0;
    i = counter2-y;
    while (i > 0)
    {
        cout << curr->data<<endl;
        curr= curr->prev;
        i--;
    }
}



void insertNode (string item, struct node *&head,int counter)
    {
      node * newNode;
        newNode= new node;//creates new node.
        newNode->data=item;//stores value from queue in node.


        if (!head) { //nothing in the list, everything points to itself.


            newNode->prev=newNode;
            newNode->next=newNode;
            head = newNode;

        }
        else { //something in list. Connect old end and old head

            newNode->prev=head->prev; //connect newNode to end.
            newNode->next=head; //connect NewNode to head.

            head->prev=newNode;//connect old head to the one before it which is the new node.
            newNode->prev->next=newNode;//connect the end to the new node.
            head = newNode; //head becomes newNode.

        }



counter--;

}


int main()
{
    struct node *head=NULL;
    int counter = 0;
    int counter2 = 0;
    int x = 0;
    int y = 0;
    string number;
    ifstream myfile;
    queue <string> storage;

  myfile.open ("input.txt");

  while (true)
    {
       getline (myfile, number);
       storage.push (number); //storing values from file in queue.
       counter++;
       counter2++;
       cout << " Iterations: " << counter << endl;
       if (myfile.eof() ) break;
    }


    while (counter != 0)
        {
            insertNode(storage.front(),head,counter); //passing values from queue.
            storage.pop();
            counter--;
        }

        if (counter < 2)//after node creation is completed.
                {
                    cout << " Please enter the number of nodes to be deleted. " << endl;
                    cin >> x;
                    y = x;

                        if (x > 0)
                            {
                                cout << "Delete function started." << endl;
                                    while (x != 0)
                                        {
                                            deleteNode(head);//function call to deleteNode.
                                            x--;
                                        }

                            }

                }

    writeHead(head,counter2,y);



    return 0;
}
Topic archived. No new replies allowed.