Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. /*Napisz program z podzia³em na funkcje z parametrami, w którym u¿yjesz dwukierunkowej
  5. listy cyklicznej do przechowywania ci¹gów znaków o d³ugoœci nieprzekraczaj¹cej 20 znaków.
  6. Dodawanie nowych znaków nale¿y zakoñczy po tym jak u¿ytkownik wprowadzi ci¹g
  7. „koniec”.*/
  8.  
  9.  
  10. struct dll_node
  11. {
  12. char data[20];
  13. struct dll_node *prev, *next;
  14. };
  15.  
  16. struct dll_node *create_list(char data[20])
  17. {
  18. struct dll_node *front=(struct dll_node*)malloc(sizeof(struct dll_node));
  19. if(front!=NULL)
  20. {
  21. front->data[20]=data;
  22. front->prev=front->next=NULL;
  23. }
  24. return front;
  25. }
  26.  
  27. struct dll_node *insert_front(struct dll_node *front, struct dll_node *new_node)
  28. {
  29. new_node->next=front;
  30. front->prev=new_node;
  31. return new_node;
  32. }
  33.  
  34. void insert_back(struct dll_node *back, struct dll_node *new_node)
  35. {
  36. back->next=new_node;
  37. new_node->prev=back;
  38. }
  39.  
  40. void insert_after(struct dll_node *node, struct dll_node *new_node)
  41. {
  42. new_node->prev=node;
  43. new_node->next=node->next;
  44. node->next->prev=new_node;
  45. node->next=new_node;
  46. }
  47.  
  48. struct dll_node *find_spot(struct dll_node *front, char data[20])
  49. {
  50. struct dll_node *prev=NULL;
  51. while((front!=NULL) && (front->data>data))
  52. {
  53. prev=front;
  54. front=front->next;
  55. }
  56. return prev;
  57. }
  58.  
  59. struct dll_node *insert_node(struct dll_node *front, char data[20])
  60. {
  61. if(front==NULL)
  62. return NULL;
  63. struct dll_node *new_node=(struct dll_node*)malloc(sizeof(struct dll_node));
  64. if(new_node!=NULL)
  65. {
  66. new_node->data[20]=data;
  67. new_node->prev=new_node->next=NULL;
  68. if(front->data<=data)
  69. {
  70. return insert_front(front,new_node);
  71. }
  72. else
  73. {
  74. struct dll_node *node=find_spot(front,data);
  75. if(node->next!=NULL)
  76. {
  77. insert_after(node, new_node);
  78. }
  79. else
  80. {
  81. insert_back(node, new_node);
  82. }
  83. }
  84. }
  85. return front;
  86. }
  87.  
  88.  
  89. struct dll_node *find_node(struct dll_node *front, char data[20])
  90. {
  91. while((front!=NULL) && (front->data!=data))
  92. {
  93. front=front->next;
  94. }
  95. return front;
  96. }
  97.  
  98.  
  99.  
  100. void remove_list(struct dll_node **front)
  101. {
  102. while(*front!=NULL)
  103. {
  104. struct dll_node *next=(*front)->next;
  105. free(*front);
  106. *front=next;
  107. }
  108. }
  109.  
  110. void print_list(struct dll_node *front)
  111. {
  112. for(;front!=NULL;front=front->next)
  113. {
  114. printf("%s\n",front->data);
  115. }
  116. puts(" ");
  117. }
  118.  
  119.  
  120. int main(void)
  121. {
  122. struct dll_node *front=create_list(0);
  123.  
  124. char input[20];
  125.  
  126. while(1)
  127. {
  128. scanf("%s",&input);
  129. if(!strncmp(input,"koniec",6))
  130. break;
  131. insert_node(&(*front),input);
  132. }
  133.  
  134. print_list(&(*front));
  135. return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement