I have a program that has a couple quick Errors that need fixed. Involves a doub
ID: 3576807 • Letter: I
Question
I have a program that has a couple quick Errors that need fixed. Involves a double linked list. Make sure it runs as shown thanks
dlist.h
{
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);
}
dlist.cpp
Sample Run
The list is:
hell{o}
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): p
The list is:
hel{l}
Select p (print), i (insert), d (delete), r (right),
l (left) or q (quit): l
Enter the distance to move left: 20
Select p (print), i (insert), d (delete), r (right),
l (left) or q (quit): p
The list is:
{h}ell
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): d
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): p
The list is:
{l}
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): p
The list is:
Select p (print), i (insert), d (delete), r (right),
l (left) or q (quit): i
Enter the character to insert: a
Select p (print), i (insert), d (delete), r (right),
l (left) or q (quit): i
Enter the character to insert: b
Select p (print), i (insert), d (delete), r (right),
l (left) or q (quit): i
Enter the character to insert: c
Select p (print), i (insert), d (delete), r (right),
l (left) or q (quit): i
Enter the character to insert: d
Select p (print), i (insert), d (delete), r (right),
l (left) or q (quit): i
Enter the character to insert: e
Select p (print), i (insert), d (delete), r (right),
l (left) or q (quit): p
The list is:
abcd{e}
Select p (print), i (insert), d (delete), r (right),
l (left) or q (quit): l
Enter the distance to move left: 3
Select p (print), i (insert), d (delete), r (right),
l (left) or q (quit): r
Enter the distance to move right: 1
Select p (print), i (insert), d (delete), r (right),
l (left) or q (quit): p
The list is:
ab{c}de
Directions I was given along with outline code
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.
Explanation / Answer
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.