Advertisement
kokokozhina

Untitled

Jan 5th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct single_list
  6. {
  7. int data;
  8. single_list *next;
  9. };
  10.  
  11. void make_single_list(int n, single_list** head)
  12. {
  13. if (n > 0)
  14. {
  15. (*head) = new single_list();
  16. cout << "Enter the value ";
  17. cin >> (*head)->data;
  18. (*head)->next = NULL;
  19. make_single_list(n - 1, &((*head)->next));
  20. }
  21. }
  22.  
  23. void print_single_list(single_list *head)
  24. {
  25. if (head != NULL)
  26. {
  27. cout << head->data << " ";
  28. print_single_list(head->next);
  29. }
  30. else
  31. cout << "\n";
  32. }
  33.  
  34. single_list* insert_item_single_list(single_list* head, int number, int dataItem)
  35. {//add item with certain number in single_list
  36. number--;
  37. single_list* newItem = new(single_list);
  38. newItem->data = dataItem;
  39. newItem->next = NULL;
  40. if(head == NULL)//list is empty
  41. head = newItem;
  42. else
  43. {
  44. single_list* current = head;
  45. for(int i=1; i < number && current->next != NULL; i++)
  46. current = current->next;
  47. if(number == 0)//add item at the first place
  48. {
  49. newItem->next = head;
  50. head = newItem;
  51. }
  52. else
  53. {
  54. if(current->next != NULL)//add item not at the first place
  55. newItem->next = current->next;
  56. current->next = newItem;
  57. }
  58. }
  59. return head;
  60. }
  61.  
  62. single_list* delete_item_single_list(single_list* head, int number)
  63. {//delete item with certain number from single_list
  64. single_list* ptr;
  65. single_list *current = head;
  66. for(int i = 1; i < number && current != NULL; i++)
  67. current = current->next;
  68. if(current != NULL)
  69. {
  70. if(current == head)//delete item == first
  71. {
  72. head = head->next;
  73. delete(current);
  74. current = head;
  75. }
  76. else
  77. {
  78. ptr = head;
  79. while(ptr->next != current)
  80. ptr = ptr->next;
  81. ptr->next = current->next;
  82. delete(current);
  83. current = ptr;
  84. }
  85. }
  86. return head;
  87. }
  88.  
  89. bool find_item_single_list(single_list* head, int dataItem)
  90. {
  91. single_list* ptr;
  92. ptr = head;
  93. while(ptr != NULL)
  94. {
  95. if(dataItem == ptr->data)
  96. return true;
  97. else
  98. ptr = ptr->next;
  99. }
  100. return false;
  101. }
  102.  
  103.  
  104. void delete_single_list(single_list* head)
  105. {
  106. if(head != NULL)
  107. {
  108. delete_single_list(head->next);
  109. delete head;
  110. }
  111. }
  112.  
  113.  
  114. int main()
  115. {
  116. single_list *head;
  117. int n;
  118. cout << "How many elements would be? ";
  119. cin >> n;
  120. make_single_list(n, &head);
  121. print_single_list(head);
  122. cout << "Enter the number for addition(1-indexation) ";
  123. int number, dataItem;
  124. cin >> number;
  125. cout << "Enter the dataItem for addition ";
  126. cin >> dataItem;
  127. head = insert_item_single_list(head, number, dataItem);
  128. print_single_list(head);
  129. cout << "Enter the number for deleting ";
  130. cin >> number;
  131. head = delete_item_single_list(head, number);
  132. print_single_list(head);
  133. cout << "What do you want to find? ";
  134. cin >> dataItem;
  135. cout << find_item_single_list(head, dataItem) << endl;
  136. delete_single_list(head);
  137. return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement