Question

In: Computer Science

In C++ In this lab we will creating two linked list classes: one that is a...

In C++

In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) .

These LinkedList classes should both be generic classes. and should contain the following methods:

  • Print

  • Add - Adds element to the end of the linked list.

  • IsEmpty

  • Push - Adds element to the beginning of the linked list

  • InsertAt - Inserts an element at a given position

  • Clear - Removes all elements from the linked list

  • Contains - Returns true if element is in the linked list

  • Get - Returns a value at a specific position

  • IndexOf - Returns the first position where an element occurs, -1 if not

  • LastOf - Returns the last position where an element occurs, -1 if not

  • Remove - Removes the last item added to the list

  • RemoveAt - Removes an element at a specific position

  • RemoveElement - Removes the first occurrence of a specific element

  • Size

  • Slice - Returns a subset of this linked list given a beginning position start and end position stop.

You will also count the number of operations that is performed in each method and will calculate the RUN-TIME of each method, as well as calculating the BIG O, OMEGA and THETA notation of each method. This information should be written in a comment block before each method ( you may count the number of operations on each line if you want ).

You are required to write self commenting code for this Lab, refer to the Google Style Sheet if you haven't become familiar with it.

Solutions

Expert Solution

Answer:

Singly and Doubly linked list classes:

//Initially, declare the class NODE:
class Node
{
public:
 int data;
 Node *next;
};
class SLinkedList
{
private:
 Node *first;
public:
 SLinkedList(){first=NULL;}
 SLinkedList(int A[],int n);
 ~SLinkedList();

 void Display();
 void Insert(int index,int x);
 int Delete(int index);
 int Length();
};
SLinkedList::SLinkedList(int A[],int n)
{
 Node *last,*t;
 int i=0;

 first=new Node;
 first->data=A[0];
 first->next=NULL;
 last=first;
 
 for(i=1;i<n;i++)
 {
 t=new Node;
 t->data=A[i];
 t->next=NULL;
 last->next=t;
 last=t;
 }
}
SLinkedList::~SLinkedList()
{
 Node *p=first;
 while(first)
 {
 first=first->next;
 delete p;
 p=first;
 }
}
void SLinkedList::Display()
{
 Node *p=first;

 while(p)
 {
 cout<<p->data<<" ";
 p=p->next;
 }
 cout<<endl;
}
int SLinkedList::Length()
{
 Node *p=first;
 int len=0;

 while(p)
 {
 len++;
 p=p->next;
 }
 return len;
}
void SLinkedList::Insert(int index,int x)
{
 Node *t,*p=first;

 if(index <0 || index > Length())
 return;
 t=new Node;
 t->data=x;
 t->next=NULL;

 if(index==0)
 {
 t->next=first;
 first=t;
 }
 else
 {
 for(int i=0;i<index-1;i++)
 p=p->next;
 t->next=p->next;
 p->next=t;
 }
}
int SLinkedList::Delete(int index)
{
 Node *p,*q=NULL;
 int x=-1;

 if(index < 1 || index > Length())
 return -1;
 if(index==1)
 {
 p=first;
 first=first->next;
 x=p->data;
 delete p;
 }
 else
 {
 p=first;
 for(int i=0;i<index-1;i++)
 {
 q=p;
 p=p->next;
 }
 q->next=p->next;
 x=p->data;
 delete p;
 }
 return x;
}

DOUBLY LINKED LIST CLASS:

//Here, we make 2 pointers pointing to the previous and next node.
class Node{
public:
    Node* prev;
    int data;
    Node* next;
};
 
class DoublyLinkedList{
private:
    Node* head;
public:
    DoublyLinkedList();
    DoublyLinkedList(int A[], int n);
    ~DoublyLinkedList();
    void Display();
    int Length();
    void Insert(int index, int x);
    int Delete(int index);
    void Reverse();
};
 
 
DoublyLinkedList::DoublyLinkedList() {
    head = new Node;
    head->prev = nullptr;
    head->data = 0;
    head->next = nullptr;
}
 
DoublyLinkedList::DoublyLinkedList(int *A, int n) {
 
    head = new Node;
    head->prev = nullptr;
    head->data = A[0];
    head->next = nullptr;
    Node* tail = head;
 
    for (int i=1; i<n; i++){
        Node* t = new Node;
        t->prev = tail;
        t->data = A[i];
        t->next = tail->next; // tail->next is pointing to NULL
        tail->next = t;
        tail = t;
    }
}
 
void DoublyLinkedList::Display() {
    Node* p = head;
    while (p != nullptr){
        cout << p->data << flush;
        p = p->next;
        if (p != nullptr){
            cout << " <-> " << flush;
        }
    }
    cout << endl;
}
 
int DoublyLinkedList::Length() {
    int length = 0;
    Node* p = head;
    while (p != nullptr){
        length++;
        p = p->next;
    }
    return length;
}
 
void DoublyLinkedList::Insert(int index, int x) {
 
    if (index < 0 || index > Length()){
        return;
    }
 
    Node* p = head;
    Node* t = new Node;
    t->data = x;
 
    if (index == 0){
        t->prev = nullptr;
        t->next = head;
        head->prev = t;
        head = t;
    } else {
        for (int i=0; i<index-1; i++){
            p = p->next;
        }
        t->prev = p;
        t->next = p->next;
        if (p->next){
            p->next->prev = t;
        }
        p->next = t;
    }
}
 
int DoublyLinkedList::Delete(int index) {
    int x = -1;
    Node* p = head;
 
    if (index < 0 || index > Length()){
        return x;
    }
 
    if (index == 1){
        head = head->next;
        if (head){
            head->prev = nullptr;
        }
        x = p->data;
        delete p;
    } else {
        for (int i=0; i<index-1; i++){
            p = p->next;
        }
        p->prev->next = p->next;
        if (p->next){
            p->next->prev = p->prev;
        }
        x = p->data;
        delete p;
    }
    return x;
}
 
void DoublyLinkedList::Reverse() {
    Node* p = head;
    Node* temp;
    while (p != nullptr){
        temp = p->next;
        p->next = p->prev;
        p->prev = temp;
        p = p->prev;
 
        // Need to check the following condition again
        if (p->next == nullptr){
            p->next = p->prev;
            p->prev = nullptr;
            head = p;
            break;
        }
    }
}
 
DoublyLinkedList::~DoublyLinkedList() {
    Node* p = head;
    while (head){
        head = head->next;
        delete p;
        p = head;
    }
}
 
 

Note: The answers are provided by an expert based on his previous expertise and knowledge. This content is plagiarism free, feel free to cross verify.

And kindly upvote for the efforts that we make every single day to answer the questions

I wish you all the best for your future endeavours.

And if you need any clarification, feel free to ask


Related Solutions

In C++ please: In this lab we will creating two linked list classes: one that is...
In C++ please: In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list....
In Java In this lab we will creating two linked list classes: one that is a...
In Java In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of songs/artist pairs. Allow your user to add song / artist pairs to the list, remove songs (and associated artist) from the list and be sure to also write a function to print the list! Have fun! Make sure you show your implementation of the use of vectors in this lab (You can use them too ) You MUST modularize your code ( meaning, there...
In C++ In this lab we will be creating a stack class and a queue class,...
In C++ In this lab we will be creating a stack class and a queue class, both with a hybrid method combining linked list and arrays in addition to the Stack methods(push, pop, peek, isEmpty, size, print) and Queue methods (enqueue, deque, peek, isEmpty, size, print). DO NOT USE ANY LIBRARY, implement each method from scratch. Both the Stack and Queue classes should be generic classes. Don't forget to comment your code.
In this lab, we will build a couple of classes, where one will be composed into...
In this lab, we will build a couple of classes, where one will be composed into the other. Work in pairs if you wish. Problem We saw Point.java in class last week. Do the following, one step at a time, making sure it works before you go on to the next step. Be sure to get help if you have any difficulties. You may use Point.java from last week or build a new one of your own. If you are...
C++ coding functions Implement the following class using linked lists. Creating a simple linked list class...
C++ coding functions Implement the following class using linked lists. Creating a simple linked list class to use is probably your first step.(Please do not use collections like .push() . pop(), etc.) and instead create the implementation A templated stack class that stores type T with the following public functions: - void Push(T t) - adds t to the top of the stack - T Pop() - asserts that the stack is not empty then removes and returns the item...
Hi, i'm creating a c++ program with a linked list of Employees, i'm running into issues...
Hi, i'm creating a c++ program with a linked list of Employees, i'm running into issues as far as displaying programmers only portion and the average salary of all the employees. Please make any changes or comments ! Employee.h file: #ifndef EMPLOYEE_H #define EMPLOYEE_H #include using namespace std; enum Role {programmer, manager, director}; struct Employee { std::string firstName; std::string lastName; int SSN; std::string department; double salary; Role role; }; #endif #ifndef NODE_H #define NODE_H Node.h file: #include "Employee.h" typedef Employee...
LAUNGEG: C# Objective: For this assignment you will be creating two classes, an interface, and a...
LAUNGEG: C# Objective: For this assignment you will be creating two classes, an interface, and a driver program: Class Calculator will implement the interface CalcOps o As such it will implement hexToDec() - a method to convert from Hexadecimal to Decimal. Class HexCalc will inherit from Calculator. Interface CalcOps will have abstract methods for add( ), subtract( ), multiply( ) and divide( ). Both classes will implement the CalcOps interface. o Class Calculator’s add, subtract, multiply and divide will take...
Linked List: Complete the following code to create a linked list from an Array. After creating...
Linked List: Complete the following code to create a linked list from an Array. After creating the list, display the elements of the linked list iteratively. Write two others function called as RDisplayTailRecursion(first) and RDisplayTailRecursion(first) which will print elements of the linked list using the tail and head recursions respectively. #include <stdio.h> #include <stdlib.h> struct Node { }*first=NULL; void create(int A[], int n) { for(i=1; i<n; i++) { } } void Display(struct Node*p) { while(p!=NULL) { } } void RDisplayTailRecursion...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT