Advertisement
Guest User

Untitled

a guest
May 26th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. struct node
  7. {
  8. int key;
  9. node *next = NULL;
  10. };
  11.  
  12. struct list
  13. {
  14. node *head = NULL;
  15. };
  16.  
  17. bool list_search (list *L, node *k)
  18. {
  19. node *x=L->head;
  20. while ((x->key!=k->key) && (x!=NULL))
  21. x=x->next;
  22.  
  23. if (x->key==k->key)
  24. return true;
  25. else
  26. return false;
  27. }
  28.  
  29. void list_insert1(list *L, node *x)
  30. {
  31. x->next=L->head;
  32. L->head=x;
  33. }
  34.  
  35. void list_insert2(list *L, node *x,node *y)
  36. {
  37. node *P=L->head;
  38.  
  39. while (P != NULL)
  40. {
  41. if(P->key==x->key)
  42. {
  43. y->next=P->next;
  44. P->next=y;
  45. }
  46. P = P->next;
  47. }
  48. }
  49.  
  50. int main ()
  51. {
  52. list *L = new list;
  53. for (int i=1;i<=5;i++)
  54. {
  55. node *z= new node;
  56. z->key = i;
  57. list_insert1(L,z);
  58. }
  59.  
  60. int x,y;
  61. cout<<"Podaj dwie liczby calkowite: "<<endl;
  62. cin>>x>>y;
  63. node *a= new node;
  64. a->key=x;
  65. node *b=new node;
  66. b->key=y;
  67. if (list_search(L,a))
  68. list_insert2(L,a,b);
  69. else
  70. list_insert1(L,b);
  71.  
  72. node *z= L->head;
  73. while (z!=NULL)
  74. {
  75. cout<< z->key <<endl;
  76. z=z->next;
  77. }
  78. return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement