Question
Goal: learn to implement a singly linked self-organizing list and dynamic memory allocation/de-allocation. Constraints: - no header nodes for list - no violation of specifications
Goal: learn to implement a singly linked self-organizing list and dynamic memory allocation/de-allocation. Constraints: - no header nodes for list - no violation of specifications (function-id, or its interface) - no line wraps (wraparound in display) keep line length <= 72 - no interleaving of code and comments - no accessing of dangling pointers
Statement: Write a modular, documented C program to implement a singly linked list for keeping track of events as they occur. Event is a random number (map the generated numbers in range 1 to 13). The list keeps track of the events processed as: event-number and its frequency of occurrence so far. List is accessible via list pointer, node is to be inserted at tail (if event is not in the list). If the event is in the list, it moves one position towards the front of list (if not already there).
Storage for node is allocated dynamically using library functions malloc(r) and free(), see below.
typedef struct node{ int eventnr; struct node *next; } node;
The modules needed are (place in file a1.c):
0. int main(int argc, char **argv); of course..
1. void usage(char *progname); { prints usage to stderr and terminates program execution }
2. void getnode(node **ptr); { gets storage for a node from system }
. void freenode(node **tofree); { returns storage for node back to system }
4. int nextevent(void); { returns next event generated (via rand(), seeded in main() using srand())}
5. int insertevent(node **list, int event); { insert event at end/tail of list, returns 0 on failure and 1 on success }
6. void searchevent(node *list, int xevent, node **current, node **previous, node **pprevoi us); { current points to node containing xevent on success, on failure current is undefined, previous always points to predecessor of current and pprevoius points to predecessor of previous. }
7. void transposenode(node **list, node *pprevious, node *previous, node *current); { transposes current node one position towards front of list if possible }
8. void displayevents(node *list); { print list contents to stdout, as comma separated events( 10 events per line of output) } (Sample output: eeee , eeee, eeee, ... eeee, ... )
9. void freelist(node **list); { returns the storage for list back to system, one node at a time, using freenode(), list is empty after this call. }
The algorithm is: set up the type and variable declarations as needed (including list pointer) check command-line args for sanity (if problems bail out) seed the random number generator while more events to go
generate next event
search list for event
if in list, adjust/transpose the node
else insert at tail
if error, continue
display the list contents on stdout return list storage to system (one node at a time)
/* Print usage to stderr on command line errors and terminate */
void usage(char *progname) { fprintf(stderr, "usage: "./%s ", progname); exit(1); }
/* Gets storage for a node from system, the pointer to new node is NULL if malloc() fails. */
void getnode(node **ptr) { *ptr = malloc(sizeof(node)); }
/* Returns storage for a node pointed by ptr to system Ptr is NULL after the operation. */
void freenode(node **ptr) { if(*ptr != NULL) { free((void *)*ptr); *ptr = NULL; } }
void searchevent(node *list, int xevent, node **current, node **previous, node **pprevious);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started