Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node
- {
- int val;
- struct node *next;
- } node_l;
- node_l *initNodes( void );
- void addNode (node_l*, int);
- void pushNode(node_l **, int);
- int popNode(node_l **);
- void print_nodes(node_l *);
- void insertNode(node_l **, int, int);
- int removeNode(node_l **, int);
- int searchNode(node_l *, int);
- void *tryalloc(int);
- int main()
- {
- node_l *mylist = initNodes();
- mylist->val = 10;
- addNode(mylist, 20);
- addNode(mylist, 30);
- insertNode(&mylist, 2, 15);
- removeNode(&mylist,searchNode(mylist,15));
- print_nodes(mylist);
- free(mylist);
- return 0;
- }
- void *tryalloc(int s)
- {
- void *ptr;
- ptr = malloc(s);
- if(ptr == NULL)printf("Failed to allocate %d bytes. Memory is full!",s);
- return ptr;
- }
- void print_nodes(node_l *base)
- {
- node_l *current = base;
- while(current != NULL)
- {
- printf("List Item: %d \n",current->val);
- current = current->next;
- }
- }
- int searchNode(node_l *base, int val)
- {
- node_l *current = base;
- register int i = 0;
- while(current != NULL)
- {
- i++;
- if(current->val == val) return i-1;
- current = current->next;
- }
- return -1;
- }
- int removeNode(node_l **base, int index)
- {
- // Verify list is with-in index bounds.
- if(index == 0)
- {
- return popNode(base);
- }
- int ret = -1;
- register int i = 0;
- node_l *tmp = NULL;
- node_l *current = *base;
- while(current != NULL && (i < index-1))
- {
- i++;
- current = current->next;
- }
- if(i < index-1)
- {
- puts("Invalid node index.");
- return -1;
- }
- // Get selected node from index.
- tmp = current->next;
- ret = tmp->val;
- // Set the next node in our list to the node after the selected one we're going to delete.
- current->next = tmp->next;
- // Deallocate selected node.
- free(tmp);
- return ret;
- }
- void insertNode(node_l **base, int index, int val)
- {
- // Verify list is with-in index bounds.
- if(index == 0)
- {
- return pushNode(base, val);
- }
- register int i = 0;
- node_l *new_node = NULL;
- node_l *current = *base;
- while(current != NULL && (i < index-1))
- {
- i++;
- current = current->next;
- }
- if(i < index-1)
- {
- puts("Invalid node index.");
- return;
- }
- // Locate nodes.
- new_node = tryalloc(sizeof(node_l));
- new_node->val = val;
- new_node->next = current->next;
- current->next = new_node;
- }
- int popNode(node_l **base)
- {
- int result = -1;
- // We need to remove the first node and instead point it to the second.
- if(*base == NULL)
- {
- // If we only have 1 item in the list, delete the list.
- return -1;
- }
- // Get next node and assign that to the new header node.
- node_l *next_node = (*base)->next;
- // Assign return result to value.
- result = (*base)->val;
- // Deallocate the memory for previous header node.
- free(*base);
- // Set header node (first node our base points to) to next node in list.
- *base = next_node;
- return result;
- }
- void pushNode(node_l **base, int value)
- {
- // Double pointer used here so we can modify the base pointer.
- node_l *new_base;
- new_base = tryalloc(sizeof(node_l));
- new_base->val = value;
- new_base->next = *base;
- *base = new_base;
- }
- void addNode(node_l *base, int value)
- {
- node_l *current = base;
- while(current->next != NULL)
- {
- current = current->next;
- }
- current->next = NULL;
- current->next = tryalloc(sizeof(node_l));
- current->next->val = value;
- current->next->next = NULL;
- }
- node_l *initNodes()
- {
- // Create pointer to first item in linked-list.
- node_l *head = NULL;
- // Get location in Heap where allocated space is available for first item in this linked list.
- head = tryalloc(sizeof(node_l));
- head->next = NULL;
- return head;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement