Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Pokedex_Assignment
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
a-j.towse
Pokedex_Assignment
Commits
1485bc92
Commit
1485bc92
authored
Dec 30, 2022
by
a-j.towse
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Pokedex struct
parent
93f7bd43
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
4 additions
and
199 deletions
+4
-199
main.c
main.c
+4
-199
No files found.
main.c
View file @
1485bc92
...
@@ -8,202 +8,7 @@ Pokedex Assignment
...
@@ -8,202 +8,7 @@ Pokedex Assignment
A-J Towse
A-J Towse
*/
*/
typedef
struct
Pokedex
{
typedef
struct
node
{
struct
node
PokemonListHead
=
NULL
;
int
val
;
struct
node
PlayerListHead
=
NULL
;
}
struct
node
*
next
;
\ No newline at end of file
}
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
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment