Question
Demo 1 Below you will find a crude implementation of a social network in C. It doesn't work though. Why? (From Lecture Notes) #include #include
Demo 1
Below you will find a crude implementation of a social network in C. It doesn't work though. Why? (From Lecture Notes)
#include |
Demo 2
This is an incomplete implementation of a linked list. Your instructor will walk you through the code step by step
#include void printCount(Node* listHead) { printf("printing count: ");
// TODO: implement } Node* deleteFirst(Node* listHead) { printf("deleting first element from list: "); // TODO: implement return listHead; } Node* deleteLast(Node* listHead) { printf("deleting last element from list: "); // TODO: implement return listHead; } Node* deleteSpecific(Node *listHead, int dataValue) { // assume data to delete always exists and is always unique in // this exercise printf("deleting %d from list ", dataValue); // TODO: implement! return listHead; } void printBackwardsFromZero(Node* listHead) { printf("printing list in backwards from the first 0 element (or end of list): "); // TODO: implement.. printf(" "); } int main() { Node *myHead = NULL; // head of the linked list, not really MY HEAD! myHead = insert(myHead, 2000); myHead = insert(myHead, 2018); printList(myHead); myHead = insert(myHead, 2001); myHead = insert(myHead, 1998); myHead = insert(myHead, 0); myHead = insert(myHead, 1999); myHead = insert(myHead, 2021); myHead = insert(myHead, 2020); printList(myHead); printCount(myHead); myHead = deleteFirst(myHead); printList(myHead); myHead = deleteLast(myHead); printList(myHead); myHead = deleteSpecific(myHead, 1998); printList(myHead); myHead = deleteSpecific(myHead, 2018); printList(myHead); printBackwardsFromZero(myHead); return 0; } |
In the above example, we have a partial implementation of linked list in C. Don't be scared of the pointers!
The main function is not fully working as some code is not implemented yet. However, we can see that the insert and print functions are working properly.
Notice that:
- As mentioned, we reference a linked list by the list's first Node ("head")
- Please notice the various use of pointers in the example; do you recall the 3 rules of using pointers? (Please refer to lecture notes if not)
- There are two insertion cases - if the list is empty, we need to create a new Node as the head; otherwise, we will append the Node in the end in this example
- We NEED to return a pointer to Node in insert because the list head pointer may change! We need to do the same for every operation that may change the list head
Problem 2
Please try to implement the unimplemented deleteFirst function in the Demo 2 program.
The deleteFirst function, as the name suggests, deletes the first element of the list. To do this, simply point the listHead to the second element in the list! You may need to handle the special case of listHead pointing to NULL (i.e. an empty list).
If done correctly, your output should look like this:
inserting first element with data 2000 inserting element with data 2018 printing list: 2000 2018 inserting element with data 2001 inserting element with data 1998 inserting element with data 0 inserting element with data 1999 inserting element with data 2021 inserting element with data 2020 printing list: 2000 2018 2001 1998 0 1999 2021 2020 printing count: 8 deleting first element from list: printing list: 2018 2001 1998 0 1999 2021 2020 ... |
Please paste your completed function here. Good job!
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