C DoublyLinked Lists The goal of the exercise is to implemen
C++: Doubly-Linked Lists
The goal of the exercise is to implement a class List of doubly- linked lists.
My goal is to implement the class in a file doubly-linked.cpp.
The class List implements lists using the following structures for list ele- ments:
struct Node { int val ;
Node next;
Node prev; };
Each Node contains a value (an integer) and two pointers: one meant to point to the next element of the list and one meant to point to the previous element in the list. The class provides the following methods that you need to implement:
• A constructor and a destructor, with no arguments;
• void insert(int n): insert an integer at the end of the list;
• void reverse(): reverse the list;
• void print(): print the list to cout in the same format as the input (i.e. integers separated by spaces);
doubly-linked.h
#ifndef __dll__
#define __dll__
#include <iostream>
using namespace std;
// Basic structure to store elements of a list
struct Node {
int val; // contains the value
Node * next; // pointer to the next element in the list
Node * prev; // pointer to the previous element in the list
};
// Class List
class List {
public:
List(void); // Constructor
~List(void); // Destructor
void insert(int n); // This should insert n in the list
void reverse(void); // This should reverse the list
void print(void); // This shoiuld print the list
private:
Node * first; // Pointer to the first (if any) element in the list
};
#endif
main.cpp
#include <iostream>
#include \"doubly-linked.h\"
using namespace std;
int main(void){
List l;
int n;
while(cin >> n){
l.insert(n);
}
// Print list as read from cin
l.print();
// Reverse the list and print it
l.reverse();
l.print();
// Reverse again and print it
l.reverse();
l.print();
return 0;
}
Solution
#include<iostream>
using namespace std;
/* Linked list structure */
struct list {
struct list *prev;
int data;
struct list *next;
} *node = NULL, *first = NULL, *last = NULL, *node1 = NULL, *node2 = NULL;
class linkedlist {
public:
/* Function for create/insert node at the beginning of Linked list */
void insrt_frnt() {
list *addBeg = new list;
cout << \"Enter value for the node:\" << endl;
cin >> addBeg->data;
if(first == NULL) {
addBeg->prev = NULL;
addBeg->next = NULL;
first = addBeg;
last = addBeg;
cout << \"Linked list Created!\" << endl;
}
else {
addBeg->prev = NULL;
first->prev = addBeg;
addBeg->next = first;
first = addBeg;
cout << \"Data Inserted at the beginning of the Linked list!\" << endl;
}
}
/* Function for create/insert node at the end of Linked list */
void insrt_end() {
list *addEnd = new list;
cout << \"Enter value for the node:\" << endl;
cin >> addEnd->data;
if(first == NULL) {
addEnd->prev = NULL;
addEnd->next = NULL;
first = addEnd;
last = addEnd;
cout << \"Linked list Created!\" << endl;
}
else {
addEnd->next = NULL;
last->next = addEnd;
addEnd->prev = last;
last = addEnd;
cout << \"Data Inserted at the end of the Linked list!\" << endl;
}
}
/* Function for Display Linked list */
void display() {
node = first;
cout << \"List of data in Linked list in Ascending order!\" << endl;
while(node != NULL) {
cout << node->data << endl;
node = node->next;
}
node = last;
cout << \"List of data in Linked list in Descending order!\" << endl;
while(node != NULL) {
cout << node->data << endl;
node = node->prev;
}
}
/* Function for delete node from Linked list */
void del() {
int count = 0, number, i;
node = node1 = node2 = first;
for(node = first; node != NULL; node = node->next)
cout << \"Enter value for the node:\" << endl;
count++;
display();
cout << count << \" nodes available here!\" << endl;
cout << \"Enter the node number which you want to delete:\" << endl;
cin >> number;
if(number != 1) {
if(number < count && number > 0) {
for(i = 2; i <= number; i++)
node = node->next;
for(i = 2; i <= number-1; i++)
node1 = node1->next;
for(i = 2; i <= number+1; i++)
node2 = node2->next;
node2->prev = node1;
node1->next = node2;
node->prev = NULL;
node->next = NULL;
node = NULL;
}
else if(number == count) {
node = last;
last = node->prev;
last->next = NULL;
node = NULL;
}
else
cout << \"Invalid node number!\" << endl;
}
else {
node = first;
first = node->next;
first->prev = NULL;
node = NULL;
}
cout << \"Node has been deleted successfully!\" << endl;
}
};
int main() {
int otpn = 0;
linkedlist lsst = linkedlist();
while(otpn != 4) {
cout << \"1. Insert at the beginning\ 2. Insert at the end\ 3. Delete\ 4. Display\ 5. Exit\" << endl;
cout << \"Enter your choice:\" << endl;
cin >> otpn;
switch(otpn) {
case 1:
lsst.insrt_frnt();
break;
case 2:
lsst.insrt_end();
break;
case 3:
lsst.del();
break;
case 4:
lsst.display();
break;
case 5:
cout << \"Bye Bye!\" << endl;
return 0;
break;
default:
cout << \"Invalid choice!\" << endl;
}
}
return 0;
}



