In: Computer Science
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.)
void remove_list(struct linked_list* list) //*This is the function to make and below is the explanation that should be written in given code.
"This function removes the list. When the list is removed, all the memory should be released to operating system (OS) so that OS lets other computer programs use this. While deleting the list, every node should be freed separately; free(list) will not remove every node in the list. To check whether the nodes are removed perfectly, for every deletion of a node, this function should print message “The node with value n (corresponding value) is deleted!” Also, if the whole list is deleted, this function should print message “The list is completely deleted: n nodes are deleted”."
Given code is written below,(There is a function to fill in last moment in this code)
linked_list.h: This is the header file of linkLQS.c that declares all the functions and values that are going to be used in linkLQS.c. You do not have to touch this function.
-----------------------------------------------------------------------
(Below code is about linked_list.h)
#include
#include
#include
struct linked_node{
int value;
struct linked_node* next;
struct linked_node* prev;
};
struct linked_list{
int type_of_list; // normal = 0, stack = 1
struct linked_node* head;
struct linked_node* tail;
int number_of_nodes;
};
--------------------------------------------------------
#include "linked_list.h"
#include "string.h"
extern int list_exist;
struct linked_list* create_list (int number_of_nodes, int
list_type)
{
int a[number_of_nodes];
int i, j;
int bFound;
if (number_of_nodes < 1)
{
printf("Function create_list: the
number of nodes is not specified correctly\n");
return NULL;
}
if(list_exist == 1)
{
printf("Function create_list: a
list already exists\nRestart a Program\n");
exit(0);
}
if(list_type != 0 && list_type != 1)
{
printf("Function create_list: the
list type is wrong\n");
exit(0);
}
struct linked_list * new_list = (struct
linked_list*)malloc(sizeof(struct linked_list));
new_list->head = NULL;
new_list->tail = NULL;
new_list->number_of_nodes = 0;
new_list->type_of_list = list_type;
//now put nodes into the list with random
numbers.
srand((unsigned int)time(NULL));
if(list_type == 0)
{
for ( i = 0; i <
number_of_nodes; ++i )
{
while ( 1
)
{
a[i] = rand() % number_of_nodes + 1;
bFound = 0;
for ( j = 0; j < i; ++j )
{
if ( a[j] == a[i] )
{
bFound =
1;
break;
}
}
if ( !bFound )
break;
}
struct
linked_node* new_node = create_node(a[i]);
insert_node(new_list, new_node);
}
}
else if(list_type == 1)
{
for ( i = 0; i <
number_of_nodes; ++i )
{
while ( 1
)
{
a[i] = rand() % number_of_nodes + 1;
bFound = 0;
for ( j = 0; j < i; ++j )
{
if ( a[j] == a[i] )
{
bFound =
1;
break;
}
}
if ( !bFound )
break;
}
struct
linked_node* new_node = create_node(a[i]);
push_Stack(new_list, new_node);
}
}
list_exist = 1;
printf("List is created!\n");
return new_list;
}
struct linked_node* create_node (int node_value)//This
functon is the example for reference of the assignment
function
{
struct linked_node* node = (struct
linked_node*)malloc(sizeof(struct linked_node));
node->value = node_value;
node->next = NULL;
node->prev = NULL;
return node;
}
void insert_node(struct linked_list* list, struct
linked_node* node)//This functon is the example for reference of
the assignment function
{
node->next = NULL;
node->prev = NULL;
if(list->head == NULL)
//if head is NULL, tail is also NULL.
{
list->head = node;
list->tail = node;
list_exist = 1;
}
else if(list->head == list->tail)
{
node->next =
list->head;
list->head->prev =
node;
list->head = node;
}
else if(list->head != list->tail)
{
node->next =
list->head;
list->head->prev =
node;
list->head = node;
}
(list->number_of_nodes)++;
}
void remove_list(struct linked_list*
list)//
{
~~~~~~~~~~~ //your code starts from
here
int deleted_nodes = 0;//Please do not erase
these sentences. you should cover these sentences!
int deleted_node_value;
}
}
Following is your required function
void remove_list(struct linked_list* list)
{
//your code starts from here
int deleted_nodes = 0;
int deleted_node_value;
//when list->head==NULL
//this means that the list is empty and hence safe to delete.
while(list->head != NULL){
void remove_list(struct linked_list* list)
{
//your code starts from here
int deleted_nodes = 0;
int deleted_node_value;
//when list->head==NULL
//this means that the list is empty and hence
safe to delete.
while(list->head != NULL){temp node
//assigning temp the address to where
head of list points
struct linked_node* temp =
list->head;
//shifting the head to the next
element in the list
list->head = temp->next;
//save the value of the temp node,
before deleting it
deleted_node_value =
temp->value;
//free the node stored in temp
free(temp);
//print the value of the deleted
node
cout<<"The node with value
"<<deleted_node_value<<" is deleted!\n";
//increase the count of deleted
nodes
deleted_nodes++;
}
//At the end of while entire list is
deleted.
cout<<"The list is completely deleted:
"<<deleted_nodes<<" nodes are deleted\n";
//We can now safely free list
free(list);
//since the list doesn't exist anymore, set
list_exist equal to zero.
list_exist = 0;
return;
}
//assigning temp the address to where head of list points
struct linked_node* temp = list->head;
//shifting the head to the next element in the list
list->head = temp->next;
//save the value of the temp node, before deleting it
deleted_node_value = temp->value;
//free the node stored in temp
free(temp);
//print the value of the deleted node
cout<<"The node with value
"<<deleted_node_value<<" is deleted!\n";
//increase the count of deleted nodes
deleted_nodes++;
}
//At the end of while entire list is deleted.
cout<<"The list is completely deleted:
"<<deleted_nodes<<" nodes are deleted\n";
//We can now safely free list
free(list);
//since the list doesn't exist anymore, set list_exist equal to
zero.
list_exist = 0;
return;
}
-----------------------------------------------------------------------------------------------------
Comments have been provided for better understanding.
Hope this helps. If you like the answer, please upvote. Cheers!!