Commit 1485bc92 authored by a-j.towse's avatar a-j.towse

Pokedex struct

parent 93f7bd43
......@@ -8,202 +8,7 @@ Pokedex Assignment
A-J Towse
*/
typedef struct node {
int val;
struct node *next;
} node;
node* NewNode(int value);
void AddNodeToStartOfList(node **ptr_to_start, int value);
void AddNodeToEndOfList(node **ptr_to_start, int value);
void PrintLinkedList(node *start);
int ListLength(node *start);
void AddNode(node **ptr_to_start,int value);
void SortList(node **start);
int main(void) {
node *head = NULL;
/*AddNodeToStartOfList(&head,2);
AddNodeToEndOfList(&head,4);
AddNodeToEndOfList(&head,6);
PrintLinkedList(head);
printf("List Length: %d\n",ListLength(head));
AddNode(&head,5);
PrintLinkedList(head);*/
AddNodeToStartOfList(&head,5);
AddNodeToEndOfList(&head,7);
AddNodeToEndOfList(&head,2);
AddNodeToEndOfList(&head,1);
AddNodeToEndOfList(&head,4);
AddNodeToEndOfList(&head,3);
SortList(&head);
PrintLinkedList(head);
return 0;
}
node* NewNode(int value)
{
node *new_node = NULL; // Create a pointer to a node structure, set it to NULL for safety
new_node =malloc(sizeof(node)); // Set aside space in memory for a node structure. If this fails new_node will be set to NULL
if (new_node != NULL) // Always ensure the node exists before accessing it.
{
new_node->val = value; // Set data values as required
new_node->next = NULL; // Set next (and any other pointers to NULL)
}
return new_node;
}
void AddNodeToStartOfList(node **ptr_to_start, int value)
{
/*
**ptr_to_start is a variable that stores a pointer to a pointer.
It is the address of the variable which stores the address of the first item in the list.
**ptr_to_start is the address of head
*ptr_to_start is the contents of the variable head, which is the address of the start of the list.
If we change *ptr_to_start (note only one * here) we are changing the contents of head.
We are changing which node is the first node in the list.
*/
node *temp = NewNode(value); // Create a new node
temp->next = *ptr_to_start; // Set the new nodes next pointer to point to the start of the list
*ptr_to_start = temp; // Change head to point to the new node
}
void PrintLinkedList(node *start)
{
// Prints the Linked list from *start
// Note if you pass in the address of a node half way through the list it will print the
// list from that point
// node *temp = start; //
printf("\n");
while (start!=NULL)
{
printf("%d\n",start->val);
start = start->next;
}
}
void AddNodeToEndOfList(node **ptr_to_start, int value)
{
node *new_node = NewNode(value);
node *temp = *ptr_to_start;
// if list is empty
if (*ptr_to_start==NULL)
{
*ptr_to_start = new_node;
return;
}
// If next is NULL we are on the last entry in the list (or we've broken our list!)
while (temp->next != NULL)
{
temp = temp->next; // Change our temp pointer from the current one to temp->next - "moving" us on to the next node
}
temp->next = new_node;
return;
}
int ListLength(node *start) {
int count = 0;
while (start!=NULL) {
count += 1;
start = start->next;
}
return count;
}
void AddNode(node **ptr_to_start,int value) {
node *current = *ptr_to_start;
node *new_node = NewNode(value);
if (*ptr_to_start == NULL) // special case if the list is empty
{
*ptr_to_start = new_node;
return;
}
while (current != NULL )
{
if (current->next->val > value)
{
if (ptr_to_start == current) //if it is smaller than the first value, add to start
{
AddNodeToStartOfList(ptr_to_start,value);
return;
}
else if (current->next == NULL) //if it hits the end of the list/ add to end
{
AddNodeToEndOfList(ptr_to_start,value);
return;
}
else //insert the node in betwen two others
{
node *temp = current->next;
current-> next = new_node;
new_node->next=temp;
return;
}
}
else
{
current = current->next;
}
}
}
void SortList (node **start)
{
node *current = *start;
node *nextnode = current->next;
node *prev = *start;
int count = 0;
for (count=0;count<ListLength(*start)-1;++count)
{
while (nextnode != NULL)
{
if (current->val > nextnode->val)
{
int temp;
temp = current->val;
current->val = nextnode->val;
nextnode->val = temp;
}
prev = current;
current = current->next;
nextnode = current->next;
}
printf("Count: %d",count);
current = *start;
nextnode = current->next;
prev = *start;
}
}
typedef struct Pokedex {
struct node PokemonListHead = NULL;
struct node PlayerListHead = NULL;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment