I need some code added to my program. Outline files below with similar code of h
ID: 3576737 • Letter: I
Question
I need some code added to my program. Outline files below with similar code of how to do it also a sample run.
I have to write public functions for a doubly linked list class. use similar code as shown in the build linklist forward and backward pics at bottom
Using the header(dlist) and mainline file (dlistapp) below
You are going to write public functions for a doubly linked list class.
The dlist class is declared in the file dlist.h as follows:
struct dlist_node
{
char contents; // contents in the node
dlist_node *back, // pointer to previous node in the list
*next; // pointer to the next node in the list
};
typedef dlist_node* dptr;
class dlist
{
private:
dptr front, // pointer to the front of the list
current; // pointer to current node in the list
public:
dlist (); // constructor creates an empty list
void insert (char ch); // inserts a new node
void remove (); // removes a node
void Move_Right(int distance); // moves current right
void Move_Left(int distance); // moves current left
void print (); // prints the list
};
The public functions that you need to define are:
dlist (): Constructor that initializes the list to be empty.
void insert (char ch): Adds a new node to the right of current containing ch and points current at the new node. Should insert first node correctly.
void remove (): Removes the node from the list pointed to by current. Points current at the node after the deleted node (if present) else points current at the node before the deleted node (if present). Should remove last node correctly and recycle nodes. Should not fail if list is empty.
void Move_Right (int distance): Moves current to the right distance nodes. If the given distance will move current off the end of the list, current should be set to point at the rightmost node. Should not fail if list is empty.
void Move_Left (int distance): Moves current to the left distance nodes. If the given distance will move current off the end of the list, current should be set to point at the leftmost node. Should not fail if list is empty.
void print (): Prints all the nodes in the list. The value pointed to by current should be printed in braces.print does not output any spaces or linefeeds. For example, if the data in the linklist represents CIS 361, the output would be :
CIS 3{6}1
The dlistapp.cpp application file is coded except for the calls to the public functions. Add the calls as directed by the comments in the file.
dlist.h
dlistapp.cpp
Sample run
Select p (print), i (insert), d (delete), r (right),
l (left) or q (quit): d
Select p (print), i (insert), d (delete), r (right),
l (left) or q (quit): r
Enter the distance to move right: 10
Select p (print), i (insert), d (delete), r (right),
l (left) or q (quit): l
Enter the distance to move left: 2
Select p (print), i (insert), d (delete), r (right),
l (left) or q (quit): p
The list is:
Select p (print), i (insert), d (delete), r (right),
l (left) or q (quit): i
Enter the character to insert: h
nodeType buildList Forward node Type first newNode last int num; Cout Enter a list of integers ending with endl cin num first NULL while (num 999) newNode new nodeType newNode info num new Node- link NULL if (first NULL) first newNode; last new Node else last link newNode last new Node cin num; //end while return first; 999Explanation / Answer
{
front = NULL;
current = NULL;
}
void dlist::insert(char ch)
{
dptr newNode = new dlist_node();
newNode->contents = ch;
newNode->next = NULL;
newNode->back = NULL;
if(front == NULL)
{
front = newNode;
current = newNode;
}
else
if(current != NULL)
{
current -> next = newNode;
newNode->back = current;
newNode->next->back = newNode;
newNode->next = current->next;
}
}
bool dlist::remove()
{
dptr delete_node;
char ch;
if(current == NULL && front == NULL)
return false;
else
if (current -> contents == ch)
{
front = front->next;
current = delete_node;
delete current;
return true;
}
else
current ->back;
return true;
}
void dlist::Move_Right(int distance)
{
dptr new_node, current;
int count;
if(new_node != NULL)
for(count = 1; count <= distance; count++)
{
current = current->next;
}
else
if(current->next == NULL)
current->next = current->back;
}
void dlist::Move_Left(int distance)
{
dptr new_node, current;
int count;
if(new_node != NULL)
{
for(count = 1; count <= distance; count++)
{
current = current->back;
}
}
else
if(current->back == NULL)
current->back = current->next;
}
void dlist::print()
{
dptr pNode = front;
if(pNode == current)
cout<<"{"<<pNode->contents<<"}";
else
if(pNode != NULL)
do
{
cout << pNode->contents;
pNode = pNode->next;
}while(pNode != NULL && pNode != current);
}