Question

Objectives Familiarize the student with: implementing data structures in C++; dynamic allocation of C++ objects. Scenario...

Objectives

Familiarize the student with:

  • implementing data structures in C++;
  • dynamic allocation of C++ objects.

Scenario

There are some classic data structures in computer science. One example of this that you've seen in the lectures is called the stack.

In the next few exercises, we'll build yet another classic data structure, the linked list.

To be exact, we'll be building the singly linked list.

The building block of a singly linked list is a node, which consists of two parts:

  • a value;
  • a link to the next node in the list, also known as the "next" pointer.

The beginning of a list is called the head and it's a pointer to the first node in the list.

When the list is empty, the head usually points to nothing, i.e. the nullptr.

Similarly, if a node is the last node in the list, the "next" pointer of that node will point to nullptr.

Let's build a list of integers.

Our initial implementation of the list should have two methods:

  • push_front, which will add a value to the front of the list;
  • pop_front, which will return and remove the value.

4
3
2
1

Please add to the code below

// An empty list:
//
// Node*
// +------+
// | head |-->nullptr
// +------+
//
//
//
// A list with two elements:
//
// Node* Node Node
// +------+ +-----+ +-----+
// | head |-->|value| +-->|value|
// +------+ +-----+ | +-----+
// |next |--+ |next |-->nullptr
// +-----+ +-----+
//
#include <iostream>

using namespace std;

class Node
{
public:
Node(int val);
int value;
Node* next;
};

Node::Node(int val) : value(val), next(nullptr)
{
}

class List
{
public:
List();
void push_front(int value);
bool pop_front(int &value);
private:
Node* head;
};

List::List() : head(nullptr)
{
}

void List::push_front(int value)
{
Node* new_head = new Node(value);
new_head->next = head;
head=new_head;
}

// START
// +------+ +-----+ +-----+
// | head |-->| X | +-->| Y |
// +------+ +-----+ | +-----+
// |next |--+ |next |-->nullptr
// +-----+ +-----+
//
// STEP 1
//
// +------+
// |popped|
// +------+
// |
// V
// +------+ +-----+ +-----+
// | head |-->| X | +-->| Y |
// +------+ +-----+ | +-----+
// |next |--+ |next |-->nullptr
// +-----+ +-----+
//
// STEP 2
// +------+
// | head |-------------------+
// +------+ |
// V
// +------+ +-----+ +-----+
// |popped|-->| X | +-->| Y |
// +------+ +-----+ | +-----+
// |next |--+ |next |-->nullptr
// +-----+ +-----+
//
// STEP 3
// returned = popped->value;
// delete popped;
// +------+ +-----+
// | head |-->| Y |
// +------+ +-----+
// |next |-->nullptr
// +-----+

bool List::pop_front(int &value)
{
// implement the pop
// don't forget to delete the popped node!
// and fix the return value
return false;
}

int main()
{
List list;
//
list.push_front(1);
list.push_front(2);
list.push_front(3);
list.push_front(4);

int value = 0;
while (list.pop_front(value))
{
cout << value << endl;
}
return 0;
}

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

Code

#include <iostream>

using namespace std;

class Node
{
public:
   Node(int val);
   int value;
   Node* next;
};

Node::Node(int val) : value(val), next(nullptr)
{
}

class List
{
public:
   List();
   void push_front(int value);
   bool pop_front(int &value);
private:
   Node * head;
};

List::List() : head(nullptr)
{
}

void List::push_front(int value)
{
   Node* new_head = new Node(value);
   new_head->next = head;
   head = new_head;
}

// START
// +------+ +-----+ +-----+
// | head |-->| X | +-->| Y |
// +------+ +-----+ | +-----+
// |next |--+ |next |-->nullptr
// +-----+ +-----+
//
// STEP 1
//
// +------+
// |popped|
// +------+
// |
// V
// +------+ +-----+ +-----+
// | head |-->| X | +-->| Y |
// +------+ +-----+ | +-----+
// |next |--+ |next |-->nullptr
// +-----+ +-----+
//
// STEP 2
// +------+
// | head |-------------------+
// +------+ |
// V
// +------+ +-----+ +-----+
// |popped|-->| X | +-->| Y |
// +------+ +-----+ | +-----+
// |next |--+ |next |-->nullptr
// +-----+ +-----+
//
// STEP 3
// returned = popped->value;
// delete popped;
// +------+ +-----+
// | head |-->| Y |
// +------+ +-----+
// |next |-->nullptr
// +-----+

bool List::pop_front(int &value)
{
   // implement the pop
   // don't forget to delete the popped node!
   // and fix the return value
   if (head == NULL)
       return false;
   Node *temp = head;
   value = temp->value;
   head= temp->next;
   delete temp;
   return true;
}

int main()
{
   List list;
   //
   list.push_front(1);
   list.push_front(2);
   list.push_front(3);
   list.push_front(4);

   int value = 0;
   while (list.pop_front(value))
   {
       cout << value << endl;
   }
   return 0;
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Objectives Familiarize the student with: implementing data structures in C++; dynamic allocation of C++ objects. Scenario...
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
  • C++ program, inventory.cpp implementation Mostly need the int load(istream&) function. Implementation: You are supp...

    C++ program, inventory.cpp implementation Mostly need the int load(istream&) function. Implementation: You are supposed to write three classes, called Item, Node and Inventory respectively Item is a plain data class with item id, name, price and quantity information accompanied by getters and setters Node is a plain linked list node class with Item pointer and next pointer (with getters/setters) Inventory is an inventory database class that provides basic linked list operations, delete load from file / formatted print functionalities. The...

  • 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...

  • (C++) You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class...

    (C++) You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided). The function will first search through the list for the provided key, and then, recursively, change all previous values in the list to instead be their distance from the node containing the key value. Do not update the node containing the key value or any nodes after it. If the key does not exist in the list, each node should contain its distance...

  • 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;...

  • You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided)....

    You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided). The function will first search through the list for the provided key, and then, recursively, change all previous values in the list to instead be their distance from the node containing the key value. Do not update the node containing the key value or any nodes after it. If the key does not exist in the list, each node should contain its distance from...

  • 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...

  • 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...

  • 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...

  • C++ Compsci 165: For your twelfth programming assignment you will be implementing a program that uses a linked list.You...

    C++ Compsci 165: For your twelfth programming assignment you will be implementing a program that uses a linked list.You will be implementing the following structure and functions: struct LinkedList { int value; LinkedList *next; }; Note that with a singly linked list, you need to maintain a head pointer (pointer to the beginning of the list). Typically a tail pointer (pointer to the end of the list) is not maintained in a singly linked list (because you can only iterate...

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

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