linked_lists.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright 2011 Institute of Communication and Computer Systems (ICCS)
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. #include "linked_lists/linked_lists.h"
  18. #include "block_header.h"
  19. void set_next(void *ptr, void *next_block) {
  20. get_header(ptr)->pointers.next = (list_node_ptr) next_block;
  21. }
  22. void * get_next(void *ptr) {
  23. return (void *) get_header(ptr)->pointers.next;
  24. }
  25. #ifdef BLOCKS_IN_DLL
  26. void set_previous(void *ptr, void *previous_block) {
  27. if( ptr != NULL) {
  28. get_header(ptr)->pointers.previous = (list_node_ptr) previous_block;
  29. }
  30. }
  31. void * get_previous(void *ptr) {
  32. return (void *) get_header(ptr)->pointers.previous;
  33. }
  34. #endif /* BLOCKS_IN_DLL */
  35. void push_block(void **block, void **starting_node) {
  36. if(*starting_node != NULL) {
  37. #ifdef BLOCKS_IN_DLL
  38. set_previous(*starting_node, *block);
  39. #endif /* BLOCKS_IN_DLL */
  40. set_next(*block, *starting_node);
  41. } else {
  42. set_next(*block, NULL);
  43. }
  44. #ifdef BLOCKS_IN_DLL
  45. set_previous(*block, NULL);
  46. #endif /* BLOCKS_IN_DLL */
  47. *starting_node = *block;
  48. }
  49. #ifdef BLOCKS_IN_SLL
  50. void remove_block(void **block, void **starting_node) {
  51. void *current_node, *previous_node;
  52. /* Traverse a list starting from the starting node until block is found. */
  53. for(current_node = *starting_node; current_node != NULL;
  54. current_node = get_next(current_node)) {
  55. if(current_node == *block) {
  56. if(current_node == *starting_node) {
  57. *starting_node = get_next(*block);
  58. } else {
  59. set_next(previous_node, get_next(*block));
  60. }
  61. break;
  62. }
  63. previous_node = current_node;
  64. }
  65. }
  66. #endif
  67. #ifdef BLOCKS_IN_DLL
  68. void remove_block(void **block, void **starting_node) {
  69. void *previous_block, *next_block;
  70. /* No need to traverse the list, just check if the block is the starting
  71. * node of the list.
  72. */
  73. previous_block = get_previous(*block);
  74. next_block = get_next(*block);
  75. if(*block == *starting_node) {
  76. *starting_node = previous_block;
  77. }
  78. if(previous_block != NULL) {
  79. set_next(previous_block, next_block);
  80. }
  81. if(next_block != NULL) {
  82. set_previous(next_block, previous_block);
  83. }
  84. }
  85. #endif /* BLOCKS_IN_DLL */