Question

In this project, you are asked to design and implement a class for double link list...

In this project, you are asked to design and implement a class for double link list to hold integers:

The class should be called DList, you also need a class called Node that contains an int as the data

Node needs the following data:

int data; //the data held by the node Node* parent; //the parent of the Node Node* child; //the child of the Node

Node needs the following public functions:
Node(int)// constructor to create a Node with the input int

int GetData(); // return the int held by the Node

DList needs a head and tail, both should be pointers to Node, and initialized to NULL

DList needs a default constructor (no parameter), a copy constructor (take const

DList needs the following public functions:

void PushFront(int a); //create a Node containing a //and add it to the front of the list

void PushEnd(int a); //create a Node containing a //and add it to the end of the list

Node* PopFront(); //popping out the first Node of the list, //if the list is empty, return NULL

void PopEnd(); //popping out the last Node of the list, //if the list is empty, return NULL

DList & as input)

void PrintListForward(); //print every element from start to end //in one line separated by a space

void PrintListReverse(); //print every element from end to start //in one line separated by a space

2 Files to turn in:

You need to turn in four files in a tar.gz file: makefile, main.C, DList.C, DList.h. Among them, you need to use the main.C provided which you cannot modify at all.

DList.h declares the class DList and Node. You can make DList the friend of Node so it can access the private data of the Node.

3 Functions:

All member function and operator prototypes are declared in xArray.h, with necessary comments before each function. The following are major functions:

PushBack function is the core of this project, and should be used by other functions if necessary (for example, it can be used by the += and >> operators). This function put the integer at the end of the array. Since the array may not have enough space to hold it, you need to grow the array if necessary.

[ ] operator should check the boundary of data, if idx is not within the boundary, print error message and exit the program; otherwise, return data[idx];

>> operator should read in an integer and put it at the end of the array.

main.C File

#include "DList.h"
#include 
using namespace std;

int main() {

    DList list;
    int i;

    //pust 10 numbers to the end
    for (i = 0; i < 10; i++) list.PushEnd(i);

    cout << "Print forward\n";
    list.PrintListForward();
    cout << "Print reverse\n";
    list.PrintListReverse();

    cout << "Pop front and print\n";
    //print the list from the beginning
    Node* n = list.PopFront();
    while ( n != NULL) {
        cout << n->GetData() << endl;
        n = list.PopFront();
    }


    //pust 10 numbers to the front
    for (i = 0; i < 10; i++) list.PushFront(i);

    //test copy constructor
    DList* list2 = new DList(list);
    
    cout << "Pop end and print\n";
    //print the list from the end
    n = list2->PopEnd();
    while ( n != NULL) {
        cout << n->GetData() << endl;
        n = list2->PopEnd();
    }

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

Answer: Note: No information about x^rrav,h . SoI couldnt solve the 3 question. File Name: DList.h //Header file declaratipo//Declare the myTailptr Node* myTailptr/ Access spec public: //default constructor DList ) //copy constructor DList (const Di//set the data to kk data^k //set the parent to nullptr arent=nu //set the child to nullptr //Method return the data int Node/luse loop to copy all the elements while (pp! //Get the current element tpp->child-new Node (pp->data) //Add İt to the this//Method inserts a at this list end void DList: PushEnd (int a) //Declare the tpp Node *tppi /Create Node with a tpp new Node//Check myHeadptr is nullptr myHeadPtr->parent //if myHeadetr is not nullpt -nullptr: else if (myHeadptr-nul1ptr) nu //returnwhile (tpp!nullptr) >data<< cout <<tpp- tpp5tpp->child; coutくくendl ; //Method prints this list from the end void DList: :PrFile Name: main.cpp #include PhistA #include <iostream> using namespace std; int main() DList list; int i; //pust 10 numberSample output: Print forward 0 1 2 3 4567 8 9 Print reverse 9 87 6 5 4 3 2 10 Pop front and print 2 Pop end and prin 2 Press

copyable code:

File Name: DList.h

//Header file declaratipon for Double link list.

#ifndef _DLIST-H

#define _DLIST_H

//forward declaration of DList

class DList;

//Declaration of Node class

class Node

{

     //Declare data for the Node

     int data;

     //Declare the parent pointer

     Node* parent;

     //Declare the child pointer

     Node* child;

//Access specifier

public:

     //constructor

     Node(int kk);

    

     //method return the value of data

     int GetData();

    

     //friend class declaration

     friend class DList;

};

//Declaration of DList class

class DList

{

//Access specifier

private:

     //Declare the myHeadPtr

     Node* myHeadPtr;

     //Declare the myTailPtr

     Node* myTailPtr;

//Access specifier.

public:

     //default constructor

     DList();

     //copy constructor

     DList(const DList &dlist);

     //Method inserts a at the list front

     void PushFront(int a);

     //Method inserts a at the list end

     void PushEnd(int a);

     //Method deletes an item from the begining

     Node* PopFront();

     //Method deletes an item from the end

     Node* PopEnd();

     //Method prints the list from the begining.

     void PrintListForward();

     //method prints the list from the end

     void PrintListReverse();

};

#endif

File Name: DList.cpp

//Include the needed header files

#include "DList.h"

#include <iostream>

#include <string>

#include <cstdlib>

//Use the namespace std

using namespace std;

//paremeterized constructor that sets the data to kk

Node::Node(int kk)

{

     //Set the data to kk

     data=kk;

     //Set the parent to nullptr

     parent=nullptr;

     //Set the child to nullptr

     child=nullptr;

}

//Method return the data

int Node::GetData()

{

     //return the data

     return data;

}

//Default constructor

DList::DList()

{

     //set the myHeadPtr to nullptr

     myHeadPtr=nullptr;

     //Set the myTailPtr to nullptr

     myTailPtr=nullptr;

}

//Copy constructor

DList::DList(const DList &dlist)

{

     //Declare myNewNode

     Node* myNewNode;

     //Get the first item from dlist

     myNewNode=new Node(dlist.myHeadPtr->data);

     //set the myHeadPtr and mytailPtr

     myHeadPtr=myNewNode;

     myTailPtr=myNewNode;

     //Declare tpp and ttp2 of Node type

     Node *tpp=myHeadPtr;

     Node *ttp2=myHeadPtr;

     //Declare pp

     Node *pp=dlist.myHeadPtr->child;

     //use loop to copy all the elements

     while(pp!=NULL)

     {

          //Get the current element

          tpp->child=new Node(pp->data);

          //Add it to the this list

          tpp->child->parent=tpp;

          //Set the child

          tpp=tpp->child;

          //Update the myTailPtr

          myTailPtr=tpp;

          //move to the next element

          pp=pp->child;

     }

}

//Method inserts a at the front of this list

void DList::PushFront(int a)

{

     //Declare tpp

     Node *tpp;

     //Create Node for a

     tpp=new Node(a);

     //Check this list is empty

     if(myHeadPtr==NULL && myTailPtr==NULL)

     {

          //Then update the myHeadPtr and myTailPtr

          myHeadPtr=tpp;

          myTailPtr=tpp;

     }

     //If this is not empty

     else

     {

          //Update the myHeadPtr

          tpp->parent=nullptr;

          myHeadPtr->parent=tpp;

          tpp->child=myHeadPtr;

          myHeadPtr=tpp;

     }

}

//Method inserts a at this list end

void DList::PushEnd(int a)

{

     //Declare the tpp

     Node *tpp;

     //Create Node with a

     tpp=new Node(a);

     //Check this list is empty

     if(myHeadPtr==NULL && myTailPtr==NULL)

     {

          //Update the myHeadPtr and myTailPtr

          myHeadPtr=tpp;

          myTailPtr=tpp;

     }

     //If this list is not empty

     else

     {

          //Update the mytailPtr

          tpp->child=nullptr;

          tpp->parent=myTailPtr;

          myTailPtr->child=tpp;

          myTailPtr=tpp;

     }

}

//Method removes an item from this list front

Node* DList::PopFront()

{

     //Check this list is empty

     if(myHeadPtr==NULL)

          //If it so then return nullptr

          return nullptr;

     //Declare the tpp

     Node *tpp;

     //Set tpp to point to myHeadPtr

     tpp=myHeadPtr;

     //Update the myHeadPtr

     myHeadPtr=myHeadPtr->child;

     //Check myHeadptr is nullptr

     if(myHeadPtr!=nullptr)

          myHeadPtr->parent=nullptr;

     //if myHeadPtr is not nullptr

     else if(myHeadPtr==nullptr)

          myTailPtr=nullptr;

     //return the tpp value

     return tpp;

}

//Method deletes a node from this list end

Node *DList::PopEnd()

{

     //If this list is empty

     if(myTailPtr==nullptr)

          //Return the nullptr

          return nullptr;

     Node *tpp;

     //Remove the item from the end

     tpp=myTailPtr;

     //Update the myTailPtr

     myTailPtr=myTailPtr->parent;

     //check myTailPtr is not nullptr

     if(myTailPtr!=nullptr)

          myTailPtr->child=nullptr;

     //if mytailPtr is nullptr

     else if(myTailPtr==nullptr)

          myHeadPtr=nullptr;

     //Update the parent of tpp

     tpp->parent=nullptr;

     //Return the tpp

     return tpp;

}

//Method prints this list from the begining

void DList::PrintListForward()

{

     Node *tpp;

     tpp=myHeadPtr;

     while(tpp!=nullptr)

     {

          cout<<tpp->data<<" ";

          tpp=tpp->child;

     }

     cout<<endl;

    

}

//Method prints this list from the end

void DList::PrintListReverse()

{

     Node *tpp;

     tpp=myTailPtr;

     while(tpp!=nullptr)

     {

          cout<<tpp->data<<" ";

          tpp=tpp->parent;

     }

     cout<<endl;

}

File Name: main.cpp

#include "DList.h"

#include <iostream>

using namespace std;

int main()

{

    DList list;

    int i;

    //pust 10 numbers to the end

    for (i = 0; i < 10; i++) list.PushEnd(i);

    cout << "Print forward\n";

    list.PrintListForward();

    cout << "Print reverse\n";

    list.PrintListReverse();

    cout << "Pop front and print\n";

    //print the list from the beginning

    Node* n = list.PopFront();

    while ( n != NULL) {

        cout << n->GetData() << endl;

        n = list.PopFront();

    }

    //pust 10 numbers to the front

    for (i = 0; i < 10; i++) list.PushFront(i);

    //test copy constructor

    DList* list2 = new DList(list);

   

    cout << "Pop end and print\n";

    //print the list from the end

    n = list2->PopEnd();

    while ( n != NULL) {

        cout << n->GetData() << endl;

        n = list2->PopEnd();

    }

system("pause");

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
In this project, you are asked to design and implement a class for double link list...
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
  • PLEASE CODE IN C++ AND MAKE IT COPYABLE! In this project, you will design and implement...

    PLEASE CODE IN C++ AND MAKE IT COPYABLE! In this project, you will design and implement an algorithm to determine the next greater element of an element in an array in Θ(n) time, where 'n' is the number of elements in the array. You could use the Stack ADT for this purpose. The next greater element (NGE) for an element at index i in an array A is the element that occurs at index j (i < j) such that...

  • Please answer in C++. Derive a class called Stack from the linked list described in Assignment...

    Please answer in C++. Derive a class called Stack from the linked list described in Assignment 2 (list of Dates). This means the Stack class will inherit all the properties (data and functions) of the linked list. But, since a stack only allows pushing and popping at the front of the list only, you will need to prevent the operations at the back. To do this, derive the Stack class in such a way that the base class (LinkedList) functions...

  • Please answer in C++ Derive a class called Queue from the linked list described in Assignment...

    Please answer in C++ Derive a class called Queue from the linked list described in Assignment 2 (list of Dates). This means the Queue class will inherit all the properties (data and functions) of the linked list. But, since a queue allows pushing only at the back and popping at the front of the list, you will need to prevent the addition in the front and removal at the back. To do this, you must derive the Queue class in...

  • C++ Implement a templated class list and listnode. You may add methods/functions as you see fit....

    C++ Implement a templated class list and listnode. You may add methods/functions as you see fit. Test these classes. I have left all of the implementation as an exercise for you. template< class NODETYPE > class List;  // forward declaration template<class NODETYPE> class ListNode {    friend class List< NODETYPE >; // make List a friend public:    ListNode( const NODETYPE &newData);  // copy constructor    NODETYPE getData() const;      // return data in the node private:    NODETYPE data;                 // data    ListNode< NODETYPE > *nextPtr; // next node...

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

  • In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the...

    In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the following sort member function on a singly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); Implement the following sort member function on a doubly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); The sort(…) methods take as a parameter a comparator function, having a default assignment of defaultCompare, a static function defined as follows: template <typename T> static bool defaultCompare(const...

  • Am Specification For this assignment, you will write a multi-file C program to define, implement ...

    Must be written and C, and compile with MinGW. Thank you! am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...

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

  • 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