Question

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

LinkedList.cpp

/*******************************

* Week 2 lesson: *

* a simple LinkedList class *

*******************************/

#include <iostream>

#include "LinkedList.h"

using namespace std;

/*

* Initializes the list to empty creating a dummy header node.

*/

LinkedList::LinkedList()

{

first = new Node;

first->next = NULL;

}

/*

* Destructor. Deallocates all the nodes of the linked list,

* including the header node.

*/

LinkedList::~LinkedList()

{

Node *temp;

while (first != NULL)

{

temp=first;

first=first->next;

delete temp;

}

}

/*

* Determines whether the list is empty.

*

* Returns true if the list is empty, false otherwise.

*/

bool LinkedList::isEmpty()

{

return first->next == NULL;

}

/*

* Prints the list elements.

*/

void LinkedList::display()

{

Node * current = first->next;

while(current != NULL)

{

cout << current->info << " ";

current = current->next;

}

cout << endl;

}

/*

* Adds the element x to the beginning of the list.

*

* x: element to be added to the list.

*/

void LinkedList::add(int x)

{

Node *p = new Node;

p->info = x;

p->next = first->next;

first->next = p;

}

/*

* Removes the first occurrence of x from the list. If x is not found,

* the list remains unchanged.

*

* x: element to be removed from the list.

*/

void LinkedList::remove(int x)

{

Node * old = first->next,

* p = first;

//Finding the address of the node before the one to be deleted

bool found = false;

while (old != NULL && !found)

{

if (old->info == x) found = true;

else

{

p = old;

old = p->next;

}

}

//if x is in the list, remove it.

if (found)

{

p->next = old->next;

delete old;

}

}

main.cpp

/*******************************

* Week 2 lesson: *

* a simple LinkedList class *

*******************************/

#include <iostream>

#include "LinkedList.h"

using namespace std;

int main()

{

LinkedList myList;

int x;

//Add 5 random numbers to list

for (int i=0; i < 5; i++)

myList.add(rand()%20);

cout << "1 - Display the list elements" << endl;

cout << "2 - Is it empty?" << endl;

cout << "3 - Add element" << endl;

cout << "4 - Delete element" << endl;

cout << "5 - Exit" << endl;

int option;

//Loop to test the LinkedList class methods

do

{

cout << endl << "Enter your choice: ";

cin >> option;

switch(option)

{

case 1:

cout << "List elements: ";

myList.display();

break;

case 2:

if (myList.isEmpty()) cout << "List is empty"<< endl;

else cout << "List is not empty" << endl;

break;

case 3:

cout << "Enter an element to add at the beginning of the list: ";

cin >> x;

myList.add(x);

break;

case 4:

cout << "Enter an element to delete from the list: ";

cin >> x;

myList.remove(x);

break;

case 5:

cout << "All done!" << endl;

break;

default: cout << "Invalid choice!" << endl;

}

} while (option != 5);

return 0;

}

LinkedList.h

/*******************************

* Week 2 lesson: *

* a simple LinkedList class *

*******************************/

/*

* Linked list node.

*/

struct Node

{

int info; //element stored in this node

Node *next; //link to next node

};

/*

* Class implementing a linked list.

*/

class LinkedList

{

public:

LinkedList();

~LinkedList();

bool isEmpty();

void display();

void add(int);

void remove(int);

private:

Node *first; //pointer to header (dummy) node

};

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

PLEASE REFER BELOW CODE

1) LinkedList.h

#include<iostream>

using namespace std;

//Node for doubly linked list

struct Node

{

int value;

Node *N,*P;

Node(int y)

{

value = y;

N=P=NULL;

}

};

//DoublyLinkedList class

class DoublyLinkedList

{

Node *front;

Node *back;

public:

DoublyLinkedList(); //constructor to initialize list

~DoublyLinkedList(); //destructor to delete list

void addEnd(int x);

void displayInReverse(); //display node in revesre

void destroyList(); //delete linked list

};

2) LInkedList.cpp

#include "LinkedList.h"

DoublyLinkedList::DoublyLinkedList()

{

//initialize both pointer to NULL

front = NULL;

back = NULL;

}

void DoublyLinkedList::addEnd(int x)

{

//creating node

Node *n = new Node(x);

//if back is NULL then update both front and back

if( back == NULL)

{

front = n;

back = n;

}

else //add at back

{

back->N = n;

n->P = back;

back = n;

}

}

void DoublyLinkedList::displayInReverse()

{

Node *temp = back; //initialize temp pointer to back

cout << "\n\nNodes in reverse order :" << endl;

while(temp != NULL)

{

cout << temp->value << " " ;

temp = temp->P;

}

}

void DoublyLinkedList::destroyList()

{

Node *T = back; //delete from back

while(T != NULL)

{

Node *T2 = T;

T = T->P;

delete T2;

}

front = NULL;

back = NULL;

}

DoublyLinkedList::~DoublyLinkedList()

{

destroyList();

}

3) main.cpp

#include"LinkedList.h"

int main()

{

DoublyLinkedList myList;

int x;

cout << "1 - Display the list elements" << endl;

cout << "2 - Add element at end" << endl;

cout << "3 - Exit" << endl;

int option;

//Loop to test the DoublyLinkedList class methods

do

{

cout << endl << "Enter your choice: ";

cin >> option;

switch(option)

{

case 1:

myList.displayInReverse();

break;

case 2:

cout << "Enter an element to add at the beginning of the list: ";

cin >> x;

myList.addEnd(x);

break;

case 3:

cout << "All done!" << endl;

break;

default:

cout << "Invalid choice!" << endl;

}

}while (option != 3);

return 0;

}

PLEASE REFER BELOW OUTPUT

khushal@khushal-ubuntu:~/Desktop/Chegg/Doubly_linked_list$ g++ -o main main.cpp LinkedList.cpp
khushal@khushal-ubuntu:~/Desktop/Chegg/Doubly_linked_list$ ./main
1 - Display the list elements
2 - Add element at end
3 - Exit

Enter your choice: 2
Enter an element to add at the beginning of the list: 23

Enter your choice: 2
Enter an element to add at the beginning of the list: 45

Enter your choice: 2
Enter an element to add at the beginning of the list: 89

Enter your choice: 2
Enter an element to add at the beginning of the list: 78

Enter your choice: 1


Nodes in reverse order :
78 89 45 23
Enter your choice: 3
All done!
khushal@khushal-ubuntu:~/Desktop/Chegg/Doubly_linked_list$

Add a comment
Know the answer?
Add Answer to:
Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...
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++ - 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...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

  • Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how...

    Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how to get size. I'm not sure. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } /* Function...

  • c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream>...

    c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node {     T data;//data field     Node * next;//link field     Node(T data) {       this->data = data;     } }; template<class T> class linked_list{ private:       Node<T> *head,*current; public:       linked_list(){//constructor, empty linked list         head = NULL;         current = NULL;       }       ~linked_list(){         current = head;         while(current != NULL) {          ...

  • CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C...

    CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C2010Lab11A Add the following to the project. //LinkedList.cpp #include <cstdlib> #include "LinkedList.h" using namespace std; //--------------------------------------------------- //List Element Members //--------------------------------------------------- ListElement::ListElement(int d, ListElement * n) {    datum=d;    next=n; } int ListElement::getDatum () const {    return datum; } ListElement const* ListElement::getNext () const {    return next; } //--------------------------------------------------- //LinkedList Members //--------------------------------------------------- LinkedList::LinkedList () {    head=NULL; } void LinkedList::insertItem(int item) {    ListElement *currPtr = head;    ListElement *prevPtr =...

  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

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

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

  • Introduction In this lab, you are supposed to implement a graph class with the data structure...

    Introduction In this lab, you are supposed to implement a graph class with the data structure implemented before like linked list and queue. graph The class graph contains three member variables: linkedList *adjacentVertices; //an array of linked list. For a vertice i, adjacentVertices[i] stores the linked list that contains all other vertices connected to vertice i. int numVertices; //The number of vertices in the graph. int maxNumVertices; //The maximum number of vertices the graph can hold. Following public methods are...

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