SlideShare a Scribd company logo
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
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
#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
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;
}

More Related Content

PDF
In the class we extensively discussed a node class called IntNode in.pdf
PDF
How do you stop infinite loop Because I believe that it is making a.pdf
PDF
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
PPTX
C Exam Help
PDF
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
PPTX
C Homework Help
PDF
Please help solve this in C++ So the program is working fin.pdf
In the class we extensively discussed a node class called IntNode in.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
C Exam Help
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
C Homework Help
Please help solve this in C++ So the program is working fin.pdf

Similar to C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf (20)

PDF
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
DOCX
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docx
DOCX
C++ Please write the whole code that is needed for this assignment- wr.docx
PDF
This assignment and the next (#5) involve design and development of a.pdf
PPTX
DSA(1).pptx
PDF
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
PDF
In C++Write a recursive function to determine whether or not a Lin.pdf
PDF
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
PDF
Data Structures in C++I am really new to C++, so links are really .pdf
PPT
17 linkedlist (1)
PDF
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
PDF
Using the provided table interface table.h and the sample linked lis.pdf
PDF
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
PDF
Write a program to implement below operations with both singly and d.pdf
PDF
Lec-4_Linked-List (1).pdf
PDF
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
PDF
C++ Background Circular Linked List A circular linked list.pdf
PDF
Consider a double-linked linked list implementation with the followin.pdf
PDF
C++ Please test your program before you submit the answer.pdf
DOCX
#include stdafx.h #include iostream using namespace std;vo.docx
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docx
C++ Please write the whole code that is needed for this assignment- wr.docx
This assignment and the next (#5) involve design and development of a.pdf
DSA(1).pptx
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
In C++Write a recursive function to determine whether or not a Lin.pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
17 linkedlist (1)
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
Using the provided table interface table.h and the sample linked lis.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Write a program to implement below operations with both singly and d.pdf
Lec-4_Linked-List (1).pdf
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
C++ Background Circular Linked List A circular linked list.pdf
Consider a double-linked linked list implementation with the followin.pdf
C++ Please test your program before you submit the answer.pdf
#include stdafx.h #include iostream using namespace std;vo.docx
Ad

More from poblettesedanoree498 (20)

PDF
Give examples of two zoonotic diseases. Explain how these zoonotic d.pdf
PDF
Create the variables, and methods needed for this classA DicePlay.pdf
PDF
Describe different flow control valves with figures. Include referenc.pdf
PDF
Consider a sample with data values of 10, 20, 12, 17, and 16.Compute.pdf
PDF
Compare and contrast transmit and receive diversity. Describe how ea.pdf
PDF
Blood in the femoral artery will flow into what artery next Bl.pdf
PDF
Assume that 96 of ticket holders show up for an airline flight. If .pdf
PDF
Although some male crab spiders find and mate with adult female spide.pdf
PDF
7. At October 1, Smithson Enterprises reported owners equity of $.pdf
PDF
5. A repeated measures analysis of variane shows, F(4,20)=14.56. How.pdf
PDF
–PLS write program in c++ thanxLinked List Sorting and Reversing.pdf
PDF
Write a Java program that calculates the distance between two points.pdf
PDF
Which of the following light waves has the most amount of energyH.pdf
PDF
What is the wobble position What is the consequence of its pres.pdf
PDF
What is an advantage of having homologous chromosomes (compared to h.pdf
PDF
What are oncogenic viruses Which of the following groups of viruses.pdf
PDF
Unlike the biological species concept, the morphospecies concept rel.pdf
PDF
1.) Which macronutrient do we consume in large amounts, but which we.pdf
PDF
11.1.2 Entered Answer Preview 28 28 128 128 At least one of the an.pdf
PDF
8. Which of the following sentences defines adequately horizontal ge.pdf
Give examples of two zoonotic diseases. Explain how these zoonotic d.pdf
Create the variables, and methods needed for this classA DicePlay.pdf
Describe different flow control valves with figures. Include referenc.pdf
Consider a sample with data values of 10, 20, 12, 17, and 16.Compute.pdf
Compare and contrast transmit and receive diversity. Describe how ea.pdf
Blood in the femoral artery will flow into what artery next Bl.pdf
Assume that 96 of ticket holders show up for an airline flight. If .pdf
Although some male crab spiders find and mate with adult female spide.pdf
7. At October 1, Smithson Enterprises reported owners equity of $.pdf
5. A repeated measures analysis of variane shows, F(4,20)=14.56. How.pdf
–PLS write program in c++ thanxLinked List Sorting and Reversing.pdf
Write a Java program that calculates the distance between two points.pdf
Which of the following light waves has the most amount of energyH.pdf
What is the wobble position What is the consequence of its pres.pdf
What is an advantage of having homologous chromosomes (compared to h.pdf
What are oncogenic viruses Which of the following groups of viruses.pdf
Unlike the biological species concept, the morphospecies concept rel.pdf
1.) Which macronutrient do we consume in large amounts, but which we.pdf
11.1.2 Entered Answer Preview 28 28 128 128 At least one of the an.pdf
8. Which of the following sentences defines adequately horizontal ge.pdf
Ad

Recently uploaded (20)

PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Institutional Correction lecture only . . .
PDF
Basic Mud Logging Guide for educational purpose
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Computing-Curriculum for Schools in Ghana
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
master seminar digital applications in india
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Cell Structure & Organelles in detailed.
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
O7-L3 Supply Chain Operations - ICLT Program
STATICS OF THE RIGID BODIES Hibbelers.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Microbial disease of the cardiovascular and lymphatic systems
Institutional Correction lecture only . . .
Basic Mud Logging Guide for educational purpose
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Computing-Curriculum for Schools in Ghana
Module 4: Burden of Disease Tutorial Slides S2 2025
master seminar digital applications in india
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Cell Structure & Organelles in detailed.
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Microbial diseases, their pathogenesis and prophylaxis
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
2.FourierTransform-ShortQuestionswithAnswers.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf

C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf

  • 1. 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 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:
  • 2. Node * first; // Pointer to the first (if any) element in the list }; #endif main.cpp #include #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 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 {
  • 3. 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;
  • 4. 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;
  • 5. 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;
  • 6. 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; }