Question: Please solve this linkedlist problem in c language. You are provided with the file 1.c where there are two functions store_linked_list and restore_linked_list. Store_linked_list function(which
Please solve this linkedlist problem in c language.
You are provided with the file 1.c where there are two functions store_linked_list and restore_linked_list.
Store_linked_list function(which is already included in code) takes a root node and stores it to a file write.bin as binary. It stores the list 1 element at a time consecutively.
Please write a function restore_linked_list . This function will read the elements from write.bin 1 at time and makes link for them . First link must be assigned to the root variable which address is given as arguement and then other link should be added in sqeuence.
for reiterating , the arguement to restore_linked_list is pointer to pointer . It is address for the pointer variable where you are storing the address of first link.
HINT : make sure you keep track of last link using last pointer by which searching for the last element of list is not required when you would like to add next element.
Also make sure you keep track if fread function runs out of elements, if so read entire linked list and terminate the function. the last element of the Linked list should point to NULL (Not junk ) !
Please write the code where it says "write your code here". Thanks
File 1.c
#include
#include "linklist.h"
void store_linked_list (struct link *root) { FILE *outfile = fopen("single.bin", "wb"); struct link *c = root;
while (c != NULL) { fwrite(&c->val, sizeof(int), 1, outfile);
c = c->next;
}
fclose(outfile);
}
void restore_linked_list(struct link **rootptr) { // Please write your code here.This is the only part you are required to write code for
}
The linked list header file is below
#ifndef _LINKLIST
#define _LINKLIST
struct link {
int val;
struct link *next;
};
struct doublelink {
int val;
struct doublelink *next;
struct doublelink *prev;
};
Also main is also already provided which is here.
#include
#include
#include "linklist.h"
extern void store_linked_list(struct link *);
extern void restore_linked_list(struct link **);
void printlist(struct link *current) {
while (current) {
printf("%d", current->val);
current = current->next;
}
}
void main() {
struct link links[10];
int i;
for (i = 0; i < 9; i++)
links[i] = (struct link) {i, &links[i+1]};
links[i] = (struct link) {i, NULL};
struct link *root = &links[0];
// Test Linked List serialization code.
printf("Original Linked List: ");
printlist(root);
printf(" ");
store_linked_list(root);
root = NULL;
restore_linked_list(&root);
printf("Restored Linked List: ");
printlist(root);
printf(" "); }
So pretty much everything is provided ,you just need to fill in code of the function restore_linked_list in the file single.c where it says Write your code here. No modifications needed to other files provided , Thanks
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
