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?
#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