Objectives
Familiarize the student with:
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:
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:
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;
}
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.
Objectives Familiarize the student with: implementing data structures in C++; dynamic allocation of C++ objects. Scenario...
C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...
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...
Fill the code in list.cpp app.cpp: #include "list.hpp" int main() { LinkedList aList; bool success = false; aList.evensFrontOddsEnd(3); aList.evensFrontOddsEnd(10); aList.evensFrontOddsEnd(6); aList.evensFrontOddsEnd(9); aList.evensFrontOddsEnd(17); aList.evensFrontOddsEnd(12); aList.printForward(); // output should be: 12 6 10 3 9 17 aList.printBackward(); // output should be: 17 9 3 10 6 12 success = aList.remove(17); if(success) cout << "17 Successfully Removed." << endl; success = aList.remove(10); if(success) cout << "10 Successfully Removed." << endl; success = aList.remove(7); // 7 is not in the list, so success should...
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 (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...
C++: I need implement this code using Double Linked List using the cosiderations 1. head point to null in an empty list 2. There is not need of a tail pointer /*This class implements the singly linked list using templates Each list has two attributes: -head: first node in the list -tail: last node in the list #include "circDLLNode.h" template class { public: //Default constructor: creates an empty list (); //Destructor: deallocate memory ~(); ...
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). 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...
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). /* definition of the list node class */ class Node { friend class LinkedList; private: int value; Node *pNext; public: /* Constructors with No Arguments...
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...