Provided the C++ Code below along with the header file, what changes would I mak
ID: 3753631 • Letter: P
Question
Provided the C++ Code below along with the header file, what changes would I make to modify the singly linked list class to create a doubly linked list?&intslist (1)appCintslisth 0 include #include "intsilist.h" 4 IntsLlist:: IntSLlist() //Destructor 6 for (IntsLLNode p; lisEpty);) p head->nlext; delete head head p; 10 12 13 Vo 14 15 16 if (tail e) 17 18 19 void Intsutist: saddToTail(int value) id Intsuuist: :addTolead(int value) head new IntSLLNode(value, head)i tail- head, // unbracketed single line blocks are valid, but not good practice 21 if (tail I- e) ( tail->next - new IntSLLNode (value); tail tail->next; 23 24 25 26 int IntsLlist::deletef romHead() 27 28 int value head->info; IntSLLNode old head; if (headtail) //one element in the list 32 head tai1- 34 else head head->next; 35 delete old; return value 37 38 int IntsLlist: :deletefromTail) lype hese to seach
Explanation / Answer
CPP program:
//initially the modification in coding from singly linked list to double linked list is defined.
//Then the creation of class in doubly linked list is done in this coding.
#include <iostream>
#include "intslist.h"
//To create a class double linked list have to declare next and previous nodes
// A linked list node
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
void push(struct Node** head_ref, int new_data)//reference pointer
{
//modifications of node from head to tail in singly linked list to
previous and next node in doubly linked list
IntSList::IntSList()
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); //for the allocation of node
new_node->data = new_data;
new_node->next = (*head_ref);
new_node->prev = NULL;
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}
//check if the given new_node is NULL
if (next_node == NULL) {
printf("the given next node cannot be NULL");
return;
}
//it is defined that the given node cannot be null
// allocation of new node to modify from sigly linked list to double linked list
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
//data in new node
new_node->data = new_data;
////deletion of a node
void deleteNode(struct Node **head_ref, struct Node *del)
{
if(*head_ref == NULL || del == NULL)
return; //
If node to be deleted is head node */
if(*head_ref == del)
*head_ref = del->next;
//Change next only if node to be deleted is NOT the last node
if(del->next != NULL)
del->next->prev = del->prev;
//Change prev only if node to be deleted is NOT the first node
if(del->prev != NULL)
del->prev->next = del->next;
//the free memory occupied by deletion
free(del);
return;
}
//Modify the prev of new node as prev of next_node
new_node->prev = next_node->prev;
//modify the prev of next_node as new_node */
next_node->prev = new_node;
new_node->next = next_node;
//Changing the next of new_node's into previous node
if (new_node->prev != NULL)
new_node->prev->next = new_node;
}
void printList(struct Node* node)
{
struct Node* last;
printf(" forward direction Traversal ");
while (node != NULL) {
printf(" %d ", node->data);
last = node;
node = node->next; //declaration of doubly linked list
}
//in reverse direction
printf(" reverse direction Traversal ");
while (last != NULL) {
printf(" %d ", last->data);
last = last->prev;
}
}
//Driver program to test the modification of above functions from singly linked list to doubly linked list*/
int main()
{
//Start with the empty list
struct Node* head = NULL;
push(&head, 4);
push(&head, 2);
push(&head, 3);
// Insert 1, before 2 .
linked list becomes 4->1->2->3->NULL
insertBefore(head->next, 1); //to check the linked list insert data
printf("Created DLL is: ");
printList(head);
getchar();
return 0;
}//creation of class for doubly linked list
//class doublylinked list
class double_linkedlist
{
public:
void create_list(int value);
void add_initial(int value);
void add_next(int value, int position);
void delete_element(int value);
void search_element(int value);
void display_dlist();
void count();
void reverse();
double_llist()
{
start = NULL;
}
int main dll()
void double_linkedlist::create_list(int value)
{
struct node *s, *temp;
temp = new(struct node);
temp->info = value;
temp->next = NULL;
if (start == NULL)
{
temp->prev = NULL;
start = temp;
}
else
{
s = start;
while (s->next != NULL)
s = s->next;
s->next = temp;
temp->prev = s;
<<enddll>>;
}
}
};
Hope you understand and this helps you.All the best
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.