Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3.  
  4. #define Nil NULL
  5. using namespace std;
  6.  
  7. struct node
  8. {
  9. int data;
  10. struct node *link;
  11. };
  12.  
  13. void insert (struct node **front, struct node **rear, int nilai)
  14. {
  15. struct node *temp;
  16. temp = (struct node *)malloc(sizeof(struct node));
  17.  
  18. if (temp == Nil)
  19. {
  20. cout<<" Error, memori penuh"<<endl;
  21. exit(0);
  22. }
  23.  
  24. temp -> data = nilai;
  25. temp -> link = Nil;
  26.  
  27. if (*rear == Nil)
  28. {
  29. *rear = temp;
  30. *front = *rear;
  31. }
  32. else
  33. {
  34. (*rear) -> link = temp;
  35. *rear = temp;
  36. }
  37. }
  38. void del (struct node **front, struct node **rear, int *nilai)
  39. {
  40. struct node *temp;
  41.  
  42. if ((*front == *rear)&& (*rear == Nil))
  43. {
  44. cout<<"Queue kosong, delete tidak dapat dilakukan"<<endl;
  45. exit(0);
  46. }
  47. *nilai = (*front) -> data;
  48. temp = *front;
  49. *front = (*front) -> link;
  50.  
  51. if (*rear == temp)
  52. *rear = (*rear) -> link;
  53. free(temp);
  54. }
  55. void main()
  56. {
  57. struct node *front = Nil, *rear = Nil;
  58. int n,nilai;
  59.  
  60. do
  61. {
  62. do
  63. {
  64. cout<<"masukan nilai elemen = ";
  65. cin>>nilai;
  66. cout<<endl;
  67. insert (&front,&rear,nilai);
  68. cout<<"tekan 1 untuk melanjutkan = ";
  69. cin>>n;
  70. } while (n == 1);
  71.  
  72. cout<<endl;
  73. cout<<"tekan 1 untuk menghapus elemen =";
  74. cin>>n;
  75. while (n == 1)
  76. {
  77. del (&front,&rear,&nilai);
  78. cout<<endl;
  79. cout<<"nilai yang dihapus = "<<nilai<<endl;
  80. cout<<"tekan 1 untuk menghapus elemen = ";
  81. cin>>n;
  82.  
  83. }
  84.  
  85. cout<<endl;
  86. cout<<"tekan 1 untuk melanjutkan = ";
  87. cin>>n;
  88. }while (n == 1);
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement