Question: Who Gets the Cake ( Part 2 ) Learning Goals Insert and delete nodes in a linked list Changes from HW 0 1 Cake In
Who Gets the Cake Part
Learning Goals
Insert and delete nodes in a linked list
Changes from HW Cake
In HW Cake, an array is used to keep track who is still in the game. In this assignment, a linked list is used. The output of your program should match the output of HW Cake. In addition, the linked list is printed every time one number is removed.
The files in expected directory have DDEBUG in Makefile to turn on the code printing the lists starting from the nodes to be eliminated. If Makefile does not have DDEBUG, the outputs should be the same as the files in HW Cake.
Initialize Pointers
You should always initialize pointers to NULL. Uninitialized pointers can make your programs' behavior unpredictable.
Printing ListNode
You should call printListNode each time BEFORE you delete any node and print the node value. In the expected files, printListNode is not called for the last value in the list. Follow the expected files format. You can turn on the DEBUG flag in your Makefile for printing.
Helpful Notes
For this assignment, it will be beneficial to first complete createList and deleteNode functions before trying to complete eliminate, as these are important building blocks for the eliminate function, which is the most complicated piece of this assignment. While working on createList and deleteNode, you are encouraged to write your own test cases to ensure your code is working as expected.
Code template:
You MUST modify this file
#include
#include
#include
#include
#include hwh
DO NOT MODIFY this function
void printListNodeListNode head
ListNode p head;
printfprintListNode: ;
while p NULL
printfd p value;
p p next;
printf
;
until here
create a linked list storing values valn
The first node head stores the next node stores
the last node stores valn
return the head of the linked list
ListNode createListint valn
return NULL;
eliminate the nodes in the linked list
starting from the head, move one node at a time and count to valk.
eliminate that node, keep counting
when reaching the end of the list, continue from the beginning of
the list
print the values of the nodes to be deleted
void eliminateListNode head, int valk
#ifdef DEBUG
this #ifdef #endif should be inside the condition BEFORE a
node' value is printed and it is deleted
ListNode todelete p;
printListNode todelete;
#endif
head points to the first node in the linked list
todelete points to the node to be deleted
delete the node and return the head of the linked list
release the memory of the deleted node
should check several conditions:
If head is NULL, the list is empty and this function returns
NULL
If todelete is NULL, nothing can be deleted, return head
If todelete is not in the list, keep the list unchanged and
return head
It is possible that todelete is the first node in the list ie
the head If this occurs, return the second node of the list.
ListNode deleteNodeListNode head, ListNode todelete
return NULL;
main file:
You may modify this file but don't submit it
#include
#include
#include
#include hwh
int mainint argc, char argv
if argc
fprintfstderr "need two numbers
;
return EXITFAILURE;
int valn int strtolargv NULL, ;
int valk int strtolargv NULL, ;
if valn valk
return EXITFAILURE;
ListNode head NULL;
head createListvaln;
eliminatehead valk;
return EXITSUCCESS;
make file:
#
# You may modify this file but don't submit it
#
WARNING Wall Wshadow pedantic
ERROR Wvla Werror
GCC gcc stdcg $WARNING $ERROR
TESTFLAGS DDEBUG
SRCS main.c hwc
OBJS $SRCS:co
hw: $OBJS
$GCC $TESTFLAGS $OBJSo hw
co:
$GCC $TESTFLAGSc $c
testall: test test test
test: hw
hw output
diff output expectedexpected
test: hw
hw output
diff output expectedexpected
test: hw
hw output
diff output expectedexpected
memory: hw
# You may consider using other flags for valgrind, such as leakcheckfull, to ensure that your program has absolutely no memory errors
valgrind hw
clean: # remove all machine generated files
rm f hwo output? ~
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
