Linked List Using C Homework Help: The Problems: PS3.0: Node and List
Linked List Using C Homework Help: The Problems: PS3.0: Node and List
The problems:
PS3.0: Node and List
Before you can work on the list operations, you'll need to create
both a node and a list. The node should be responsible for node-y
things, and the list for list-y things, in other words, they won't
know much about each other. The list will accept a filled-in node
and add it to the list.
The node should be a struct, and it looks about the same as the
one we've seen in lectures:
Here are the signatures for both node's and list's functions. Note
that this is just the starting set of functions; each problem will
require you to (possibly) add new functions to one or both of list
and node.
For nodes:
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
node* createNode(int value); //Create a new node with a
given value
For lists:
bool addNode(node* node); //Add a node to the list
node* findNode(int value); //Find a node in the
list bool deleteNode (node* node); //Delete a node in the
list
void printList(void); //Print the values in
the list
[89, 39, 18, 96, 71, 25, 2, 55, 60, -8, 9, 42, 69, 96, 24]
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
PS3.2 Delete the largest nodes
From main, ask the list to delete all of the nodes that contain the
largest value in the list.
The signature for this function will be similar to
void deleteLargest(void);
the same largest value (there should be more than one), and then
call the internal deleteNode function to delete all of the nodes
found. Hint: You can use an array to collect the largest nodes as
you find them.
From main, ask the list to return the number of nodes in the list,
and print the value.
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
PS3.4 Sort the list
•Starting from the top of the list, compare the value in the
first node with the value in the second node
•If the value in the second node is smaller, swap the two
nodes, otherwise leave them in place
•Move to the second node and compare its value with the
third node, swapping them if necessary
•Repeat this until you've reached the end of the list
•Next, go back to the top and do it all over again
•The list is sorted when you walk the entire list from top to
bottom and do not perform any swaps
The list should be sorted in-place. In other words, don't use
a second list. Print the list
Solution:
List:
#include <stdio.h>
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
#include <stdlib.h>
#include "list.h"
if (head == NULL)
{
// This is add node to empty list. Just assign head to new
node head = new_node;
}
else
{
// This is list with elment. Try to add new node to end of
list node *current_node = head;
while (current_node->next != NULL)
{
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
current_node = current_node->next;
}
// Now we are in end of list. Just add next to
current node current_node->next = new_node;
}
return true;
}
//Find a node in the list node* findNode(int value)
{
node *current_node = head; while (current_node !=
NULL)
{
if (current_node->value == value)
{
// If this node contain value is the same. Just
return it return current_node;
}
// Go the next node for next check current_node =
current_node->next;
}
// Go to end of node. And we can not found
anything. Just return null. return NULL;
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
}
//Delete a node in the list bool deleteNode(node
*del_node)
{
if (head == NULL)
{
// Current list is empty. We can not delete any more
return false;
}
else if (head->value == del_node->value)
{
// If this is delete the head node. node *tmp = head-
>next; free(head);
head = tmp; return true;
}
else
{
// If this is delete the middle node node *current_node =
head;
node *prev_node = NULL;
while (current_node != NULL)
{
if (current_node->value == del_node->value)
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
{
// Find the node to be delete. prev_node->next =
current_node->next; free(current_node);
return true;
}
// If this is not a node delete. Just move to next
node for
checking.
prev_node = current_node; current_node =
current_node->next;
}
}
// Can not found any node in list for remove.
return false;
}
//Print the values in the list void printList(void)
{
node *current_node = head; while (current_node)
{
printf("%d ", current_node->value); current_node = current_node-
>next;
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
}
printf("\n");
}
void deleteLargest(void)
{
// First step is find the largest node int largest_value = head-
>value;
node *current_node = head->next; while (current_node)
{
if (current_node->value > largest_value)
{
// If found new node largest. Just keep track it. largest_value
= current_node->value;
}
// Move to next node for check current_node = current_node-
>next;
}
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
del_node->next = NULL;
// Loop until no more largest value in node. while
(findNode(largest_value) != NULL)
{
deleteNode(del_node);
}
free(del_node);
}
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
node* current_node = head; node*
compare_node = NULL;
while(current_node) {
compare_node = current_node->next;
while(compare_node) {
if(compare_node->value <
current_node->value) {
// We need to swap 2 node
int tmp_value = current_node->value;
current_node->value = compare_node-
>value; compare_node->value =
tmp_value;
}
compare_node = compare_node->next;
}
current_node = current_node->next;
}
}
}
Main:
#include <stdio.h>
#include <stdlib.h>
#include "node.h"
#include "list.h"
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers
// PS3.4 - Sort the list printf("PS3.4 - List after sort: ");
sort();
printList();
// j o h n n y t u o t - g m a i l - c o m return 0;
}
Node:
#include <stdio.h>
#include <stdlib.h>
#include "node.h"
© 2022 All Rights Reserved. Programming Homework Help | Expert Homework Helpers