Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "list.h"
- #include <stdlib.h>
- #include <stdio.h>
- void list_node_init( struct List_Node* node, int val)
- {
- node->data = val;
- node->next = 0;
- }
- void list_init ( Linked_List* list )
- {
- list->nodeCount = 0;
- list->first = 0;
- list->last = 0;
- }
- void list_clear ( Linked_List* list )
- {
- while ( list->first != 0 ) {
- list_erase( list, 0);
- }
- }
- void list_push_back ( Linked_List* l, u_int val )
- {
- struct List_Node* newNode = (struct List_Node* ) malloc ( sizeof ( newNode ) );
- list_node_init( newNode, val);
- if ( l->first == 0 ) {
- l->first = newNode;
- }
- if( l->last != 0 ) {
- l->last->next = newNode;
- }
- l->last = newNode;
- l->nodeCount++;
- }
- void list_erase ( Linked_List* l, u_int where )
- {
- struct List_Node* temp = 0;
- if ( where == 0 ) {
- temp = l->first;
- l->first = l->first->next;
- l->nodeCount--;
- free ( temp );
- return;
- }
- struct List_Node* conductor = l->first;
- for (int i = 0; i < where - 1; i++ ) {
- conductor = conductor->next;
- }
- temp = conductor->next;
- conductor->next = conductor->next->next;
- free ( conductor );
- l->nodeCount--;
- }
- int list_get_node_val ( Linked_List* list, int where )
- {
- struct List_Node* conductor = list->first;
- for (int i = 0; i < where; i++ ) {
- conductor = conductor->next;
- }
- return conductor->data;
- }
Add Comment
Please, Sign In to add comment