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 push_Stack (struct linked_list* list, struct linked_node* node) //*This is the function to make and below is the explanation that should be written in given code.
This function inserts a node in stack manner. If the type of list is not stack, print the error message “Function push_Stack: The list type is not a stack” The new node will be always inserted to tail of the list which means the tail of the list should be changed after a new node is inserted.
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 <stdio.h>
#include <stdlib.h>
#include <time.h>
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 push_Stack(struct linked_list* list, struct
linked_node* node)//The function to be written!!
{
~~~~~~~~~~~~~~~~ //your code starts from
here
}
Hi,
Please find updated code below.
#include "linked_list.h"
#include "string.h"
extern int list_exist =0;
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 push_Stack(struct linked_list* list, struct
linked_node* node)//The function to be written!!
{
//your code starts from here
if(list->type_of_list != 1)
{
printf("Function push_Stack: The list type is not a
stack\n");
exit(0);
}
else
{
if(list->head == NULL) //if head is NULL, tail is also NULL.(so
the list was empty)
{
list->head = node;
list->tail = node;
list_exist = 1;
}
else
{
list->tail->next = node;
list->tail = list->tail->next;
}
(list->number_of_nodes)++;
}
}
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;
}
static void display(struct linked_node* head)
{
printf("%d ", head->value);
if (head->next == NULL)
{
return;
}
display(head->next);
}
int main()
{
struct linked_list* myList = create_list(4,1);
if(myList->head == NULL)
{
printf("No NODES IN LIST");
}
else
{
display(myList->head);
}
// Now lets test push_stack
// creating a new node to insert into stack implemented as linked
list.
struct linked_node* my_new_node = create_node(10);
push_Stack(myList,my_new_node);
printf("\nPrinting the list after push_stack try 1");
display(myList->head);
struct linked_node* my_new_node1 = create_node(100);
push_Stack(myList,my_new_node1);
printf("\nPrinting the list after push_stack try 2");
display(myList->head);
}
output attached:
Thanks,
kindly upvote.