Created
June 20, 2010 03:12
-
-
Save azisaka/445522 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
#include <stdlib.h> | |
#include <stdio.h> | |
struct node { | |
int value; | |
struct node *next; | |
}; | |
typedef struct node node; | |
node *add_item(node *head, int value) { | |
node *current = malloc(sizeof(node)); | |
current->value = value; | |
current->next = head; | |
return current; | |
} | |
void delete_node(node *head, int position) { | |
node *current = head; | |
int i = 1; | |
while(i < position) { | |
current = current->next; | |
i++; | |
} | |
node *item = current->next; | |
printf("Removing item #%i: %i\n", i, item->value); | |
current->next = item->next; | |
free(item); | |
} | |
void list_items(node *head) { | |
node *current = head; | |
while(current) { | |
printf("%i\n", current->value); | |
current = current->next; | |
} | |
} | |
int list_size(node *head) { | |
node *current = head; | |
int size = 0; | |
while(current) { | |
size++; | |
current = current->next; | |
} | |
return size; | |
} | |
int main () { | |
node *head = NULL; | |
int i; | |
for(i = 0; i < 10; i++) head = add_item(head, i); | |
delete_node(head, 7); // Position #7 | |
list_items(head); | |
printf("Total of items: %i\n", list_size(head)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment