In this lab, you need to implement a simple linked list using dynamic memory allocation. The structure of node in the linked list has been declared in “lab3.h”. Also, the functions of linked list have been declared in “lab3.h” as well. In “main.c”, these functions are called. Your work is to finish these function implementations in a separate C source file, and create a makefile which can compile your separate C source file along with “main.c” and “lab3.h”. You can follow the comments in “main.c” and “lab3.h” to finish this lab. Please do not change “main.c” and “lab3.h”, because your separate C source file will be compiled with these two files posted on BlackBoard. When you test your program, please put “main.c”, “lab3.h”, your separate C source file and your makefile in the same directory and compile. Code provided: lab3.h #include #include #ifndef LAB3_H #define LAB3_H struct node { int data; struct node *next; }; struct node *init (); //initialize: create an empty head node (whose "data" is intentionally missing); This head node will not be used to store any data; void insert(struct node *head, int data); //Create a new node to store data and insert it to the end of current linked list; the head node will still be empty and data in the array in "main.c" are not stored in head node void display (struct node *head); //print data for all nodes in the linked list except the head node (which is empty) void deleteAll (struct node *head); //delete the entire linked list including the head node #endif main.c #include"lab3.h" int main() { int array[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; struct node *head; head = init(); //after this statement, a linked list has been created with only a head node int i; for (i=0;i<10;i++) { insert(head, array[i]); //create a new node to store an array element. This new node is inserted to the end of current linked list } display(head); //print data for all nodes in the linked list except the head node (which is empty) deleteAll(head); //delete the entire linked list including the head node return 1; } This is needed to be coded in C.
Please written by computer source
In this lab, you need to implement a simple linked list using dynamic memory allocation. The structure of node in the linked list has been declared in “lab3.h”. Also, the functions of linked list have been declared in “lab3.h” as well. In “main.c”, these functions are called. Your work is to finish these function implementations in a separate C source file, and create a makefile which can compile your separate C source file along with “main.c” and “lab3.h”.
You can follow the comments in “main.c” and “lab3.h” to finish this lab. Please do not change “main.c” and “lab3.h”, because your separate C source file will be compiled with these two files posted on BlackBoard. When you test your program, please put “main.c”, “lab3.h”, your separate C source file and your makefile in the same directory and compile.
Code provided:
lab3.h
#include<stdio.h> #include<stdlib.h> #ifndef LAB3_H #define LAB3_H struct node { int data; struct node *next; }; struct node *init (); //initialize: create an empty head node (whose "data" is intentionally missing); This head node will not be used to store any data; void insert(struct node *head, int data); //Create a new node to store data and insert it to the end of current linked list; the head node will still be empty and data in the array in "main.c" are not stored in head node void display (struct node *head); //print data for all nodes in the linked list except the head node (which is empty) void deleteAll (struct node *head); //delete the entire linked list including the head node #endifmain.c
#include"lab3.h" int main() { int array[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; struct node *head; head = init(); //after this statement, a linked list has been created with only a head node int i; for (i=0;i<10;i++) { insert(head, array[i]); //create a new node to store an array element. This new node is inserted to the end of current linked list } display(head); //print data for all nodes in the linked list except the head node (which is empty) deleteAll(head); //delete the entire linked list including the head node return 1; }
This is needed to be coded in C.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images