Skip to content

Instantly share code, notes, and snippets.

@borcean
Last active June 10, 2021 10:43
Show Gist options
  • Save borcean/3befa6e00161c62d71c6 to your computer and use it in GitHub Desktop.
Save borcean/3befa6e00161c62d71c6 to your computer and use it in GitHub Desktop.
CS 162 Assignment 5 at Oregon State University

CS 162 Assignment 5 at Oregon State University

Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
/*
* list.c
*
* Created on: June 5, 2014
* Author: Jeffrey Borcean
*/
#include "list.h"
#include <stdio.h>
#include <stdlib.h>
/*********************************************************************
** Function: length
** Description: the number of elements in the list is counted
** Parameters: head
** Pre-Conditions: head exists
** Post-Conditions: the count is returned
*********************************************************************/
int length(struct node *head)
{
struct node *iterator = head;
//iterator = head;
int count = 0;
while(iterator != NULL) //When not null then increment
{ //and set the pointer to the
count++; //next node
iterator = iterator->next;
}
return count;
}
/*********************************************************************
** Function: push
** Description: adds new value to front of list
** Parameters: head_ref, new_val
** Pre-Conditions: n/a
** Post-Conditions: there now exists a new value at the front
*********************************************************************/
void push(struct node **head_ref, int new_val)
{
struct node *temp;
temp = malloc(sizeof(struct node)); //allocate mem for the new node
temp->val = new_val; //then change the temp val to
temp->next = *head_ref; //the value of the new val
*head_ref = temp;
}
/*********************************************************************
** Function: append
** Description: adds to end of linked list
** Parameters: head_ref, new_val
** Pre-Conditions: n/a
** Post-Conditions: value appended to linked list
*********************************************************************/
void append(struct node **head_ref, int new_val)
{
struct node *temp = malloc(sizeof(struct node));
temp->val = new_val;
temp->next = NULL;
struct node *iterator = *head_ref;
if (iterator == NULL)
{
push(head_ref, new_val);
return;
}
while (iterator != NULL)
{
if (iterator->next == NULL)
{
printf("%d\n", iterator->val);
iterator->next = temp;
break;
}
else
iterator = iterator->next;
}
}
/*********************************************************************
** Function: print
** Description: displays the linked list to the user's monitor
** Parameters: head, length
** Pre-Conditions: valid address of head, and int for length
** Post-Conditions: list is printed
*********************************************************************/
void print(struct node *head, int length)
{
int i; //to be used as an index for num of nodes
struct node *iterator = head; //set the new node to the head
for (i = 0; i < length; i++) //print up until you are the length
{
printf("The value of node #%d is: %d\n", i, iterator->val);
iterator = iterator->next;
}
}
/*********************************************************************
** Function: clear
** Description: deletes all items from the list
** Parameters: head
** Pre-Conditions: valid head address passed
** Post-Conditions: the list is freed from memory
*********************************************************************/
void clear(struct node **head) {
struct node *following = NULL;
for(struct node *temp = *head; temp != NULL; temp = following)
{
following = temp->next; // start freeing the memory
free(temp); // go through and free for each
} // location in the memory of the list
(*head) = NULL;
}
/*********************************************************************
** Function: delete
** Description: deletes an item from the list
** Parameters: head, node_val
** Pre-Conditions: valid head address passed
** Post-Conditions: the specified list item is freed from memory
*********************************************************************/
void delete(struct node **head, int node_val){
struct node *node_now = *head;
int i;
i = 0;
while (i < node_val) // delete the value in the node
{ // free the memory for the current node
node_now = node_now->next;
i++;
}
free(node_now);
}
/*********************************************************************
** Function: sort_ascending
** Description: lowest to highest sorting of list
** Parameters: head
** Pre-Conditions: ints in linked list
** Post-Conditions: the numbers in the list are sorted
*********************************************************************/
void sort_ascending(struct node **head) {
int hold; // temporary storage for value
struct node *temp = *head; //init for start address
struct node *following = temp->next;
while(following != NULL)
{
while (temp != following)
{
if (following->val <= temp->val)
{
hold = following->val; //If next then go to check if equal
following->val = temp->val; //place the hold into the value of the
temp->val = hold; //following, then go in for next node
} //repeat, and set the temp to head
temp = temp ->next;
}
following = following->next;
temp = *head;
}
}
/*********************************************************************
** Function: sort_descending
** Description: greatest to least sorting of list
** Parameters: head
** Pre-Conditions: ints in linked list
** Post-Conditions: the numbers in the list are sorted
*********************************************************************/
void sort_descending(struct node** head) {
struct node *temp = *head;
struct node *following = temp->next;
int hold;
while(following != NULL)
{
while (temp != following)
{
if (following->val > temp->val)
{
hold = following->val; //Same as above function with
following->val = temp->val; //the exception that the "<="
temp->val = hold; //has become a ">" to accomodate
} //for the other sort direction
temp = temp ->next;
}
following = following->next;
temp = *head;
}
}
struct node { int val; struct node *next; }; //Implemented in Lab #9 int length(struct node *); void push(struct node **, int); void print(struct node *, int); void append(struct node **, int); void clear(struct node **); void delete(struct node **, int); void sort_ascending(struct node **); //Assign 5 stuff void sort_descending(struct node **); //Assign 5 stuff
all:
gcc -std=c99 list.c test_list.c -o linkedList
clean:
rm linkedList
#include "list.h" #include <stdio.h> #include <stdlib.h> int main (){ char ans; int num; struct node *head = NULL; do { do { printf("Enter a number: "); scanf("%d", &num); push(&head, num);//Can change to append for back printf("Do you want another num (y or n): "); scanf("%1s",&ans); } while(ans == 'y'); printf("Sort ascending or descending (a or d)? "); scanf("%1s",&ans); if(ans == 'a') sort_ascending(&head); else if(ans == 'd') sort_descending(&head); print(head, length(head)); printf("Do you want to do this again (y or n)? "); scanf("%1s",&ans); if(ans == 'y') clear(&head); } while(ans == 'y'); return 0; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment