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

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

PDF
Consider a double-linked linked list implementation with the followin.pdf
sales98
 
PDF
C++ Program to Implement Doubly Linked List #includei.pdf
Lalkamal2
 
PDF
In C++Write a recursive function to determine whether or not a Lin.pdf
flashfashioncasualwe
 
PPTX
Lec5-Doubly-Linked-List-24102022-110112am.pptx
IqraHanif27
 
PDF
include ltfunctionalgt include ltiteratorgt inclu.pdf
naslin841216
 
PDF
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
info114
 
PDF
Need to be done in C++ Please Sorted number list implementation wit.pdf
aathiauto
 
PDF
Write a function to merge two doubly linked lists. The input lists ha.pdf
info706022
 
PDF
Need to be done in C Please Sorted number list implementation with.pdf
aathmaproducts
 
PDF
All code should be in C++Using the UnsortedList class (UnsortedLis.pdf
akashenterprises93
 
PPTX
Unit – III.pptx Data Structures and Algorithms
snehalkulkarni78
 
PDF
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
stopgolook
 
PPTX
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
RashidFaridChishti
 
DOCX
Doubly linklist
ilsamaryum
 
PDF
C++ CodeConsider the LinkedList class and the Node class that we s.pdf
armyshoes
 
DOCX
C++ please put everthing after you answer it- thanks Complete the stub.docx
MatthPYNashd
 
PDF
Data Structures in C++I am really new to C++, so links are really .pdf
rohit219406
 
PDF
Change the driver file (the main .cpp) so that it asks the user to e.pdf
fatoryoutlets
 
PDF
This will need to be in a header file called LinkedList.hInser.pdf
cleanhome88
 
PPT
linked-list.ppt
DikkySuryadiSKomMKom
 
Consider a double-linked linked list implementation with the followin.pdf
sales98
 
C++ Program to Implement Doubly Linked List #includei.pdf
Lalkamal2
 
In C++Write a recursive function to determine whether or not a Lin.pdf
flashfashioncasualwe
 
Lec5-Doubly-Linked-List-24102022-110112am.pptx
IqraHanif27
 
include ltfunctionalgt include ltiteratorgt inclu.pdf
naslin841216
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
info114
 
Need to be done in C++ Please Sorted number list implementation wit.pdf
aathiauto
 
Write a function to merge two doubly linked lists. The input lists ha.pdf
info706022
 
Need to be done in C Please Sorted number list implementation with.pdf
aathmaproducts
 
All code should be in C++Using the UnsortedList class (UnsortedLis.pdf
akashenterprises93
 
Unit – III.pptx Data Structures and Algorithms
snehalkulkarni78
 
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
stopgolook
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
RashidFaridChishti
 
Doubly linklist
ilsamaryum
 
C++ CodeConsider the LinkedList class and the Node class that we s.pdf
armyshoes
 
C++ please put everthing after you answer it- thanks Complete the stub.docx
MatthPYNashd
 
Data Structures in C++I am really new to C++, so links are really .pdf
rohit219406
 
Change the driver file (the main .cpp) so that it asks the user to e.pdf
fatoryoutlets
 
This will need to be in a header file called LinkedList.hInser.pdf
cleanhome88
 
linked-list.ppt
DikkySuryadiSKomMKom
 

More from aathiauto (20)

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

Recently uploaded (20)

PPTX
Controller Request and Response in Odoo18
Celine George
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
PPTX
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
Controller Request and Response in Odoo18
Celine George
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Dimensions of Societal Planning in Commonism
StefanMz
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 

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.