here is the finished article all working
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct Node
{
int number;
struct Node *next;
}Node;
typedef struct Linkedlist
{
struct Node *head;
struct Node *tail;
}Linkedlist;
void initializelists(Linkedlist *mylist, Linkedlist *sortedlist);
void populatelist(Linkedlist *mylist);
void appendlist(Linkedlist **list, Node *tmp_node);
Node *getmemory(void);
void sortlist(Linkedlist *mylist, Linkedlist *sortedlist);
void removenode(Linkedlist **mylist, Node *tmp_current, Node *tmp_previous);
void printlist(const Linkedlist list);
void deletelist(Linkedlist *list);
int main()
{
Linkedlist mylist, sortedlist;
srand(time(NULL));
initializelists(&mylist, &sortedlist);
populatelist(&mylist);
printlist(mylist);
sortlist(&mylist, &sortedlist);
printlist(sortedlist);
deletelist(&sortedlist);
return 0;
}
void initializelists(Linkedlist *mylist, Linkedlist *sortedlist)
{
mylist->head = NULL;
mylist->tail = NULL;
sortedlist->head = NULL;
sortedlist->tail = NULL;
}
Node *getmemory(void)
{
Node *tmp_node = NULL;
tmp_node = malloc(sizeof(*tmp_node));
if (!tmp_node)
{
printf("error aquiring memory\n");
exit(EXIT_FAILURE);
}
return tmp_node;
}
void appendlist(Linkedlist **list, Node *tmp_node)
{
if (!(*list)->head) // list empty
{
(*list)->head = tmp_node;
(*list)->tail = (*list)->head;
}
else if (!(*list)->head->next) //second node
{
(*list)->head->next = tmp_node;
(*list)->tail = tmp_node;
(*list)->tail->next = NULL;
}
else // another node to append
{
tmp_node->next = NULL;
(*list)->tail->next = tmp_node;
(*list)->tail = tmp_node;
}
}
void populatelist(Linkedlist *mylist)
{
int i;
Node *tmp_node = NULL;
for (i = 20; i < 90; i += 10)
{
tmp_node = getmemory();
tmp_node->number = rand() % i * 10 + 1;
appendlist(&mylist, tmp_node);
}
}
void printlist(const Linkedlist list)
{
Node *current = list.head;
while (current)
{
printf("number = %d\n", current->number);
current = current->next;
}
}
void deletelist(Linkedlist *list)
{
Node *current = list->head, *tmp_node;
while (current)
{
tmp_node = current->next;
free(current);
printf("node freed\n");
current = tmp_node;
}
}
void removenode(Linkedlist **mylist, Node *tmp_current, Node *tmp_previous)
{
if (tmp_current == (*mylist)->head)//smallest current is the head
{
(*mylist)->head = (*mylist)->head->next;
tmp_current->next = NULL;
}
else if (!tmp_current->next) //smallest current is the tail
{
(*mylist)->tail = tmp_previous;
tmp_previous->next = NULL;
}
else //middle node
{
tmp_previous->next = tmp_current->next;
tmp_current->next = NULL;
}
}
void sortlist(Linkedlist *mylist, Linkedlist *sortedlist)
{
int smallest;
Node *current = NULL, *previous = NULL, *smallest_current, *smallest_previous;
while (mylist->head)
{
current = mylist->head;
previous = NULL;
smallest = current->number;
smallest_current = current;
smallest_previous = previous;
while (current)
{
if (smallest > current->number)
{
smallest = current->number;
smallest_current = current;
smallest_previous = previous;
}
previous = current;
current = current->next;
}
//remove node from list
printf("removing %d\n", smallest_current->number);
removenode(&mylist, smallest_current, smallest_previous);
//add node to sorted list
appendlist(&sortedlist, smallest_current);
}
}
coop