Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #define Nil NULL
- using namespace std;
- struct node
- {
- int data;
- struct node *link;
- };
- void insert (struct node **front, struct node **rear, int nilai)
- {
- struct node *temp;
- temp = (struct node *)malloc(sizeof(struct node));
- if (temp == Nil)
- {
- cout<<" Error, memori penuh"<<endl;
- exit(0);
- }
- temp -> data = nilai;
- temp -> link = Nil;
- if (*rear == Nil)
- {
- *rear = temp;
- *front = *rear;
- }
- else
- {
- (*rear) -> link = temp;
- *rear = temp;
- }
- }
- void del (struct node **front, struct node **rear, int *nilai)
- {
- struct node *temp;
- if ((*front == *rear)&& (*rear == Nil))
- {
- cout<<"Queue kosong, delete tidak dapat dilakukan"<<endl;
- exit(0);
- }
- *nilai = (*front) -> data;
- temp = *front;
- *front = (*front) -> link;
- if (*rear == temp)
- *rear = (*rear) -> link;
- free(temp);
- }
- void main()
- {
- struct node *front = Nil, *rear = Nil;
- int n,nilai;
- do
- {
- do
- {
- cout<<"masukan nilai elemen = ";
- cin>>nilai;
- cout<<endl;
- insert (&front,&rear,nilai);
- cout<<"tekan 1 untuk melanjutkan = ";
- cin>>n;
- } while (n == 1);
- cout<<endl;
- cout<<"tekan 1 untuk menghapus elemen =";
- cin>>n;
- while (n == 1)
- {
- del (&front,&rear,&nilai);
- cout<<endl;
- cout<<"nilai yang dihapus = "<<nilai<<endl;
- cout<<"tekan 1 untuk menghapus elemen = ";
- cin>>n;
- }
- cout<<endl;
- cout<<"tekan 1 untuk melanjutkan = ";
- cin>>n;
- }while (n == 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement