SlideShare a Scribd company logo
DoublyList.cpp:
#include "DoublyList.h"
using namespace std;
void DoublyList::insertFront(int newData)
{
if (first == nullptr)
{
first = new DLLNode(newData, nullptr, nullptr);
last = first;
// Common error: Forgetting to reset pointer last.
}
else
{
first = new DLLNode(newData, nullptr, first);
first->getNext()->setPrev(first);
// Common error: Forgetting to connect pointer
// prev of what is now the second node to the
// new first node.
}
++count;
}
void DoublyList::printForward() const
{
DLLNode* current = first;
while (current != nullptr)
{
cout << current->getData() << " ";
current = current->getNext();
}
}
void DoublyList::printReverse() const
{
DLLNode* current = last;
while (current != nullptr)
{
cout << current->getData() << " ";
current = current->getPrev();
}
}
void DoublyList::clearList()
{
DLLNode* temp = first;
while (first != nullptr)
{
first = first->getNext();
delete temp;
temp = first;
}
last = nullptr;
// Don't forget to reset pointer last to nullptr.
count = 0;
}
DoublyList::~DoublyList()
{
if (first != nullptr)
clearList();
}
DoublyList.h
#ifndef DOUBLYLIST_H
#define DOUBLYLIST_H
#include <string>
#include <iostream>
class DLLNode
{
public:
DLLNode() : data(0), prev(nullptr), next(nullptr) {}
DLLNode(int theData, DLLNode* prevLink, DLLNode* nextLink)
: data(theData), prev(prevLink), next(nextLink) {}
int getData() const { return data; }
DLLNode* getPrev() const { return prev; }
DLLNode* getNext() const { return next; }
void setData(int theData) { data = theData; }
void setPrev(DLLNode* prevLink) { prev = prevLink; }
void setNext(DLLNode* nextLink) { next = nextLink; }
~DLLNode(){}
private:
int data; // To simplify, we are using only one piece of data.
DLLNode* prev;
DLLNode* next;
};
class DoublyList
{
public:
DoublyList() : first(nullptr), last(nullptr), count(0) {}
void insertFront(int newData);
void printForward() const;
void printReverse() const;
void rotateNodesRight(int);
void clearList();
~DoublyList();
private:
// Pointer to the first node in the list.
DLLNode*first;
// Pointer to the last node in the list.
DLLNode*last;
// Number of nodes in the list.
int count;
};
#endif
Main.cpp
#include "DoublyList.h"
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<vector<int>> data = {
{25, 76, 35, 67, 15, 98},
{1, 2, 3, 4, 5, 6, 7, 8, 9},
{10, 20},
{34, 56, 78, 12, 89, 34, 76, 28, 54, 22, 41},
{123, 873, 619},
};
vector<int> nodesToRotate = { 2, 3, 1, 9, 2 };
{
DoublyList doublyList;
int vectorSize = static_cast<int>(data.size());
for (int i = 0; i < vectorSize; ++i)
{
int innerSize = static_cast<int>(data[i].size());
for (int j = innerSize - 1; j >= 0; --j)
doublyList.insertFront(data[i].at(j));
cout << "Rotate right: " << nodesToRotate[i] << "n";
cout << " List is: ";
doublyList.printForward();
cout << "n";
doublyList.rotateNodesRight(nodesToRotate[i]);
cout << "After rotating:";
cout << "n Print forward: ";
doublyList.printForward();
cout << "nPrint backwards: ";
doublyList.printReverse();
cout << "nn";
doublyList.clearList();
}
}
cout << "n";
system("Pause");
return 0;
Function rotateNodesRight() - This function is a member function of the class DoublyList. -
Parameter: An int that stores the number of times that the rotation occurs. - The function works
as the previous one, with the difference that the rotation occurs to the right. Again, this is about
resetting pointers, not about moving data. - Example Page 4 of 5 List is: 257635671598
Parameter is: 2 After rotating, list is: 15 98 25 76 35 67 - Assumptions - The list has at least 2
elements. - The parameter is always smaller than the number of elements in the list. -
Restrictions: - Cannot create helper functions.
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf

More Related Content

DOCX
Lab Week 2 Game Programming.docx
PDF
Computer_Practicals-file.doc.pdf
PPT
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
PDF
C++ practical
DOC
Ds 2 cycle
PDF
C program
DOCX
C++ file
DOCX
C++ file
Lab Week 2 Game Programming.docx
Computer_Practicals-file.doc.pdf
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
C++ practical
Ds 2 cycle
C program
C++ file
C++ file

Similar to DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf (20)

PDF
Statistics.cpp
PPT
Data structures cs301 power point slides lecture 03
PDF
C++ programs
PDF
Look Ma, “update DB to HTML5 using C++”, no hands! 
PDF
Data struture lab
DOCX
PDF
maincpp include ListItemh include ltstringgt in.pdf
PDF
Chainer-Compiler 動かしてみた
PDF
Data Structure in C++Doubly Linked Lists of ints httpstaffwww.pdf
PDF
Zoro123456789123456789123456789123456789
 
PDF
labb123456789123456789123456789123456789
 
PPTX
C++ lectures all chapters in one slide.pptx
DOCX
Lab. Programs in C
PDF
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
DOCX
Program flowchart
PDF
54602399 c-examples-51-to-108-programe-ee01083101
DOC
Oops lab manual2
PPTX
Lecture11 standard template-library
PPTX
Object Oriented Programming Using C++: C++ Namespaces.pptx
PDF
Go vs C++ - CppRussia 2019 Piter BoF
Statistics.cpp
Data structures cs301 power point slides lecture 03
C++ programs
Look Ma, “update DB to HTML5 using C++”, no hands! 
Data struture lab
maincpp include ListItemh include ltstringgt in.pdf
Chainer-Compiler 動かしてみた
Data Structure in C++Doubly Linked Lists of ints httpstaffwww.pdf
Zoro123456789123456789123456789123456789
 
labb123456789123456789123456789123456789
 
C++ lectures all chapters in one slide.pptx
Lab. Programs in C
CUDA First Programs: Computer Architecture CSE448 : UAA Alaska : Notes
Program flowchart
54602399 c-examples-51-to-108-programe-ee01083101
Oops lab manual2
Lecture11 standard template-library
Object Oriented Programming Using C++: C++ Namespaces.pptx
Go vs C++ - CppRussia 2019 Piter BoF

More from aathiauto (20)

PDF
The challenges of delivering climate change policy at the sub-national.pdf
PDF
Please give an explanation and show all steps- Thanks 1- Find a- P(Z-2.pdf
PDF
You are leading a central working group that encompasses representativ.pdf
PDF
What type of defense is a cough reflex-A Nonspecific- secondary line o.pdf
PDF
Twelve jurors are randomly solected from a population of 4 milion resi.pdf
PDF
Use the given discrete probability distribution for the number of head.pdf
PDF
The following graphs represent various types of cost behaviors- Graph.pdf
PDF
The day after the Oscars- Blue Rose Research conducted a poll of peopl.pdf
PDF
The Barteimann Corporaton sold iss credit subsidiacy on Detnmber 31 of.pdf
PDF
Since World War II- national governments have focused on job growth- a.pdf
PDF
Stars on the main sequence obey a mass-luminosity relation- According.pdf
PDF
Health Informatics- Theoretical Foundations- and Practice Evolution- D.pdf
PDF
In what ways are national income statistics usetul-.pdf
PDF
If a population has a standard deviation of 12- what is the VARIANCE o.pdf
PDF
Given that over 97- of climate scientists agree that the climate chang.pdf
PDF
Given this sounding- what kind of convective event would you expect- C.pdf
PDF
a- Recessions typically hurtb- What is the general trend observed amon.pdf
PDF
Between 2001 and 2009-3730 adults obtained high school diplomas throug.pdf
PDF
ages -c(20-11-18-5-33).pdf
PDF
1a) Which factors are involved in the movement of metals- Give a brief.pdf
The challenges of delivering climate change policy at the sub-national.pdf
Please give an explanation and show all steps- Thanks 1- Find a- P(Z-2.pdf
You are leading a central working group that encompasses representativ.pdf
What type of defense is a cough reflex-A Nonspecific- secondary line o.pdf
Twelve jurors are randomly solected from a population of 4 milion resi.pdf
Use the given discrete probability distribution for the number of head.pdf
The following graphs represent various types of cost behaviors- Graph.pdf
The day after the Oscars- Blue Rose Research conducted a poll of peopl.pdf
The Barteimann Corporaton sold iss credit subsidiacy on Detnmber 31 of.pdf
Since World War II- national governments have focused on job growth- a.pdf
Stars on the main sequence obey a mass-luminosity relation- According.pdf
Health Informatics- Theoretical Foundations- and Practice Evolution- D.pdf
In what ways are national income statistics usetul-.pdf
If a population has a standard deviation of 12- what is the VARIANCE o.pdf
Given that over 97- of climate scientists agree that the climate chang.pdf
Given this sounding- what kind of convective event would you expect- C.pdf
a- Recessions typically hurtb- What is the general trend observed amon.pdf
Between 2001 and 2009-3730 adults obtained high school diplomas throug.pdf
ages -c(20-11-18-5-33).pdf
1a) Which factors are involved in the movement of metals- Give a brief.pdf

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
master seminar digital applications in india
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Lesson notes of climatology university.
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
01-Introduction-to-Information-Management.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
GDM (1) (1).pptx small presentation for students
Final Presentation General Medicine 03-08-2024.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Module 4: Burden of Disease Tutorial Slides S2 2025
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Microbial disease of the cardiovascular and lymphatic systems
master seminar digital applications in india
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Lesson notes of climatology university.
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Final Presentation General Medicine 03-08-2024.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
01-Introduction-to-Information-Management.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
A systematic review of self-coping strategies used by university students to ...
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
GDM (1) (1).pptx small presentation for students

DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf

  • 1. DoublyList.cpp: #include "DoublyList.h" using namespace std; void DoublyList::insertFront(int newData) { if (first == nullptr) { first = new DLLNode(newData, nullptr, nullptr); last = first; // Common error: Forgetting to reset pointer last. } else { first = new DLLNode(newData, nullptr, first); first->getNext()->setPrev(first); // Common error: Forgetting to connect pointer // prev of what is now the second node to the // new first node. } ++count; } void DoublyList::printForward() const { DLLNode* current = first; while (current != nullptr) { cout << current->getData() << " "; current = current->getNext(); } } void DoublyList::printReverse() const { DLLNode* current = last; while (current != nullptr) { cout << current->getData() << " "; current = current->getPrev(); } }
  • 2. void DoublyList::clearList() { DLLNode* temp = first; while (first != nullptr) { first = first->getNext(); delete temp; temp = first; } last = nullptr; // Don't forget to reset pointer last to nullptr. count = 0; } DoublyList::~DoublyList() { if (first != nullptr) clearList(); } DoublyList.h #ifndef DOUBLYLIST_H #define DOUBLYLIST_H #include <string> #include <iostream> class DLLNode { public: DLLNode() : data(0), prev(nullptr), next(nullptr) {} DLLNode(int theData, DLLNode* prevLink, DLLNode* nextLink) : data(theData), prev(prevLink), next(nextLink) {} int getData() const { return data; } DLLNode* getPrev() const { return prev; } DLLNode* getNext() const { return next; } void setData(int theData) { data = theData; } void setPrev(DLLNode* prevLink) { prev = prevLink; } void setNext(DLLNode* nextLink) { next = nextLink; } ~DLLNode(){} private: int data; // To simplify, we are using only one piece of data. DLLNode* prev;
  • 3. DLLNode* next; }; class DoublyList { public: DoublyList() : first(nullptr), last(nullptr), count(0) {} void insertFront(int newData); void printForward() const; void printReverse() const; void rotateNodesRight(int); void clearList(); ~DoublyList(); private: // Pointer to the first node in the list. DLLNode*first; // Pointer to the last node in the list. DLLNode*last; // Number of nodes in the list. int count; }; #endif Main.cpp #include "DoublyList.h" #include <iostream> #include <vector> using namespace std; int main() { vector<vector<int>> data = { {25, 76, 35, 67, 15, 98}, {1, 2, 3, 4, 5, 6, 7, 8, 9}, {10, 20}, {34, 56, 78, 12, 89, 34, 76, 28, 54, 22, 41}, {123, 873, 619},
  • 4. }; vector<int> nodesToRotate = { 2, 3, 1, 9, 2 }; { DoublyList doublyList; int vectorSize = static_cast<int>(data.size()); for (int i = 0; i < vectorSize; ++i) { int innerSize = static_cast<int>(data[i].size()); for (int j = innerSize - 1; j >= 0; --j) doublyList.insertFront(data[i].at(j)); cout << "Rotate right: " << nodesToRotate[i] << "n"; cout << " List is: "; doublyList.printForward(); cout << "n"; doublyList.rotateNodesRight(nodesToRotate[i]); cout << "After rotating:"; cout << "n Print forward: "; doublyList.printForward(); cout << "nPrint backwards: "; doublyList.printReverse(); cout << "nn"; doublyList.clearList(); } } cout << "n"; system("Pause"); return 0; Function rotateNodesRight() - This function is a member function of the class DoublyList. - Parameter: An int that stores the number of times that the rotation occurs. - The function works as the previous one, with the difference that the rotation occurs to the right. Again, this is about resetting pointers, not about moving data. - Example Page 4 of 5 List is: 257635671598 Parameter is: 2 After rotating, list is: 15 98 25 76 35 67 - Assumptions - The list has at least 2 elements. - The parameter is always smaller than the number of elements in the list. - Restrictions: - Cannot create helper functions.