Skip to content

jmulani/373hw3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

#include <assert.h> #include <stdlib.h>

#include "list.h"

// Takes a valid, sorted list starting at head and adds the element // new_element to the list. The list is sorted based on the value of the // index member in ascending order. Returns a pointer to the head of the list // after the insertion of the new element. list_t* insert_sorted(list_t* head, list_t* new_element) { assert(head != NULL);

assert(new_element != NULL);

list_t* first = head;

list_t* second = head->next;

if(first->index > new_element->index)
{
	new_element->next = head;
	head = new_element;
	return head;
}

while(second != NULL && second->index < new_element->index)
{
	first = first->next;
	second = second->next;
}


first->next = new_element;
new_element->next = second;
return head;

}

// Reverses the order of the list starting at head and returns a pointer to // the resulting list. You do not need to preserve the original list. list_t* reverse(list_t* head) { assert(head != NULL);

list_t* first = head;
list_t* second = head->next;
list_t* temp = NULL;

head->next = NULL;

while(second != NULL)
{
	temp = second->next;
	second->next = first;
	first = second;
	second = temp;
}

head = first;

return head;

}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published