In: Computer Science
Please solve in C++ only
class is not templated
You need to write a class called LinkedList that implements the following List operations:
public void add(int index, Object item);
// adds an item to the list at the given index, that index may be
at start, end or after or before the
// specific element
2.public void remove(int index);
// removes the item from the list that has the given index
3.public void remove(Object item);
// finds the item from list and removes that item from the list
4.public List duplicate();
// creates a duplicate of the list
// postcondition: returns a copy of the linked list
5.public List duplicateReversed();
// creates a duplicate of the list with the nodes in reverse order
// postcondition: returns a copy of the linked list with the nodes in
6.public List ReverseDisplay();
//print list in reverse order
7.public Delete_Smallest();
// Delete smallest element from linked list
8.public List Delete_duplicate();
// Delete duplicate elements from a given linked list.Retain the earliest entries.
9 Make a function that adds a linked list to itself at the end.
Input:
4 -> 2 -> 1 -> NULL
Output:
4 -> 2 -> 1 -> 4 -> 2 -> 1 -> NULL
note : code should work on Visual studio 2017 and provide screenshot of output
C++ code is:
#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
};
class linked_list
{
private:
node *head;
int size;
public:
linked_list()
{
head = NULL;
// tail = NULL;
size=0;
}
void add(int index,int item) {
if(index < 0 or index>size) cout << "Invalid position!" << endl;
else {
node* tmp = new node();
tmp->data = item;
tmp->next = NULL;
if(index==0){
tmp->next = head;
head = tmp;
}else{
node* curr = head;
int i=0;
while(i<index-1){
curr=curr->next;
i++;
}
tmp->next = curr->next;
curr->next = tmp;
}
size++;
}
}
void remove(int index){
if(head==nullptr)return;
node* curr = head;
if(index==0){
head = head->next;
free(curr);
return;
}
for (int i=0; curr!=NULL && i<index-1; i++)
curr = curr->next;
if (curr == NULL || curr->next == NULL)
return;
node *next = curr->next->next;
free(curr->next);
curr->next = next;
}
//HERE I have used int as item data type, since there is sim with above method, do change the data type of int to delete by index
void removeItem(int item){
node* curr = head;
if(head->data==item){
head = head->next;
free(curr);
return;
}else{
while(curr->next->data!=item)curr=curr->next;
node* f = curr->next;
curr->next = f->next;
free(f);
return;
}
}
linked_list duplicate(){
linked_list newlinklist ;
node* curr = head;
int i=0;
while(curr->next!=nullptr){
newlinklist.add(i++,curr->data);
curr=curr->next;
}
newlinklist.add(i++,curr->data);
return newlinklist;
}
linked_list duplicateReversed(){
linked_list newlinklist ;
node* curr = head;
while(curr->next!=nullptr){
newlinklist.add(0,curr->data);
curr=curr->next;
}
newlinklist.add(0,curr->data);
return newlinklist;
}
void ReverseDisplay(){
node* current = head;
node *prev = NULL, *next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
println();
}
void Delete_Smallest(){
int x = head->data;
node* curr = head;
while(curr!=nullptr){
if(curr->data<x)x=curr->data;
curr=curr->next;
}
removeItem(x);
}
void Delete_duplicate(){
node *ptr1, *ptr2, *dup;
ptr1 = head;
while (ptr1 != NULL && ptr1->next != NULL)
{
ptr2 = ptr1;
while (ptr2->next != NULL)
{
if (ptr1->data == ptr2->next->data)
{
dup = ptr2->next;
ptr2->next = ptr2->next->next;
delete(dup);
}
else
ptr2 = ptr2->next;
}
ptr1 = ptr1->next;
}
}
void add_linkedlist(){
linked_list samelink = duplicate();
node* curr = head;
while(curr->next!=nullptr)curr=curr->next;
curr->next = samelink.head;
}
void println(){
node* curr = head;
while(curr!=nullptr){
cout<<curr->data<<" --> ";
curr=curr->next;
}
cout<<endl;
}
};
int main(){
linked_list l;
l.add(0,12);
l.add(1,24);
l.add(2,45);
l.add(2,32);
l.add(4,55);
l.add(3,11);
l.add(3,45);
l.println();
l.remove(0);
l.println();
l.removeItem(11);
l.println();
linked_list news;
news = l.duplicate();
news.println();
// l.duplicate().println();
linked_list rev_news = news.duplicateReversed();
rev_news.println();
rev_news.Delete_Smallest();
rev_news.println();
rev_news.Delete_duplicate();
rev_news.println();
return 0;
}
Output: