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* tail;
};
List::List(){
head = nullptr;
tail = nullptr;
}
List::List(List &other){
head = nullptr;
tail = nullptr;
for(Node *n = other.head; n != nullptr; n = n->next)
push_back(n->value);
}
void List::push_front(int value){
Node *n = new Node(value);
n->next = head;
head = n;
if(tail == nullptr)
tail = n;
}
bool List::pop_front(int &value){
if(head != nullptr){
value = head->value;
Node *temp = head;
head = head->next;
if(head == nullptr)
tail = nullptr;
delete temp;
return true;
}
else
return false;
}
void List::push_back(int value){
Node *n = new Node(value);
n->next = nullptr;
if(tail == nullptr){
tail = n;
head = n;
}
else{
tail->next = n;
tail = n;
}
}
bool List::pop_back(int &value){
if(head == nullptr)
return false;
if(head == tail) //only one node?
{
value = tail->value;
delete head;
head = nullptr;
tail = nullptr;
}
else{
Node *prevToTail = head;
while(prevToTail->next != tail)
prevToTail = prevToTail->next;
prevToTail->next = nullptr;
delete tail;
tail = prevToTail;
}
}
int List::at(int index){
int i = 0;
Node *n;
for( n= head; n != nullptr && i < index; n =
n->next)
i++;
if(n == nullptr)
return -1;
else
return n->value;
}
void List::insert_at(int index, int value){
if(index == 0)
push_front(value);
else{
Node *prev = head;
Node *curr = head->next;
for(int i = 1; i < index && curr != nullptr; i++)
{
prev = curr;
curr = curr->next;
}
Node *n = new Node(value);
n->next = curr;
prev->next = n;
if(prev == tail) //update tail if it was last node
tail = n;
}
}
void List::remove_at(int index){
int value;
if(index == 0)
pop_front(value);
else{
Node *prev = head;
Node *curr = head->next;
for(int i = 1; i < index && curr != nullptr; i++)
{
prev = curr;
curr = curr->next;
}
if(curr == nullptr) //no such index
return;
prev->next = curr->next;
if(curr == tail) //deleted last node?
tail = prev;
delete curr;
}
}
int List::size(){
int s = 0;
for(Node *n = head; n != nullptr; n=n->next)
s++;
return s;
}
// ...
void printList(List &list)
{
for (int i = 0; i < list.size(); i++)
{
cout << "list[" << i << "] == " <<
list.at(i) << endl;
}
}
int main()
{
List list1;
list1.push_front(1);
list1.push_front(2);
list1.push_front(3);
list1.push_front(4);
cout << "list1" << endl;
printList(list1);
cout << endl;
List list2(list1);
cout << "list2" << endl;
printList(list2);
cout << endl;
list1.insert_at(1, 6);
list2.remove_at(2);
cout << "list1" << endl;
printList(list1);
cout << "list2" << endl;
printList(list2);
cout << endl;
return 0;
}
C++ Commented Code;-
#include <iostream>
using namespace std;
// A linked list Node
class Node
{
public:
Node(int val); //method which assign val to value
int value; // value is interger variable
Node* next; // next is a pointer which points to next node
};
// Node(int val) is method which assign value val to
variable value.
Node::Node(int val){
value = val;
}
// List formed using Node
class List
{
public:
List();
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:
Node* head;
Node* tail;
};
// List initially it assign head pointer to null and tail
point to null
List::List(){
head = nullptr;
tail = nullptr;
}
// List (List &other) it copy the data from one list to
other
List::List(List &other){
head = nullptr;
tail = nullptr;
for(Node *n = other.head; n != nullptr; n = n->next)
push_back(n->value);//insert the data to new list
}
// push_front(int value) this method add new node to the
List at First position
void List::push_front(int value){
Node *n = new Node(value);// n is new Node and assign assign value
to Node n.
n->next = head; // next Node of this new node become head.
head = n; // now update the head pointer.
if(tail == nullptr) // it there is n is the only Node in the
List
tail = n; // update tail pointer to n
}
// pop_front(int & value) this method checkes there is
first node is available or not
bool List::pop_front(int &value){
if(head != nullptr){ // if List is not empty
value = head->value; // assign value with head-value
Node *temp = head; // create temp node which points to head
head = head->next; // now update the head pointer to
head->next
if(head == nullptr)//
tail = nullptr;
delete temp; // then delete the temp node
return true; // return true if List is not empty means there is
first node in the list
}
else
return false; // if list is empty then return false
}
// push_back(int value) this method add the new node with value at
the end and update the tail pointer
void List::push_back(int value){
Node *n = new Node(value); // create new node n with value
n->next = nullptr;// assign n->next to null
if(tail == nullptr){ // if List is empty
tail = n; // then tail pinter points to node n
head = n; // then head pinter points to node n
}
else{ //if List is not empty
tail->next = n; //update tail->next
tail = n; // then update tail because n add at the end so tail node
is now n
}
}
// this method checks possibility to remove node from last
of List
bool List::pop_back(int &value){
if(head == nullptr)// if List is empty
return false; // means there is no node in the List so we cant
delete it.
if(head == tail) //only one node?
{
value = tail->value;
delete head;
head = nullptr; // after deletion of single node head points to
null
tail = nullptr; // after deletion of single node tail points to
null
}
else{
Node *prevToTail = head; // new node prevToTail assign to
head
while(prevToTail->next != tail) //find the second last node in
the list
prevToTail = prevToTail->next;
prevToTail->next = nullptr; // here second last node prevToTail
so assign prevToTail->next to null
delete tail; // delete the tail pointer
tail = prevToTail; //update tail pointer to prevToTail node
}
}
// at(index) method it return the value of node at
index.
int List::at(int index){
int i = 0;
Node *n; // create new node n
for( n= head; n != nullptr && i < index; n = n->next)
// assign n to head and find the node at index
i++;
if(n == nullptr) // if node n is null
return -1; // return -1 there is no node at this index
else
return n->value; // otherwise return node value at index
}
// insert_at(index, value) is method which add the new node
at perticular index
void List::insert_at(int index, int value){
if(index == 0)// if index is 0
push_front(value);// add node to the front of the list
else{
Node *prev = head; // create new Node prev assign to head.
Node *curr = head->next; //create new Node curr assign it to
head->next.
for(int i = 1; i < index && curr != nullptr; i++)// it
finds the node which is previous to the node at that index.
{
prev = curr;
curr = curr->next;
}
Node *n = new Node(value); // create new node n with value
n->next = curr; // it assign n->next to the node curr which
is the node at the index
prev->next = n; // now prev->next to n so n become the node
at index
if(prev == tail) //update tail if it was last node
tail = n;
}
}
// remove_at() this method remove node value from list at
perticular index
void List::remove_at(int index){
int value;
if(index == 0) //if index is 0 means delete the fist node from
list
pop_front(value);// it remove the first node
else{
Node *prev = head;
Node *curr = head->next;
for(int i = 1; i < index && curr != nullptr; i++)// it
finds the node which is previous to the node at index and node at
index
{
prev = curr; // when for loop end prev is previous node of the node
that we want to deleted
curr = curr->next; // and curr id the node that we want to
delete
}
if(curr == nullptr) //no such index
return;
prev->next = curr->next; // update the prev->next to the
curr->next, here curr is the node that we want to remove
if(curr == tail) //deleted last node?
tail = prev;
delete curr; // delete the node at index
}
}
// size() this method finds the size of the List
int List::size(){
int s = 0;
for(Node *n = head; n != nullptr; n=n->next)
s++;// calculate the number of nodes in the list
return s;
}
// printList function to print the List
void printList(List &list)
{
for (int i = 0; i < list.size(); i++)
{
cout << "list[" << i << "] == " <<
list.at(i) << endl;
}
}
int main()
{
List list1;
list1.push_front(1);
list1.push_front(2);
list1.push_front(3);
list1.push_front(4);
cout << "list1" << endl;
printList(list1);
cout << endl;
List list2(list1);
cout << "list2" << endl;
printList(list2);
cout << endl;
list1.insert_at(1, 6);// call insert_at()/ method
list2.remove_at(2);// call remove_at() method
cout << "list1" << endl;
printList(list1);
cout << "list2" << endl;
printList(list2);
cout << endl;
return 0;
}
C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public:...
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 ~(); ...
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...
In C++, change this code to a circular linked list #include <iostream> using namespace std; struct node { string data; node *next; }; class linked_list { private: node *head,*tail; public: linked_list() { head = NULL; tail = NULL; } void add_node(string n) { node *tmp = new node; tmp->data = n; tmp->next = NULL; if(head == NULL) { head = tmp; tail = tmp; } else { tail->next = tmp; tail = tail->next; } } void display() { while (tmp...
(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...
Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...
CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare a node datastruct struct Node { char c; Node *next; }; class LinkedString { Node *head; public: LinkedString(); LinkedString(char[]); LinkedString(string); char charAt(int) const; string concat(const LinkedString &obj) const; bool isEmpty() const; int length() const; LinkedString substring(int, int) const; //added helper function to add to linekd list private: void add(char c); };...
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:...
Hello, I have this code but its not running like it should:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class node
{
public:
typedef int data_t;
node *next;
data_t data;
node(data_t d) { next = NULL; data = d; }
};
class linked_list
{
private:
node *head;
public:
linked_list()
{
head = NULL;
}
int size()
{
node *tptr = head;
int c = 0;
while (tptr)
{
c++;
tptr = tptr->next;
}
return c;
}
void add(int...
C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...
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...