Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define SIZE_OF_LIST 10
- struct node
- {
- int data;
- struct node* link;
- };
- int GetListLength(struct node** list);
- void PrintHRLine();
- void PrintListData(struct node** list);
- void AddLastNode(struct node** list, struct node** new_node);
- void SearchNode(struct node** list, int data);
- void RemoveNodeByIndex(struct node** list, int index);
- void InsertNode(struct node** list, struct node** new_node, int position);
- struct node* GetLastNode(struct node** list);
- struct node* GetNodeByIndex(struct node** list, int index);
- int main()
- {
- struct node* head = NULL;
- struct node* item = NULL;
- struct node* edit_node = NULL;
- int i;
- head = (struct node*)malloc(sizeof(struct node));
- item = (struct node*)malloc(sizeof(struct node) * 10);
- edit_node = (struct node*)malloc(sizeof(struct node));
- head->data = 999;
- head->link = item;
- // Create a list of 10 elements
- for (i = 0; i < SIZE_OF_LIST; i++)
- {
- (item+i)->data = i;
- printf("(item+%d)->data = %d\n",i,i);
- if (i<SIZE_OF_LIST-1)
- {
- (item+i)->link = (item+i+1);
- printf("(item+%d->link = (item+%d+1);\n", i, i);
- printf("(item+%d adress = 0x%lx\n",i, (unsigned long int) &item+i);
- }
- else
- {
- (item+i)->link = NULL;
- printf("(item+%d->link = NULL;\n", i);
- }
- }
- // RemoveNodeByIndex(head, 5);
- // PrintListData(&head);
- edit_node->data = 1001;
- AddLastNode(&head, &edit_node);
- SearchNode(&head, 101);
- RemoveNodeByIndex(&head, 8);
- PrintListData(&head);
- // Free memory
- free(item);
- free(edit_node);
- free(head);
- return 0;
- }
- void PrintListData(struct node** list)
- {
- int index = 0;
- struct node* current = *list;
- if (current)
- {
- while (current != NULL)
- {
- printf("List[%d] = %d\n", index, current->data);
- index++;
- current = current->link;
- }
- printf("In total %d items in list\n", index);
- }
- }
- int GetListLength(struct node** list)
- {
- // Return length of list
- /* While node<-link != null
- increase node
- increase count
- return count
- */
- struct node* current = *list;
- int len = 0;
- if (current != NULL)
- {
- len++;
- while (current->link != NULL)
- {
- len++;
- current = current->link;
- }
- }
- return len;
- }
- struct node* GetLastNode(struct node** list)
- {
- // Return the last node of the list
- struct node* current = *list;
- if (current)
- {
- while (current->link != NULL)
- {
- current = current->link;
- }
- }
- else
- {
- return NULL;
- }
- return current;
- }
- struct node* GetNodeByIndex(struct node** list, int index)
- {
- // Return node by an index given
- // if error it returns NULL
- // for i < index
- // next_node <- node.link
- // node <- next.node
- // return next_node
- struct node* current = *list;
- int counter = 0;
- if (current)
- {
- while (current->link != NULL)
- {
- if (counter == index)
- return current;
- current = current->link;
- counter++;
- }
- }
- else
- {
- return NULL;
- }
- return NULL;
- }
- void AddLastNode(struct node** list, struct node** new_node)
- {
- // Add an extra node to the list at the last position
- struct node* curr_new = *new_node;
- struct node* curr_list = *list;
- struct node* current = GetLastNode(&curr_list);
- PrintHRLine();
- printf("Trying add node to last\n");
- printf("Data in new node = %d\n", curr_new->data);
- current->link = *new_node;
- printf("Data in current last node = %d\n", current->data);
- PrintHRLine();
- }
- void AddStartNode(struct node** head, struct node** new_node)
- {
- // Add an extra node to the lit at the start position
- // This won't do it bc head is the entrance to the list
- struct node* new = *new_node;
- new->link = *head;
- }
- void SearchNode(struct node** list, int data)
- {
- // Search the node for given data
- struct node* current = *list;
- int index = 0;
- PrintHRLine();
- printf("Searching list for item with value = %d\n", data);
- if (current)
- {
- while (current != NULL)
- {
- if (current->data == data)
- {
- printf("FOUND! at adress :0x%lx , index = %d\n", (unsigned long int) ¤t,index);
- break;
- }
- index++;
- current = current->link;
- }
- }
- PrintHRLine();
- }
- void RemoveNodeByIndex(struct node** head,int index) // NOT WORKING YET
- {
- // Remove node list
- struct node* current = GetNodeByIndex(*(&head), index);
- struct node* prev = GetNodeByIndex(*(&head), index);
- // For debugging
- PrintHRLine();
- printf("Deleting item %d from list. Located : 0x%lx\n", index, (unsigned long int) ¤t);
- printf("Item %d data = %d\n", index, current->data);
- PrintHRLine();
- // Change link from prev link node to the next
- prev->link = current->link;
- // Unlink node wished to delete
- current->link = NULL;
- current->data = 0;
- // Free data
- free((struct node*) current);
- }
- void InsertNode(struct node** list, struct node** new_node, int position)
- {
- // Insert node list
- if (position == 0)
- {
- // Head node
- }
- else if (position == GetListLength(list))
- {
- // End nodoe
- }
- else
- {
- struct node* prev = GetNodeByIndex(&(*list), position-1);
- struct node* next = GetNodeByIndex(&(*list), position);
- struct node* new = *new_node;
- new->link = (struct node*) prev->link;
- prev->link = *new_node;
- }
- }
- void PrintHRLine()
- {
- printf("===================================\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement