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.
Computer Science
lab3.h
-------------
#include<stdio.h>
#include<stdlib.h>
#ifndef LAB3_H
#define LAB3_H
// A linked list node
struct Node
{
int data; //Data
struct Node *next; // Address to the next node
};
//initialize: create an empty head node (whose "data" is intentionally missing); This head node will not be used to store any data;
struct Node *init () {
//create head node
struct Node *head = (struct Node*)malloc(sizeof(struct Node));
}
//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 insert(struct node *head, int data) {
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
new_node->data = data;
new_node->next= head;
}
//print data for all nodes in the linked list except the head node (which is empty)
void display (struct Node *head) {
struct Node *current_node = head;
while ( current_node != NULL) {
printf("%d ", current_node->data);
current_node = current_node->next;
}
}
//delete the entire linked list including the head node
void deleteAll (struct Node *head) {
}
#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;
}
-----------------
makefile
-------------------
//create a makefile to compile the code using VIM terminal (Hint: gcc, -o, ./, etc.)
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 2 images