Question

PART 1: Design and implement a class representing a doubly linked list. The class must have...

PART 1:

Design and implement a class representing a doubly linked list. The class must have the following requirements:

1)The linked list and the nodes must be implemented as C++ templates

2)The list must be generic

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//LinkedList with SumLists()

#include <iostream>
#include <set>

using namespace std;

template<class T>
class Node
{
public:
T data;
Node<T> * next;
Node<T>(const T& d):data(d), next() {}
Node<T>(const Node<T>& copyNode) : data(copyNode.data), next() {}

private:
Node<T>& operator=(const Node<T>&);
};

template<class T>
class LinkedList
{
public:

Node<T> * head;
Node<T> * tail;

LinkedList(const LinkedList& LL);
LinkedList& operator=(LinkedList byValList);
LinkedList(): head(NULL), tail(NULL) {}
LinkedList(Node<T> * newNode) : head(newNode), tail(newNode) {}
~LinkedList();

static LinkedList<int> sumLists(const LinkedList<int>& LL1, LinkedList<int>& LL2);

void insertToTail(T val);
void insertToHead(T val);
void print();
void printBackwards();
};

template<class T>
LinkedList<T>::LinkedList(const LinkedList<T>& LL) : head(NULL), tail(NULL)
{
const Node<T> * curr = LL.head;

if (!head && curr)
{
head = new Node<T>(curr->data);
tail = head;
curr = curr->next;
}

while (curr)
{
Node<T> * newNode = new Node<T>(curr->data);
tail->next = newNode;
tail = newNode;
curr = curr->next;
}
}

template<class T>
LinkedList<T>& LinkedList<T>::operator=(LinkedList byValList)
{
std::swap(head, byValList.head);
return *this;
}

template<class T>
LinkedList<T>::~LinkedList()
{
Node<T> * curr = head;
while (head)
{
head = head->next;
delete curr;
curr = head;
}
}

template<class T>
void LinkedList<T>::insertToTail(T val)
{
Node<T> * newNode = new Node<T>(val);
if (tail == NULL)
{
newNode->next = tail;
tail = newNode;
head = newNode;
return;
}
tail->next = newNode;
tail = tail->next;
}

template<class T>
void LinkedList<T>::insertToHead(T val)
{
Node<T> * newNode = new Node<T>(val);
newNode->next = head;
head = newNode;
if (head->next == NULL)
tail = newNode;
}

template<class T>
void LinkedList<T>::print()
{
Node<T> * curr = head;
while (curr)
{
cout<<curr->data<<" --> ";
curr = curr->next;
}
cout<<"NULL"<<endl;
}

template<class T>
void LinkedList<T>::printBackwards()
{
Node<T> * curr;
LinkedList ReversedList(new Node<T>(head->data));
curr = head->next;
while (curr)
{
ReversedList.insertToHead(curr->data);
curr = curr->next;
}

curr = ReversedList.head;
while (curr)
{
cout<<curr->data<<" --> ";
curr = curr->next;
}
cout<<"NULL\n";
}

template<class T>
LinkedList<int> LinkedList<T>::sumLists(const LinkedList<int>& LL1, LinkedList<int>& LL2)
{
Node<T>* curr1 = LL1.head;
Node<T>* curr2 = LL2.head;

LinkedList<int> ResultList;

int newData;
int carry = 0;

while (curr1 && curr2)
{
newData = (curr1->data + curr2->data + carry) % 10;
carry = (curr1->data + curr2->data + carry) / 10;
ResultList.insertToTail(newData);

curr1 = curr1->next;
curr2 = curr2->next;
}

while (curr1 || curr2)
{
if (carry)
{
if (curr1)
ResultList.insertToTail(curr1->data + carry);
if (curr2)
ResultList.insertToTail(curr2->data + carry);
carry = 0;
continue;
}
if (curr1)
{
ResultList.insertToTail(curr1->data);
curr1 = curr1->next;
}
if (curr2)
{
ResultList.insertToTail(curr2->data + carry);
curr2 = curr2->next;

}


}

return ResultList;
}

int main()
{
LinkedList<int> LL1(new Node<int>(7));
LL1.insertToTail(1);
LL1.insertToTail(6);
LL1.insertToTail(5);
LL1.insertToTail(4);

LinkedList<int> LL2(new Node<int>(5));
LL2.insertToTail(9);
LL2.insertToTail(2);

LinkedList<int> LL = LL1.sumLists(LL1, LL2);
LL.print();
LL2.print();
LL = LL2;
LL.print();
LL2.print();

return 0;
}

Add a comment
Know the answer?
Add Answer to:
PART 1: Design and implement a class representing a doubly linked list. The class must have...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C++ language, Design and implement a class representing a doubly linked list. The class must...

    Using C++ language, Design and implement a class representing a doubly linked list. The class must have the following requirements: The linked list and the nodes must be implemented as a C++ templates The list must be generic – it should not implement arithmetic/logic functions. (template class) It must include a destructor and a copy constructor It must include methods to insert at the front and at the back of the list It must include a method to return the...

  • This class implements a doubly linked list in which the nodes are of the class DLNode....

    This class implements a doubly linked list in which the nodes are of the class DLNode. This class must implement the interface DLListADT.java that specifies the public methods in this class. The header for this class will then be public class DLList implements DLListADT This class will have three private instance variables: • private DLNode front. This is a reference to the first node of the doubly linked list. • private DLNode rear. This is a reference to the last...

  • In C++ Create a data structure doubly linked list, implement the following operations for the doubly...

    In C++ Create a data structure doubly linked list, implement the following operations for the doubly linked list: addFirst addLast insertBefore insertAfter delete printList

  • In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class...

    In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class (50 pts): Create node with public properties: Block type block and block ptr next. Create a linked list class that uses the node you generated without an add or delete method with a head and optional tail and counter. Make a driver that generates a node to test your implementation. Part 2 Add Method (30 pts): Create an add method in your linked list...

  • Implement a simple Doubly Linked List using nodes. Functions to include: an insert in a sorted...

    Implement a simple Doubly Linked List using nodes. Functions to include: an insert in a sorted list, and a delete function, including constructor. Please include comments to make the code traceable and understandable

  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

  • Write a function to implement linked list consisting of five nodes. Store the data in the...

    Write a function to implement linked list consisting of five nodes. Store the data in the nodes in the order given below. Also, write a function to display the data stored in the implemented linked list. in C++ programming George, Paul, Ross, Joe, Elaine, Robert1 Insert three nodes in between second node and third node in the linked list that you implemented in problem 1. Store the following data in the new (inserted) nodes in the following order. (You have...

  • You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow...

    You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow the format** **Don't worry about the readme.txt** THANK YOU SO SO MUCH! You have to implement the doubly linked list in DList.h and DList.cpp and the queue in the LinkedQueue.h and LinkedQueue.cpp; all as template classes. You have to provide the main.cpp file as we discussed in class to allow the user to interact with your queue using enqueue, dequeue, front, back, size, empty,...

  • Write a function to implement linked list consisting of six nodes. Store the data in the...

    Write a function to implement linked list consisting of six nodes. Store the data in the nodes in the order given below. Also, write a function to display the data stored in the implemented linked list. in C++ programming use these as names as nodes George, Paul, Ross, Joe, Elaine, Robert1. Insert three nodes in between second node and third node in the linked list that you implemented in problem 1. Store the following data in the new (inserted) nodes...

  • C++ Linked List Implementation Motivation As we discussed in class, the data structures that you use...

    C++ Linked List Implementation Motivation As we discussed in class, the data structures that you use to implement your program can have a profound impact on it's overall performance. A poorly written program will often need much more RAM and CPU time then a well-written implementation. One of the most basic data structure questions revolves around the difference between an array and a linked list. After you finish this assignment you should have a firm understanding of their operation. Problem...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT