Skip to content

Instantly share code, notes, and snippets.

@zachelko
Created April 21, 2010 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachelko/373335 to your computer and use it in GitHub Desktop.
Save zachelko/373335 to your computer and use it in GitHub Desktop.
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