fstream help

Hello everyone, I am having a hard time figuring out how to form a circular doubly linked list from a text file. With this code I manage to read the start node and the last node from the text file, but I am not sure how to read all the data to the list properly and in a rational way as possible. What should I add to 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdio>
#include <cstdlib>
using namespace std;

struct node
{
    int info;
    struct node *next;
    struct node *prev;
}*start, *last , *t, *q;
int counter = 0;

class double_clist
{
    public:
        node *create_node(int);
        void insert_begin(int value);
        void insert_last(int value);
        void insert_pos();
        void delete_pos();
        void display();
        double_clist()
        {
            start = NULL;
            last = NULL;
        }
};

int main()
{
    double_clist cdl;

    ifstream fin("1.txt");
    int value;
    fin >> value;
    cdl.insert_begin(value);
    cdl.insert_last(value);
    int choice;
    while (1)
    {
        cout<<"Operations on Doubly Circular linked list"<<endl;
        cout<<"1.Insert at Beginning"<<endl;
        cout<<"2.Insert at Last"<<endl;
        cout<<"3.Insert at Position"<<endl;
        cout<<"4.Delete at Position"<<endl;
        cout<<"5.Display List"<<endl;
        cout<<"6.Exit"<<endl;
        cout<<"Enter your choice : ";
        cin>>choice;
        switch(choice)
        {
        case 1:
            cdl.insert_begin(value);
            break;
        case 2:
            cdl.insert_last(value);
            break;
        case 3:
            cdl.insert_pos();
            break;
        case 4:
            cdl.delete_pos();
            break;
        case 5:
            cdl.display();
            break;
        case 6:
            exit(1);
        default:
            cout<<"Wrong choice"<<endl;
        }
    }
    return 0;
}

node* double_clist::create_node(int value)
{
    counter++;
    struct node *temp;
    temp = new(struct node);
    temp->info = value;
    temp->next = NULL;
    temp->prev = NULL;
    return temp;
}

void double_clist::insert_begin(int value)
{
    struct node *temp;
    temp = create_node(value);
    if (start == last && start == NULL)
    {
        cout<<"Element inserted in empty list"<<endl;
        start = last = temp;
        start->next = last->next = NULL;
        start->prev = last->prev = NULL;
    }
    else
    {
        temp->next = start;
        start->prev = temp;
        start = temp;
        start->prev = last;
        last->next = start;
        cout<<"Element inserted"<<endl;
    }
}

void double_clist::insert_last(int value)
{
    struct node *temp;
    temp = create_node(value);
    if (start == last && start == NULL)
    {
        cout<<"Element inserted in empty list"<<endl;
        start = last = temp;
        start->next = last->next = NULL;
        start->prev = last->prev = NULL;
    }
    else
    {
        last->next = temp;
        temp->prev = last;
        last = temp;
        start->prev = last;
        last->next = start;
    }
}

void double_clist::insert_pos()
{
    int value, pos, i;
    cout<<endl<<"Enter the element to be inserted: ";
    cin>>value;
    cout<<endl<<"Enter the position of element inserted: ";
    cin>>pos;
    struct node *temp, *s, *ptr;
    temp = create_node(value);
    if (start == last && start == NULL)
    {
        if (pos == 1)
        {
            start = last = temp;
            start->next = last->next = NULL;
            start->prev = last->prev = NULL;
        }
        else
        {
            cout<<"Position out of range"<<endl;
            counter--;
            return;
        }
    }
    else
    {
        if (counter < pos)
        {
             cout<<"Position out of range"<<endl;
             counter--;
             return;
        }
        s = start;
        for (i = 1;i <= counter;i++)
        {
            ptr = s;
            s = s->next;
            if (i == pos - 1)
            {
                ptr->next = temp;
                temp->prev = ptr;
                temp->next = s;
                s->prev = temp;
                cout<<"Element inserted"<<endl;
                break;
            }
        }
    }
}

void double_clist::delete_pos()
{
    int pos, i;
    node *ptr, *s;
    if (start == last && start == NULL)
    {
        cout<<"List is empty, nothing to delete"<<endl;
        return;
    }
    cout<<endl<<"Enter the postion of element to be deleted: ";
    cin>>pos;
    if (counter < pos)
    {
        cout<<"Position out of range"<<endl;
        return;
    }
    s = start;
    if(pos == 1)
    {
        counter--;
        last->next = s->next;
        s->next->prev = last;
        start = s->next;
        free(s);
        cout<<"Element Deleted"<<endl;
        return;
    }
    for (i = 0;i < pos - 1;i++ )
    {
        s = s->next;
        ptr = s->prev;
    }
    ptr->next = s->next;
    s->next->prev = ptr;
    if (pos == counter)
    {
        last = ptr;
    }
    counter--;
    free(s);
    cout<<"Element Deleted"<<endl;
}

void double_clist::display()
{
    int i;
    struct node *s;
    if (start == last && start == NULL)
    {
        cout<<"The List is empty, nothing to display"<<endl;
        return;
    }
    s = start;
    for (i = 0;i < counter-1;i++)
    {
        cout<<s->info<<"<->";
        s = s->next;
    }
    cout<<s->info<<endl;
}
Last edited on
This:
1
2
3
4
while(fin >> value)
{
    cdl.insert_last(value);
}
I am trying to implement this while loop, but it still always print that the list is empty...

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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdio>
#include <cstdlib>
using namespace std;

struct node
{
    int info;
    struct node *next;
    struct node *prev;
}*start, *last , *t, *q;
int counter = 0;

class double_clist
{
    public:
        node *create_node(int);
        void insert_begin(int value);
        void insert_last(int value);
        void insert_pos();
        void delete_pos();
        void display();
        double_clist()
        {
            start = NULL;
            last = NULL;
        }
};

int main()
{
    double_clist cdl;

    ifstream fin("1.txt");
    int value;
    while(fin >> value)
    {
        cdl.insert_last(value);
    }
    int choice;
    while (1)
    {
        cout<<"Operations on Doubly Circular linked list"<<endl;
        cout<<"1.Insert at Beginning"<<endl;
        cout<<"2.Insert at Last"<<endl;
        cout<<"3.Insert at Position"<<endl;
        cout<<"4.Delete at Position"<<endl;
        cout<<"5.Display List"<<endl;
        cout<<"6.Exit"<<endl;
        cout<<"Enter your choice : ";
        cin>>choice;
        switch(choice)
        {
        case 1:
            cdl.insert_begin(value);
            break;
        case 2:
            cdl.insert_last(value);
            break;
        case 3:
            cdl.insert_pos();
            break;
        case 4:
            cdl.delete_pos();
            break;
        case 5:
            cdl.display();
            break;
        case 6:
            exit(1);
        default:
            cout<<"Wrong choice"<<endl;
        }
    }
    return 0;
}

node* double_clist::create_node(int value)
{
    counter++;
    struct node *temp;
    temp = new(struct node);
    temp->info = value;
    temp->next = NULL;
    temp->prev = NULL;
    return temp;
}

void double_clist::insert_begin(int value)
{
    struct node *temp;
    temp = create_node(value);
    if (start == last && start == NULL)
    {
        cout<<"Element inserted in empty list"<<endl;
        start = last = temp;
        start->next = last->next = NULL;
        start->prev = last->prev = NULL;
    }
    else
    {
        temp->next = start;
        start->prev = temp;
        start = temp;
        start->prev = last;
        last->next = start;
        cout<<"Element inserted"<<endl;
    }
}

void double_clist::insert_last(int value)
{
    struct node *temp;
    temp = create_node(value);
    if (start == last && start == NULL)
    {
        cout<<"Element inserted in empty list"<<endl;
        start = last = temp;
        start->next = last->next = NULL;
        start->prev = last->prev = NULL;
    }
    else
    {
        last->next = temp;
        temp->prev = last;
        last = temp;
        start->prev = last;
        last->next = start;
    }
}

void double_clist::insert_pos()
{
    int value, pos, i;
    cout<<endl<<"Enter the element to be inserted: ";
    cin>>value;
    cout<<endl<<"Enter the position of element inserted: ";
    cin>>pos;
    struct node *temp, *s, *ptr;
    temp = create_node(value);
    if (start == last && start == NULL)
    {
        if (pos == 1)
        {
            start = last = temp;
            start->next = last->next = NULL;
            start->prev = last->prev = NULL;
        }
        else
        {
            cout<<"Position out of range"<<endl;
            counter--;
            return;
        }
    }
    else
    {
        if (counter < pos)
        {
             cout<<"Position out of range"<<endl;
             counter--;
             return;
        }
        s = start;
        for (i = 1;i <= counter;i++)
        {
            ptr = s;
            s = s->next;
            if (i == pos - 1)
            {
                ptr->next = temp;
                temp->prev = ptr;
                temp->next = s;
                s->prev = temp;
                cout<<"Element inserted"<<endl;
                break;
            }
        }
    }
}

void double_clist::delete_pos()
{
    int pos, i;
    node *ptr, *s;
    if (start == last && start == NULL)
    {
        cout<<"List is empty, nothing to delete"<<endl;
        return;
    }
    cout<<endl<<"Enter the postion of element to be deleted: ";
    cin>>pos;
    if (counter < pos)
    {
        cout<<"Position out of range"<<endl;
        return;
    }
    s = start;
    if(pos == 1)
    {
        counter--;
        last->next = s->next;
        s->next->prev = last;
        start = s->next;
        free(s);
        cout<<"Element Deleted"<<endl;
        return;
    }
    for (i = 0;i < pos - 1;i++ )
    {
        s = s->next;
        ptr = s->prev;
    }
    ptr->next = s->next;
    s->next->prev = ptr;
    if (pos == counter)
    {
        last = ptr;
    }
    counter--;
    free(s);
    cout<<"Element Deleted"<<endl;
}

void double_clist::display()
{
    int i;
    struct node *s;
    if (start == last && start == NULL)
    {
        cout<<"The List is empty, nothing to display"<<endl;
        return;
    }
    s = start;
    for (i = 0;i < counter-1;i++)
    {
        cout<<s->info<<"<->";
        s = s->next;
    }
    cout<<s->info<<endl;
}
it still always print that the list is empty

Supposing your “1.txt” looks like:
01 02 03 04 05 06 07 08 09 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

this version prints the list (please note: I haven’t carried out other tests, it’s just for hints):
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <fstream>
#include <iostream>

struct node {
    int info;
    struct node *next;
    struct node *prev;
    node();
};

node::node() : info {}, next { nullptr }, prev { nullptr }  {}

class double_clist {
public:
    double_clist();
    node* create_node(int);
    void insert_begin(int value);
    void insert_last(int value);
    void insert_pos();
    void delete_pos();
    void display();
private:
    node* start,
        * last;
    int counter;
};

double_clist::double_clist()
    : start { nullptr }, last { nullptr }, counter {}
{}

node* double_clist::create_node(int value)
{
    counter++;
    node* temp = new node;
    temp->info = value;
    temp->next = nullptr;
    temp->prev = nullptr;
    return temp;
}

void double_clist::insert_begin(int value)
{
    node* temp = create_node(value);
    if (start == last && start == nullptr) {
        start = last = temp;
        start->next = last->next = nullptr;
        start->prev = last->prev = nullptr;
        std::cout << "Element inserted in empty list\n";
    } else {
        temp->next = start;
        start->prev = temp;
        start = temp;
        start->prev = last;
        last->next = start;
        std::cout << "Element inserted\n";
    }
}

void double_clist::insert_last(int value)
{
    node* temp = create_node(value);
    if (start == last && start == nullptr) {
        start = last = temp;
        start->next = last->next = nullptr;
        start->prev = last->prev = nullptr;
        std::cout << "Element inserted in empty list\n";
    } else {
        last->next = temp;
        temp->prev = last;
        last = temp;
        start->prev = last;
        last->next = start;
    }
}

void double_clist::insert_pos()
{
    std::cout << "\nEnter the element to be inserted: ";
    int value;
    std::cin >> value;
    std::cout << "\nEnter the position of element inserted: ";
    int pos;
    std::cin >> pos;
    node *temp = create_node(value);
    if (start == last && start == nullptr) {
        if (pos == 1) {     // call insert_begin() ?
            start = last = temp;
            start->next = last->next = nullptr;
            start->prev = last->prev = nullptr;
        } else {
            std::cout << "Position out of range\n";
            counter--;
            delete temp;    // <---
            return;
        }
    } else {
        if (counter < pos) {
            std::cout << "Position out of range\n";
            counter--;
            delete temp;    // <---
            return;
        }
        node* s = start;
        for (int i = 1; i <= counter; ++i) {
            node* ptr = s;
            s = s->next;
            if (i == pos - 1) {
                ptr->next = temp;
                temp->prev = ptr;
                temp->next = s;
                s->prev = temp;
                std::cout << "Element inserted\n";
                break;
            }
        }
    }
}

void double_clist::delete_pos()
{
    int i;
    if (start == last && start == nullptr) {
        std::cout << "List is empty, nothing to delete\n";
        return;
    }
    std::cout << "\nEnter the postion of element to be deleted: ";
    int pos;
    std::cin >> pos; 
    if (counter < pos) {
        std::cout << "Position out of range\n";
        return;
    }
    node* s = start;
    if(pos == 1) {
        counter--;
        last->next = s->next;
        s->next->prev = last;
        start = s->next;
        // free(s);
        delete s;
        std::cout << "Element Deleted\n";
        return;
    }
    node* ptr;
    for (i = 0; i < pos - 1; ++i) {
        s = s->next;
        ptr = s->prev;
    }
    ptr->next = s->next;
    s->next->prev = ptr;
    if (pos == counter) { last = ptr; }
    counter--;
    // free(s);
    delete s;
    std::cout << "Element Deleted\n";
}

void double_clist::display()
{
    if (start == last && start == nullptr) {
        std::cout << "The List is empty, nothing to display\n";
        return;
    }
    node* s = start;
    for (int i = 0; i < counter - 1; ++i) {
        std::cout << s->info << " <-> ";
        s = s->next;
    }
    std::cout << s->info << '\n';
}

int main()
{
    double_clist cdl;

    std::ifstream fin("1.txt");
    for(int value {}; fin >> value; /**/) { cdl.insert_last(value); }
    int choice;
    while (1)
    {
        std::cout << "Operations on Doubly Circular linked list\n"
                     "1.Insert at Beginning\n"
                     "2.Insert at Last\n"
                     "3.Insert at Position\n"
                     "4.Delete at Position\n"
                     "5.Display List\n"
                     "6.Exit\n"
                     "Enter your choice : ";
        std::cin >> choice;
        int value { 666 };
        switch(choice) {
        case 1: cdl.insert_begin(value); break;
        case 2: cdl.insert_last(value);  break;
        case 3: cdl.insert_pos();        break;
        case 4: cdl.delete_pos();        break;
        case 5: cdl.display();           break;
        case 6: return 1;
        default: std::cout << "Wrong choice\n"; break;
        }
    }
    return 0;
}


Output:
Element inserted in empty list
Operations on Doubly Circular linked list
1.Insert at Beginning
2.Insert at Last
3.Insert at Position
4.Delete at Position
5.Display List
6.Exit
Enter your choice : 5
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
Operations on Doubly Circular linked list
1.Insert at Beginning
2.Insert at Last
3.Insert at Position
4.Delete at Position
5.Display List
6.Exit
Enter your choice : 6

Topic archived. No new replies allowed.