SlideShare a Scribd company logo
1 of 7
Download to read offline
Consider a double-linked linked list implementation with the following node: struct Node {int
data; Node *prev; Node *next;} Write a copyList method that is not a member of any class.
The method should take a head pointer and return another pointer. Do not modify the input.
Solution
struct Node {
Node *prev; // previous node
Node *next; // next node
int data; // stored value
};
#include
#include "List.h" // std: #include
using namespace std;
typedef DataList ; // std: typedef list Data;
int main() {
Data k;
// back stuff
k.push_back(5);
k.push_back(6);
cout << k.back() << endl;
k.pop_back();
// front stuff
k.push_front(4);
k.push_front(3);
cout << k.front() << endl;
k.pop_front();
// forward iterator
Data::iterator pos;
for (pos = k.begin(); pos != k.end(); ++pos)
cout << *pos << endl;
// output and delete list
while (!k.empty()) {
cout << k.front() << endl;
k.pop_front();
}
k.push_front(5);
k.push_front(6);
// remove and erase
k.remove(5);
pos = k.begin();
k.erase(pos);
k.push_front(5);
k.push_front(6);
// copy constructor
Data l = k;
// assignment operator
Data m;
m = k;
return 0;
}
// List.h
struct Node;
classIterator List;
class List {
public:
typedef ListIterator iterator;
// constructor
List();
// destructor
virtual ~List();
// copy constructor
List(const List& k);
// assignment operator
List& operator=(const List& k);
// insert value in front of list
void push_front(double data);
// insert value in back of list
void push_back(double data);
// delete value from front of list
void pop_front();
// delete value from back of list
void pop_back();
// return value on front of list
double front() const;
// return value on back of list
double back() const;
// delete value specified by iterator
void erase(const iterator& i);
// delete all nodes with specified value
void remove(double data);
// return true if list is empty
bool empty() const;
// return reference to first element in list
iterator begin() const;
// return reference to one past last element in list
iterator end() const;
private:
Node *head; // head of list
};
class ListIterator {
public:
// default constructor
ListIterator() {
i = 0;
}
// construct iterator for given pointer (used for begin/end)
ListIterator(Node *p) {
i = p;
}
// convert iterator to Node*
operator Node*() const {
return i;
}
// test two iterators for not equal
bool operator!=(const ListIterator& k) const {
return i != k.i;
}
// preincrement operator
ListIterator& operator++() {
i = i->next;
return *this;
}
// return value associated with iterator
double& operator*() const {
return i->data;
}
private:
Node *i; // current value of iterator
};
list.cpp
// delete list
static void deleteList(Node *head) {
Node *p = head->next;
while (p != head) {
Node *next = p->next;
delete p;
p = next;
}
delete head;
}
// copy list
static void copyList(const Node *from, Node *&to) {
// create dummy header
to = new Node;
to->next = to->prev = to;
// copy nodes
for (Node *p = from->next; p != from; p = p->next) {
Node *t = new Node;
t->value = p->value;
// insert at end of list
t->next = to;
t->prev = to->prev;
t->prev->next = t;
t->next->prev = t;
}
}
// constructor
List::List() {
head = new Node;
head->next = head->prev = head;
}
// destructor
List::~List() {
deleteList(head);
}
// copy constructor
List::List(const List& k) {
copyList(k.head, head);
}
// assignment operator
List& List::operator=(const List& k) {
if (this == &k) return *this;
deleteList(head);
copyList(k.head, head);
return *this;
}
// insert value in front of list
void List::push_front(double value) {
Node *p = new Data;
p->Data = value;
p->next = head->next;
p->prev = head;
p->next->prev = p;
p->prev->next = p;
}
// insert value in back of list
void List::push_back(double value) {
Node *p = new Node;
p->Data = Data;
p->next = head;
p->prev = head->prev;
p->next->prev = p;
p->prev->next = p;
}
// delete value from front of list
void List::pop_front() {
Node *p = head->next;
p->prev->next = p->next;
p->next->prev = p->prev;
delete p;
}
// delete value from back of list
void List::pop_back() {
Node *p = head->prev;
p->prev->next = p->next;
p->next->prev = p->prev;
delete p;
}
// return value on front of list
double List::front() const {
return head->next->data;
}
// return value on back of list
double List::back() const {
return head->prev->data;
}
// delete value specified by iterator
void List::erase(const iterator& i) {
Node *p = i;
p->prev->next = p->next;
p->next->prev = p->prev;
delete p;
}
void List::remove(double Data) {
Node *p = head->next;
while (p != head) {
Node *next = p->next;
if (p->Data == Data) {
p->prev->next = p->next;
p->next->prev = p->prev;
delete p;
}
p = next;
}
}
// return true if list is empty
bool List::empty() const {
return head->next == head;
}
// return reference to first element in list
List::iterator List::begin() const {
return head->next;
}
// return reference to one past last element in list
List::iterator List::end() const {
return head;
}

More Related Content

Similar to Consider a double-linked linked list implementation with the followin.pdf

Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfrohit219406
 
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdfNeed Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdfEdwardw5nSlaterl
 
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdfC++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdfcallawaycorb73779
 
Implement the following specification of UnsortedType using circular.pdf
Implement the following specification of UnsortedType using circular.pdfImplement the following specification of UnsortedType using circular.pdf
Implement the following specification of UnsortedType using circular.pdfudit652068
 
Below is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxBelow is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxgilliandunce53776
 
Use C++class Node{public   Node ( int = 0 );       constru.pdf
Use C++class Node{public   Node ( int = 0 );        constru.pdfUse C++class Node{public   Node ( int = 0 );        constru.pdf
Use C++class Node{public   Node ( int = 0 );       constru.pdfoptokunal1
 
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
  import java.util.Iterator; import java.util.NoSuchElementException; .pdf  import java.util.Iterator; import java.util.NoSuchElementException; .pdf
import java.util.Iterator; import java.util.NoSuchElementException; .pdfdeepakangel
 
include ltfunctionalgt include ltiteratorgt inclu.pdf
include ltfunctionalgt include ltiteratorgt inclu.pdfinclude ltfunctionalgt include ltiteratorgt inclu.pdf
include ltfunctionalgt include ltiteratorgt inclu.pdfnaslin841216
 
Main-cpp #include -iostream- #include -List-h- int main() { retur.pdf
Main-cpp  #include -iostream- #include -List-h- int main() {     retur.pdfMain-cpp  #include -iostream- #include -List-h- int main() {     retur.pdf
Main-cpp #include -iostream- #include -List-h- int main() { retur.pdfPeterM9sWhitej
 
Qestion Please add pre-condition, post-conditions and descriptions .pdf
Qestion Please add pre-condition, post-conditions and descriptions .pdfQestion Please add pre-condition, post-conditions and descriptions .pdf
Qestion Please add pre-condition, post-conditions and descriptions .pdfarihantmobileselepun
 
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docx
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docxWrite a C++ function that delete nodes in a doubly linkedlist- It shou.docx
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docxnoreendchesterton753
 
In C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfIn C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfflashfashioncasualwe
 
tested on eclipseDoublyLinkedList class.pdf
tested on eclipseDoublyLinkedList class.pdftested on eclipseDoublyLinkedList class.pdf
tested on eclipseDoublyLinkedList class.pdfshanki7
 
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhlinked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhvasavim9
 
How to do insertion sort on a singly linked list with no header usin.pdf
How to do insertion sort on a singly linked list with no header usin.pdfHow to do insertion sort on a singly linked list with no header usin.pdf
How to do insertion sort on a singly linked list with no header usin.pdfarihantelehyb
 
Create a link list. Add some nodes to it, search and delete nodes fro.pdf
Create a link list. Add some nodes to it, search and delete nodes fro.pdfCreate a link list. Add some nodes to it, search and delete nodes fro.pdf
Create a link list. Add some nodes to it, search and delete nodes fro.pdfhadpadrrajeshh
 
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfA)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfanton291
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfJUSTSTYLISH3B2MOHALI
 
Dividing a linked list into two sublists of almost equal sizesa. A.pdf
Dividing a linked list into two sublists of almost equal sizesa. A.pdfDividing a linked list into two sublists of almost equal sizesa. A.pdf
Dividing a linked list into two sublists of almost equal sizesa. A.pdftesmondday29076
 
Javai have to make a method that takes a linked list and then retu.pdf
Javai have to make a method that takes a linked list and then retu.pdfJavai have to make a method that takes a linked list and then retu.pdf
Javai have to make a method that takes a linked list and then retu.pdfstopgolook
 

Similar to Consider a double-linked linked list implementation with the followin.pdf (20)

Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdfNeed Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
 
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdfC++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
 
Implement the following specification of UnsortedType using circular.pdf
Implement the following specification of UnsortedType using circular.pdfImplement the following specification of UnsortedType using circular.pdf
Implement the following specification of UnsortedType using circular.pdf
 
Below is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxBelow is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docx
 
Use C++class Node{public   Node ( int = 0 );       constru.pdf
Use C++class Node{public   Node ( int = 0 );        constru.pdfUse C++class Node{public   Node ( int = 0 );        constru.pdf
Use C++class Node{public   Node ( int = 0 );       constru.pdf
 
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
  import java.util.Iterator; import java.util.NoSuchElementException; .pdf  import java.util.Iterator; import java.util.NoSuchElementException; .pdf
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
 
include ltfunctionalgt include ltiteratorgt inclu.pdf
include ltfunctionalgt include ltiteratorgt inclu.pdfinclude ltfunctionalgt include ltiteratorgt inclu.pdf
include ltfunctionalgt include ltiteratorgt inclu.pdf
 
Main-cpp #include -iostream- #include -List-h- int main() { retur.pdf
Main-cpp  #include -iostream- #include -List-h- int main() {     retur.pdfMain-cpp  #include -iostream- #include -List-h- int main() {     retur.pdf
Main-cpp #include -iostream- #include -List-h- int main() { retur.pdf
 
Qestion Please add pre-condition, post-conditions and descriptions .pdf
Qestion Please add pre-condition, post-conditions and descriptions .pdfQestion Please add pre-condition, post-conditions and descriptions .pdf
Qestion Please add pre-condition, post-conditions and descriptions .pdf
 
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docx
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docxWrite a C++ function that delete nodes in a doubly linkedlist- It shou.docx
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docx
 
In C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfIn C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdf
 
tested on eclipseDoublyLinkedList class.pdf
tested on eclipseDoublyLinkedList class.pdftested on eclipseDoublyLinkedList class.pdf
tested on eclipseDoublyLinkedList class.pdf
 
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhlinked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
 
How to do insertion sort on a singly linked list with no header usin.pdf
How to do insertion sort on a singly linked list with no header usin.pdfHow to do insertion sort on a singly linked list with no header usin.pdf
How to do insertion sort on a singly linked list with no header usin.pdf
 
Create a link list. Add some nodes to it, search and delete nodes fro.pdf
Create a link list. Add some nodes to it, search and delete nodes fro.pdfCreate a link list. Add some nodes to it, search and delete nodes fro.pdf
Create a link list. Add some nodes to it, search and delete nodes fro.pdf
 
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfA)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
Dividing a linked list into two sublists of almost equal sizesa. A.pdf
Dividing a linked list into two sublists of almost equal sizesa. A.pdfDividing a linked list into two sublists of almost equal sizesa. A.pdf
Dividing a linked list into two sublists of almost equal sizesa. A.pdf
 
Javai have to make a method that takes a linked list and then retu.pdf
Javai have to make a method that takes a linked list and then retu.pdfJavai have to make a method that takes a linked list and then retu.pdf
Javai have to make a method that takes a linked list and then retu.pdf
 

More from sales98

Genetic evidence supports which of the following explanations for the.pdf
Genetic evidence supports which of the following explanations for the.pdfGenetic evidence supports which of the following explanations for the.pdf
Genetic evidence supports which of the following explanations for the.pdfsales98
 
Complete the following table for InvestmentsFair value method(s).pdf
Complete the following table for InvestmentsFair value method(s).pdfComplete the following table for InvestmentsFair value method(s).pdf
Complete the following table for InvestmentsFair value method(s).pdfsales98
 
Find the values of the trigonometric functions of from the informat.pdf
Find the values of the trigonometric functions of  from the informat.pdfFind the values of the trigonometric functions of  from the informat.pdf
Find the values of the trigonometric functions of from the informat.pdfsales98
 
Evolution is driven by environmental that put selection pressure on o.pdf
Evolution is driven by environmental that put selection pressure on o.pdfEvolution is driven by environmental that put selection pressure on o.pdf
Evolution is driven by environmental that put selection pressure on o.pdfsales98
 
e) As a practical matter, trade-offs of qualitative characteristics o.pdf
e) As a practical matter, trade-offs of qualitative characteristics o.pdfe) As a practical matter, trade-offs of qualitative characteristics o.pdf
e) As a practical matter, trade-offs of qualitative characteristics o.pdfsales98
 
Determine whether the relation defines y as a function of x. Give the.pdf
Determine whether the relation defines y as a function of x. Give the.pdfDetermine whether the relation defines y as a function of x. Give the.pdf
Determine whether the relation defines y as a function of x. Give the.pdfsales98
 
Connor and his wife are guests at the hotel, having reserved a room .pdf
Connor and his wife are guests at the hotel, having reserved a room .pdfConnor and his wife are guests at the hotel, having reserved a room .pdf
Connor and his wife are guests at the hotel, having reserved a room .pdfsales98
 
Define the role of the entrepreneur in the U.S. economy and describe.pdf
Define the role of the entrepreneur in the U.S. economy and describe.pdfDefine the role of the entrepreneur in the U.S. economy and describe.pdf
Define the role of the entrepreneur in the U.S. economy and describe.pdfsales98
 
8. You may have heard that drinking soda or acid is bad for your teet.pdf
8. You may have heard that drinking soda or acid is bad for your teet.pdf8. You may have heard that drinking soda or acid is bad for your teet.pdf
8. You may have heard that drinking soda or acid is bad for your teet.pdfsales98
 
alter ego theory. TRUE FALSE Multiple Choice her nephew Dru to manag.pdf
alter ego theory. TRUE FALSE Multiple Choice her nephew Dru to manag.pdfalter ego theory. TRUE FALSE Multiple Choice her nephew Dru to manag.pdf
alter ego theory. TRUE FALSE Multiple Choice her nephew Dru to manag.pdfsales98
 
2)Are enterprise information portals making executive information sy.pdf
2)Are enterprise information portals making executive information sy.pdf2)Are enterprise information portals making executive information sy.pdf
2)Are enterprise information portals making executive information sy.pdfsales98
 
5. How the tax system changes the distribution of income among capita.pdf
5. How the tax system changes the distribution of income among capita.pdf5. How the tax system changes the distribution of income among capita.pdf
5. How the tax system changes the distribution of income among capita.pdfsales98
 
Write Java FX code for this pseudocode of the void initilizaHistoryL.pdf
Write Java FX code for this pseudocode of the void initilizaHistoryL.pdfWrite Java FX code for this pseudocode of the void initilizaHistoryL.pdf
Write Java FX code for this pseudocode of the void initilizaHistoryL.pdfsales98
 
Your name Date Explanation of Program -Modifying the first .pdf
Your name Date  Explanation of Program -Modifying the first .pdfYour name Date  Explanation of Program -Modifying the first .pdf
Your name Date Explanation of Program -Modifying the first .pdfsales98
 
Why is it that ozone is not desirable at the surface of earth, but i.pdf
Why is it that ozone is not desirable at the surface of earth, but i.pdfWhy is it that ozone is not desirable at the surface of earth, but i.pdf
Why is it that ozone is not desirable at the surface of earth, but i.pdfsales98
 
Write a program that moves the ball in a pane. You should define a p.pdf
Write a program that moves the ball in a pane. You should define a p.pdfWrite a program that moves the ball in a pane. You should define a p.pdf
Write a program that moves the ball in a pane. You should define a p.pdfsales98
 
Which ape has been known to make territory patrols and wage war o.pdf
Which ape has been known to make territory patrols and wage war o.pdfWhich ape has been known to make territory patrols and wage war o.pdf
Which ape has been known to make territory patrols and wage war o.pdfsales98
 
What are the limitations of an expert systemSolutionLIMITATIO.pdf
What are the limitations of an expert systemSolutionLIMITATIO.pdfWhat are the limitations of an expert systemSolutionLIMITATIO.pdf
What are the limitations of an expert systemSolutionLIMITATIO.pdfsales98
 
Using the scenario provided in the Milestone One Guidelines and Rubr.pdf
Using the scenario provided in the Milestone One Guidelines and Rubr.pdfUsing the scenario provided in the Milestone One Guidelines and Rubr.pdf
Using the scenario provided in the Milestone One Guidelines and Rubr.pdfsales98
 
To avoid crises, companies should design their organizations to impl.pdf
To avoid crises, companies should design their organizations to impl.pdfTo avoid crises, companies should design their organizations to impl.pdf
To avoid crises, companies should design their organizations to impl.pdfsales98
 

More from sales98 (20)

Genetic evidence supports which of the following explanations for the.pdf
Genetic evidence supports which of the following explanations for the.pdfGenetic evidence supports which of the following explanations for the.pdf
Genetic evidence supports which of the following explanations for the.pdf
 
Complete the following table for InvestmentsFair value method(s).pdf
Complete the following table for InvestmentsFair value method(s).pdfComplete the following table for InvestmentsFair value method(s).pdf
Complete the following table for InvestmentsFair value method(s).pdf
 
Find the values of the trigonometric functions of from the informat.pdf
Find the values of the trigonometric functions of  from the informat.pdfFind the values of the trigonometric functions of  from the informat.pdf
Find the values of the trigonometric functions of from the informat.pdf
 
Evolution is driven by environmental that put selection pressure on o.pdf
Evolution is driven by environmental that put selection pressure on o.pdfEvolution is driven by environmental that put selection pressure on o.pdf
Evolution is driven by environmental that put selection pressure on o.pdf
 
e) As a practical matter, trade-offs of qualitative characteristics o.pdf
e) As a practical matter, trade-offs of qualitative characteristics o.pdfe) As a practical matter, trade-offs of qualitative characteristics o.pdf
e) As a practical matter, trade-offs of qualitative characteristics o.pdf
 
Determine whether the relation defines y as a function of x. Give the.pdf
Determine whether the relation defines y as a function of x. Give the.pdfDetermine whether the relation defines y as a function of x. Give the.pdf
Determine whether the relation defines y as a function of x. Give the.pdf
 
Connor and his wife are guests at the hotel, having reserved a room .pdf
Connor and his wife are guests at the hotel, having reserved a room .pdfConnor and his wife are guests at the hotel, having reserved a room .pdf
Connor and his wife are guests at the hotel, having reserved a room .pdf
 
Define the role of the entrepreneur in the U.S. economy and describe.pdf
Define the role of the entrepreneur in the U.S. economy and describe.pdfDefine the role of the entrepreneur in the U.S. economy and describe.pdf
Define the role of the entrepreneur in the U.S. economy and describe.pdf
 
8. You may have heard that drinking soda or acid is bad for your teet.pdf
8. You may have heard that drinking soda or acid is bad for your teet.pdf8. You may have heard that drinking soda or acid is bad for your teet.pdf
8. You may have heard that drinking soda or acid is bad for your teet.pdf
 
alter ego theory. TRUE FALSE Multiple Choice her nephew Dru to manag.pdf
alter ego theory. TRUE FALSE Multiple Choice her nephew Dru to manag.pdfalter ego theory. TRUE FALSE Multiple Choice her nephew Dru to manag.pdf
alter ego theory. TRUE FALSE Multiple Choice her nephew Dru to manag.pdf
 
2)Are enterprise information portals making executive information sy.pdf
2)Are enterprise information portals making executive information sy.pdf2)Are enterprise information portals making executive information sy.pdf
2)Are enterprise information portals making executive information sy.pdf
 
5. How the tax system changes the distribution of income among capita.pdf
5. How the tax system changes the distribution of income among capita.pdf5. How the tax system changes the distribution of income among capita.pdf
5. How the tax system changes the distribution of income among capita.pdf
 
Write Java FX code for this pseudocode of the void initilizaHistoryL.pdf
Write Java FX code for this pseudocode of the void initilizaHistoryL.pdfWrite Java FX code for this pseudocode of the void initilizaHistoryL.pdf
Write Java FX code for this pseudocode of the void initilizaHistoryL.pdf
 
Your name Date Explanation of Program -Modifying the first .pdf
Your name Date  Explanation of Program -Modifying the first .pdfYour name Date  Explanation of Program -Modifying the first .pdf
Your name Date Explanation of Program -Modifying the first .pdf
 
Why is it that ozone is not desirable at the surface of earth, but i.pdf
Why is it that ozone is not desirable at the surface of earth, but i.pdfWhy is it that ozone is not desirable at the surface of earth, but i.pdf
Why is it that ozone is not desirable at the surface of earth, but i.pdf
 
Write a program that moves the ball in a pane. You should define a p.pdf
Write a program that moves the ball in a pane. You should define a p.pdfWrite a program that moves the ball in a pane. You should define a p.pdf
Write a program that moves the ball in a pane. You should define a p.pdf
 
Which ape has been known to make territory patrols and wage war o.pdf
Which ape has been known to make territory patrols and wage war o.pdfWhich ape has been known to make territory patrols and wage war o.pdf
Which ape has been known to make territory patrols and wage war o.pdf
 
What are the limitations of an expert systemSolutionLIMITATIO.pdf
What are the limitations of an expert systemSolutionLIMITATIO.pdfWhat are the limitations of an expert systemSolutionLIMITATIO.pdf
What are the limitations of an expert systemSolutionLIMITATIO.pdf
 
Using the scenario provided in the Milestone One Guidelines and Rubr.pdf
Using the scenario provided in the Milestone One Guidelines and Rubr.pdfUsing the scenario provided in the Milestone One Guidelines and Rubr.pdf
Using the scenario provided in the Milestone One Guidelines and Rubr.pdf
 
To avoid crises, companies should design their organizations to impl.pdf
To avoid crises, companies should design their organizations to impl.pdfTo avoid crises, companies should design their organizations to impl.pdf
To avoid crises, companies should design their organizations to impl.pdf
 

Recently uploaded

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 

Recently uploaded (20)

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 

Consider a double-linked linked list implementation with the followin.pdf

  • 1. Consider a double-linked linked list implementation with the following node: struct Node {int data; Node *prev; Node *next;} Write a copyList method that is not a member of any class. The method should take a head pointer and return another pointer. Do not modify the input. Solution struct Node { Node *prev; // previous node Node *next; // next node int data; // stored value }; #include #include "List.h" // std: #include using namespace std; typedef DataList ; // std: typedef list Data; int main() { Data k; // back stuff k.push_back(5); k.push_back(6); cout << k.back() << endl; k.pop_back(); // front stuff k.push_front(4); k.push_front(3); cout << k.front() << endl; k.pop_front(); // forward iterator Data::iterator pos; for (pos = k.begin(); pos != k.end(); ++pos) cout << *pos << endl; // output and delete list while (!k.empty()) { cout << k.front() << endl; k.pop_front();
  • 2. } k.push_front(5); k.push_front(6); // remove and erase k.remove(5); pos = k.begin(); k.erase(pos); k.push_front(5); k.push_front(6); // copy constructor Data l = k; // assignment operator Data m; m = k; return 0; } // List.h struct Node; classIterator List; class List { public: typedef ListIterator iterator; // constructor List(); // destructor virtual ~List(); // copy constructor List(const List& k); // assignment operator List& operator=(const List& k); // insert value in front of list void push_front(double data); // insert value in back of list void push_back(double data); // delete value from front of list void pop_front();
  • 3. // delete value from back of list void pop_back(); // return value on front of list double front() const; // return value on back of list double back() const; // delete value specified by iterator void erase(const iterator& i); // delete all nodes with specified value void remove(double data); // return true if list is empty bool empty() const; // return reference to first element in list iterator begin() const; // return reference to one past last element in list iterator end() const; private: Node *head; // head of list }; class ListIterator { public: // default constructor ListIterator() { i = 0; } // construct iterator for given pointer (used for begin/end) ListIterator(Node *p) { i = p; } // convert iterator to Node* operator Node*() const { return i; } // test two iterators for not equal bool operator!=(const ListIterator& k) const { return i != k.i;
  • 4. } // preincrement operator ListIterator& operator++() { i = i->next; return *this; } // return value associated with iterator double& operator*() const { return i->data; } private: Node *i; // current value of iterator }; list.cpp // delete list static void deleteList(Node *head) { Node *p = head->next; while (p != head) { Node *next = p->next; delete p; p = next; } delete head; } // copy list static void copyList(const Node *from, Node *&to) { // create dummy header to = new Node; to->next = to->prev = to; // copy nodes for (Node *p = from->next; p != from; p = p->next) { Node *t = new Node; t->value = p->value; // insert at end of list t->next = to; t->prev = to->prev;
  • 5. t->prev->next = t; t->next->prev = t; } } // constructor List::List() { head = new Node; head->next = head->prev = head; } // destructor List::~List() { deleteList(head); } // copy constructor List::List(const List& k) { copyList(k.head, head); } // assignment operator List& List::operator=(const List& k) { if (this == &k) return *this; deleteList(head); copyList(k.head, head); return *this; } // insert value in front of list void List::push_front(double value) { Node *p = new Data; p->Data = value; p->next = head->next; p->prev = head; p->next->prev = p; p->prev->next = p; } // insert value in back of list void List::push_back(double value) { Node *p = new Node;
  • 6. p->Data = Data; p->next = head; p->prev = head->prev; p->next->prev = p; p->prev->next = p; } // delete value from front of list void List::pop_front() { Node *p = head->next; p->prev->next = p->next; p->next->prev = p->prev; delete p; } // delete value from back of list void List::pop_back() { Node *p = head->prev; p->prev->next = p->next; p->next->prev = p->prev; delete p; } // return value on front of list double List::front() const { return head->next->data; } // return value on back of list double List::back() const { return head->prev->data; } // delete value specified by iterator void List::erase(const iterator& i) { Node *p = i; p->prev->next = p->next; p->next->prev = p->prev; delete p; } void List::remove(double Data) {
  • 7. Node *p = head->next; while (p != head) { Node *next = p->next; if (p->Data == Data) { p->prev->next = p->next; p->next->prev = p->prev; delete p; } p = next; } } // return true if list is empty bool List::empty() const { return head->next == head; } // return reference to first element in list List::iterator List::begin() const { return head->next; } // return reference to one past last element in list List::iterator List::end() const { return head; }