Question

In: Computer Science

Please only edit the list.cpp file only, implement the push_front method that will insert a new...

Please only edit the list.cpp file only, implement the push_front method that will insert a new element to the front of the list.

//list.h

// Doubly linked list
#ifndef Q2_H
#define Q2_H

template<typename T> class List;
template<typename T> class Iterator;

template <typename T>
class Node {
   public:
       Node(T element);
   private:
       T data;
       Node* previous;
       Node* next;
   friend class List<T>;
   friend class Iterator<T>;
};

template <typename T>
class List {
   public:
       List(); // Constructor
       ~List(); // Destructor
       void push_front(T element); // Inserts to front of list
       Iterator<T> begin(); // Point to beginning of list
       Iterator<T> end(); // Point to past end of list
   private:
       Node<T>* head;
       Node<T>* tail;
   friend class Iterator<T>;
};


template <typename T>
class Iterator {
   public:
       Iterator();
       T get() const; // Get value pointed to by iterator
       void next(); // Advance iterator forward
       void previous(); // Advance iterator backward
       bool equals(Iterator<T> other) const; // Compare values pointed to by two iterators
   private:
       Node<T>* position; // Node pointed to by iterator
       List<T>* container; // List the iterator is used to iterate
   friend class List<T>;
};

#endif

//list.cpp

// Implement the push_front method

#include <string>
#include "q2.h"

using namespace std;

// Node class implemenation

template <typename T>
Node<T>::Node(T element) { // Constructor
   data = element;
   previous = nullptr;
   next = nullptr;
}

// List implementation

template <typename T>
List<T>::List() {
   head = nullptr;
   tail = nullptr;
}


template <typename T>
List<T>::~List() { // Destructor
   for(Node<T>* n = head; n != nullptr; n = n->next) {
  
       delete n; //Deconstructor create an error evertime i ran it.
   }

}

template <typename T>
void List<T>::push_front(T element) {
   // Your code here
}


template <typename T>
Iterator<T> List<T>::begin() {
   Iterator<T> iter;
   iter.position = head;
   iter.container = this;
   return iter;
}

template <typename T>
Iterator<T> List<T>::end() {
   Iterator<T> iter;
   iter.position = nullptr;
   iter.container = this;
   return iter;
}

// Iterator implementation

template <typename T>
Iterator<T>::Iterator() {
   position = nullptr;
   container = nullptr;
}


template <typename T>
T Iterator<T>::get() const {
   return position->data;
}

template <typename T>
void Iterator<T>::next() {
   position = position->next;
}

template <typename T>
void Iterator<T>::previous() {
   if (position == nullptr) {
       position = container->tail;
   } else {
       position = position->previous;
   }
}

template <typename T>
bool Iterator<T>::equals(Iterator<T> other) const {
return position == other.position;
}

list_test.cpp

// Test for push_front method
#include <iostream>
#include "q2.h"
#include "q2.cpp"
using namespace std;

int main() {
   List<string> stack;
   stack.push_front("My");
   stack.push_front("name");
   stack.push_front("is");
   stack.push_front("Ozymandias");
   stack.push_front("king");
   stack.push_front("of");
   stack.push_front("kings");

   // Should print - kings of king Ozymandias is name My
   for (auto p = stack.begin(); !p.equals(stack.end()); p.next())
       cout << p.get() << " ";
   cout << endl;

}

Solutions

Expert Solution

The required code followed by the screenshot of output has been added below, please note that the modification is in list.cpp file only so I have given only that file not all. Also to run your code, replace q2 with list in remaining files as q2 do not exist here to include. The explanation of the modified code has been added in front of it in comment lines.

CODE:

//list.cpp

// Implement the push_front method

#include <string>
#include "list.h"

using namespace std;

// Node class implemenation

template <typename T>
Node<T>::Node(T element) { // Constructor
   data = element;
   previous = nullptr;
   next = nullptr;
}

// List implementation

template <typename T>
List<T>::List() {
   head = nullptr;
   tail = nullptr;
}


template <typename T>
List<T>::~List() { // Destructor
   for(Node<T>* n = head; n != nullptr; n = n->next) {

       delete n; //Deconstructor create an error evertime i ran it.
   }

}

template <typename T>
void List<T>::push_front(T element) {
    Node<T>* newnode = new Node<T>(element);    // made a new node

    Iterator<T> iterhead;                       // made an iterator for head
    iterhead.position = head;                   // set its position to head

    Iterator<T> iternew;                        // made an iterator for new
    iternew.position = newnode;                 // set its position to new

    iternew.position->next = head;              // set the next of new node to head
    if (head == nullptr)                        // if head is null then add the newnode to tail
        tail = newnode;
    else                                        // else add to the previous of head
        iterhead.position->previous = newnode;

    head = newnode;                             // in last made the new node to head.
}


template <typename T>
Iterator<T> List<T>::begin() {
   Iterator<T> iter;
   iter.position = head;
   iter.container = this;
   return iter;
}

template <typename T>
Iterator<T> List<T>::end() {
   Iterator<T> iter;
   iter.position = nullptr;
   iter.container = this;
   return iter;
}

// Iterator implementation

template <typename T>
Iterator<T>::Iterator() {
   position = nullptr;
   container = nullptr;
}


template <typename T>
T Iterator<T>::get() const {
   return position->data;
}

template <typename T>
void Iterator<T>::next() {
   position = position->next;
}

template <typename T>
void Iterator<T>::previous() {
   if (position == nullptr) {
       position = container->tail;
   } else {
       position = position->previous;
   }
}

template <typename T>
bool Iterator<T>::equals(Iterator<T> other) const {
return position == other.position;
}

OUTPUT:

NOTE: In case of any query, you can mention it in the comment section. HAPPY LEARNING!!


Related Solutions

Please fill in the code where it says to implement Methods needed to implement include: insert,...
Please fill in the code where it says to implement Methods needed to implement include: insert, find, remove, and toIndex // // STRINGTABLE.JAVA // A hash table mapping Strings to their positions in the the pattern sequence // You get to fill in the methods for this part. // public class StringTable {    private LinkedList<Record>[] buckets; private int nBuckets; // // number of records currently stored in table -- // must be maintained by all operations // public int...
JAVA Write a class to implement a list with the ability to insert a new item...
JAVA Write a class to implement a list with the ability to insert a new item at any location within the list, remove an item, and search for an item. The list should use a linked list implementation. This implementation should make use of a dummy node at the head of the list and should have explict references head and previous. Add a method to the list that inserts elements in acsending order assuming the list is already sorted before...
Please edit the code with linked lists in C++ Please provide your BookData.txt file as well...
Please edit the code with linked lists in C++ Please provide your BookData.txt file as well BookRecord.cpp file #include "BookRecord.h" #include <stdio.h> #include <string.h> #include <iostream> #include <fstream> using namespace std; BookRecord::BookRecord() {    strcpy_s(m_sName, "");    m_lStockNum = 0;    m_iClassification = 0;    m_dCost = 0.0;    m_iCount = 0; } BookRecord::BookRecord(const char* name, long sn, int cl, double cost) {    strcpy_s(m_sName, name);    m_lStockNum = sn;    m_iClassification = cl;    m_dCost = cost;    m_iCount...
C programming - Implement a dynamic array, please read and implement the dynarray.c file with what...
C programming - Implement a dynamic array, please read and implement the dynarray.c file with what is given dynarray.h /* * This file contains the definition of the interface for the dynamic array * you'll implement. You can find descriptions of the dynamic array functions, * including their parameters and their return values, in dynarray.c. You * should not modify anything in this file. */ #ifndef __DYNARRAY_H #define __DYNARRAY_H /* * Structure used to represent a dynamic array. */ struct...
In java please create a method that will insert a value after a specific node for...
In java please create a method that will insert a value after a specific node for a linked list. public void insertAfter(Node a, int newData){ }
In Java please: 1) Part 1: Edit the getTileList method (add length method too) Dear Developer,...
In Java please: 1) Part 1: Edit the getTileList method (add length method too) Dear Developer, It seems an overzealous programmer tried to create a Fibonacci slider puzzle from our old code. This brought up the fact there is a data integrity issue in our SlidingSquarePuzzle class. It makes sense because the class’s data only consists of an int[]. Since, you are new this is a good opportunity to get your feet wet. I want you to change the offending...
C programming assignment. instructions are given below and please edit this code only. also include screenshot...
C programming assignment. instructions are given below and please edit this code only. also include screenshot of the output //In this assignment, we write code to convert decimal integers into hexadecimal numbers //We pratice using arrays in this assignment #include <stdio.h> #include <stdlib.h> #include <assert.h> //convert the decimal integer d to hexadecimal, the result is stored in hex[] void dec_hex(int d, char hex[]) {    char digits[] ={'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',   ...
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
Answer the following problems in an Excel file. Please upload only one Excel file with all...
Answer the following problems in an Excel file. Please upload only one Excel file with all of your answers, including #3 (which requires an explanation rather than a calculation). All problems must be solved using the PV and FV functions in Excel. If I deposit $8,000 in a bank account that pays interest of 1.5%, compounded annually, how much will I have in the account after 10 years? If I deposit $8,000 in a bank account that pays simple interest...
7. Use the substitution & method of INSERT command to populate EMP_PROJ table. INSERT INTO EMP_PROJ...
7. Use the substitution & method of INSERT command to populate EMP_PROJ table. INSERT INTO EMP_PROJ VALUES (‘&empNo’, ‘&projNo’, &hoursWorked); NOTE: enclose &empNo in ‘ ‘ if the datatype is a string – VARCHAR2 or CHAR If empNo is NUMBER datatype then do not enclose &empNo in ‘ ‘! empNo projNo hoursWorked 1000 30 32.5 1000 50 7.5 2002 10 40.0 1444 20 20.0 1760 10 5.0 1760 20 10.0 1740 50 15.0 2060 40 12.0
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT