+1 (315) 557-6473 

Linked lists assignment sample

Are you looking for help with linked lists assignments? Let your search end here! ProgrammingAssignmentHelper.com brings you the best academic assistance so you can have tasks on this topic completed professionally.

Linked Lists


Linked Lists and Modular Programming


This assignment explores malloc, makefile, modular programming, and git. The central problem you will solve is a linked-list.
Construct a modular program from three sources files: main.c, list.c and list.h. The file main.c
will be used to call the functions provided by the file list.c. The file list.c builds a private linkedlist within the list.c named space. The file list.h contains the definition of the list’s node
structure. Use #ifndef to protect the possible multiple inclusion of list.h.
Use a makefile to compile your assignment. Use git to version control the development of your
assignment. To prove that you used a makefile submit your makefile file. To prove that you used
git, save the output from git log into a text file and submit that file.
The program will run as follows:
• The main() function uses a while loop accepting only positive integer numbers (n>0) from
the user. The loop terminates once the user enters a non-positive value. The user is
prompted for each number one at a time.
• In the loop, main() calls functions from the list.o named space that creates a private linked
list using malloc.
• The following function signatures are supported by list.c:
o void newList();
▪ This function assumes there is a private global linked-list pointer in list.c
called head that is used to point to the beginning of a linked-list. This
function call simply initializes this pointer to NULL.
ointaddNode(int value);
▪ This function mallocs a new node and copies the parameter value into the
node. This function then adds the node to the head of the linked-list. The
function ends by returning true for success and false for failure. A node has
the following structure: struct NODE { int data; struct NODE *next; }.
ovoid prettyPrint();
▪ This function assumes a global pointer called head exists. Using this head
pointer is traverses the linked-list printing all the values stored in the list.
• When the loop terminates the program prints to screen all the numbers in the list. The
output should be in reverse order.
• The program then terminates.
• Do NOT use recursion.

Solution

list.c

/*

* list.c

*

*  Created on: Nov 27, 2017

*      Author:

*/

#include

#include

#include "list.h"

NODE *head;

/*

* This function assumes there is private global linked-list pointer in list.c

* called head that is used to point to the beginning of linked-list

* This function call simple initializes this pointer to NULL

*/

voidnewList()

{

head = NULL;

}

/*

* This function mallocs a new node and copies the parameter value into the node.

* This function then adds the node to the head of linked-list.

* The function ends by returning true for success and false for failure.

*/

intaddNode(int value)

{

NODE *tmp = (NODE *) malloc(sizeof(NODE));

if (tmp == NULL)

{

return EXIT_FAILURE; /* 1 (failure) */

}

tmp->data = value; /* assign value to data field */

if (head == NULL)

{

/* first node in the list */

tmp->next = NULL;

} else

{

tmp->next = head; /* chain to list (at head of list) */

}

head = tmp; /* add to head */

return EXIT_SUCCESS;       //0 (success)

}

/*

* This function assumes a global pointer called head exists.

* Using this head pointer is traverses the linked-list printing all the values

* stored in the list

*/

voidprettyPrint()

{

NODE* currentNode = head;

if (currentNode == NULL)

{

printf("Your list is empty.\n");

} else

{

while (currentNode != NULL)

{

printf("%d ", currentNode->data);

currentNode = currentNode->next;

}

printf("\n");

}

}

list.h

/*

* list.h

*

*  Created on: Nov 27, 2017

*      Author:

*/

#ifndef LIST_H_

#define LIST_H_

/*Define NODE*/

typedefstruct NODE

{

int data;

struct NODE *next;

} NODE;

/*Prototype function define*/

voidnewList(); /*Initialize new list*/

intaddNode(int); /* Add new node to the list */

voidprettyPrint(); /* Print list */

#endif /* LIST_H_ */

main.c

/*

* main.c

*

*  Created on: Nov 27, 2017

*      Author:

*/

#include

#include

#include "list.h"

int main()

{

intnum = 0;

newList(); /* initialize a list */

printf("Enter a positive number (non-positive on exit): "); /* prompt user (first time)*/

scanf("%d", &num);

while (num> 0)

{

if (addNode(num) == EXIT_FAILURE)

{

/* error handling for addNode failure */

printf("A problem occured when adding your number.\n");

exit(1);

} else

{

printf("Enter a positive number (non-positive to exit): "); /* prompt user (in loop) */

scanf("%d", &num);

}

}

printf("You enter the non-positive number: %d. Exit the program!!!.\n", num); /* entered a non-positve number */

printf("The liked-list:\n");

prettyPrint(); /* print the list in reverse order */

return 0;

}