Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Makefile that builds the program list from a C file list.c . Your Makefile should use the compiler flags discussed in class, and

Write a Makefile that builds the program list from a C file list.c. Your Makefile should
use the compiler flags discussed in class, and have the all and clean targets as shown
previously. You should also include the flag -Wextra as discussed in the lab.
Extending the List
Insert
As mentioned in LE5, there are different types of insertion to a list. We will now add the
more complicated insert before and insert after.
Insert Before Adds a node to the list immediately prior to a given node (current node).
There are four steps for this operation:
1. Get the node pointed to by the free list
2. Update free list to point at the new nodes next
3. Update the new nodes next to point at current node
4. Update the node prior to current node to point at the new node
Insert After Adds a node to the list immediately after a given node (current node).
There are four steps for this operation:
1. Get the node pointed to by the free list
2. Update free list to point at the new nodes next
3. Update the new nodes next to point at current node->next
4. Update the node current node to point at the new node
Remove
Remove Removes a specific node (current node). There are four steps for this operation:
1. Get the node pointed to by current node
2. Update the node prior to current node to point at current node->next
2
3. Update the new nodes next to point at list free
4. Update free list to point at the removed node
Clear Removes all nodes from a list and adds them to the free list. There are sev-
eral methods to accomplish this. However, since we have link-lists, we can do better than
repeatedly calling the remove function.
Functions to write
You probably noticed these overlap with the prior functions, but also with each other. We
will reuse our prior helper functions.
list find Write a struct node *list find(struct node *list head, int value) which
finds the value and returns a pointer to that node. It should return a valid pointer if value
is found, or NULL if it is not.
list insert before Write a int list insert before(struct node **list head, struct
node **free list, int reference, int value) which first finds the node containing the
reference, and then performs an insert of value before (as described above). Use any of the
helper functions you have previously created. The function should return an int where 0 is
success, and -1 is failure.
list insert after Write a int list insert after(struct node **list head, struct
node **free list, int reference, int value) which first finds the node containing the
value, and then performs an insert after (as described above). Use any of the helper functions
you have previously created. The function should return an int where 0 is success, and -1
is failure.
list remove Write a int list remove(struct node **list head, struct node **free list,
int reference) which first finds the node containing the value, and then removes that node
(as described above). Use any of the helper functions you have previously created. The func-
tion should return an int where 0 is success, and -1 is failure.
list clear Write a void list clear(struct node **list head, struct node **free list)
The list clear function removes all nodes from the list and return them to the free list.
main Extend the main function from the previous lab with the b,i,r,f and c
operations described below.
The complete set of commands should be:
a for append; e.g.,
3
a
123
Append an item to the list
e for prepend; e.g.,
e
456
Prepend an item to the list
o for pop; e.g.,
o
Pop an item from the list
b for insert before; e.g.,
b
123
456
Finds the value given (first argument), and inserts the value (second argument) before
it.
i for insert after; e.g.,
i
123
456
Finds the value given (first argument), and inserts the value (second argument) after
it.
f for find e.g.,
f
123
Finds an item in the list and prints its memory location and value.
r for remove e.g.,
4
r
123
Removes an item from the list.
c for clear e.g.,
c
Removes all items from the list.
p for print all; e.g.,
p
Print the list on a single line as with the following format: value -> value ->
value...
q to quit the program.
All cases should print the message success if the operation is successful, or fail if the
operation fails.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Experts Guide To Database 2

Authors: Bruce L. Larson

1st Edition

0070232679, 978-0070232679

More Books

Students also viewed these Databases questions

Question

Why We Listen?

Answered: 1 week ago