SlideShare a Scribd company logo
C++ Code
Consider the LinkedList class and the Node class that we saw in lecture. The LinkedList class
basically represents a singly linked list.
Now, for this lab, consider a doubly linked list, i.e. each node has a next and previous pointers to
the next and previous node, respectively.
Modify the Node class to reflect this change and implement the following methods for
DoublyLinkedList class:
addFirst
addLast
addAtIndex
removeFirst
removeLast
removeAtIndex
Note: We saw how to implement these methods for a singly linked list and the code is added as
an attachment.
C++ Code
*****************************************************************************
******************
*****************************************************************************
******************
C++ Code
Solution
#include
#include
using namespace std;
template
class Node
{
public:
T element; // Element contained in the node
Node *next; // Pointer to the next node
Node *previous;
Node(T element) // Constructor
{
this->element = element;
next = NULL;
}
};
template
class LinkedList
{
public:
LinkedList();
void addFirst(T element);
void addLast(T element);
T removeFirst() ;
T removeLast();
void addAtIndex(int index, T element);
T removeAtIndex(int index);
void traverseList();
private:
Node *head;
Node *tail;
int size;
};
template
LinkedList::LinkedList()
{
head = NULL;
tail = NULL;
size = 0;
}
template
void LinkedList::addFirst(T element)
{
Node *newNode = new Node(element);
newNode->next = head;
if(head != NULL) {
head->previous = newNode;
}
newNode->previous = NULL;
head = newNode;
size++;
if (tail == NULL)
tail = head;
}
template
void LinkedList::addLast(T element)
{
if (tail == NULL)
{
head = tail = new Node(element);
}
else {
tail->next = new Node(element);
tail = tail->next;
}
size++;
}
template
void LinkedList::addAtIndex(int index, T element)
{
if (index == 0)
addFirst(element);
else if (index >= size)
addLast(element);
else
{
Node *current = head;
for (int i = 1; i < index; i++)
current = current->next;
Node *temp = current->next;
current->next = new Node(element);
(current->next)->next = temp;
size++;
}
}
template
T LinkedList::removeFirst()
{
if (size == 0)
exit(1);
else
{
Node *temp = head;
head = head->next;
size--;
T element = temp->element;
delete temp;
return element;
}
}
template
T LinkedList::removeLast()
{
if (size == 0)
exit(1);
else if (size == 1)
{
Node *temp = head;
head = tail = NULL;
size = 0;
T element = temp->element;
delete temp;
return element;
}
else
{
Node *current = head;
for (int i = 0; i < size - 2; i++)
current = current->next;
Node *temp = tail;
tail = current;
tail->next = NULL;
size--;
T element = temp->element;
delete temp;
return element;
}
}
template
T LinkedList::removeAtIndex(int index)
{
if (index < 0 || index >= size)
exit(1);
else if (index == 0)
return removeFirst();
else if (index == size - 1)
return removeLast();
else {
Node *previous = head;
for (int i = 1; i < index; i++)
{
previous = previous->next;
}
Node *current = previous->next;
previous->next = current->next;
size--;
T element = current->element;
delete current;
return element;
}
}
template
void LinkedList::traverseList()
{
Node *temp = head;
while(temp !=NULL) {
cout<element<<" ";
temp = temp->next;
}
}
int main() {
LinkedList *myList = new LinkedList();
myList->addFirst(10);
myList->addFirst(20);
myList->addLast(30);
myList->removeAtIndex(1);
myList->traverseList();
delete myList;
}

More Related Content

Similar to C++ CodeConsider the LinkedList class and the Node class that we s.pdf

Data structures cs301 power point slides lecture 03
Data structures   cs301 power point slides lecture 03Data structures   cs301 power point slides lecture 03
Data structures cs301 power point slides lecture 03Nasir Mehmood
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
ankit11134
 
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdfAssignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
formicreation
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
info114
 
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdfimplement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
FOREVERPRODUCTCHD
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
Himadri Sen Gupta
 
To complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfTo complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdf
ezycolours78
 
Stack - PPT Slides.pptx-data sturutures and algorithanms
Stack - PPT Slides.pptx-data sturutures and algorithanmsStack - PPT Slides.pptx-data sturutures and algorithanms
Stack - PPT Slides.pptx-data sturutures and algorithanms
AdithaBuwaneka
 
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 The MyLinkedList class used in Listing 24.6 is a one-way directional .docx The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
Komlin1
 
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfWrite a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
rozakashif85
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
forladies
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
Lifna C.S
 
Linked List
Linked ListLinked List
Linked List
Md gulam sarwar
 
Array linked list.ppt
Array  linked list.pptArray  linked list.ppt
Array linked list.ppt
Waf1231
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
DaniyalAli81
 
Programming Assignment Help
Programming Assignment HelpProgramming Assignment Help
Programming Assignment Help
Programming Homework Help
 
i am looking for help on the method AddSorted and the method Copy only.pdf
i am looking for help on the method AddSorted and the method Copy only.pdfi am looking for help on the method AddSorted and the method Copy only.pdf
i am looking for help on the method AddSorted and the method Copy only.pdf
sonunotwani
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structuresNiraj Agarwal
 
Week 2 - Data Structures and Algorithms
Week 2 - Data Structures and AlgorithmsWeek 2 - Data Structures and Algorithms
Week 2 - Data Structures and Algorithms
Ferdin Joe John Joseph PhD
 

Similar to C++ CodeConsider the LinkedList class and the Node class that we s.pdf (20)

Data structures cs301 power point slides lecture 03
Data structures   cs301 power point slides lecture 03Data structures   cs301 power point slides lecture 03
Data structures cs301 power point slides lecture 03
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
 
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdfAssignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
 
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdfimplement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
To complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfTo complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdf
 
Stack - PPT Slides.pptx-data sturutures and algorithanms
Stack - PPT Slides.pptx-data sturutures and algorithanmsStack - PPT Slides.pptx-data sturutures and algorithanms
Stack - PPT Slides.pptx-data sturutures and algorithanms
 
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 The MyLinkedList class used in Listing 24.6 is a one-way directional .docx The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfWrite a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Linked List
Linked ListLinked List
Linked List
 
Array linked list.ppt
Array  linked list.pptArray  linked list.ppt
Array linked list.ppt
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
 
Programming Assignment Help
Programming Assignment HelpProgramming Assignment Help
Programming Assignment Help
 
List
ListList
List
 
i am looking for help on the method AddSorted and the method Copy only.pdf
i am looking for help on the method AddSorted and the method Copy only.pdfi am looking for help on the method AddSorted and the method Copy only.pdf
i am looking for help on the method AddSorted and the method Copy only.pdf
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
 
Week 2 - Data Structures and Algorithms
Week 2 - Data Structures and AlgorithmsWeek 2 - Data Structures and Algorithms
Week 2 - Data Structures and Algorithms
 

More from armyshoes

Foofy obtained a significant Fobt from an experiment with five level.pdf
Foofy obtained a significant Fobt from an experiment with five level.pdfFoofy obtained a significant Fobt from an experiment with five level.pdf
Foofy obtained a significant Fobt from an experiment with five level.pdf
armyshoes
 
Explain how water interacts with the following types of compounds to.pdf
Explain how water interacts with the following types of compounds to.pdfExplain how water interacts with the following types of compounds to.pdf
Explain how water interacts with the following types of compounds to.pdf
armyshoes
 
Explain how the financial reporting of gains and losses on assets.pdf
Explain how the financial reporting of gains and losses on assets.pdfExplain how the financial reporting of gains and losses on assets.pdf
Explain how the financial reporting of gains and losses on assets.pdf
armyshoes
 
Explain two ways in which microbiologists have overcome the difficul.pdf
Explain two ways in which microbiologists have overcome the difficul.pdfExplain two ways in which microbiologists have overcome the difficul.pdf
Explain two ways in which microbiologists have overcome the difficul.pdf
armyshoes
 
During the preparation of the bank reconciliation for Building Conce.pdf
During the preparation of the bank reconciliation for Building Conce.pdfDuring the preparation of the bank reconciliation for Building Conce.pdf
During the preparation of the bank reconciliation for Building Conce.pdf
armyshoes
 
DNA Paternity Testing Simulation Post-lab QuestionsA)Why do differ.pdf
DNA Paternity Testing Simulation Post-lab QuestionsA)Why do differ.pdfDNA Paternity Testing Simulation Post-lab QuestionsA)Why do differ.pdf
DNA Paternity Testing Simulation Post-lab QuestionsA)Why do differ.pdf
armyshoes
 
Describe how the lung cancer travels to the first site of metastasis.pdf
Describe how the lung cancer travels to the first site of metastasis.pdfDescribe how the lung cancer travels to the first site of metastasis.pdf
Describe how the lung cancer travels to the first site of metastasis.pdf
armyshoes
 
Define the following engineering terms1. engineering ethics2. W.pdf
Define the following engineering terms1. engineering ethics2. W.pdfDefine the following engineering terms1. engineering ethics2. W.pdf
Define the following engineering terms1. engineering ethics2. W.pdf
armyshoes
 
Data structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdfData structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdf
armyshoes
 
You have been cut! While you wage war in the streets of Gotham for y.pdf
You have been cut! While you wage war in the streets of Gotham for y.pdfYou have been cut! While you wage war in the streets of Gotham for y.pdf
You have been cut! While you wage war in the streets of Gotham for y.pdf
armyshoes
 
Why has Mexico encountered such a significant collapse from an econom.pdf
Why has Mexico encountered such a significant collapse from an econom.pdfWhy has Mexico encountered such a significant collapse from an econom.pdf
Why has Mexico encountered such a significant collapse from an econom.pdf
armyshoes
 
Which of the following isare true of BOTH natural selection and gen.pdf
Which of the following isare true of BOTH natural selection and gen.pdfWhich of the following isare true of BOTH natural selection and gen.pdf
Which of the following isare true of BOTH natural selection and gen.pdf
armyshoes
 
Which of the following information-management systems uses artificia.pdf
Which of the following information-management systems uses artificia.pdfWhich of the following information-management systems uses artificia.pdf
Which of the following information-management systems uses artificia.pdf
armyshoes
 
What steps and tools are used for Pen TestingSolutionSteps fo.pdf
What steps and tools are used for Pen TestingSolutionSteps fo.pdfWhat steps and tools are used for Pen TestingSolutionSteps fo.pdf
What steps and tools are used for Pen TestingSolutionSteps fo.pdf
armyshoes
 
which event in this life cycle is comparable to fertilizarion .pdf
which event in this life cycle is comparable to fertilizarion .pdfwhich event in this life cycle is comparable to fertilizarion .pdf
which event in this life cycle is comparable to fertilizarion .pdf
armyshoes
 
what is the value of nursing educationSolutionThe main value o.pdf
what is the value of nursing educationSolutionThe main value o.pdfwhat is the value of nursing educationSolutionThe main value o.pdf
what is the value of nursing educationSolutionThe main value o.pdf
armyshoes
 
What is the exact output of the following code#include stdio.h.pdf
What is the exact output of the following code#include stdio.h.pdfWhat is the exact output of the following code#include stdio.h.pdf
What is the exact output of the following code#include stdio.h.pdf
armyshoes
 
Using a Fabry-Perot interferometer, we can Observe small objects. D.pdf
Using a Fabry-Perot interferometer, we can  Observe small objects.  D.pdfUsing a Fabry-Perot interferometer, we can  Observe small objects.  D.pdf
Using a Fabry-Perot interferometer, we can Observe small objects. D.pdf
armyshoes
 
TRUE of falseA researcher found 95 confident interval for the aver.pdf
TRUE of falseA researcher found 95 confident interval for the aver.pdfTRUE of falseA researcher found 95 confident interval for the aver.pdf
TRUE of falseA researcher found 95 confident interval for the aver.pdf
armyshoes
 
The major type of interactive forces between molecules of HCl are i.pdf
The major type of interactive forces between molecules of HCl are  i.pdfThe major type of interactive forces between molecules of HCl are  i.pdf
The major type of interactive forces between molecules of HCl are i.pdf
armyshoes
 

More from armyshoes (20)

Foofy obtained a significant Fobt from an experiment with five level.pdf
Foofy obtained a significant Fobt from an experiment with five level.pdfFoofy obtained a significant Fobt from an experiment with five level.pdf
Foofy obtained a significant Fobt from an experiment with five level.pdf
 
Explain how water interacts with the following types of compounds to.pdf
Explain how water interacts with the following types of compounds to.pdfExplain how water interacts with the following types of compounds to.pdf
Explain how water interacts with the following types of compounds to.pdf
 
Explain how the financial reporting of gains and losses on assets.pdf
Explain how the financial reporting of gains and losses on assets.pdfExplain how the financial reporting of gains and losses on assets.pdf
Explain how the financial reporting of gains and losses on assets.pdf
 
Explain two ways in which microbiologists have overcome the difficul.pdf
Explain two ways in which microbiologists have overcome the difficul.pdfExplain two ways in which microbiologists have overcome the difficul.pdf
Explain two ways in which microbiologists have overcome the difficul.pdf
 
During the preparation of the bank reconciliation for Building Conce.pdf
During the preparation of the bank reconciliation for Building Conce.pdfDuring the preparation of the bank reconciliation for Building Conce.pdf
During the preparation of the bank reconciliation for Building Conce.pdf
 
DNA Paternity Testing Simulation Post-lab QuestionsA)Why do differ.pdf
DNA Paternity Testing Simulation Post-lab QuestionsA)Why do differ.pdfDNA Paternity Testing Simulation Post-lab QuestionsA)Why do differ.pdf
DNA Paternity Testing Simulation Post-lab QuestionsA)Why do differ.pdf
 
Describe how the lung cancer travels to the first site of metastasis.pdf
Describe how the lung cancer travels to the first site of metastasis.pdfDescribe how the lung cancer travels to the first site of metastasis.pdf
Describe how the lung cancer travels to the first site of metastasis.pdf
 
Define the following engineering terms1. engineering ethics2. W.pdf
Define the following engineering terms1. engineering ethics2. W.pdfDefine the following engineering terms1. engineering ethics2. W.pdf
Define the following engineering terms1. engineering ethics2. W.pdf
 
Data structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdfData structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdf
 
You have been cut! While you wage war in the streets of Gotham for y.pdf
You have been cut! While you wage war in the streets of Gotham for y.pdfYou have been cut! While you wage war in the streets of Gotham for y.pdf
You have been cut! While you wage war in the streets of Gotham for y.pdf
 
Why has Mexico encountered such a significant collapse from an econom.pdf
Why has Mexico encountered such a significant collapse from an econom.pdfWhy has Mexico encountered such a significant collapse from an econom.pdf
Why has Mexico encountered such a significant collapse from an econom.pdf
 
Which of the following isare true of BOTH natural selection and gen.pdf
Which of the following isare true of BOTH natural selection and gen.pdfWhich of the following isare true of BOTH natural selection and gen.pdf
Which of the following isare true of BOTH natural selection and gen.pdf
 
Which of the following information-management systems uses artificia.pdf
Which of the following information-management systems uses artificia.pdfWhich of the following information-management systems uses artificia.pdf
Which of the following information-management systems uses artificia.pdf
 
What steps and tools are used for Pen TestingSolutionSteps fo.pdf
What steps and tools are used for Pen TestingSolutionSteps fo.pdfWhat steps and tools are used for Pen TestingSolutionSteps fo.pdf
What steps and tools are used for Pen TestingSolutionSteps fo.pdf
 
which event in this life cycle is comparable to fertilizarion .pdf
which event in this life cycle is comparable to fertilizarion .pdfwhich event in this life cycle is comparable to fertilizarion .pdf
which event in this life cycle is comparable to fertilizarion .pdf
 
what is the value of nursing educationSolutionThe main value o.pdf
what is the value of nursing educationSolutionThe main value o.pdfwhat is the value of nursing educationSolutionThe main value o.pdf
what is the value of nursing educationSolutionThe main value o.pdf
 
What is the exact output of the following code#include stdio.h.pdf
What is the exact output of the following code#include stdio.h.pdfWhat is the exact output of the following code#include stdio.h.pdf
What is the exact output of the following code#include stdio.h.pdf
 
Using a Fabry-Perot interferometer, we can Observe small objects. D.pdf
Using a Fabry-Perot interferometer, we can  Observe small objects.  D.pdfUsing a Fabry-Perot interferometer, we can  Observe small objects.  D.pdf
Using a Fabry-Perot interferometer, we can Observe small objects. D.pdf
 
TRUE of falseA researcher found 95 confident interval for the aver.pdf
TRUE of falseA researcher found 95 confident interval for the aver.pdfTRUE of falseA researcher found 95 confident interval for the aver.pdf
TRUE of falseA researcher found 95 confident interval for the aver.pdf
 
The major type of interactive forces between molecules of HCl are i.pdf
The major type of interactive forces between molecules of HCl are  i.pdfThe major type of interactive forces between molecules of HCl are  i.pdf
The major type of interactive forces between molecules of HCl are i.pdf
 

Recently uploaded

Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 

Recently uploaded (20)

Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 

C++ CodeConsider the LinkedList class and the Node class that we s.pdf

  • 1. C++ Code Consider the LinkedList class and the Node class that we saw in lecture. The LinkedList class basically represents a singly linked list. Now, for this lab, consider a doubly linked list, i.e. each node has a next and previous pointers to the next and previous node, respectively. Modify the Node class to reflect this change and implement the following methods for DoublyLinkedList class: addFirst addLast addAtIndex removeFirst removeLast removeAtIndex Note: We saw how to implement these methods for a singly linked list and the code is added as an attachment. C++ Code ***************************************************************************** ****************** ***************************************************************************** ****************** C++ Code Solution #include #include using namespace std; template class Node { public: T element; // Element contained in the node Node *next; // Pointer to the next node Node *previous;
  • 2. Node(T element) // Constructor { this->element = element; next = NULL; } }; template class LinkedList { public: LinkedList(); void addFirst(T element); void addLast(T element); T removeFirst() ; T removeLast(); void addAtIndex(int index, T element); T removeAtIndex(int index); void traverseList(); private: Node *head; Node *tail; int size; }; template LinkedList::LinkedList() { head = NULL; tail = NULL; size = 0; } template void LinkedList::addFirst(T element) { Node *newNode = new Node(element); newNode->next = head;
  • 3. if(head != NULL) { head->previous = newNode; } newNode->previous = NULL; head = newNode; size++; if (tail == NULL) tail = head; } template void LinkedList::addLast(T element) { if (tail == NULL) { head = tail = new Node(element); } else { tail->next = new Node(element); tail = tail->next; } size++; } template void LinkedList::addAtIndex(int index, T element) { if (index == 0) addFirst(element); else if (index >= size) addLast(element); else { Node *current = head; for (int i = 1; i < index; i++) current = current->next; Node *temp = current->next; current->next = new Node(element);
  • 4. (current->next)->next = temp; size++; } } template T LinkedList::removeFirst() { if (size == 0) exit(1); else { Node *temp = head; head = head->next; size--; T element = temp->element; delete temp; return element; } } template T LinkedList::removeLast() { if (size == 0) exit(1); else if (size == 1) { Node *temp = head; head = tail = NULL; size = 0; T element = temp->element; delete temp; return element; } else { Node *current = head;
  • 5. for (int i = 0; i < size - 2; i++) current = current->next; Node *temp = tail; tail = current; tail->next = NULL; size--; T element = temp->element; delete temp; return element; } } template T LinkedList::removeAtIndex(int index) { if (index < 0 || index >= size) exit(1); else if (index == 0) return removeFirst(); else if (index == size - 1) return removeLast(); else { Node *previous = head; for (int i = 1; i < index; i++) { previous = previous->next; } Node *current = previous->next; previous->next = current->next; size--; T element = current->element; delete current; return element; } } template void LinkedList::traverseList()
  • 6. { Node *temp = head; while(temp !=NULL) { cout<element<<" "; temp = temp->next; } } int main() { LinkedList *myList = new LinkedList(); myList->addFirst(10); myList->addFirst(20); myList->addLast(30); myList->removeAtIndex(1); myList->traverseList(); delete myList; }