template <class T>
struct nodeType
{
T data;
nodeType *next;
};
template <class T>
class LinkedList {
nodeType<T> *head, *tail;
int count;
public:
LinkedList();
void insertFirst(T item);
void insertLast(T item);
void insertAt(T item, int pos);
void print();
bool simplecompare(LinkedList<T> &rL)
{
if (count != rL.count)
return false;
nodeType<T> *cur, *rCur;
for (cur = head, rCur = rL.head; cur != NULL; cur = cur->next, rCur = rCur->next)
if (cur->data != rCur->data)
return false;
return true;
}
int compare(LinkedList<T> &rL);
~LinkedList();
};
//driver program:
int main(void)
{
char item;
LinkedList<char> list1, list2;
cout << "Please enter the first list of characters"
<< "nPress Ctrl+Z to stop entering." << endl;
cin >> item;
while (!cin.eof()) {
list1.insertLast(item);
cin >> item;
}
cin.clear();
cout << "Please enter the second list of characters"
<< "nPress Ctrl+Z to stop entering." << endl;
cin >> item;
while (!cin.eof()) {
list2.insertLast(item);
cin >> item;
}
list1.print();
list2.print();
int result = list1.compare(list2);
if (result == 0)
cout << "The lists are same!n";
else if (result == -1)
cout << "first list < second listn" << endl;
else cout << "first list > second listn" << endl;
return 0;
}
1 Questions A simplified doublyLinkedList class is provided in lab03doublyLinkedList_template.h
file on CATS. Solve the following questions by using that elass. 1. The definition of insertFirst(T
item) function is missing. Complete it. 2. The definition of insertLast(T item) function is missing.
Complete it. 3. The definition of insertAt(T item, int order) function is missing. Complete it. 4.
Update print() function to print the list in forward direction. 5. The definition of deleteItem(T item)
function is missing. Complete it. 6. Add an extractMax(T max) function to the doublyLinkedList
class. The function must extract all nodes containing elements less than max from the list, and
construct a new list using those nodes. After the function call, the original list must only have the
nodes whose data fields are greater than max, while the new list is containing the smaller ones.
The new list must be returned by the function. You're not allowed to delete and/or create new
nodes. Your function must just move the subjected nodes from one list to the other. Let the
following list be the original list: If the function is called as newList = myList.extractMax (30), then
the newList becomes: 2 Assignment Implement a class to represent a circular linked list. You can
directly adapt SinglyLinkedList class that we've already defined in linkedList.h. Only the following
member functions will be defined for the class: - default constructor to set the pointer data
members to NULL and count to 0 . - insertItem to insert a given item to the list. - deleteItem to
remove a specific item from the list. Item may occur multiple times, thus the function must remove
every occurrences of the item from the list. 1 - print to print the linked list from head to tail. .
1. template <class T>
struct nodeType
{
T data;
nodeType *next;
};
template <class T>
class LinkedList {
nodeType<T> *head, *tail;
int count;
public:
LinkedList();
void insertFirst(T item);
void insertLast(T item);
void insertAt(T item, int pos);
void print();
bool simplecompare(LinkedList<T> &rL)
{
if (count != rL.count)
return false;
nodeType<T> *cur, *rCur;
for (cur = head, rCur = rL.head; cur != NULL; cur = cur->next, rCur = rCur->next)
if (cur->data != rCur->data)
return false;
return true;
}
int compare(LinkedList<T> &rL);
~LinkedList();
};
//driver program:
int main(void)
{
char item;
LinkedList<char> list1, list2;
cout << "Please enter the first list of characters"
<< "nPress Ctrl+Z to stop entering." << endl;
cin >> item;
while (!cin.eof()) {
list1.insertLast(item);
cin >> item;
}
cin.clear();
2. cout << "Please enter the second list of characters"
<< "nPress Ctrl+Z to stop entering." << endl;
cin >> item;
while (!cin.eof()) {
list2.insertLast(item);
cin >> item;
}
list1.print();
list2.print();
int result = list1.compare(list2);
if (result == 0)
cout << "The lists are same!n";
else if (result == -1)
cout << "first list < second listn" << endl;
else cout << "first list > second listn" << endl;
return 0;
}
1 Questions A simplified doublyLinkedList class is provided in lab03doublyLinkedList_template.h
file on CATS. Solve the following questions by using that elass. 1. The definition of insertFirst(T
item) function is missing. Complete it. 2. The definition of insertLast(T item) function is missing.
Complete it. 3. The definition of insertAt(T item, int order) function is missing. Complete it. 4.
Update print() function to print the list in forward direction. 5. The definition of deleteItem(T item)
function is missing. Complete it. 6. Add an extractMax(T max) function to the doublyLinkedList
class. The function must extract all nodes containing elements less than max from the list, and
construct a new list using those nodes. After the function call, the original list must only have the
nodes whose data fields are greater than max, while the new list is containing the smaller ones.
The new list must be returned by the function. You're not allowed to delete and/or create new
nodes. Your function must just move the subjected nodes from one list to the other. Let the
following list be the original list: If the function is called as newList = myList.extractMax (30), then
the newList becomes: 2 Assignment Implement a class to represent a circular linked list. You can
directly adapt SinglyLinkedList class that we've already defined in linkedList.h. Only the following
member functions will be defined for the class: - default constructor to set the pointer data
members to NULL and count to 0 . - insertItem to insert a given item to the list. - deleteItem to
remove a specific item from the list. Item may occur multiple times, thus the function must remove
every occurrences of the item from the list. 1 - print to print the linked list from head to tail. -
destructor to destroy the list.- print to print the linked list from head to tail. - destructor to destroy
the list. Use your circular linked list to solve hot potato game, which is a modern-day equivalent of
Josephus problem (See wiki). It is set as a game as follows: N people, numbered 1 to N, are
sitting in a circle. Starting at person 1, a hot potato is passed. After M passes, the person holding
the hot potato is eliminated, the circle closes ranks, and the game continues with the person who
was sitting after the eliminated person picking up the hot potato. The last remaining person wins.
Thus, if M=0 and N=5, players are eliminated in order, and player 5 wins. If M=1 and N=5, thee
3. order of elimination is 2, 4, 1, 5 . Write a non-member function with the following prototype: int
JosephusSolver (CircularLinkedList int >& players, int m ) ; to solve the Josephus problem for
general values of M and N. Try to make your program as efficient as possible. Make sure you
dispose of cells. What is the running time of your program?