SlideShare a Scribd company logo
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 .pdf
rohit219406
 
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
Edwardw5nSlaterl
 
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
callawaycorb73779
 
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
udit652068
 
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
gilliandunce53776
 
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
optokunal1
 
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
deepakangel
 
include ltfunctionalgt include ltiteratorgt inclu.pdf
include ltfunctionalgt include ltiteratorgt inclu.pdfinclude ltfunctionalgt include ltiteratorgt inclu.pdf
include ltfunctionalgt include ltiteratorgt inclu.pdf
naslin841216
 
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
PeterM9sWhitej
 
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
arihantmobileselepun
 
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
noreendchesterton753
 
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
flashfashioncasualwe
 
tested on eclipseDoublyLinkedList class.pdf
tested on eclipseDoublyLinkedList class.pdftested on eclipseDoublyLinkedList class.pdf
tested on eclipseDoublyLinkedList class.pdf
shanki7
 
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhlinked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
vasavim9
 
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
arihantelehyb
 
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
hadpadrrajeshh
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
RashidFaridChishti
 
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
anton291
 
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
JUSTSTYLISH3B2MOHALI
 
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
tesmondday29076
 

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
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
 
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
 

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.pdf
sales98
 
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
sales98
 
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
sales98
 
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
sales98
 
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
sales98
 
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
sales98
 
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
sales98
 
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
sales98
 
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
sales98
 
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
sales98
 
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
sales98
 
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
sales98
 
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
sales98
 
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
sales98
 
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
sales98
 
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
sales98
 
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
sales98
 
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
sales98
 
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
sales98
 
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
sales98
 

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

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
 
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
 
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
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
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
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
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
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
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
 
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
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
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
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 

Recently uploaded (20)

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
 
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
 
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
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
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
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
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...
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
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
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 

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