Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
header file for linked list functions
- Loading branch information
1 parent
a63b052
commit 7f89e1d
Showing
1 changed file
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#ifndef LIST_H | ||
#define LIST_H | ||
|
||
// This is a little fancier than what we've been doing, so we'll | ||
// use a "smarter" linked list that keeps track of the root node | ||
typedef struct node | ||
{ | ||
int data; | ||
struct node *next; | ||
} Node; | ||
|
||
typedef struct list | ||
{ | ||
int size; | ||
Node *head; | ||
} List; | ||
|
||
List * createList(int value) | ||
{ | ||
Node *root = NULL; | ||
root = malloc(sizeof(Node)); | ||
|
||
root -> data = value; | ||
root -> next = NULL; | ||
|
||
List *list = NULL; | ||
list = malloc(sizeof(List)); | ||
|
||
list -> size = 0; | ||
list -> head = root; | ||
|
||
return list; | ||
} | ||
|
||
Node * appendToList(int value, List *list) | ||
{ | ||
Node *currentNode = list -> head; | ||
|
||
// Traverse to the end of the list | ||
while (currentNode -> next != NULL) | ||
{ | ||
currentNode = currentNode -> next; | ||
} | ||
|
||
Node *newNode = (Node *) malloc(sizeof(Node)); | ||
newNode -> data = value; | ||
newNode -> next = NULL; | ||
|
||
currentNode -> next = newNode; | ||
|
||
list -> size++; | ||
|
||
return newNode; | ||
} | ||
|
||
Node * prependToList(int value, List *list) | ||
{ | ||
// Given the current root, create and set a new one | ||
Node *newRoot = NULL; | ||
newRoot = malloc(sizeof(Node)); | ||
|
||
newRoot -> data = value; | ||
newRoot -> next = list -> head; | ||
|
||
list -> head = newRoot; | ||
|
||
list -> size++; | ||
|
||
return newRoot; | ||
} | ||
|
||
void deleteNode(int value, List *list) | ||
{ | ||
Node *previousNode = list -> head; | ||
Node *currentNode = previousNode -> next; | ||
|
||
while(previousNode -> data != value && currentNode -> data != value) | ||
{ | ||
previousNode = previousNode -> next; | ||
currentNode = currentNode -> next; | ||
} | ||
|
||
// If the node to be deleted is the head node | ||
if (previousNode -> data == value) | ||
{ | ||
list -> head = list -> head -> next; | ||
} | ||
else | ||
{ | ||
previousNode -> next = previousNode -> next -> next; | ||
} | ||
|
||
list -> size--; | ||
|
||
previousNode = NULL; | ||
currentNode = NULL; | ||
free(previousNode); | ||
free(currentNode); | ||
} | ||
|
||
void displayList(List *list) | ||
{ | ||
Node *node = list -> head; | ||
while (node != NULL) | ||
{ | ||
printf("%d ", node -> data); | ||
node = node -> next; | ||
} | ||
printf("\n"); | ||
} | ||
|
||
#endif | ||
|