Question

Language: C++ Complete this function 1.Object &raw_front() { // Return the element at the front of...

Language: C++

Complete this function

1.Object &raw_front() { // Return the element at the front of the list *without* using the iterator classes // (You may assume the list is not empty) // Place your code here.

Code:

#ifndef LIST_H
#define LIST_H

#include 

using namespace std;

template
class List {
private:
  // The basic doubly linked list node.
  // Nested inside of List, can be public
  // because the Node is itself private
  struct Node {
    Object data;
    Node *prev;
    Node *next;

    Node(const Object &d = Object{}, Node *p = nullptr, Node *n = nullptr)
        : data{d}, prev{p}, next{n} {}

    Node(Object &&d, Node *p = nullptr, Node *n = nullptr)
        : data{std::move(d)}, prev{p}, next{n} {}
  };

public:
  class const_iterator {
  public:

    // Public constructor for const_iterator.
    const_iterator() : current{nullptr} {}

    // Return the object stored at the current position.
    // For const_iterator, this is an accessor with a
    // const reference return type.

    const Object &operator*() const { return retrieve(); }

    const_iterator &operator++() {
      current = current->next;
      return *this;
    }

    const_iterator operator++(int) {
      const_iterator old = *this;
      ++(*this);
      return old;
    }

    const_iterator &operator--() {
      current = current->prev;
      return *this;
    }

    const_iterator operator--(int) {
      const_iterator old = *this;
      --(*this);
      return old;
    }

    bool operator==(const const_iterator &rhs) const { return current == rhs.current; }

    bool operator!=(const const_iterator &rhs) const { return !(*this == rhs); }

  protected:
    Node *current;

    // Protected helper in const_iterator that returns the object
    // stored at the current position. Can be called by all
    // three versions of operator* without any type conversions.
    Object &retrieve() const { return current->data; }

    // Protected constructor for const_iterator.
    // Expects a pointer that represents the current position.
    const_iterator(Node *p) : current{p} {}

    friend class List;
  };

  class iterator : public const_iterator {
  public:

    // Public constructor for iterator.
    // Calls the base-class constructor.
    // Must be provided because the private constructor
    // is written; otherwise zero-parameter constructor
    // would be disabled.
    iterator() {}

    Object &operator*() { return const_iterator::retrieve(); }

    // Return the object stored at the current position.
    // For iterator, there is an accessor with a
    // const reference return type and a mutator with
    // a reference return type. The accessor is shown first.
    const Object &operator*() const { return const_iterator::operator*(); }

    iterator &operator++() {
      this->current = this->current->next;
      return *this;
    }

    iterator operator++(int) {
      iterator old = *this;
      ++(*this);
      return old;
    }

    iterator &operator--() {
      this->current = this->current->prev;
      return *this;
    }

    iterator operator--(int) {
      iterator old = *this;
      --(*this);
      return old;
    }

  protected:
    // Protected constructor for iterator.
    // Expects the current position.
    iterator(Node *p) : const_iterator{p} {}

    friend class List;
  };

public:
  List() { init(); }

  ~List() {
    // Place your code here.
      clear( );
      delete head;
      delete tail;
  }

  List(const List &rhs) {
    init();
    for (auto &x : rhs)
      push_back(x);
  }

  List &operator=(const List &rhs) {
    List copy = rhs;
    std::swap(*this, copy);
    return *this;
  }


  List(List &&rhs) : theSize{rhs.theSize}, head{rhs.head}, tail{rhs.tail} {
    rhs.theSize = 0;
    rhs.head = nullptr;
    rhs.tail = nullptr;
  }

  List &operator=(List &&rhs) {
    std::swap(theSize, rhs.theSize);
    std::swap(head, rhs.head);
    std::swap(tail, rhs.tail);

    return *this;
  }

  // Return iterator representing beginning of list.
  // Mutator version is first, then accessor version.
  iterator begin() { return iterator(head->next); }

  const_iterator begin() const { return const_iterator(head->next); }

  // Return iterator representing endmarker of list.
  // Mutator version is first, then accessor version.
  iterator end() { return iterator(tail); }

  const_iterator end() const { return const_iterator(tail); }

  // Return number of elements currently in the list.
  int size() const { return theSize; }

  // Return true if the list is empty, false otherwise.
  bool empty() const { return size() == 0; }

  void clear() {
    while (!empty())
      pop_front();
  }

  // front, back, push_front, push_back, pop_front, and pop_back
  // are the basic double-ended queue operations.
  Object &front() { return *begin(); }

  Object &raw_front() {
    // Return the element at the front of the list *without* using the iterator classes
    // (You may assume the list is not empty)

      Node *p = new Node(, nullptr, head);
      if(tail == nullptr)
          tail = p;
      head = p;
      ++theSize;

    // (Yes, this is bad code. I just needed something that will allow the stub to compile.)

  }

  const Object &front() const { return *begin(); }

  Object &back() { return *--end(); }

  Object &raw_back() {
    // Return the element at the end of the list *without* using the iterator classes
    // (You may assume the list is not empty)

    // Place your code here.  

    // (Yes, this is bad code. I just needed something that will allow the stub to compile.)

  }

  const Object &back() const { return *--end(); }

  void push_front(const Object &x) { insert(begin(), x); }

  void push_back(const Object &x) { insert(end(), x); }

  void raw_push_front(const Object &x) {
    // insert x at the head of the list *without* using the iterator classes
    // Place your code here.
      Node *p = new Node(x, nullptr, head);
      if(tail == nullptr)
          tail = p;
      head = p;
      ++theSize;
  }

  void raw_push_back(const Object &x) {
    // insert x at the tail of the list *without* using the iterator classes
    // Place your code here.
      Node *p = new Node(x, tail, nullptr);
      if(head == nullptr)
          head = p;
      tail = p;
      ++theSize;
  }


  void raw_insert_in_order(const Object &x) {
    // insert x into the sorted list *without* using the iterator classes.
    // You may assume the list is either empty or correctly sorted.

    // Place your code here.  
  }

  void push_front(Object &&x) { insert(begin(), std::move(x)); }

  void push_back(Object &&x) { insert(end(), std::move(x)); }

  void pop_front() { erase(begin()); }

  void pop_back() { erase(--end()); }

  // Insert x before itr.
  iterator insert(iterator itr, const Object &x) {
    Node *p = itr.current;
    ++theSize;
    return iterator(p->prev = p->prev->next = new Node{x, p->prev, p});
  }

  // Insert x before itr.
  iterator insert(iterator itr, Object &&x) {
    Node *p = itr.current;
    ++theSize;
    return iterator(p->prev = p->prev->next = new Node{std::move(x), p->prev, p});
  }

  // Erase item at itr.
  iterator erase(iterator itr) {
    Node *p = itr.current;
    iterator retVal(p->next);
    p->prev->next = p->next;
    p->next->prev = p->prev;
    delete p;
    --theSize;

    return retVal;
  }

  iterator erase(iterator from, iterator to) {
    for (iterator itr = from; itr != to;)
      itr = erase(itr);

    return to;
  }

  void splice(iterator position, List &lst ) {
    // Removes all the items from lst, add them prior to position in *this.
    // You may assume lst and *this are different lists.
    // **Your routine must run in constant time.**


      lst.begin().current->prev=position.current->prev;
      position.current->prev->next=lst.begin();
      position.current->prev=lst.end();
      lst.end().current->next=position;
      lst.head->next=lst.tail;
      lst.tail->prev=lst.head;

    //  if (lst.theSize == 0) return;
     // if (position.curr->prev) hook(position.curr->prev, lst.first);
     // else first = lst.first;
    //  hook(lst.last->prev, position.curr);
    //  lst.first = lst.last = nullptr;
     // theSize += lst.theSize;
     // lst.theSize = 0;

  }

private:
  int theSize;
  Node *head;
  Node *tail;

  void init() {
    theSize = 0;
    head = new Node;
    tail = new Node;
    head->next = tail;
    tail->prev = head;
  }
};

#endif
0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have completed the raw_front() function. Also the implementations for raw_push_back() and raw_push_front() had to be corrected. The code you had for those functions was correct only if you did not have dummy head and tail nodes for your list. But I notice you have dummy nodes. So I have corrected the code for raw_push_back() and raw_push_front() as well. Hope it helps.

#ifndef LIST_H
#define LIST_H

#include

using namespace std;

template
class List {
private:
// The basic doubly linked list node.
// Nested inside of List, can be public
// because the Node is itself private
struct Node {
Object data;
Node *prev;
Node *next;

Node(const Object &d = Object{}, Node *p = nullptr, Node *n = nullptr)
: data{d}, prev{p}, next{n} {}

Node(Object &&d, Node *p = nullptr, Node *n = nullptr)
: data{std::move(d)}, prev{p}, next{n} {}
};

public:
class const_iterator {
public:

// Public constructor for const_iterator.
const_iterator() : current{nullptr} {}

// Return the object stored at the current position.
// For const_iterator, this is an accessor with a
// const reference return type.

const Object &operator*() const { return retrieve(); }

const_iterator &operator++() {
current = current->next;
return *this;
}

const_iterator operator++(int) {
const_iterator old = *this;
++(*this);
return old;
}

const_iterator &operator--() {
current = current->prev;
return *this;
}

const_iterator operator--(int) {
const_iterator old = *this;
--(*this);
return old;
}

bool operator==(const const_iterator &rhs) const { return current == rhs.current; }

bool operator!=(const const_iterator &rhs) const { return !(*this == rhs); }

protected:
Node *current;

// Protected helper in const_iterator that returns the object
// stored at the current position. Can be called by all
// three versions of operator* without any type conversions.
Object &retrieve() const { return current->data; }

// Protected constructor for const_iterator.
// Expects a pointer that represents the current position.
const_iterator(Node *p) : current{p} {}

friend class List;
};

class iterator : public const_iterator {
public:

// Public constructor for iterator.
// Calls the base-class constructor.
// Must be provided because the private constructor
// is written; otherwise zero-parameter constructor
// would be disabled.
iterator() {}

Object &operator*() { return const_iterator::retrieve(); }

// Return the object stored at the current position.
// For iterator, there is an accessor with a
// const reference return type and a mutator with
// a reference return type. The accessor is shown first.
const Object &operator*() const { return const_iterator::operator*(); }

iterator &operator++() {
this->current = this->current->next;
return *this;
}

iterator operator++(int) {
iterator old = *this;
++(*this);
return old;
}

iterator &operator--() {
this->current = this->current->prev;
return *this;
}

iterator operator--(int) {
iterator old = *this;
--(*this);
return old;
}

protected:
// Protected constructor for iterator.
// Expects the current position.
iterator(Node *p) : const_iterator{p} {}

friend class List;
};

public:
List() { init(); }

~List() {
// Place your code here.
clear( );
delete head;
delete tail;
}

List(const List &rhs) {
init();
for (auto &x : rhs)
push_back(x);
}

List &operator=(const List &rhs) {
List copy = rhs;
std::swap(*this, copy);
return *this;
}


List(List &&rhs) : theSize{rhs.theSize}, head{rhs.head}, tail{rhs.tail} {
rhs.theSize = 0;
rhs.head = nullptr;
rhs.tail = nullptr;
}

List &operator=(List &&rhs) {
std::swap(theSize, rhs.theSize);
std::swap(head, rhs.head);
std::swap(tail, rhs.tail);

return *this;
}

// Return iterator representing beginning of list.
// Mutator version is first, then accessor version.
iterator begin() { return iterator(head->next); }

const_iterator begin() const { return const_iterator(head->next); }

// Return iterator representing endmarker of list.
// Mutator version is first, then accessor version.
iterator end() { return iterator(tail); }

const_iterator end() const { return const_iterator(tail); }

// Return number of elements currently in the list.
int size() const { return theSize; }

// Return true if the list is empty, false otherwise.
bool empty() const { return size() == 0; }

void clear() {
while (!empty())
pop_front();
}

// front, back, push_front, push_back, pop_front, and pop_back
// are the basic double-ended queue operations.
Object &front() { return *begin(); }

Object &raw_front() {
// Return the element at the front of the list *without* using the iterator classes
// (You may assume the list is not empty)
return (head->next).data;
}

const Object &front() const { return *begin(); }

Object &back() { return *--end(); }

Object &raw_back() {
// Return the element at the end of the list *without* using the iterator classes
// (You may assume the list is not empty)

// Place your code here.

// (Yes, this is bad code. I just needed something that will allow the stub to compile.)

}

const Object &back() const { return *--end(); }

void push_front(const Object &x) { insert(begin(), x); }

void push_back(const Object &x) { insert(end(), x); }

void raw_push_front(const Object &x) {
// insert x at the head of the list *without* using the iterator classes
// Place your code here.
Node *p = new Node(x, head, head->next);
   head->next = p;
   p->next->prev = p;
++theSize;
}

void raw_push_back(const Object &x) {
// insert x at the tail of the list *without* using the iterator classes
// Place your code here.
Node *p = new Node(x, tail->prev, tail);
   tail->prev = p;
   p->prev->next = p;
++theSize;
}


void raw_insert_in_order(const Object &x) {
// insert x into the sorted list *without* using the iterator classes.
// You may assume the list is either empty or correctly sorted.

// Place your code here.
}

void push_front(Object &&x) { insert(begin(), std::move(x)); }

void push_back(Object &&x) { insert(end(), std::move(x)); }

void pop_front() { erase(begin()); }

void pop_back() { erase(--end()); }

// Insert x before itr.
iterator insert(iterator itr, const Object &x) {
Node *p = itr.current;
++theSize;
return iterator(p->prev = p->prev->next = new Node{x, p->prev, p});
}

// Insert x before itr.
iterator insert(iterator itr, Object &&x) {
Node *p = itr.current;
++theSize;
return iterator(p->prev = p->prev->next = new Node{std::move(x), p->prev, p});
}

// Erase item at itr.
iterator erase(iterator itr) {
Node *p = itr.current;
iterator retVal(p->next);
p->prev->next = p->next;
p->next->prev = p->prev;
delete p;
--theSize;

return retVal;
}

iterator erase(iterator from, iterator to) {
for (iterator itr = from; itr != to;)
itr = erase(itr);

return to;
}

void splice(iterator position, List &lst ) {
// Removes all the items from lst, add them prior to position in *this.
// You may assume lst and *this are different lists.
// **Your routine must run in constant time.**


lst.begin().current->prev=position.current->prev;
position.current->prev->next=lst.begin();
position.current->prev=lst.end();
lst.end().current->next=position;
lst.head->next=lst.tail;
lst.tail->prev=lst.head;

// if (lst.theSize == 0) return;
// if (position.curr->prev) hook(position.curr->prev, lst.first);
// else first = lst.first;
// hook(lst.last->prev, position.curr);
// lst.first = lst.last = nullptr;
// theSize += lst.theSize;
// lst.theSize = 0;

}

private:
int theSize;
Node *head;
Node *tail;

void init() {
theSize = 0;
head = new Node;
tail = new Node;
head->next = tail;
tail->prev = head;
}
};

#endif

Add a comment
Know the answer?
Add Answer to:
Language: C++ Complete this function 1.Object &raw_front() { // Return the element at the front of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. void raw_push_front(const Object &x) { // insert x at the head of the list *without*...

    1. void raw_push_front(const Object &x) { // insert x at the head of the list *without* using the iterator classes // Place your code here. } 2. void raw_push_back(const Object &x) { // insert x at the tail of the list *without* using the iterator classes // Place your code here. } #ifndef LIST_H #define LIST_H #include <algorithm> using namespace std; template<typename Object> class List { private: // The basic doubly linked list node. // Nested inside of List, can...

  • (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and...

    (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and is implemented with a header node and a tail node. // SortedLinkedList.h // SortedLinkedList.h // A collection of data are stored in the list by ascending order #ifndef SORTEDLIST_H #define SORTEDLIST_H using namespace std; template <typename T> class SortedList { private: // The basic single linked list node type. // Nested inside of SortedList. struct NodeType { T data; NodeType* next; NodeType* prev; NodeType(const...

  • I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding...

    I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding of how to implement copy constructors, destructors, and assignment operators is essential when working with data structures using dynamic memory allocation. Failure to implement these methods correctly can and probably will result in memory leaks. In this project, you are provided with a working implementation of a doubly-linked list in which the copy constructor, destructor, and assignment operator methods are not complete. To complete...

  • Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write...

    Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write an additional method called push_back(int) that will add an integer to the end of the list. You can modify the provided code. 2.Modify the Node class and LinkedList class so that you can access your parent node (double linked-list). #include #include using namespace std; typedef int Type; enum Boolean { False = 0, True }; class Item { friend class SLList; public: Type getVal()...

  • In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the...

    In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the following sort member function on a singly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); Implement the following sort member function on a doubly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); The sort(…) methods take as a parameter a comparator function, having a default assignment of defaultCompare, a static function defined as follows: template <typename T> static bool defaultCompare(const...

  • What is the specific answer for 1. and 2. Thanks! Add a new method, find, to...

    What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...

  • Template Dequeue Class (C++ ) In this assignment, we will use a given test menu for the template ...

    Template Dequeue Class (C++ ) In this assignment, we will use a given test menu for the template Deque Class (a Linked List based Double Ended Queue, Deque) that you have put together. Starter testDeque.cpp which contains: Timer class holder (you need to go through the LearnCpp Ch15 and import it in) Node class Deque class specification (you need to fill out the definition) here is the testDeque.cpp code: // C++ implementation of doubly linked list Deque doubly linked list...

  • Requirements Print a range Write a bag member function with two parameters. The two parameters are...

    Requirements Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be compared for equality using ==. Use the following header for the function: void print_value_range(const Item& x, const Item& y); print_value_range can be interpreted in a number of...

  • C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType va...

    C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType value;    Node *next; }; class LinkedList { private:    Node *head;    // You may add whatever private data members or private member functions you want to this class.    void printReverseRecursiveHelper(Node *temp) const; public:    // default constructor    LinkedList() : head(nullptr) { }    // copy constructor    LinkedList(const LinkedList& rhs);    // Destroys all the dynamically allocated memory    //...

  • // thanks for helping // C++ homework // The homework is to complete below in the...

    // thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT