Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct list_node {
  5. int data;
  6. int licznik;
  7. struct list_node *next;
  8. } *list_pointer;
  9.  
  10. struct list_node *create_list(int data)
  11. {
  12. struct list_node *first = (struct list_node *)malloc(sizeof(struct list_node));
  13. if(first) {
  14. first->data = data;
  15. first->licznik = 1;
  16. first->next = NULL;
  17. }
  18. return first;
  19. }
  20.  
  21. struct list_node *add_at_front(struct list_node *list_pointer, struct list_node *new_node)
  22. {
  23. new_node->next = list_pointer;
  24. return new_node;
  25. }
  26.  
  27. struct list_node *find_spot(struct list_node *list_pointer, int data)
  28. {
  29. struct list_node *previous = NULL;
  30. while(list_pointer && list_pointer->data<data) {
  31. previous = list_pointer;
  32. list_pointer = list_pointer->next;
  33. }
  34. return previous;
  35. }
  36.  
  37. void add_in_middle_or_at_back(struct list_node *node, struct list_node *new_node)
  38. {
  39. new_node->next = node->next;
  40. node->next = new_node;
  41. }
  42.  
  43. struct list_node *add_node(struct list_node *list_pointer, int data)
  44. {
  45. struct list_node *new_node = (struct list_node *)malloc(sizeof(struct list_node));
  46. if(list_pointer && new_node) {
  47. new_node->data = data;
  48. list_pointer->licznik = list_pointer->licznik+1;
  49. if(list_pointer->data>=data) {
  50. return add_at_front(list_pointer, new_node);
  51. } else {
  52. struct list_node *node= find_spot(list_pointer,data);
  53. add_in_middle_or_at_back(node,new_node);
  54. }
  55. }
  56. return list_pointer;
  57. }
  58.  
  59.  
  60. void print_list(struct list_node *list_pointer)
  61. {
  62. while(list_pointer) {
  63. printf("%d ",list_pointer->data);
  64. list_pointer=list_pointer->next;
  65. }
  66. puts("");
  67. }
  68.  
  69. int main(void)
  70. {
  71. list_pointer = create_list(0);
  72. int i;
  73. for(i=1; i<5; i++)
  74. list_pointer=add_node(list_pointer,i);
  75. print_list(list_pointer);
  76. printf("Ilosc elemetow w tablicy %d",list_pointer->licznik);
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement