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