Skip to content

Instantly share code, notes, and snippets.

@zzggbb
Created April 17, 2015 04:01
Show Gist options
  • Select an option

  • Save zzggbb/ee5e22101142d7cbf67a to your computer and use it in GitHub Desktop.

Select an option

Save zzggbb/ee5e22101142d7cbf67a to your computer and use it in GitHub Desktop.
struct t_node {
struct t_node *next;
int value;
} t_node;
struct t_list {
t_node *head;
t_node *tail;
int size;
} t_list;
void insert_node(t_list *list, t_node *node, t_node *new_node) {
if (list->size == 0) {
// list is empty
list->head = new_node;
list->tail = new_node;
list->size = 1;
} else {
if (node == 0) {
// put the new node at the beginning
new_node->next = list->head;
list->head = new_node;
list->size++;
} else {
// insert the new node right after the node
new_node->next = node->next;
node->next = new_node;
if (list->tail == node) {
// we want to insert after the tail
list->tail = new_node;
list->size++;
}
}
}
}
void delete_node(t_list *list, t_node *dnode) {
t_node *node_ptr;
if (list->size == 1) {
list->head = 0;
list->tail = 0;
list->size = 0;
} else {
if (list->head == dnode) {
// we want to delete the head of our list
list->head = dnode->next;
list->size--;
} else {
node_ptr = list->head;
while(node_ptr != 0) {
if (node_ptr->next == dnode) {
if(dnode == list->tail) {
list->tail = node_ptr;
}
node_ptr->next = dnode->next;
list->size--;
return;
}
node_ptr = node_ptr->next;
}
}
}
}
int main() {
t_list list = {0,0,0};
t_node node = {0, 20};
insert_node(&list, 0, &node);
printlist(&list);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment