Advertisement
Guest User

Untitled

a guest
Nov 6th, 2014
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define SIZE_OF_LIST 10
  5.  
  6. struct node
  7. {
  8.   int data;
  9.   struct node* link;
  10. };
  11.  
  12. int GetListLength(struct node** list);
  13. void PrintHRLine();
  14. void PrintListData(struct node** list);
  15. void AddLastNode(struct node** list, struct node** new_node);
  16. void SearchNode(struct node** list, int data);
  17. void RemoveNodeByIndex(struct node** list, int index);
  18. void InsertNode(struct node** list, struct node** new_node, int position);
  19. struct node* GetLastNode(struct node** list);
  20. struct node* GetNodeByIndex(struct node** list, int index);
  21.  
  22. int main()
  23. {
  24.   struct node* head = NULL;
  25.   struct node* item = NULL;
  26.   struct node* edit_node = NULL;
  27.  
  28.   int i;
  29.  
  30.   head = (struct node*)malloc(sizeof(struct node));
  31.   item = (struct node*)malloc(sizeof(struct node) * 10);
  32.   edit_node = (struct node*)malloc(sizeof(struct node));
  33.  
  34.   head->data = 999;
  35.   head->link = item;
  36.  
  37.   // Create a list of 10 elements
  38.   for (i = 0; i < SIZE_OF_LIST; i++)
  39.     {
  40.       (item+i)->data = i;
  41.       printf("(item+%d)->data = %d\n",i,i);
  42.       if (i<SIZE_OF_LIST-1)
  43.     {
  44.       (item+i)->link = (item+i+1);
  45.       printf("(item+%d->link = (item+%d+1);\n", i, i);
  46.       printf("(item+%d adress = 0x%lx\n",i, (unsigned long int) &item+i);
  47.     }
  48.       else
  49.     {
  50.       (item+i)->link = NULL;
  51.       printf("(item+%d->link = NULL;\n", i);
  52.     }
  53.     }
  54.  
  55.   // RemoveNodeByIndex(head, 5);
  56.   // PrintListData(&head);
  57.   edit_node->data = 1001;
  58.   AddLastNode(&head, &edit_node);
  59.   SearchNode(&head, 101);
  60.   RemoveNodeByIndex(&head, 8);
  61.   PrintListData(&head);
  62.  
  63.   // Free memory
  64.   free(item);
  65.   free(edit_node);
  66.   free(head);
  67.  
  68.   return 0;
  69. }
  70.  
  71.  
  72. void PrintListData(struct node** list)
  73. {
  74.   int index = 0;
  75.   struct node* current = *list;
  76.   if (current)
  77.     {
  78.       while (current != NULL)
  79.     {
  80.       printf("List[%d] = %d\n", index, current->data);
  81.       index++;
  82.       current = current->link;
  83.     }
  84.       printf("In total %d items in list\n", index);
  85.     }
  86. }
  87.  
  88. int GetListLength(struct node** list)
  89. {
  90.   // Return length of list
  91.   /* While node<-link != null
  92.          increase node
  93.          increase count
  94.      return count
  95.   */
  96.   struct node* current = *list;
  97.   int len = 0;
  98.  
  99.   if (current != NULL)
  100.     {
  101.       len++;
  102.       while (current->link != NULL)
  103.     {
  104.       len++;
  105.       current = current->link;
  106.     }
  107.     }
  108.   return len;
  109. }
  110.  
  111.    
  112.  
  113. struct node* GetLastNode(struct node** list)
  114. {
  115.   // Return the last node of the list
  116.   struct node* current = *list;
  117.  
  118.   if (current)
  119.     {
  120.       while (current->link != NULL)
  121.     {
  122.       current = current->link;
  123.     }
  124.     }
  125.   else
  126.     {
  127.       return NULL;
  128.     }
  129.  
  130.   return current;
  131. }
  132.  
  133.  
  134.  
  135. struct node* GetNodeByIndex(struct node** list, int index)
  136. {
  137.   // Return node by an index given
  138.   // if error it returns NULL
  139.   // for i < index
  140.   //       next_node <- node.link
  141.   //     node <- next.node
  142.   // return next_node
  143.  
  144.   struct node* current = *list;
  145.   int counter = 0;
  146.  
  147.   if (current)
  148.     {
  149.       while (current->link != NULL)
  150.     {
  151.       if (counter == index)
  152.         return current;
  153.      
  154.       current = current->link;
  155.       counter++;
  156.     }
  157.     }
  158.   else
  159.     {
  160.       return NULL;
  161.     }
  162.  
  163.   return NULL;
  164. }
  165.  
  166.  
  167. void AddLastNode(struct node** list, struct node** new_node)
  168. {
  169.   // Add an extra node to the list at the last position
  170.   struct node* curr_new = *new_node;
  171.   struct node* curr_list = *list;
  172.   struct node* current = GetLastNode(&curr_list);
  173.   PrintHRLine();
  174.   printf("Trying add node to last\n");
  175.   printf("Data in new node = %d\n", curr_new->data);
  176.   current->link = *new_node;
  177.   printf("Data in current last node = %d\n", current->data);
  178.   PrintHRLine();
  179. }
  180.  
  181.  
  182. void AddStartNode(struct node** head, struct node** new_node)
  183. {
  184.   // Add an extra node to the lit at the start position
  185.   // This won't do it bc head is the entrance to the list
  186.   struct node* new = *new_node;
  187.   new->link = *head;
  188. }
  189.  
  190.  
  191. void SearchNode(struct node** list, int data)
  192. {
  193.   // Search the node for given data
  194.   struct node* current = *list;
  195.   int index = 0;
  196.   PrintHRLine();
  197.   printf("Searching list for item with value = %d\n", data);
  198.   if (current)
  199.     {
  200.       while (current != NULL)
  201.     {
  202.       if (current->data == data)
  203.         {
  204.           printf("FOUND! at adress :0x%lx , index = %d\n", (unsigned long int) &current,index);
  205.           break;
  206.         }
  207.       index++;
  208.       current = current->link;
  209.     }
  210.     }
  211.  
  212.   PrintHRLine();
  213. }
  214.  
  215.  
  216. void RemoveNodeByIndex(struct node** head,int index) // NOT WORKING YET
  217. {
  218.   // Remove node list
  219.   struct node* current = GetNodeByIndex(*(&head), index);
  220.   struct node* prev    = GetNodeByIndex(*(&head), index);
  221.  
  222.   // For debugging
  223.   PrintHRLine();
  224.   printf("Deleting item %d from list. Located : 0x%lx\n", index, (unsigned long int) &current);
  225.   printf("Item %d data = %d\n", index, current->data);
  226.   PrintHRLine();
  227.   // Change link from prev link node to the next
  228.   prev->link = current->link;
  229.  
  230.   // Unlink node wished to delete
  231.   current->link = NULL;
  232.   current->data = 0;
  233.  
  234.   // Free data
  235.   free((struct node*) current);
  236. }
  237.  
  238.  
  239.  
  240. void InsertNode(struct node** list, struct node** new_node, int position)
  241. {
  242.   // Insert node list
  243.  
  244.   if (position == 0)
  245.     {
  246.       // Head node
  247.     }
  248.  else if (position == GetListLength(list))
  249.    {
  250.      // End nodoe
  251.    }
  252.  else
  253.     {    
  254.       struct node* prev = GetNodeByIndex(&(*list), position-1);
  255.       struct node* next = GetNodeByIndex(&(*list), position);
  256.       struct node* new  = *new_node;
  257.       new->link = (struct node*) prev->link;
  258.       prev->link     = *new_node;
  259.     }
  260.  
  261. }
  262.  
  263. void PrintHRLine()
  264. {
  265.   printf("===================================\n");
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement