SlideShare a Scribd company logo
1 of 6
Download to read offline
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 03
Nasir 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
 
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
 
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
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
Niraj Agarwal
 

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

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
 
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
 
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
 
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
 

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

Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 

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; }