I need some code added to my program. I have to write public functions for a dou
ID: 3576693 • Letter: I
Question
I need some code added to my program.
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.
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
front currentExplanation / Answer
#include <stdio.h>
#include <stdlib.h>
struct node
;
typedef struct node n;
n* create_node(int);
void add_node();
void insert_at_first();
void insert_at_end();
void insert_at_position();
void delete_node_position();
void sort_list();
void update();
void search();
void display_from_beg();
void display_in_rev();
n *new, *ptr, *prev;
n *first = NULL, *last = NULL;
int variety = 0;
void main()
come new;
}
/*
*ADDS NEW NODE
*/
void add_node()
next = new;
new->prev = last;
last = new;
last->next = first;
first->prev = last;
" id="tip_19">
}
/*
*INSERTS part initially
*/
void insert_at_first()
currently new node is inserted however at first");
initial = last = new;
first->next = last->next = NULL;
first->prev = last->prev = NULL;
}
else
}
/*
*INSERTS THE part AT GIVEN POSITION
*/
void insert_at_position()
{
int info, pos, len = 0, i;
n *prevnode;
printf(" enter the worth that you simply would love to insert:");
scanf("%d", &info);
printf(" enter the position wherever you have got to enter:");
scanf("%d", &pos);
new = create_node(info);
if (first == last && initial == NULL)
{
if (pos == 1)
{
initial = last = new;
first->next = last->next = NULL;
first->prev = last->prev = NULL;
}
else
printf(" empty coupled list you cant insert at that specific position");
}
else
}
}
for (ptr = initial, i = 0;i < number;ptr = ptr->next,i++)
printf(" %d", ptr->val);
}
}
/*
*DELETION is completed
*/
void delete_node_position()
zero, key, i, f = 0;
printf(" enter the worth to be searched:");
scanf("%d", &key);
if (first == last && initial == NULL)
printf(" list is empty no elemnets in list to search");
else
{
for (ptr = initial,i = 0;i < number;i++,ptr = ptr->next)
{
count++;
if (ptr->val == key)
{
printf(" the worth is found at position at %d", count);
f = 1;
}
}
if (f == 0)
printf(" the worth isn't found in linkedlist");
}
}
/*
*DISPLAYING IN starting
*/
void display_from_beg()
{
int i;
if (first == last && initial == NULL)
printf(" list is empty no elemnts to print");
else
{
printf(" %d variety of nodes square measure there", number);
for (ptr = initial, i = 0;i < number;i++,ptr = ptr->next)
printf(" %d", ptr->val);
}
}
/*
* DISPLAYING IN REVERSE
*/
void display_in_rev()
{
int i;
if (first == last && initial == NULL)
printf(" list is empty there aren't any elments");
else