Question

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

/* definition of the list node class */
class Node
{
friend class LinkedList;
private:
int value;
Node *pNext;
public:
/* Constructors with No Arguments */
Node(void)
: pNext(NULL)
{ }
/* Constructors with a given value */
Node(int val)
: value(val), pNext(NULL)
{ }
  
/* Constructors with a given value and a link of the next node */
Node(int val, Node* next)
: value(val), pNext(next)
{}   
/* Getters */
int getValue(void)
{ return value; }
Node* getNext(void)
{ return pNext; }
};

/* definition of the linked list class */
class LinkedList
{
private:
/* pointer of head node */
Node *pHead;
/* pointer of tail node */
Node *pTail;
public:
/* Constructors with No Arguments */
LinkedList(void);
/* Constructors with a given value of a list node */
LinkedList(int val);
/* Destructor */
~LinkedList(void);
  
/* Traversing the list and printing the value of each node */
void traverse_and_print();
  
   void push_back(int val);
};

LinkedList::LinkedList()
{
/* Initialize the head and tail node */
pHead = pTail = NULL;
}

LinkedList::LinkedList(int val)
{
/* Create a new node, acting as both the head and tail node */
pHead = new Node(val);
pTail = pHead;
}

LinkedList::~LinkedList()
{
}

void LinkedList::traverse_and_print()
{
Node *p = pHead;

/* The list is empty? */
if (pHead == NULL) {
cout << "The list is empty" << endl;
return;
}
  
cout << "LinkedList: ";
/* A basic way of traversing a linked list */
while (p != NULL) { /* while there are some more nodes left */
/* output the value */
cout << p->value;
/* The pointer moves along to the next one */
p = p->pNext;
}
cout << endl;
}

void LinkedList::push_pack(int val){
   /*Your code here*/
  
}

int main(int argc, const char * argv[])
{
/* Create an empty list */
LinkedList list1;
cout << "Created an empty list named list1." << endl;
/* output the result */
cout << "list1:" << endl;
list1.traverse_and_print();
  
/* Create a list with only one node */
LinkedList list2(10);
cout << "Created a list named list2 with only one node." << endl;
/* output the result */
cout << "list2:" << endl;
list2.traverse_and_print();
  
   /*your testing code here*/
   for (int i = 0 ; i < 10; i++){
       list2.push_back(i);
   }
   list2.traverse_and_print();
return 0;
}

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

1.

#include<iostream>
using namespace std;
class Node
{
friend class LinkedList;
private:
int value;
Node *pNext;
public:
/* Constructors with No Arguments */
Node(void)
: pNext(NULL)
{ }
/* Constructors with a given value */
Node(int val)
: value(val), pNext(NULL)
{ }

/* Constructors with a given value and a link of the next node */
Node(int val, Node* next)
: value(val), pNext(next)
{}
/* Getters */
int getValue(void)
{ return value; }
Node* getNext(void)
{ return pNext; }
};

/* definition of the linked list class */
class LinkedList
{
private:
/* pointer of head node */
Node *pHead;
/* pointer of tail node */
Node *pTail;
public:
/* Constructors with No Arguments */
LinkedList(void);
/* Constructors with a given value of a list node */
LinkedList(int val);
/* Destructor */
~LinkedList(void);

/* Traversing the list and printing the value of each node */
void traverse_and_print();

void push_back(int val);
};

LinkedList::LinkedList()
{
/* Initialize the head and tail node */
pHead = pTail = NULL;
}

LinkedList::LinkedList(int val)
{
/* Create a new node, acting as both the head and tail node */
pHead = new Node(val);
pTail = pHead;
}

LinkedList::~LinkedList()
{
}

void LinkedList::traverse_and_print()
{
Node *p = pHead;

/* The list is empty? */
if (pHead == NULL) {
cout << "The list is empty" << endl;
return;
}

cout << "LinkedList: ";
/* A basic way of traversing a linked list */
while (p != NULL) { /* while there are some more nodes left */
/* output the value */
cout << p->value<<" ";
/* The pointer moves along to the next one */
p = p->pNext;
}
cout << endl;
}

void LinkedList::push_back(int val){
/*Your code here*/
Node* newnode = new Node(val);
Node* p=pHead;
while (!(p->pNext==NULL))
p=p->pNext;
p->pNext = newnode;
newnode->pNext = NULL;
}

int main(int argc, const char * argv[])
{
/* Create an empty list */
LinkedList list1;
cout << "Created an empty list named list1." << endl;
/* output the result */
cout << "list1:" << endl;
list1.traverse_and_print();

/* Create a list with only one node */
LinkedList list2(10);
cout << "Created a list named list2 with only one node." << endl;
/* output the result */
cout << "list2:" << endl;
list2.traverse_and_print();

/*your testing code here*/
for (int i = 0 ; i < 10; i++){
list2.push_back(i);
}
list2.traverse_and_print();
return 0;
}

2. For doubly linked list we will have to create another Node pointer in the Class Node that points towards the previous Node.

I made the neccessary changes in functions and constructors:

#include<iostream>
using namespace std;
class Node
{
friend class LinkedList;
private:
int value;
Node *pPrev;
Node *pNext;
public:
/* Constructors with No Arguments */
Node(void)
: pNext(NULL), pPrev(NULL)
{ }
/* Constructors with a given value */
Node(int val)
: value(val), pNext(NULL), pPrev(NULL)
{ }

/* Constructors with a given value and a link of the next node */
Node(int val, Node* next)
: value(val), pNext(next), pPrev(NULL)
{}

/* Constructors with a given value and a link of the next and previous node */
Node(int val, Node* next, Node* prev)
: value(val), pNext(next), pPrev(prev)
{ }

/* Getters */
int getValue(void)
{ return value; }
Node* getNext(void)
{ return pNext; }
Node* getPrev(void)
{ return pPrev; }
};

/* definition of the linked list class */
class LinkedList
{
private:
/* pointer of head node */
Node *pHead;
/* pointer of tail node */
Node *pTail;
public:
/* Constructors with No Arguments */
LinkedList(void);
/* Constructors with a given value of a list node */
LinkedList(int val);
/* Destructor */
~LinkedList(void);

/* Traversing the list and printing the value of each node */
void traverse_and_print();

void push_back(int val);
};

LinkedList::LinkedList()
{
/* Initialize the head and tail node */
pHead = pTail = NULL;
}

LinkedList::LinkedList(int val)
{
/* Create a new node, acting as both the head and tail node */
pHead = new Node(val);
pTail = pHead;
}

LinkedList::~LinkedList()
{
}

void LinkedList::traverse_and_print()
{
Node *p = pHead;

/* The list is empty? */
if (pHead == NULL) {
cout << "The list is empty" << endl;
return;
}

cout << "LinkedList: ";
/* A basic way of traversing a linked list */
while (p != NULL) { /* while there are some more nodes left */
/* output the value */
cout << p->value<<" ";
/* The pointer moves along to the next one */
p = p->pNext;
}
cout << endl;
}

void LinkedList::push_back(int val){
/*Your code here*/
Node* newnode = new Node(val);
Node* p=pHead;
while (!(p->pNext== NULL))
p=p->pNext;
p->pNext = newnode;
newnode->pPrev=p;
newnode->pNext = NULL;
}

int main(int argc, const char * argv[])
{
/* Create an empty list */
LinkedList list1;
cout << "Created a doubly linked empty list named list1." << endl;
/* output the result */
cout << "list1:" << endl;
list1.traverse_and_print();

/* Create a list with only one node */
LinkedList list2(10);
cout << "Created a doubly linked list named list2 with only one node." << endl;
/* output the result */
cout << "list2:" << endl;
list2.traverse_and_print();

/*your testing code here*/
for (int i = 0 ; i < 10; i++){
list2.push_back(i);
}
list2.traverse_and_print();
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write...
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
  • 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()...

  • C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public:...

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

  • Linkedlist implementation in C++ The below code I have written is almost done, I only need...

    Linkedlist implementation in C++ The below code I have written is almost done, I only need help to write the definition for delete_last() functio​n. ​ Language C++ // LinkedList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> using namespace std; struct Node { int dataItem;//Our link list stores integers Node *next;//this is a Node pointer that will be areference to next node in the list }; class LinkedList { private: Node *first;...

  • C++: I need implement this code using Double Linked List using the cosiderations 1. head point...

    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    ~();  ...

  • Fill the code in list.cpp app.cpp: #include "list.hpp" int main() {         LinkedList aList; bool success =...

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

  • You need to complete a function called insertList. Given list A, list B and an integer...

    You need to complete a function called insertList. Given list A, list B and an integer n, insertList inserts list B into the middle of list A, after the list value n. NOTE: 1) All positions and indices start from 0. 2) Write your code in the empty function of insertList, DO NOT change anything in the main. Example Assume we are starting with two lists: List A is: 2->3->9->11 List B is: 5->6->8 Insert after node value: 3 Resulting...

  • C++ - I have a doubly linked list, but I haven't been able to get the...

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

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • Your task is to complete the following function/functions: 1. Given a position in the linked list,...

    Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...

  • I'm having trouble getting this program to compile and run. It is in C++ Language and...

    I'm having trouble getting this program to compile and run. It is in C++ Language and being run with CodeBlocks. Problem Statement: create and manage a linked list. Program will loop displaying a menu of user operations concerning the management of a linked list. Included will be:       H create link at head       R remove link at head       T create link at tail       K remove link at tail       I remove link at ID          S search...

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