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

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

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

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

  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

  • Add the following method to xxxxxp3: // deletes x from sorted list k if exist, k...

    Add the following method to xxxxxp3: // deletes x from sorted list k if exist, k = 0, 1, 2 private void delete(x, k) // returns position of x in sorted list k if exist otherwise, -1. k = 0, 1, 2 private int search(x, k) import java.util.Scanner; class xxxxxp3{ private node[] head = new node[3]; private class node{ int num; node link; node(int x){ num=x; link = null; } } public void prtlst(int k){ System.out.printf("\nContents of List-%d:",k); for(node cur...

  • Can anyone help me to complete this C plus plus program? Please notice: You are required...

    Can anyone help me to complete this C plus plus program? Please notice: You are required to implement insertion sort by using smart pointer. Please notice: You are required to implement insertion sort by using smart pointer. Please notice: You are required to implement insertion sort by using smart pointer. Please notice: You are required to implement insertion sort by using smart pointer. Please notice: You are required to implement insertion sort by using smart pointer. //Implment insertion sort using...

  • CSCI-2467 Lab 11 – Refactor LinkedList Application to use Generics Background The code consists of three...

    CSCI-2467 Lab 11 – Refactor LinkedList Application to use Generics Background The code consists of three files that implement and use a simple linked list. The code was written in early Java-style using the Object class in order to allow the linked list to be a list of objects of any type. While the code works, it is not type-safe. Refactor the code to use Java Generics. You will need to change the Main class to create a linked list...

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