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

  • 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

  • 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,...

  • QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities....

    QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities. enqueue (inserts element to the end) dequeue (removes the front element and provides content) isEmpty (checks whether the Queue is empty or not) makeEmpty () peek (provides the element sitting at the top/front, but does not remove) print (prints all the elements from front to the end) reversePrint(prints all the elements from end to the front with the help of back pointers inside your...

  • Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface:...

    Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> {      /**       * Append an item to the end of the list       *       * @param item – item to be appended       */ public void append(E item);      /**       * Insert an item at a specified index position       *       * @param item – item to be...

  • Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that...

    Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that stores integers. Implement a non-recursive function that takes a linked list, searches for an integer, and removes the node with the first occurrence of that integer and also removes the node directly after it regardless of value . This function will return to address of the resulting list. You ca n assume that there will be at least three nodes, and if there is...

  • Suppose we implement a doubly linked list class template LinkedList with template type T. LinkedList has...

    Suppose we implement a doubly linked list class template LinkedList with template type T. LinkedList has fields Node *headPtr, Node *tailPtr and int length, where the struct type Node has fields prev and next of type Node* along with data of type T. The prev and next pointers of each Node points to the previous and next Nodes in the list (or are respectively null in the case of the list’s head or tail node). We wish to detect "invalid"...

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
ADVERTISEMENT