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_node(struct linked_list* list, int rm_node_value) (the function to make)
This function removes a node with specified value. If there is only one node in the list, remove the node and remove the list also since there is nothing left. While removing a node, the node should be perfectly freed. If the type of list is stack, print the error message "Function remove_node: The list type is not normal. Removal is not allowed" Also, if there is no such node to remove from the list, print the error message “Function remove_node: There is no such node to remove” and exit the function.
<<This is the function and its explanation that should be written in given code.
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 add any more functions or variables in order to complete the project and you are NOT ALLOWED to.
string.h : This header file defines several functions to manipulate C strings and arrays.
-----------------------------------------------------------------------
#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_node(struct linked_list* list, int
rm_node_value)(the function to be written!!)
{
struct linked_node * del_Node//please do
not erase it. you should cover this sentence.
~~~~~//your code starts from here
}
}
void remove_node(struct linked_list* list, int
rm_node_value)
{
struct linked_node * del_Node = list->head;
struct linked_node *tail = list->tail;
if(del_Node == NULL) return;
if(del_Node->value == rm_node_value){
if(del_Node == tail){
free(del_Node);
list->next = NULL;
list->tail = NULL;
free(list);
}
else{
list->head =
del_Node->next;
del_Node->next->prev = NULL;
free(del_Node);
}
return;
}
while(del_Node != NULL && del_Node->value
!= rm_node_value){
del_Node =
del_Node->next;
}
if(del_Node == NULL) return;
if(del_Node == tail){
tail->prev->next =
NULL;
list->tail =
tail->prev;
}
else{
del_Node->prev->next =
del_Node->next;
del_Node->next->prev =
del_Node->prev;
}
free(del_Node);
return;
}