Created
April 21, 2010 02:12
-
-
Save zachelko/373335 to your computer and use it in GitHub Desktop.
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
typedef struct Node | |
{ | |
int val; | |
struct Node * next; | |
} Node; | |
void insert(Node** head, Node* toIns) | |
{ | |
toIns->next = *head; | |
*head = toIns; | |
} | |
int length(Node* head) | |
{ | |
int count = 0; | |
while (head) | |
{ | |
++count; | |
head = head->next; | |
} | |
return count; | |
} | |
int contains(Node* head, int data) | |
{ | |
while (head) | |
{ | |
if (head->val == data) | |
{ | |
return 1; | |
} | |
head = head->next; | |
} | |
return 0; | |
} | |
void delete(Node** head, Node* toDel) | |
{ | |
Node* temp = *head; | |
if (toDel == *head) | |
{ | |
*head = (*head)->next; | |
free(toDel); | |
} else | |
{ | |
while (temp) | |
{ | |
if (temp->next == toDel) | |
{ | |
temp->next = toDel->next; | |
free(toDel); | |
} | |
temp = temp->next; | |
} | |
} | |
} | |
int count(Node* head, int data) | |
{ | |
int count = 0; | |
while (head) | |
{ | |
if (head->val == data) | |
{ | |
++count; | |
} | |
head = head->next; | |
} | |
return count; | |
} | |
Node* reverse(Node* head) | |
{ | |
Node* current = NULL; | |
Node* previous = NULL; | |
while (head != NULL) | |
{ | |
current = head; | |
head = head->next; | |
current->next = previous; | |
previous = current; | |
} | |
// This now holds the head element | |
return previous; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment