In: Computer Science
The List method addAll(i,c) inserts all elements of the Collection c into the list at position i. (The add(i,x) method is a special case where c = {x}.) Explain why, for the data structures in this chapter, it is not efficient to implement addAll(i,c) by repeated calls to add(i,x). Design and implement a more efficient implementation.
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
List.h
#ifndef LIST_H
#define LIST_H
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <set>
using namespace std;
template <typename T>
struct LinkNode
{
T val;
LinkNode *link;
};
template <class T>
class List
{
private:
typedef LinkNode<T> Node;
Node *head;
int count;
public:
List();
~List();
bool isEmpty() const;
int size() const;
bool add(int i, T x);
bool addALL(int i, set<T> c);
T get(int i) const;
void printList() const;
};
#endif
Main.cpp
#include "List.h"
template <typename T>
List<T>::List()
{
head = nullptr;
count = 0;
}
template <typename T>
List<T>::~List()
{
Node *tmp0;
Node *tmp1 = head;
while (tmp1 != nullptr)
{
tmp0 = tmp1;
tmp1 = tmp1->link;
delete tmp0;
}
}
template <typename T>
bool List<T>::isEmpty() const
{
if (count == 0)
return true;
else
return false;
}
template <typename T>
int List<T>::size() const
{
return count;
}
template <typename T>
bool List<T>::add(int idx, T x)
{
if (idx == 0)
{
Node *newNode = new Node();
newNode->val = x;
newNode->link = head;
head = newNode;
count += 1;
return true;
}
if (idx < count)
{
Node *prevNode = nullptr;
Node *curNode = head;
Node *newNode = new Node();
newNode->val = x;
int i;
for (i = 0; (i < idx) & (curNode != nullptr); i++)
{
prevNode = curNode;
curNode = curNode->link;
}
if ((i == idx) & (prevNode != nullptr))
{
prevNode->link = newNode;
newNode->link = curNode;
}
else
{
newNode->link = head;
head = newNode;
}
count += 1;
}
else
{
cout << "Index out of bounds \n";
return false;
}
}
template <typename T>
bool List<T>::addALL(int idx, set<T> c)
{
if (idx < count)
{
int i;
Node *prevNode = nullptr;
Node *nextNode = head;
Node *newNode = new Node();
// find the Link where to insert given idx
for (i = 0; (i < idx) & (nextNode != nullptr); ++i)
{
//cout << "Inside Loop - prev " << prevNode << " nextNode " << nextNode << " I : " << i << "\n";
prevNode = nextNode;
nextNode = nextNode->link;
}
//cout << "Inserting After " << prevNode->val << " At " << i << " , given idx : " << idx << " Node \n";
// start inserting the
for (auto itr = c.crbegin(); itr != c.crend(); ++itr)
{
newNode = new Node();
newNode->val = *itr;
// If inserting first node check if its head node
if (itr == c.crbegin())
{
if ((i == idx) & (prevNode != nullptr))
{
prevNode->link = newNode;
//cout << " Non head node \n";
}
else
{
newNode->link = head;
head = newNode;
}
prevNode = newNode;
//cout << "Setting the prev Node \n";
}
else
{
//cout << "Setting prev_link to new node " << prevNode->val << " to " << newNode->val << "\n";
prevNode->link = newNode;
prevNode = newNode;
}
count += 1;
}
// set the last inserted node link to nextNode from the top
//cout <<" Setting the last node" << prevNode->val << " to " << nextNode->val << " \n";
prevNode->link = nextNode;
}
else
{
cout << "Index out of bounds \n";
return false;
}
}
template <typename T>
T List<T>::get(int idx) const
{
Node *curNode = head;
int i;
if (idx < count)
{
for (i = 0; (i < idx) & (curNode != nullptr); i++)
{
curNode = curNode->link;
}
if ((i == idx) && (curNode != nullptr))
return curNode->val;
else
return NULL;
}
else
{
cout << "GET:: Index out of range \n";
return NULL;
}
}
template <typename T>
void List<T>::printList() const
{
Node *curNode = this->head;
cout << " List : ( ";
for (int i = 0; (i < this->count) & (curNode->link != nullptr); i++)
{
cout << curNode->val << ", ";
curNode = curNode->link;
}
cout << curNode->val << " ) \n";
}
int main()
{
List<int> ll;
set<int> s1 = {3, 4, 5, 6, 9, 10, 12, 15};
set<int> s2 = {351, 45};
set<int> s3 = {450, 345};
ll.add(0, 25);
cout << "Size : " << ll.size() << " ; ";
ll.printList();
ll.add(0, 35);
cout << "Size : " << ll.size() << " ; ";
ll.printList();
cout << "Trying to insert add(2,390) \n";
ll.add(2, 390);
cout << "Size : " << ll.size() << " ; ";
ll.printList();
ll.addALL(1, s2);
cout << "Size : " << ll.size() << " ; ";
ll.printList();
ll.addALL(3, s1);
cout << "Size : " << ll.size() << " ; ";
ll.printList();
cout << "Tyring to insert addAll(12,s3) \n";
ll.addALL(12, s3);
cout << "Size : " << ll.size() << " ; ";
ll.printList();
}