1
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
D
alam istilah ilmu komputer, sebuah struktur data adalah cara penyimpanan, pengorganisasian dan pengaturan data di dalam media penyimpanan komputer sehingga data tersebut dapat digunakan secara efisien. Dalam tehnik pemrograman, struktur data berarti tata letak le tak data yang berisi kolom-kolom data, baik itu kolom yang tampak oleh pengguna (user) ataupun kolom yang hanya digunakan untuk keperluan pemrograman yang tiadak tampak oleh pengguna. Setiap baris dari dar i kumpulan kolom-kolom kolom-kolom tersebut dinamakan catatan (record).
Srtuktur Data Struktur data adalah cara menyimpan atau merepresentasikan data di dalam komputer agar bisa dipakai secara efisien. Sedangkan data adalah representasi dari fakta dunia nyata. Fakta atau keterangan tentang kenyataan yang disimpan, direkam atau direpresentasikan dalam bentuk tulisan, suara, gambar, sinyal atau simbol.
Stack Dalam ilmu komputer, stack atau tumpukan merupakan sebuah koleksi objek yang menggunakan prinsip LIFO (Last In First Out), yaitu data yang terakhr kali dimasukkan akan pertama kali keluar dari stack tersebut. Stack dapat diimplementasikan sebagai representasi berkait atau kontigu (dengan tabel fix). Ciri Stack : * Elemen TOP (puncak) diketahui * penisipan dan penghapusan elemen selalu dilakukan di TOP * LIFO Pemanfaatan Stack : * Perhitungan ekspresi aritmatika (posfix) * algoritma backtraking (runut balik) * algoritma rekursif Operasi Stack yang biasanya : 1. Push (input E : typeelmt, typeelmt, input/outpu input/outputt data : stack): stack): menambahkan sebuah elemen ke stack 2. Pop (input/ (input/outpu outputt data : stack, stack, output output E : typeelmt typeelmt ) : menghapus sebuah elemen stack 3. IsEm IsEmpt pty y () 4. IsF IsFull ull () 5. dan beber beberapas apas selekt selektor or yang yang lain lain
Contoh Soal Stack =========================== SOAL 1 ======================= #include
#include
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
2
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
#include struct list{ int bil; struct list * next; }; typedef struct list NODE; typedef NODE * PNODE; void tampil(PNODE atas){ PNODE pos; pos = atas; cout<<"\n\nbilangan cout<<"\n\nbilangan : "; while(pos!=NULL){ cout<bil<<", cout<bil<<", "; pos=pos->next; } } void PUSH(PNODE *atas, PNODE baru){ if(*atas==NULL){ *atas = baru; }else{ baru->next = *atas; *atas = baru; } } void POP(PNODE *atas){ PNODE PH; PH = *atas; if(*atas==NULL){ cout<<"\n\nSTACK KOSONG !!!\n\n"; }else{ if (PH->next==NULL){ *atas = NULL; free(PH); }else{ *atas = (*atas)->next; free(PH); } } } PNODE masuk(void){ PNODE baru; baru=(NODE *)malloc(sizeof(NODE)); cout<<"\nbil : ";cin>>baru->bil; baru->next=NULL; return(baru); } void main(){
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
3
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
PNODE atas=NULL; PNODE baru; int pil=1; clrscr(); while(pil!=4){ cout<<"\n\n1. PUSH STACK"; cout<<"\n2. LIHAT DATA STACK"; cout<<"\n3. POP STACK"; cout<<"\n4. EXIT"; cout<<"\nPilihan cout<<"\nPilihan : ";cin>>pil; if (pil==1){ baru=masuk(); PUSH(&atas, baru); } if (pil==2) tampil(atas); if (pil==3){ POP(&atas); } } }
=========================== SOAL 2 ======================= #include #include #include #include class brg{ struct list{ char kode[10]; char nama[30]; char satu[20]; long harga; struct list * next; }; public: typedef struct list NODE; typedef NODE * PNODE; void tampil(PNODE atas); void PUSH(PNODE *atas, PNODE baru); void POP(PNODE *atas); PNODE masuk(void); }x; void brg::tampil(PNODE atas){ PNODE pos; pos = atas; while(pos!=NULL){ cout <kode<<"\t"; cout <nama<<"\t";
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
4
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
cout <satu<<"\t"; cout <harga<<"\n"; pos=pos->next; } } void brg::PUSH(PNODE *atas, PNODE baru){ if(*atas==NULL){ *atas = baru; }else{ baru->next = *atas; *atas = baru; } } void brg::POP(PNODE *atas){ PNODE PH; PH = *atas; if(*atas==NULL){ cout<<"\n\nSTACK KOSONG !!!\n\n"; }else{ if (PH->next==NULL){ (PH->next==NULL){ *atas = NULL; free(PH); }else{ *atas = (*atas)->next; (*atas)->next; free(PH); } } } PNODE brg::masuk(void){ PNODE baru; baru=(NODE *)malloc(sizeof(NODE)); cout<<"\nKode cout<<"\nKode : ";gets(baru->kode); ";gets(baru->kode); cout<<"\nNama cout<<"\nNama : ";gets(baru->nama); ";gets(baru->nama); cout<<"\nSatuan cout<<"\nSatuan : ";gets(baru->satu); ";gets(baru->satu); cout<<"\nharga : ";cin>>baru->kode; baru->next=NULL; return(baru); } void main(){ PNODE atas=NULL; PNODE baru; int pil=1; clrscr(); while(pil!=4){ cout<<"\n\n1. cout<<"\n\n1. INPUT BARANG"; cout<<"\n2. LIHAT DAFTAR"; cout<<"\n3. HAPUS DAFTAR"; cout<<"\n4. EXIT";
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
5
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
cout<<"\nPilihan : ";cin>>pil; if (pil==1){ baru=x.masuk(); x.PUSH(&atas, x.PUSH(&atas, baru); } if (pil==2) x.tampil(atas); if (pil==3){ x.POP(&atas); } } }
=========================== SOAL 3 ======================= #include #include #include class elemen{ struct list{ int bil; struct list * next; }; public: typedef struct list NODE; typedef NODE * PNODE; void tampil(PNODE atas); void PUSH(PNODE *atas, PNODE baru); void POP(PNODE *atas); PNODE masuk(void); }; void elemen::tampil(PNODE atas){ PNODE pos; pos = atas; cout<<"\n\nbilangan cout<<"\n\nbilangan : "; while(pos!=NULL){ cout<bil<<", cout<bil<<", "; pos=pos->next; } } void elemen::PUSH(PNODE elemen::PUSH(PNODE *atas, PNODE baru){ if(*atas==NULL){ *atas = baru; }else{ baru->next = *atas; *atas = baru; } }
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
6
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
void elemen::POP(PNODE *atas){ PNODE PH; PH = *atas; if(*atas==NULL){ cout<<"\n\nSTACK KOSONG !!!\n\n"; }else{ if (PH->next==NULL){ *atas = NULL; free(PH); }else{ *atas = (*atas)->next; free(PH); } } } PNODE elemen::masuk(void){ PNODE baru; baru=(NODE *)malloc(sizeof(NODE)); cout<<"\nbil : ";cin>>baru->bil; baru->next=NULL; return(baru); } void main(){ PNODE atas=NULL; PNODE baru; int pil=1; elemen data; clrscr(); while(pil!=4){ cout<<"\n\n1. PUSH STACK"; cout<<"\n2. LIHAT DATA STACK"; cout<<"\n3. POP STACK"; cout<<"\n4. EXIT"; cout<<"\nPilihan cout<<"\nPilihan : ";cin>>pil; if (pil==1){ baru=data.masuk(); data.PUSH(&atas, baru); } if (pil==2) data.tampil(atas); if (pil==3){ data.POP(&atas); } } } =========================== SOAL 4 ======================= #include #include #include
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
7
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
#include #include class elemen { struct list { int bil; struct list *next; }; public: typedef struct list NODE; typedef NODE *PNODE; void tampil_stack (PNODE atas); void PUSH (PNODE *atas, PNODE baru); void POP (PNODE *atas); PNODE masuk_stack (void); void tampil_queue (PNODE head); void insert (PNODE *tail,PNODE *head, PNODE baru); void INSERT_QUEUE (PNODE *tail,PNODE *head, PNODE baru); void DELETE_QUEUE (PNODE *head, PNODE *tail); PNODE masuk_queue (void); }; void elemen :: tampil_stack tampil_stack (PNODE atas) { PNODE pos; pos = atas; cout<<"\n\nbilangan cout<<"\n\nbilangan : "; while(pos != NULL) { cout< bil<<", "; pos = pos -> next; } cout<<"\n\n\n [TEKAN ENTER UNTUK MELANJUTKAN]"; MELANJUTKAN]" ; getch(); } void elemen :: PUSH (PNODE *atas, PNODE baru) { if (*atas == NULL) { *atas = baru; } else { baru -> next = *atas; *atas = baru; }
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
8
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
} void elemen :: POP (PNODE *atas) { PNODE PH; PH = *atas; if(*atas == NULL) { cout<<"\n\nSTACK KOSONG !!!\n\n"; } else { if (PH -> next == NULL) { *atas = NULL; free(PH); } else { *atas = (*atas) -> next; free(PH); } } getch(); } PNODE elemen :: masuk_stack (void) { PNODE baru; baru = (NODE *) malloc (sizeof(NODE)); cout<<"\nbil : ";cin>>baru->bil; baru -> next = NULL; return (baru); getch(); } void elemen :: tampil_queue tampil_queue (PNODE head) { PNODE pos; pos = head; cout<<"\n\nbilangan cout<<"\n\nbilangan : "; while(pos != NULL) { cout< bil<<", "; pos = pos -> next; } cout<<"\n\n\n [TEKAN ENTER UNTUK MELANJUTKAN]"; MELANJUTKAN]" ; getch(); } void elemen :: INSERT_QUEUE INSERT_QUEUE (PNODE *tail,PNODE *head, PNODE baru)
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
9
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
{ if(*tail == NULL) { *head = baru; *tail = baru; } else { (*tail) -> next = baru; *tail = baru; } } void elemen :: DELETE_QUEUE (PNODE *head, PNODE *tail) { PNODE PH; PH = *head; if(*head == NULL) { cout<<"\n\nQUEUE KOSONG !!!\n\n"; } else { if (PH -> next == NULL) { *head = NULL; *tail = NULL; free(PH); } else { *head = (*head) -> next; free(PH); } } getch(); } PNODE elemen :: masuk_queue (void) { PNODE baru; baru = (NODE *)malloc(sizeof(NODE)); cout<<"\nbil : ";cin>>baru -> bil; baru -> next = NULL; return(baru); getch(); } void main() { PNODE atas = NULL; PNODE baru; PNODE head = NULL,tail = NULL;
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
10
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
int pil=1, x, m; char *a = "Created bY Tendi Setiadi (6306211) "; char *b = "Í"; elemen data; textbackground (RED); clrscr(); m=strlen(a); gotoxy (20,20); for (x=0; x Menu Utama <-"< Stack"; cout<<"\n2> Queue"; cout<<"\n3> Exit"; cout<<"\nPilihan : ";cin>>pil; if (pil == 1) { while(pil != 4) { clrscr(); cout<<"\n>> Menu STACK <<"<>pil; if (pil == 1) { baru = data.masuk_stack(); data.masuk_stack(); data.PUSH(&atas, data.PUSH(&atas, baru); } if (pil == 2) data.tampil_stack(atas); if (pil == 3)
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
11
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
{ data.POP(&atas); } } } if (pil == 2) { while(pil != 4) { clrscr(); cout<<"\n>> Menu QUEUE <<"<>pil; if (pil == 1) { baru=data.masuk_queue(); data.INSERT_QUEUE(&tail,&head,baru); } if (pil == 2) data.tampil_queue(head); if (pil == 3) { data.DELETE_QUEUE(&head,&tail); } } } } }
#include #include #include #include
=========================== SOAL 5 ======================= "iostream.h" "stdlib.h" "stdio.h" "conio.h"
struct list { char nama[35]; char kode[10]; struct list * next; }; typedef struct list node; typedef node * pnode; typedef struct list NODE; typedef NODE * PNODE;
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
12
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
void TAMPIL(PNODE ATAS) { int x; PNODE POS; POS = ATAS; cout<<"\nKode cout<<"\nKode : "; while(POS!=NULL) { cout<kode<<" cout<kode<<" - "; POS=POS->next; } } void PUSH(PNODE *ATAS,PNODE BARU) { if(*ATAS==NULL) { *ATAS = BARU; } else { BARU->next = *ATAS; *ATAS = BARU; } } void POP(PNODE *ATAS) { PNODE PH; PH = *ATAS; if(*ATAS==NULL) { cout<<"\n\nSTACK KOSONG !!!\n\n"; } else { if (PH->next==NULL) { *ATAS = NULL; free(PH); } else { *ATAS = (*ATAS)->next; (*ATAS)->next; free(PH); } } } PNODE MASUK(void) {
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
13
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
PNODE BARU; BARU=new(NODE); cout<<"KODE : "; gets(BARU->kode); BARU->next=NULL; return(BARU); } void tampil(pnode atas) { int x; pnode pos; pos = atas; cout<<"NEGARA cout<<"NEGARA : "; while(pos!=NULL) { cout<nama<<" cout<nama<<" - "; pos=pos->next; } } void push(pnode *atas, pnode baru) { if(*atas==NULL) { *atas = baru; } else { baru->next = *atas; *atas = baru; } } void pop(pnode *atas) { pnode ph; ph = *atas; if(*atas==NULL) { cout<<"\n\nSTACK KOSONG !!!\n\n"; } else { if (ph->next==NULL) { *atas = NULL; free(ph); } else { *atas = (*atas)->next; (*atas)->next;
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
14
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
free(ph); } } } pnode masuk(void) { pnode baru; baru=new(node); cout<<"\nNEGARA : "; gets(baru->nama); baru->next=NULL; return(baru); } void main() { pnode atas=NULL; pnode baru; PNODE BARU; PNODE ATAS=NULL; int pil=1; clrscr(); while(pil!=4) { cout<<"\n\n=======STACK======="; cout<<"\n1. PUSH STACK"; cout<<"\n2. VIEW DATA STACK"; cout<<"\n3. POP STACK"; cout<<"\n4. EXIT"; cout<<"\nPilihan cout<<"\nPilihan [1/2/3/4] : "; cin>>pil; if (pil==1) { clrscr(); baru=masuk(); BARU=MASUK(); push(&atas,baru); PUSH(&ATAS,BARU); } if (pil==2) { clrscr(); tampil(atas); TAMPIL(ATAS); } if (pil==3) { clrscr(); pop(&atas); POP(&ATAS); } }
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
15
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
}
=========================== SOAL 6 ======================= #include #include #include struct simpul{ char kode[7]; char nama[20]; simpul *next; }; class negara{ simpul *head; public: negara(); ~negara(); void push(); void pop(); void view(); void menu(); }; negara::negara(){ head=NULL; menu(); } negara::~negara(){ simpul *tampung; //tampung=head; while(head!=NULL){ tampung=head; head=head->next; delete(tampung); } delete head; } void negara::menu(){ negara::menu(){ clrscr(); cout<<"\nMenu:"; cout<<"\n1. cout<<"\n1. Push"; cout<<"\n2. Pop"; cout<<"\n3. cout<<"\n3. View"; cout<<"\n4. cout<<"\n4. Exit"; cout<<"\nPilihan[1/2/3/4]:"; int pil; cin>>pil;
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
16
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
switch(pil){ case 1:push();menu();break; case 2:pop();menu();break; 2:pop();menu();break; case 3:view();menu();break; case 4:break; default:break; } } void negara::push(){ negara::push(){ simpul *baru; baru = new simpul; cout<<"\nMasukkan kode negara: "; gets(baru->kode); cout<<"\nMasukkan nama negara: "; gets(baru->nama); if(head==NULL){ head=baru; head->next=NULL; } else{ baru->next=head; head=baru; } } void negara::pop(){ } void negara::view(){ negara::view(){ simpul *lihat; lihat=head; while(lihat!=NULL){ cout<<"\nKode Negara:"<kode; cout<<"\nNama Negara:"<nama; lihat=lihat->next; } getch(); } void main(){ clrscr(); negara stack; getch(); } =========================== SOAL 7 ======================= #include #include #include //----------------------
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
17
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
struct nilaiMatKul{ char nim[10]; char nama[50]; float nilai; }; struct elemen{ nilaiMatKul elmt; struct elemen *next; }; struct stack{ struct elemen *top; }; //---------------------void createEmpty(stack *S){ (*S).top = NULL; } //---------------------bool isEmpty(stack S){ bool hasil = false; if(S.top == NULL){ hasil = true; } return hasil; } //---------------------int countElement(stack S){ int hasil = 0; if(S.top != NULL){ /* stack tidak kosong */ elemen *elmt; /* inisialisasi */ elmt = S.top; while(elmt != NULL){ /* proses */ hasil= hasil + 1; /* iterasi */ elmt = elmt->next; } } return hasil;
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
18
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
} //---------------------void push(char nim[], char nama[], float nilai, stack *S ){ elemen *elmt; elmt = (elemen *) malloc (sizeof (elemen)); strcpy(elmt->elmt.nim, nim); strcpy(elmt->elmt.nama strcpy(elmt->elmt.nama, , nama); elmt->elmt.nilai = nilai; elmt->next = (*S).top; (*S).top = elmt; elmt = NULL; } //---------------------void pop(stack *S){ if((*S).top != NULL){ /* jika stack bukan stack kosong */ elemen *elmt = (*S).top; (*S).top = (*S).top->next; elmt->next = NULL; free(elmt); } } //---------------------void printStack(stack S){ if(S.top != NULL){ printf("------isi stack------\n"); elemen *elmt = S.top; int i = 1; while(elmt != NULL){ printf("====================\n"); printf("elemen ke : %d\n", i); printf("nim : %s\n", elmt->elmt.nim); printf("nama : %s\n", elmt->elmt.nama); printf("nilai : %f\n", elmt->elmt.nilai); /* iterasi */ elmt = elmt->next; i = i + 1; } printf("---------------------\n"); } else{ /* proses jika stack kosong */ printf("stack kosong\n"); } }
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
19
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
//---------------------int main(){ struct stack S; createEmpty(&S); printStack(S); printf("=================\n"); push("13507701", "Imam", 64.75, &S); push("13507702", "Cinta", 75.11, &S); push("13507703", "Jasmine", 84.63, &S); printStack(S); printf("=================\n"); pop(&S); pop(&S); printStack(S); printf("=================\n"); return 0; }
=========================== SOAL 8 ======================= #include #include //---------------------struct nilaiMatKul{ char nim[10]; char nama[50]; float nilai; }; struct stack{ int top; nilaiMatKul data[10]; }; //---------------------void createEmpty(stack *S){ (*S).top = -1; } //---------------------bool isEmpty(stack S){ bool hasil = false;
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
20
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
if(S.top == -1){ hasil = true; } return hasil; } //---------------------bool isFull(stack S){ bool hasil = false; if(S.top == 9){ hasil = true; } return hasil; } //---------------------void push(char nim[], char nama[], float nilai, stack *S ){ if(isFull(*S) == true){ /*jika stack penuh*/ printf("stack printf("stack penuh\n"); } else{ if(isEmpty(*S) == true){ /* jika stack kosong */ (*S).top = 0; strcpy((*S).data[0].nim, nim); strcpy((*S).data[0].nama, nama); (*S).data[0].nilai = nilai; } else{ /* jika stack tidak kosong */ (*S).top = (*S).top + 1; strcpy((*S).data[(*S).top].nim, nim); strcpy((*S).data[(*S).top].nama, nama); (*S).data[(*S).top].nilai = nilai; } } } //---------------------void pop(stack *S){ if((*S).top == 0){ /*jika stack berisi satu elemen*/ (*S).top = -1; } else{ if((*S).top != -1){ /*jika stack tidak kosong*/ (*S).top = (*S).top - 1;
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
21
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
} } } //---------------------void printStack(stack S){ if(S.top != -1){ printf("------isi stack------\n"); int i; for(i=S.top;i>=0;i--){ printf("====================\n"); printf("elemen ke : %d\n", i); printf("nim : %s\n", S.data[i].nim); printf("nama : %s\n", S.data[i].nama); printf("nilai : %f\n", S.data[i].nilai); } printf("---------------------\n"); } else{ /* proses jika stack kosong */ printf("stack kosong\n"); } } //---------------------int main(){ struct stack S; createEmpty(&S); printStack(S); printf("=================\n"); push("13507701", "Nana", 64.75, &S); push("13507702", "Rudi", 75.11, &S); push("13507703", "Dea", 84.63, &S); printStack(S); printf("=================\n"); pop(&S); pop(&S); printStack(S); printf("=================\n"); return 0; }
=========================== SOAL 9 ======================= #include #include #include
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
22
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
#include #define MAX_CITY 30 #define MAX_STATE 30 #define MAX_ZIP 5 void main (void); int is_empty (struct node *); int push (struct node **); int pop (struct node **); void search (struct node **); void free_nodes (struct node **pstack); struct node { char zip_code[MAX_ZIP+1]; char city[MAX_CITY]; char state[MAX_STATE]; struct node *link; }; void main (void) { struct node *pstack = NULL; int ok_so_far = 1, no_of_nodes = 0; while (ok_so_far == 1) { ok_so_far = push(&pstack); push(&pstack); if (ok_so_far == 1) no_of_nodes ++; else if(ok_so_far == 0) { puts("\nAn unexpected error has occurred terminating terminating program"); exit(1); } } search (&pstack); free_nodes(&pstack); } int push(struct node **pstack) { struct node *new_ptr; new_ptr = (struct node *) malloc(sizeof(struct malloc(sizeof(struct node)); if(new_ptr == (struct node *) NULL) { printf("ERROR! Unable to allocate memory - Abort\n"); free(new_ptr); return (0); } else { printf("\n\nEnter %d digit zip code or 'q' to quit>>", MAX_ZIP); gets(new_ptr->zip_code); new_ptr->zip_code[MAX_ZIP] = '\0'; if (strcmp(new_ptr->zip_code, (strcmp(new_ptr->zip_code, "q") != 0) {
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
23
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
printf("\nEnter a less than %d character state name>>\n", MAX_STATE); gets(new_ptr->state); printf("\nEnter a less than %d character city name>>\n", MAX_CITY); gets(new_ptr->city); new_ptr->link new_ptr->link = *pstack; *pstack = new_ptr; return (1); } else return (2); } } void search (struct node **pstack) { struct node *ptemp; int test = 0; char ch, find[6]; ptemp = *pstack; printf("\n\nEnter %d digit zip code to search for \nor 'e' to print entire list>>", MAX_ZIP); gets(find); find[MAX_ZIP] find[MAX_ZIP] = '\0'; if (find[0] =='E' || find[0] =='e') { test = 1; while (test != 0) test = pop (pstack); } else { while (test == 0 || ptemp != NULL) { if (strcmp(ptemp->zip_code, (strcmp(ptemp->zip_code, find) == 0) { test = 1; printf("Zip Code: %s\n", ptemp->zip_code); printf("State: %s\n", ptemp->state); printf("City: %s\n\n", ptemp->city); } else if (ptemp == NULL) { printf("The printf("The zip code %s was not found.\n", find); test = 1; } ptemp = ptemp->link; } puts ("\nType 'y' if you would you like to see the entire list"); puts ("or any other key to continue>>"); if (ch == 'y' || ch == 'Y') { test = 1;
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
24
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
while (test != 0) test = pop (pstack); } } } int pop (struct node **pstack) { struct node *temp; if (is_empty(*pstack)== 1) { printf("\nStack is now empty"); return(0); } else { temp = *pstack; printf("Zip Code: %s\n", temp->zip_code); printf("State: %s\n", temp->state); printf("City: %s\n\n", temp->city); *pstack = (*pstack)->link; (*pstack)->link; free(temp); return(1); } } int is_empty (struct node *stack) { if (stack == NULL) return(1); return(0); } void free_nodes (struct node **pstack) { struct node *temp; while (*pstack != NULL) { temp = *pstack; *pstack = (*pstack)->link; (*pstack)->link; free(temp); } } =========================== SOAL 10 ======================= # include # include struct node { int data; struct node *link; };
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
25
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
struct node *push(struct node *p, int value) { struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); /* creates new node using data value passed as parameter */ if(temp==NULL) { printf("No Memory available Error\n"); exit(0); } temp->data = value; temp->link = p; p = temp; return(p); } struct node *pop(struct node *p, int *value) { struct node *temp; if(p==NULL) { printf(" The stack is empty can not pop Error\n"); exit(0); } *value = p->data; temp = p; p = p->link; free(temp); return(p); } void main() { struct node *top = NULL; int n,value; do { do { printf("Enter the element to be pushed\n"); scanf("%d",&value); top = push(top,value); printf("Enter 1 to continue\n"); scanf("%d",&n); } while(n == 1); printf("Enter 1 to pop an element\n"); scanf("%d",&n); while( n == 1) { top = pop(top,&value); pop(top,&value);
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
26
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
printf("The value poped is %d\n",value); %d\n",value); printf("Enter printf("Enter 1 to pop an element\n"); element\n"); scanf("%d",&n); } printf("Enter 1 to continue\n"); scanf("%d",&n); } while(n == 1); }
=========================== SOAL 11 ======================= #include #include //deklarasi 'STACK' dengan struct dan array typedef struct STACK { int data[5]; int atas; }; //deklarasi //deklarasi variabel 'tumpuk' dari struct STACK tumpuk; void main() { clrscr(); int pilihan,baru,i; //inisialisasi awal tumpuk.atas=-1; do { clrscr(); printf("1.Push Data\n"); printf("2.Pop printf("2.Pop Data\n"); printf("3.Print printf("3.Print Data\n"); printf("\nPilihan printf("\nPilihan = "); scanf("%i",&pilihan); switch(pilihan) { case 1: { if(tumpuk.atas==5-1) { printf("Tumpukan printf("Tumpukan penuh"); getch(); } else { printf("Data yang akan di-push = "); scanf("%d",&baru); tumpuk.atas++; tumpuk.data[tumpuk.atas]=baru;
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
27
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
} break; } case 2: { if(tumpuk.atas==-1) { printf("Tumpukan printf("Tumpukan kosong"); getch(); } else { printf("Data printf("Data yang akan di-pop = %d", tumpuk.data[tumpuk.atas]); tumpuk.atas--; getch(); } break; } case 3: { if(tumpuk.atas==-1) { printf("Tumpukan printf("Tumpukan kosong"); getch(); } else { printf("Data printf("Data = "); for(i=0; i<=tumpuk.atas; i++) { printf("%d ",tumpuk.data[i]); } getch(); } break; } default: { printf("\nTidak ada dalam pilihan"); } } }while(pilihan>=1 && pilihan<=3); getch(); } =========================== SOAL 12 ======================= #include #include #define MAX 8 typedef struct{
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
28
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
int data[MAX]; } Stack; Stack stack; int top[2]; void Create(){ //kosongkan stack top[0] = -1; top[1] = MAX; //top[1] = 8 karena elemen array 0-7 } void Tampil(){ int i; printf("Stack printf("Stack 1\n"); for(i=0;i<=top[0];i++){ printf("%d ",stack.data[i]); } printf("\nStack printf("\nStack 2\n"); for(i=MAX-1;i>=top[1];i--){ printf("%d ",stack.data[i]); } } void Clear(int noStack){ if(noStack==1) top[0]=-1; else if(noStack==2) top[1]=MAX; } int IsFull(){ int full = 0; if(top[0]+1 >= top[1]) full=1; return full; } void Push(int d, int noStack){ if(IsFull()==0){ if(noStack == 1) top[0]++; else if(noStack == 2) top[1]--; stack.data[top[noStack-1]] = d; printf("masuk %d !",stack.data[top[noStack-1]]); } } int IsEmpty(int noStack){ int empty=0; if(noStack==1){ if(top[0] == 0) empty=1; } else if(noStack==2){ if(top[1] == MAX) empty=1; } return empty; } int Pop(int noStack){ int e; if(IsEmpty(noStack)==0){ e=stack.data[top[noStack-1]]; if(noStack==1) top[0]--; else if(noStack==2) top[1]++;
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
29
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
} return e; } void Clear(int noStack){ if(noStack==1) top[0]=-1; else if(noStack==2) top[1]=MAX; } void Tampil(){ int i; printf("Stack printf("Stack 1\n"); for(i=0;i<=top[0];i++){ printf("%d ",stack.data[i]); } printf("\nStack printf("\nStack 2\n"); for(i=MAX-1;i>=top[1];i--){ printf("%d ",stack.data[i]); } } void main(){ int pil; int no; int data; Create(); do{ clrscr(); printf("1. Push\n"); printf("2. Pop\n"); printf("3. Tampil\n"); printf("4. Clear\n"); printf("5. Exit\n"); printf("Pilihan = ");scanf("%d",&pil); switch(pil){ case 1: printf("stack ke (1/2) : ");scanf("%d",&no); printf("Data = ");scanf("%d",&data); Push(data,no); break; case 2: printf("stack ke (1/2) : ");scanf("%d",&no); printf("Elemen yang keluar : %d",Pop(no)); break; case 3: Tampil(); break; case 4: printf("stack ke (1/2) : ");scanf("%d",&no); Clear(no); break; } getch(); } while(pil!=5); } =========================== SOAL 13 ======================= #include #include
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
30
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
#include struct list{ int bil; struct list * next; }; typedef struct list NODE; typedef NODE * PNODE; void tampil(PNODE atas){ PNODE pos; pos = atas; cout<<"\n\nbilangan cout<<"\n\nbilangan : "; while(pos!=NULL){ cout<bil<<", cout<bil<<", "; pos=pos->next; } } void PUSH(PNODE *atas, PNODE baru){ if(*atas==NULL){ *atas = baru; }else{ baru->next = *atas; *atas = baru; } } void POP(PNODE *atas){ PNODE PH; PH = *atas; if(*atas==NULL){ cout<<"\n\nSTACK KOSONG !!!\n\n"; }else{ if (PH->next==NULL){ *atas = NULL; free(PH); }else{ *atas = (*atas)->next; free(PH); } } } PNODE masuk(void){ PNODE baru; baru=(NODE *)malloc(sizeof(NODE)); cout<<"\nbil : ";cin>>baru->bil; baru->next=NULL; return(baru); } void main(){
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
31
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
PNODE atas=NULL; PNODE baru; int pil=1; clrscr(); while(pil!=4){ cout<<"\n\n1. PUSH STACK"; cout<<"\n2. LIHAT DATA STACK"; cout<<"\n3. POP STACK"; cout<<"\n4. EXIT"; cout<<"\nPilihan cout<<"\nPilihan : ";cin>>pil; if (pil==1){ baru=masuk(); PUSH(&atas, baru); } if (pil==2) tampil(atas); if (pil==3){ POP(&atas); } } }
=========================== SOAL 14 ======================= #include #include #include #include #include class elemen { struct list { int bil; struct list *next; }; public: typedef struct list NODE; typedef NODE *PNODE; void tampil_stack (PNODE atas); void PUSH (PNODE *atas, PNODE baru); void POP (PNODE *atas); PNODE masuk_stack (void); void tampil_queue (PNODE head); void insert (PNODE *tail,PNODE *head, PNODE baru); void INSERT_QUEUE (PNODE *tail,PNODE *head, PNODE baru); void DELETE_QUEUE (PNODE *head, PNODE *tail); PNODE masuk_queue (void); };
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
32
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
void elemen :: tampil_stack tampil_stack (PNODE atas) { PNODE pos; pos = atas; cout<<"\n\nbilangan cout<<"\n\nbilangan : "; while(pos != NULL) { cout< bil<<", "; pos = pos -> next; } cout<<"\n\n\n [TEKAN ENTER UNTUK MELANJUTKAN]"; MELANJUTKAN]" ; getch(); } void elemen :: PUSH (PNODE *atas, PNODE baru) { if (*atas == NULL) { *atas = baru; } else { baru -> next = *atas; *atas = baru; } } void elemen :: POP (PNODE *atas) { PNODE PH; PH = *atas; if(*atas == NULL) { cout<<"\n\nSTACK KOSONG !!!\n\n"; } else { if (PH -> next == NULL) { *atas = NULL; free(PH); } else { *atas = (*atas) -> next; free(PH); } } getch(); }
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
33
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
PNODE elemen :: masuk_stack (void) { PNODE baru; baru = (NODE *) malloc (sizeof(NODE)); cout<<"\nbil : ";cin>>baru->bil; baru -> next = NULL; return (baru); getch(); } void elemen :: tampil_queue tampil_queue (PNODE head) { PNODE pos; pos = head; cout<<"\n\nbilangan cout<<"\n\nbilangan : "; while(pos != NULL) { cout< bil<<", "; pos = pos -> next; } cout<<"\n\n\n [TEKAN ENTER UNTUK MELANJUTKAN]"; MELANJUTKAN]" ; getch(); } void elemen :: INSERT_QUEUE INSERT_QUEUE (PNODE *tail,PNODE *head, PNODE baru) { if(*tail == NULL) { *head = baru; *tail = baru; } else { (*tail) -> next = baru; *tail = baru; } } void elemen :: DELETE_QUEUE (PNODE *head, PNODE *tail) { PNODE PH; PH = *head; if(*head == NULL) { cout<<"\n\nQUEUE KOSONG !!!\n\n"; } else { if (PH -> next == NULL) { *head = NULL;
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
34
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
*tail = NULL; free(PH); } else { *head = (*head) -> next; free(PH); } } getch(); } PNODE elemen :: masuk_queue (void) { PNODE baru; baru = (NODE *)malloc(sizeof(NODE)); cout<<"\nbil : ";cin>>baru -> bil; baru -> next = NULL; return(baru); getch(); }
void main() { PNODE atas = NULL; PNODE baru; PNODE head = NULL,tail = NULL; int pil=1, x, m; char *a = "Created bY Tendi Setiadi (6306211) "; char *b = "Í"; elemen data; textbackground (RED); clrscr(); m=strlen(a); gotoxy (20,20); for (x=0; x
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
35
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
cout<<"\n-> Menu Utama <-"< Stack"; cout<<"\n2> Queue"; cout<<"\n3> Exit"; cout<<"\nPilihan : ";cin>>pil; if (pil == 1) { while(pil != 4) { clrscr(); cout<<"\n>> Menu STACK <<"<>pil; if (pil == 1) { baru = data.masuk_stack(); data.masuk_stack(); data.PUSH(&atas, data.PUSH(&atas, baru); } if (pil == 2) data.tampil_stack(atas); if (pil == 3) { data.POP(&atas); } } } if (pil == 2) { while(pil != 4) { clrscr(); cout<<"\n>> Menu QUEUE <<"<>pil; if (pil == 1) { baru=data.masuk_queue(); data.INSERT_QUEUE(&tail,&head,baru); } if (pil == 2)
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
36
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
data.tampil_queue(head); if (pil == 3) { data.DELETE_QUEUE(&head,&tail); } } } } }
=========================== SOAL 15 ======================= #include #include #include class elemen{ struct list{ int bil; struct list * next; }; public: typedef struct list NODE; typedef NODE * PNODE; void tampil(PNODE atas); void PUSH(PNODE *atas, PNODE baru); void POP(PNODE *atas); PNODE masuk(void); }; void elemen::tampil(PNODE atas){ PNODE pos; pos = atas; cout<<"\n\nbilangan cout<<"\n\nbilangan : "; while(pos!=NULL){ cout<bil<<", cout<bil<<", "; pos=pos->next; } } void elemen::PUSH(PNODE elemen::PUSH(PNODE *atas, PNODE baru){ if(*atas==NULL){ *atas = baru; }else{ baru->next = *atas; *atas = baru; } } void elemen::POP(PNODE *atas){ PNODE PH;
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
37
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
PH = *atas; if(*atas==NULL){ cout<<"\n\nSTACK KOSONG !!!\n\n"; }else{ if (PH->next==NULL){ *atas = NULL; free(PH); }else{ *atas = (*atas)->next; free(PH); } } } PNODE elemen::masuk(void){ PNODE baru; baru=(NODE *)malloc(sizeof(NODE)); cout<<"\nbil : ";cin>>baru->bil; baru->next=NULL; return(baru); } void main(){ PNODE atas=NULL; PNODE baru; int pil=1; elemen data; clrscr(); while(pil!=4){ cout<<"\n\n1. PUSH STACK"; cout<<"\n2. LIHAT DATA STACK"; cout<<"\n3. POP STACK"; cout<<"\n4. EXIT"; cout<<"\nPilihan cout<<"\nPilihan : ";cin>>pil; if (pil==1){ baru=data.masuk(); data.PUSH(&atas, baru); } if (pil==2) data.tampil(atas); if (pil==3){ data.POP(&atas); } } } =========================== SOAL 16 =======================
=========================== SOAL 17 =======================
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
38
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
=========================== SOAL 18 ======================= =========================== SOAL 19 ======================= =========================== SOAL 20 ======================= =========================== SOAL 21 ======================= =========================== SOAL 22 ======================= =========================== SOAL 23 ======================= =========================== SOAL 24 ======================= =========================== SOAL 25 ======================= =========================== SOAL 26 ======================= =========================== SOAL 27 ======================= =========================== SOAL 28 ======================= =========================== SOAL 29 ======================= =========================== SOAL 30 ======================= =========================== SOAL 31 ======================= =========================== SOAL 32 ======================= =========================== SOAL 33 =======================
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
39
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
=========================== SOAL 34 ======================= =========================== SOAL 35 ======================= =========================== SOAL 36 ======================= =========================== SOAL 37 ======================= =========================== SOAL 38 ======================= =========================== SOAL 39 ======================= =========================== SOAL 40 ======================= =========================== SOAL 41 ======================= =========================== SOAL 42 ======================= =========================== SOAL 43 ======================= =========================== SOAL 44 ======================= =========================== SOAL 45 ======================= =========================== SOAL 46 ======================= =========================== SOAL 47 ======================= =========================== SOAL 48 =======================
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
40
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
=========================== SOAL 49 ======================= =========================== SOAL 50 =======================
=========================================== ================
Queue Queue (Antrian) adalah list linier yang : 1. Dikenali Dikenali elemen elemen pertama (Head) (Head) dan elemen elemen terakhirnya terakhirnya (Tail) 2. Aturan Aturan penyisipan penyisipan dan penghapus penghapusan an elemennya elemennya disefin disefinisika isikan n sebagai berikut : - Penyisipan selalu dilakukan setelah elemen terakhir - Penghapusan selalu dilakukan pada elemen pertama 3. Satu elemen elemen dengan elemen lain lain dapat dapat diakses diakses melalui informasi Next
Contoh Soal Queue =========================== SOAL 1 ======================= #include #include #include #define MAX 8 typedef struct { char data[MAX]; int head; int tail; }Queue; Queue antrian; void Create() { antrian.head=antrian.tail=-1; } int IsEmpty() { if(antrian.tail==-1) return 1; else return 0;
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
41
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
} int IsFull() { if(antrian.tail==MAX-1) return 1; else return 0; } void Enqueue(int data) { if(IsEmpty()==1) { antrian.head=antrian.tail=0; antrian.data[antrian.tail]=data; printf("%d masuk!",antrian.data[antrian.tail]); } else if(IsFull()==0) { antrian.tail++; antrian.data[antrian.tail]=data; printf("%d masuk!",antrian.data[antrian.tail]); } } int Dequeue() { int i; int e = antrian.data[antrian.head]; antrian.data[antrian.head]; for(i=antrian.head;i<=antrian.tail-1;i++) { antrian.data[i] antrian.data[i] = antrian.data[i+1]; antrian.data[i+1]; } antrian.tail--; return e; } void Clear() { antrian.head=antrian.tail=-1; printf("data clear"); } void Tampil() { if(IsEmpty()==0) { for(int i=antrian.head;i<=antri i=antrian.head;i<=antrian.tail;i++) an.tail;i++) { printf("%d ",antrian.data[i]); }
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
42
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
} else printf("data kosong!\n"); } void main() { int pil; char kota[MAX]; Create(); do { clrscr(); printf("1. Enqueue\n"); printf("2. Dequeue\n"); printf("3. Tampil\n"); printf("4. Clear\n"); printf("5. Exit\n"); printf("Pilihan printf("Pilihan = ");scanf("%d",&pil); ");scanf("%d",&pil); switch(pil) { case 1: { cout<<"Kota = "; gets(kota); Enqueue(*kota); break; } case 2: { printf("Elemen printf("Elemen yang keluar : %d",Dequeue()); break; } case 3: Tampil(); break; case 4: Clear(); break; } getch(); }while(pil!=5); }
=========================== SOAL 2 ======================= #include class element { public: int value; element* next; };//class element class Queue
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
43
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
{ public: int size; element* head; element* tail; Queue() { size=0; head=NULL; tail=NULL; }//default constructor void Enqueue(int); Enqueue(int); void Dequeue(); int isEmpty(); int getQueueSize(); void printQueueSize(); printQueueSize(); void printQueueElements(); printQueueElements(); void printQueueMenu(); printQueueMenu(); }; void Queue::Enqueue(int ele) { if(head==NULL) if(head==NULL ) // first element { head=new element; tail=head; //head==tail if one element head->value=ele; head->next=NULL; } else { tail->next=new element; tail->next->value=ele; tail->next->next=NULL; cout<next->value<next; } size++; //printQueueElements(); } void Queue::Dequeue() { if(getQueueSize()==0) return; else if(head==tail) { head=NULL; } else
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
44
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
{ element *curr,*prev; //remove the first element inserted and curr=head; //point the head to next element head=curr->next; curr=NULL; } size--; } int Queue::isEmpty() { if(getQueueSize()==0) return 1; return 0; } int Queue::getQueueSize() { return size; }//returns size of the Queue void Queue::printQueueSize() Queue::printQueueSize() { cout<<"\nThe Size of the Queue:"<value<<"|"; |"<value<<"|"; curr2=curr2->next; } cout<
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
45
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
void main() { Queue qt; qt.printQueueMenu(); char Option=0; int val; while(1) { qt.printQueueMenu(); cin>>Option; switch(Option) { case '1': cout<<"Enter a Number \n"; cin>>val; qt.Enqueue(val); break; case '2': qt.Dequeue(); break; case '3': qt.printQueueElements(); break; case '4': qt.printQueueSize(); break; case '5': exit(0); break; } } } =========================== SOAL 3 ======================= #include #include #include struct list{ int bil; struct list * next; }; typedef struct list NODE; typedef NODE * PNODE; void tampil(PNODE head){ PNODE pos; pos = head; cout<<"\n\nbilangan cout<<"\n\nbilangan : "; while(pos!=NULL){ cout<bil<<", cout<bil<<", "; pos=pos->next;
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
46
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
} } void INSERT_QUEUE(PNODE INSERT_QUEUE(PNODE *tail,PNODE *head, PNODE baru){ if(*tail==NULL){ *head = baru; *tail = baru; }else{ (*tail)->next (*tail)->next = baru; *tail = baru; } } void DELETE_QUEUE(PNODE *head, PNODE *tail){ PNODE PH; PH = *head; if(*head==NULL){ cout<<"\n\nQUEUE KOSONG !!!\n\n"; }else{ if (PH->next==NULL){ *head = NULL; *tail = NULL; free(PH); }else{ *head = (*head)->next; free(PH); } } } PNODE masuk(void){ PNODE baru; baru=(NODE *)malloc(sizeof(NODE)); cout<<"\nbil : ";cin>>baru->bil; baru->next=NULL; return(baru); } void main(){ PNODE head=NULL,tail=NULL; head=NULL,tail=NULL; PNODE baru; int pil=1; clrscr(); while(pil!=4){ cout<<"\n\n1. INSERT_QUEUE LIST"; cout<<"\n2. LIHAT DATA LIST"; cout<<"\n3. DELETE_QUEUE LIST"; cout<<"\n4. EXIT"; cout<<"\nPilihan cout<<"\nPilihan : ";cin>>pil; if (pil==1){ baru=masuk(); INSERT_QUEUE(&tail,&head,baru); }
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
47
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
if (pil==2) tampil(head); if (pil==3){ DELETE_QUEUE(&head,&tail); } } }
=========================== SOAL 4 ======================= #include #include #include struct list{ int bil; struct list * next; }; typedef struct list NODE; typedef NODE * PNODE; void tampil(PNODE head){ PNODE pos; pos = head; cout<<"\n\nbilangan cout<<"\n\nbilangan : "; while(pos!=NULL){ cout<bil<<", cout<bil<<", "; pos=pos->next; } } void INSERT_QUEUE(PNODE INSERT_QUEUE(PNODE *tail,PNODE *head, PNODE baru){ if(*tail==NULL){ *head = baru; *tail = baru; }else{ (*tail)->next (*tail)->next = baru; *tail = baru; } } void DELETE_QUEUE(PNODE *head, PNODE *tail){ PNODE PH; PH = *head; if(*head==NULL){ cout<<"\n\nQUEUE KOSONG !!!\n\n"; }else{ if (PH->next==NULL){ *head = NULL; *tail = NULL; free(PH); }else{ *head = (*head)->next;
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
48
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
free(PH); } } } PNODE masuk(void){ PNODE baru; baru=(NODE *)malloc(sizeof(NODE)); cout<<"\nbil : ";cin>>baru->bil; baru->next=NULL; return(baru); } void main(){ PNODE head=NULL,tail=NULL; head=NULL,tail=NULL; PNODE baru; int pil=1; clrscr(); while(pil!=4){ cout<<"\n\n1. INSERT_QUEUE LIST"; cout<<"\n2. LIHAT DATA LIST"; cout<<"\n3. DELETE_QUEUE LIST"; cout<<"\n4. EXIT"; cout<<"\nPilihan cout<<"\nPilihan : ";cin>>pil; if (pil==1){ baru=masuk(); INSERT_QUEUE(&tail,&head,baru); } if (pil==2) tampil(head); if (pil==3){ DELETE_QUEUE(&head,&tail); } } } =========================== SOAL 5 ======================= #include #include #include class elemen{ struct list{ int bil; struct list * next; }; public: typedef struct list NODE; typedef NODE * PNODE; void tampil(PNODE head); void insert(PNODE *tail,PNODE *head, PNODE baru);
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
49
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
void INSERT_QUEUE(PNODE INSERT_QUEUE(PNODE *tail,PNODE *head, PNODE baru); void DELETE_QUEUE(PNODE *head, PNODE *tail); PNODE masuk(void); }; void elemen::tampil(PNODE head){ PNODE pos; pos = head; cout<<"\n\nbilangan cout<<"\n\nbilangan : "; while(pos!=NULL){ cout<bil<<", cout<bil<<", "; pos=pos->next; } } void elemen::INSERT_QUEUE(PNODE *tail,PNODE *head, PNODE baru){ if(*tail==NULL){ *head = baru; *tail = baru; }else{ (*tail)->next (*tail)->next = baru; *tail = baru; } } void elemen::DELETE_QUEUE(PNODE elemen::DELETE_QUEUE(PNODE *head, PNODE *tail){ PNODE PH; PH = *head; if(*head==NULL){ cout<<"\n\nQUEUE KOSONG !!!\n\n"; }else{ if (PH->next==NULL){ *head = NULL; *tail = NULL; free(PH); }else{ *head = (*head)->next; free(PH); } } } PNODE elemen::masuk(void){ PNODE baru; baru=(NODE *)malloc(sizeof(NODE)); cout<<"\nbil : ";cin>>baru->bil; baru->next=NULL; return(baru); } void main(){ PNODE head=NULL,tail=NULL; head=NULL,tail=NULL; PNODE baru;
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
50
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
int pil=1; elemen data; clrscr(); while(pil!=4){ cout<<"\n\n1. INSERT_QUEUE LIST"; cout<<"\n2. LIHAT DATA LIST"; cout<<"\n3. DELETE_QUEUE LIST"; cout<<"\n4. EXIT"; cout<<"\nPilihan cout<<"\nPilihan : ";cin>>pil; if (pil==1){ baru=data.masuk(); data.INSERT_QUEUE(&tail,&head,baru); } if (pil==2) data.tampil(head); if (pil==3){ data.DELETE_QUEUE(&head,&tail); } } }
#include #include #include #include #include
=========================== SOAL 6 =======================
class elemen { struct list { int bil; struct list *next; }; public: typedef struct list NODE; typedef NODE *PNODE; void tampil_stack (PNODE atas); void PUSH (PNODE *atas, PNODE baru); void POP (PNODE *atas); PNODE masuk_stack (void); void tampil_queue (PNODE head); void insert (PNODE *tail,PNODE *head, PNODE baru); void INSERT_QUEUE (PNODE *tail,PNODE *head, PNODE baru); void DELETE_QUEUE (PNODE *head, PNODE *tail); PNODE masuk_queue (void); };
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
51
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
void elemen :: tampil_stack tampil_stack (PNODE atas) { PNODE pos; pos = atas; cout<<"\n\nbilangan cout<<"\n\nbilangan : "; while(pos != NULL) { cout< bil<<", "; pos = pos -> next; } cout<<"\n\n\n [TEKAN ENTER UNTUK MELANJUTKAN]"; MELANJUTKAN]" ; getch(); } void elemen :: PUSH (PNODE *atas, PNODE baru) { if (*atas == NULL) { *atas = baru; } else { baru -> next = *atas; *atas = baru; } } void elemen :: POP (PNODE *atas) { PNODE PH; PH = *atas; if(*atas == NULL) { cout<<"\n\nSTACK KOSONG !!!\n\n"; } else { if (PH -> next == NULL) { *atas = NULL; free(PH); } else { *atas = (*atas) -> next; free(PH); } } getch(); }
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
52
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
PNODE elemen :: masuk_stack (void) { PNODE baru; baru = (NODE *) malloc (sizeof(NODE)); cout<<"\nbil : ";cin>>baru->bil; baru -> next = NULL; return (baru); getch(); } void elemen :: tampil_queue tampil_queue (PNODE head) { PNODE pos; pos = head; cout<<"\n\nbilangan cout<<"\n\nbilangan : "; while(pos != NULL) { cout< bil<<", "; pos = pos -> next; } cout<<"\n\n\n [TEKAN ENTER UNTUK MELANJUTKAN]"; MELANJUTKAN]" ; getch(); } void elemen :: INSERT_QUEUE INSERT_QUEUE (PNODE *tail,PNODE *head, PNODE baru) { if(*tail == NULL) { *head = baru; *tail = baru; } else { (*tail) -> next = baru; *tail = baru; } } void elemen :: DELETE_QUEUE (PNODE *head, PNODE *tail) { PNODE PH; PH = *head; if(*head == NULL) { cout<<"\n\nQUEUE KOSONG !!!\n\n"; } else { if (PH -> next == NULL) { *head = NULL; *tail = NULL;
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
53
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
free(PH); } else { *head = (*head) -> next; free(PH); } } getch(); } PNODE elemen :: masuk_queue (void) { PNODE baru; baru = (NODE *)malloc(sizeof(NODE)); cout<<"\nbil : ";cin>>baru -> bil; baru -> next = NULL; return(baru); getch(); }
void main() { PNODE atas = NULL; PNODE baru; PNODE head = NULL,tail = NULL; int pil=1, x, m; char *a = "Created bY Tendi Setiadi (6306211) "; char *b = "Í"; elemen data; textbackground (RED); clrscr(); m=strlen(a); gotoxy (20,20); for (x=0; x Menu Utama <-"<
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
54
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
cout<<"\n1> Stack"; cout<<"\n2> Queue"; cout<<"\n3> Exit"; cout<<"\nPilihan : ";cin>>pil; if (pil == 1) { while(pil != 4) { clrscr(); cout<<"\n>> Menu STACK <<"<>pil; if (pil == 1) { baru = data.masuk_stack(); data.masuk_stack(); data.PUSH(&atas, data.PUSH(&atas, baru); } if (pil == 2) data.tampil_stack(atas); if (pil == 3) { data.POP(&atas); } } } if (pil == 2) { while(pil != 4) { clrscr(); cout<<"\n>> Menu QUEUE <<"<>pil; if (pil == 1) { baru=data.masuk_queue(); data.INSERT_QUEUE(&tail,&head,baru); } if (pil == 2) data.tampil_queue(head);
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
55
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
if (pil == 3) { data.DELETE_QUEUE(&head,&tail); } } } } }
=========================== SOAL 7 ======================= #include #include #include //---------------------struct nilaiMatKul{ char nim[10]; char nama[50]; float nilai; }; struct elemen{ nilaiMatKul elmt; struct elemen *next; }; struct queue{ struct elemen *first; struct elemen *last; }; //---------------------void createEmpty(queue *Q){ (*Q).first = NULL; (*Q).last = NULL; } //---------------------bool isEmpty(queue Q){ bool hasil = false; if(Q.first == NULL){ hasil = true; } return hasil; } //----------------------
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
56
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
int countElement(queue Q){ int hasil = 0; if(Q.first != NULL){ /* queue tidak kosong */ elemen *elmt; /* inisialisasi */ elmt = Q.first; while(elmt != NULL){ /* proses */ hasil= hasil + 1; /* iterasi */ elmt = elmt->next; } } return hasil; } //---------------------void add(char nim[], char nama[], float nilai, queue *Q ){ elemen *elmt; elmt = (elemen *) malloc (sizeof (elemen)); strcpy(elmt->elmt.nim, nim); strcpy(elmt->elmt.nama strcpy(elmt->elmt.nama, , nama); elmt->elmt.nilai = nilai; elmt->next = NULL; if((*Q).first == NULL){ (*Q).first = elmt; } else{ (*Q).last->next = elmt; } (*Q).last = elmt; elmt = NULL; } //---------------------void del(queue *Q){ if((*Q).first != NULL){ /* jika queue bukan queue kosong */ elemen *elmt = (*Q).first; (*Q).first = (*Q).first->next; elmt->next = NULL; free(elmt); }
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
57
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
} //---------------------void printQueue(queue Q){ if(Q.first != NULL){ printf("------isi queue------\n"); elemen *elmt = Q.first; int i = 1; while(elmt != NULL){ printf("====================\n"); printf("elemen ke : %d\n", i); printf("nim : %s\n", elmt->elmt.nim); printf("nama : %s\n", elmt->elmt.nama); printf("nilai : %f\n", elmt->elmt.nilai); /* iterasi */ elmt = elmt->next; i = i + 1; } printf("---------------------\n"); } else{ /* proses jika queue kosong */ printf("queue kosong\n"); } } //---------------------int main(){ struct queue Q; createEmpty(&Q); printQueue(Q); printf("=================\n"); add("13507701", add("13507701", "Imam", 64.75, &Q); add("13507702", "Cinta", 75.11, &Q); add("13507703", "Jasmine", 84.63, &Q); printQueue(Q); printf("=================\n"); del(&Q); del(&Q); printQueue(Q); printf("=================\n"); return 0; }
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
58
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
=========================== SOAL 8 ======================= #include #include //---------------------struct nilaiMatKul{ char nim[10]; char nama[50]; float nilai; }; struct queue{ int first; int last; nilaiMatKul data[10]; }; //---------------------void createEmpty(queue *Q){ (*Q).first = -1; (*Q).last = -1; } //---------------------bool isEmpty(queue Q){ bool hasil = false; if(Q.first == -1){ hasil = true; } return hasil; } //---------------------bool isFull(queue Q){ bool hasil = false; if(Q.last == 9){ hasil = true; } return hasil; } //---------------------void add(char nim[], char nama[], float nilai, queue *Q ){
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
59
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
if(isEmpty(*Q) == true){ /* jika queue kosong */ (*Q).first = 0; (*Q).last = 0; strcpy((*Q).data[0].nim, nim); strcpy((*Q).data[0].nama, nama); (*Q).data[0].nilai = nilai; } else{ /* jika queue tidak kosong */ if(isFull(*Q) != true){ (*Q).last = (*Q).last + 1; strcpy((*Q).data[(*Q).last].nim, nim); strcpy((*Q).data[(*Q).last].nama, nama); (*Q).data[(*Q).last].nilai = nilai; } else{ printf("queue penuh\n"); } } } //---------------------void del(queue *Q){ if((*Q).last == 0){ (*Q).first = -1; (*Q).last = -1; } else{ /*menggeser elemen ke depan*/ int i; for(i=((*Q).first for(i=((*Q).first + 1);i<=(*Q).last;i++){ 1);i<=(*Q).last;i++){ strcpy((*Q).data[i-1].nim, (*Q).data[i].nim); strcpy((*Q).data[i-1].nama, (*Q).data[i].nama); (*Q).data[i-1].nilai = (*Q).data[i].nilai; } (*Q).last = (*Q).last - 1; } } //---------------------void printQueue(queue Q){ if(Q.first != -1){ printf("------isi queue------\n"); int i; for(i=Q.last;i>=Q.first;i--){ printf("====================\n"); printf("elemen ke : %d\n", i); printf("nim : %s\n", Q.data[i].nim); printf("nama : %s\n", Q.data[i].nama);
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
60
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
printf("nilai : %f\n", Q.data[i].nilai); } printf("---------------------\n"); } else{ /* proses jika queue kosong */ printf("queue kosong\n"); } } //---------------------int main(){ queue Q; createEmpty(&Q); printQueue(Q); printf("=================\n"); add("13507701", add("13507701", "Nana", 64.75, &Q); add("13507702", add("13507702", "Rudi", 75.11, &Q); add("13507703", "Dea", 84.63, &Q); printQueue(Q); printf("=================\n"); del(&Q); del(&Q); printQueue(Q); printf("=================\n"); return 0; }
=========================== SOAL 9 ======================= #include #define MAX 10 /* The maximum size of the queue */ #include void insert(int queue[], int *rear, int value) { if(*rear < MAX-1) { *rear= *rear +1; queue[*rear] = value; } else { printf("The queue is full can not insert a value\n"); exit(0); }
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
61
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
} void delete(int queue[], int *front, int rear, int * value) { if(*front == rear) { printf("The queue is empty can not delete a value\n"); exit(0); } *front = *front + 1; *value = queue[*front]; queue[*front]; } void main() { int queue[MAX]; int front,rear; int n,value; front=rear=(-1); do { do { printf("Enter the element to be inserted\n"); scanf("%d",&value); insert(queue,&rear,value); printf("Enter 1 to continue\n"); scanf("%d",&n); } while(n == 1); printf("Enter printf("Enter 1 to delete an element\n"); scanf("%d",&n); while( n == 1) { delete(queue,&front,rea delete(queue,&front,rear,&value); r,&value); printf("The value deleted is %d\n",value); printf("Enter printf("Enter 1 to delete an element\n"); scanf("%d",&n); } printf("Enter 1 to continue\n"); scanf("%d",&n); } while(n == 1); }
===================== SOAL 10 Circular Circular Queue ================== #include #define MAX 10 /* The maximum size of the queue */ #include void insert(int queue[], int *rear, int front, int value) { *rear= (*rear +1) % MAX;
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
62
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
if(*rear == front) { printf("The queue is full can not insert a value\n"); exit(0); } queue[*rear] queue[*rear] = value; } void delete(int queue[], int *front, int rear, int * value) { if(*front == rear) { printf("The queue is empty can not delete a value\n"); exit(0); } *front = (*front + 1) % MAX; *value = queue[*front]; queue[*front]; } void main() { int queue[MAX]; int front,rear; int n,value; front=0; rear=0; do { do { printf("Enter the element to be inserted\n"); scanf("%d",&value); insert(queue,&rear,front,value); printf("Enter 1 to continue\n"); scanf("%d",&n); } while(n == 1); printf("Enter 1 to delete an element\n"); scanf("%d",&n); while( n == 1) { delete(queue,&front,rear,&value); printf("The printf("The value deleted is %d\n",value); %d\n",value); printf("Enter 1 to delete an element\n"); scanf("%d",&n); } printf("Enter 1 to continue\n"); scanf("%d",&n); } while(n == 1); }
=============== SOAL 11 Queue Dengan Link List ================ # include
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
63
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
# include struct node { int data; struct node *link; }; void insert(struct node **front, struct node **rear, int value) { struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); /* creates new node using data value passed as parameter */ if(temp==NULL) { printf("No Memory available Error\n"); exit(0); } temp->data = value; temp->link=NULL; if(*rear == NULL) { *rear = temp; *front = *rear; } else { (*rear)->link (*rear)->link = temp; *rear = temp; } } void delete(struct node **front, struct node **rear, int *value) { struct node *temp; if((*front == *rear) && (*rear == NULL)) { printf(" The queue is empty cannot delete Error\n"); exit(0); } *value = (*front)->data; (*front)->data; temp = *front; *front = (*front)->link; (*front)->link; if(*rear == temp) *rear = (*rear)->link; free(temp); } void main() { struct node *front=NULL,*rear = NULL; int n,value;
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
64
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
do { do { printf("Enter the element to be inserted\n"); scanf("%d",&value); insert(&front,&rear,value); printf("Enter 1 to continue\n"); scanf("%d",&n); } while(n == 1); printf("Enter printf("Enter 1 to delete an element\n"); scanf("%d",&n); while( n == 1) { delete(&front,&rear,&value); printf("The value deleted is %d\n",value); %d\n",value); printf("Enter printf("Enter 1 to delete an element\n"); scanf("%d",&n); } printf("Enter 1 to continue\n"); scanf("%d",&n); } while(n == 1); }
==================== SOAL 12 Priority Queue =================== # include # include struct node { int data; int priority; struct node *link; }; void insert(struct node **front, struct node **rear, int value, int priority) { struct node *temp,*temp1; *temp,*temp1; temp=(struct node *)malloc(sizeof(struct node)); /* creates new node using data value passed as parameter */ if(temp==NULL) { printf("No Memory available Error\n"); exit(0); } temp->data = value; temp->priority temp->priority = priority; temp->link=NULL; if(*rear == NULL) /* This is the first node */ { *rear = temp;
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
65
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
*front = *rear; } else { if((*front)->priority if((*front)->priority < priority) /* the element to be inserted has highest priority hence should be the first element*/ { temp->link = *front; *front = temp; } else if( (*rear)->priority > priority) /* the element to be inserted has lowest priority hence should be the last element*/ { (*rear)->link (*rear)->link = temp; *rear = temp; } else { temp1 = *front; while((temp1->link)->priority >= priority) /* find the position and insert the new element */ temp1=temp1->link; temp->link = temp1->link; temp1->link; temp1->link = temp; } } void delete(struct node **front, struct node **rear, int *value, int *priority) { struct node *temp; if((*front == *rear) && (*rear == NULL)) { printf(" The queue is empty cannot delete Error\n"); exit(0); } *value = (*front)->data; (*front)->data; *priority = (*front)->priority; temp = *front; *front = (*front)->link; (*front)->link; if(*rear == temp) *rear = (*rear)->link; free(temp); } void main() { struct node *front=NULL,*rear = NULL;
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
66
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
int n,value, priority; do { do { printf("Enter the element to be inserted and its priority\n"); scanf("%d %d",&value,&priority); insert(&front,&rear,value,priority); printf("Enter 1 to continue\n"); scanf("%d",&n); } while(n == 1); printf("Enter 1 to delete an element\n"); scanf("%d",&n); while( n == 1) { delete(&front,&rear,&value,&priority); printf("The value deleted is %d\ and its priority is %d \n", value,priority); printf("Enter 1 to delete an element\n"); scanf("%d",&n); } printf("Enter printf("Enter 1 to delete an element\n"); scanf("%d",&n); } while( n == 1) } =========================== SOAL 13 ======================= #include #include #define MAX 8 typedef struct{ int data[MAX]; int head; int tail; } Queue; Queue antrian; void Create(){ antrian.head=antrian.tail=-1; } int IsEmpty(){ if(antrian.tail==-1) return 1; else return 0; } int IsFull(){ if(antrian.tail==MAX-1) return 1; else return 0; }
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
67
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
void Enqueue(int data){ if(IsEmpty()==1){ antrian.head=antrian.tail=0; antrian.data[antrian.tail]=data; printf("%d masuk!",antrian.data[antrian.tail]); void Tampil(){ if(IsEmpty()==0){ for(int i=antrian.head;i<=antrian.tail;i++){ printf("%d ",antrian.data[i]); } }else printf("data kosong!\n"); } } else if(IsFull()==0){ antrian.tail++; antrian.data[antrian.tail]=data; printf("%d masuk!",antrian.data[antrian.tail]); } } int Dequeue(){ int i; int e = antrian.data[antrian.head]; for(i=antrian.head;i<=antrian.tail-1;i++){ antrian.data[i] = antrian.data[i+1]; } antrian.tail--; return e; } void Clear(){ antrian.head=antrian.tail=-1; printf("data clear"); } void Tampil(){ if(IsEmpty()==0){ for(int i=antrian.head;i<=antrian.tail;i++){ printf("%d ",antrian.data[i]); } }else printf("data kosong!\n"); } void main(){ int pil; int data; Create(); do{ clrscr(); printf("1. Enqueue\n"); printf("2. Dequeue\n"); printf("3. Tampil\n"); printf("4. Clear\n"); printf("5. Exit\n"); printf("Pilihan = ");scanf("%d",&pil); switch(pil){ case 1: printf("Data = ");scanf("%d",&data);
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
68
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
Enqueue(data); break; case 2: printf("Elemen yang keluar : %d",Dequeue()); break; case 3: Tampil(); break; case 4: Clear(); break; } getch(); } while(pil!=5); } =========================== SOAL 14 ======================= #include #include #include #include #include int main() { int k; Queue timeLine; cout << "\nSampling"; for( int i = 0; i < 7; i++ ) { struct time snapShot; gettime( &snapShot ); Time sample( snapShot.ti_hour, snapShot.ti_min, snapShot.ti_sec, snapShot.ti_hund ); timeLine.put ( *(new Time( sample )) ); cout << "."; randomize(); k = rand(); for(int j = 0; j < k; ++j ) // Delay loop { cout << ""; } } cout << "\nThe timing samples are:\n\n"; while( !timeLine.isEmpty() ) { Time& sampleTime = (Time&)timeLine.get(); cout << sampleTime << "\n"; delete &sampleTime; } return 0; }
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
69
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
=========================== SOAL 15 ======================= #include #include #include #define MAX 8 typedef struct { char data[MAX]; int head; int tail; }Queue; Queue antrian; void Create() { antrian.head=antrian.tail=-1; } int IsEmpty() { if(antrian.tail==-1) return 1; else return 0; } int IsFull() { if(antrian.tail==MAX-1) return 1; else return 0; } void Enqueue(int data) { if(IsEmpty()==1) { antrian.head=antrian.tail=0; antrian.data[antrian.tail]=data; printf("%d masuk!",antrian.data[antrian.tail]); } else if(IsFull()==0) { antrian.tail++; antrian.data[antrian.tail]=data; printf("%d masuk!",antrian.data[antrian.tail]); } }
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
70
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
int Dequeue() { int i; int e = antrian.data[antrian.head]; antrian.data[antrian.head]; for(i=antrian.head;i<=antrian.tail-1;i++) { antrian.data[i] antrian.data[i] = antrian.data[i+1]; antrian.data[i+1]; } antrian.tail--; return e; } void Clear() { antrian.head=antrian.tail=-1; printf("data clear"); } void Tampil() { if(IsEmpty()==0) { for(int i=antrian.head;i<=antri i=antrian.head;i<=antrian.tail;i++) an.tail;i++) { printf("%d ",antrian.data[i]); } } else printf("data kosong!\n"); } void main() { int pil; char kota[MAX]; Create(); do { clrscr(); printf("1. Enqueue\n"); printf("2. Dequeue\n"); printf("3. Tampil\n"); printf("4. Clear\n"); printf("5. Exit\n"); printf("Pilihan printf("Pilihan = ");scanf("%d",&pil); ");scanf("%d",&pil); switch(pil) { case 1: { cout<<"Kota = "; gets(kota);
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
71
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
Enqueue(*kota); break; } case 2: { printf("Elemen printf("Elemen yang keluar : %d",Dequeue()); break; } case 3: Tampil(); break; case 4: Clear(); break; } getch(); }while(pil!=5); }
=========================== SOAL 16 ======================= #include #include #include class elemen{ struct list{ char kota[30]; struct list * next; }; public: typedef struct list NODE; typedef NODE * PNODE; void tampil(PNODE head); void INSERT_QUEUE(PNODE *tail,PNODE *head, PNODE baru); void DELETE_QUEUE(PNODE *head, PNODE *tail); void PUSH(PNODE *head, PNODE baru); void POP(PNODE *head); PNODE masuk(void); }data;
void elemen::PUSH(PNOD elemen::PUSH(PNODE E *head, PNODE baru){ if(*head==NULL){ *head = baru; }else{ baru->next = *head; *head = baru; } } void elemen::POP(PNODE *head){ PNODE PH; PH = *head; if(*head==NULL){
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
72
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
cout<<"\n\nSTACK KOSONG !!!\n\n"; }else{ if (PH->next==NULL){ (PH->next==NULL){ *head = NULL; free(PH); }else{ *head = (*head)->next; (*head)->next; free(PH); } } }
void elemen::tampil(PNODE head){ PNODE pos; pos = head; if(pos==NULL){ cout<<"\n\nLIST KOSONG."; }else{ cout<<"\n\nDaftar Kota : "; while(pos!=NULL){ cout<kota<<", cout<kota<<", "; pos=pos->next; } cout<<"\b\b."; } } void elemen::INSERT_QUEUE(PNODE *tail,PNODE *head, PNODE baru){ if(*tail==NULL){ *head = baru; *tail = baru; }else{ (*tail)->next (*tail)->next = baru; *tail = baru; } } void elemen::DELETE_QUEUE(PNODE elemen::DELETE_QUEUE(PNODE *head, PNODE *tail){ PNODE PH; PH = *head; if(*head==NULL){ cout<<"\n\nQUEUE KOSONG !!!\n\n"; }else{ if (PH->next==NULL){ (PH->next==NULL){ *head = NULL; *tail = NULL; free(PH); }else{ *head = (*head)->next; (*head)->next; free(PH); } }
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
73
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
} PNODE elemen::masuk(void){ PNODE baru; baru=(NODE *)malloc(sizeof(NODE)); cout<<"\nKota : ";cin>>baru->kota; baru->next=NULL; return(baru); } void main(){ PNODE head=NULL,tail=NULL; head=NULL,tail=NULL; PNODE baru; int pil=1,cari=1; clrscr(); while(pil!=4){ clrscr(); cout<<"\n\n1. cout<<"\n\n1. INSERT_QUEUE INSERT_QUEUE LIST"; cout<<"\n2. LIHAT DATA LIST"; cout<<"\n3. DELETE_QUEUE LIST"; cout<<"\n4. EXIT"; cout<<"\nPilihan : ";cin>>pil; if (pil==1){ cout<<"\n\n1. cout<<"\n\n1. Insert Queue"; cout<<"\n2. Insert Stack"; cout<<"\nPilih ? "; cin>>cari; baru=data.masuk(); if(cari==1) { data.INSERT_QUEUE(&tail,&head,baru); } if(cari==2){ data.PUSH(&head, baru); } } if (pil==2) data.tampil(head); getch(); if (pil==3){ cout<<"\n\n1. Delete Queue"; cout<<"\n2. Delete stack"; cout<<"\nPilih ? "; cin>>cari; if(cari==1){ data.DELETE_QUEUE(&head,&tail); } if(cari==2){ data.POP(&head); } } }
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
74
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
}
=========================== SOAL 17 ======================= #include #include #include struct list{ int bil; struct list * next; }; typedef struct list NODE; typedef NODE * PNODE; void tampil(PNODE head){ PNODE pos; pos = head; cout<<"\n\nbilangan cout<<"\n\nbilangan : "; while(pos!=NULL){ cout<bil<<", cout<bil<<", "; pos=pos->next; } } void INSERT_QUEUE(PNODE INSERT_QUEUE(PNODE *tail,PNODE *head, PNODE baru){ if(*tail==NULL){ *head = baru; *tail = baru; }else{ (*tail)->next (*tail)->next = baru; *tail = baru; } } void DELETE_QUEUE(PNODE *head, PNODE *tail){ PNODE PH; PH = *head; if(*head==NULL){ cout<<"\n\nQUEUE KOSONG !!!\n\n"; }else{ if (PH->next==NULL){ *head = NULL; *tail = NULL; free(PH); }else{ *head = (*head)->next; free(PH); } } } PNODE masuk(void){
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
75
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
PNODE baru; baru=(NODE *)malloc(sizeof(NODE)); cout<<"\nbil : ";cin>>baru->bil; baru->next=NULL; return(baru); } void main(){ PNODE head=NULL,tail=NULL; head=NULL,tail=NULL; PNODE baru; int pil=1; clrscr(); while(pil!=4){ cout<<"\n\n1. INSERT_QUEUE LIST"; cout<<"\n2. LIHAT DATA LIST"; cout<<"\n3. DELETE_QUEUE LIST"; cout<<"\n4. EXIT"; cout<<"\nPilihan cout<<"\nPilihan : ";cin>>pil; if (pil==1){ baru=masuk(); INSERT_QUEUE(&tail,&head,baru); } if (pil==2) tampil(head); if (pil==3){ DELETE_QUEUE(&head,&tail); } } } =========================== SOAL 18 ======================= #include #include #include #include #include class elemen { struct list { int bil; struct list *next; }; public: typedef struct list NODE; typedef NODE *PNODE; void tampil_stack (PNODE atas); void PUSH (PNODE *atas, PNODE baru); void POP (PNODE *atas);
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
76
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
PNODE masuk_stack (void); void tampil_queue (PNODE head); void insert (PNODE *tail,PNODE *head, PNODE baru); void INSERT_QUEUE (PNODE *tail,PNODE *head, PNODE baru); void DELETE_QUEUE (PNODE *head, PNODE *tail); PNODE masuk_queue (void); }; void elemen :: tampil_stack tampil_stack (PNODE atas) { PNODE pos; pos = atas; cout<<"\n\nbilangan cout<<"\n\nbilangan : "; while(pos != NULL) { cout< bil<<", "; pos = pos -> next; } cout<<"\n\n\n [TEKAN ENTER UNTUK MELANJUTKAN]"; MELANJUTKAN]" ; getch(); } void elemen :: PUSH (PNODE *atas, PNODE baru) { if (*atas == NULL) { *atas = baru; } else { baru -> next = *atas; *atas = baru; } } void elemen :: POP (PNODE *atas) { PNODE PH; PH = *atas; if(*atas == NULL) { cout<<"\n\nSTACK KOSONG !!!\n\n"; } else { if (PH -> next == NULL) { *atas = NULL; free(PH); }
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
77
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
else { *atas = (*atas) -> next; free(PH); } } getch(); } PNODE elemen :: masuk_stack (void) { PNODE baru; baru = (NODE *) malloc (sizeof(NODE)); cout<<"\nbil : ";cin>>baru->bil; baru -> next = NULL; return (baru); getch(); } void elemen :: tampil_queue tampil_queue (PNODE head) { PNODE pos; pos = head; cout<<"\n\nbilangan cout<<"\n\nbilangan : "; while(pos != NULL) { cout< bil<<", "; pos = pos -> next; } cout<<"\n\n\n [TEKAN ENTER UNTUK MELANJUTKAN]"; MELANJUTKAN]" ; getch(); } void elemen :: INSERT_QUEUE INSERT_QUEUE (PNODE *tail,PNODE *head, PNODE baru) { if(*tail == NULL) { *head = baru; *tail = baru; } else { (*tail) -> next = baru; *tail = baru; } } void elemen :: DELETE_QUEUE (PNODE *head, PNODE *tail) { PNODE PH; PH = *head; if(*head == NULL)
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
78
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
{ cout<<"\n\nQUEUE KOSONG !!!\n\n"; } else { if (PH -> next == NULL) { *head = NULL; *tail = NULL; free(PH); } else { *head = (*head) -> next; free(PH); } } getch(); } PNODE elemen :: masuk_queue (void) { PNODE baru; baru = (NODE *)malloc(sizeof(NODE)); cout<<"\nbil : ";cin>>baru -> bil; baru -> next = NULL; return(baru); getch(); }
void main() { PNODE atas = NULL; PNODE baru; PNODE head = NULL,tail = NULL; int pil=1, x, m; char *a = "Created bY Tendi Setiadi (6306211) "; char *b = "Í"; elemen data; textbackground (RED); clrscr(); m=strlen(a); gotoxy (20,20); for (x=0; x
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
79
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
{ cout<<*b; delay(40); } while (pil != 3) { clrscr(); cout<<"\n-> Menu Utama <-"< Stack"; cout<<"\n2> Queue"; cout<<"\n3> Exit"; cout<<"\nPilihan : ";cin>>pil; if (pil == 1) { while(pil != 4) { clrscr(); cout<<"\n>> Menu STACK <<"<>pil; if (pil == 1) { baru = data.masuk_stack(); data.masuk_stack(); data.PUSH(&atas, data.PUSH(&atas, baru); } if (pil == 2) data.tampil_stack(atas); if (pil == 3) { data.POP(&atas); } } } if (pil == 2) { while(pil != 4) { clrscr(); cout<<"\n>> Menu QUEUE <<"<>pil;
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
80
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
if (pil == 1) { baru=data.masuk_queue(); data.INSERT_QUEUE(&tail,&head,baru); } if (pil == 2) data.tampil_queue(head); if (pil == 3) { data.DELETE_QUEUE(&head,&tail); } } } } }
=========================== SOAL 19 ======================= #include #include #include class elemen{ struct list{ int bil; struct list * next; }; public: typedef struct list NODE; typedef NODE * PNODE; void tampil(PNODE head); void insert(PNODE *tail,PNODE *head, PNODE baru); void INSERT_QUEUE(PNODE INSERT_QUEUE(PNODE *tail,PNODE *head, PNODE baru); void DELETE_QUEUE(PNODE *head, PNODE *tail); PNODE masuk(void); }; void elemen::tampil(PNODE head){ PNODE pos; pos = head; cout<<"\n\nbilangan cout<<"\n\nbilangan : "; while(pos!=NULL){ cout<bil<<", cout<bil<<", "; pos=pos->next; } } void elemen::INSERT_QUEUE(PNODE *tail,PNODE *head, PNODE baru){ if(*tail==NULL){
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
81
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
*head = baru; *tail = baru; }else{ (*tail)->next (*tail)->next = baru; *tail = baru; } } void elemen::DELETE_QUEUE(PNODE elemen::DELETE_QUEUE(PNODE *head, PNODE *tail){ PNODE PH; PH = *head; if(*head==NULL){ cout<<"\n\nQUEUE KOSONG !!!\n\n"; }else{ if (PH->next==NULL){ *head = NULL; *tail = NULL; free(PH); }else{ *head = (*head)->next; free(PH); } } } PNODE elemen::masuk(void){ PNODE baru; baru=(NODE *)malloc(sizeof(NODE)); cout<<"\nbil : ";cin>>baru->bil; baru->next=NULL; return(baru); } void main(){ PNODE head=NULL,tail=NULL; head=NULL,tail=NULL; PNODE baru; int pil=1; elemen data; clrscr(); while(pil!=4){ cout<<"\n\n1. INSERT_QUEUE LIST"; cout<<"\n2. LIHAT DATA LIST"; cout<<"\n3. DELETE_QUEUE LIST"; cout<<"\n4. EXIT"; cout<<"\nPilihan cout<<"\nPilihan : ";cin>>pil; if (pil==1){ baru=data.masuk(); data.INSERT_QUEUE(&tail,&head,baru); } if (pil==2) data.tampil(head); if (pil==3){ data.DELETE_QUEUE(&head,&tail);
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
82
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
} } }
=========================== SOAL 20 ======================= =========================== SOAL 21 ======================= =========================== SOAL 22 ======================= =========================== SOAL 23 ======================= =========================== SOAL 24 ======================= =========================== SOAL 25 ======================= =========================== SOAL 26 ======================= =========================== SOAL 27 ======================= =========================== SOAL 28 ======================= =========================== SOAL 29 ======================= =========================== SOAL 30 ======================= =========================== SOAL 31 ======================= =========================== SOAL 32 ======================= =========================== SOAL 33 ======================= =========================== SOAL 34 =======================
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
83
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
=========================== SOAL 35 ======================= =========================== SOAL 36 ======================= =========================== SOAL 37 ======================= =========================== SOAL 38 ======================= =========================== SOAL 39 ======================= =========================== SOAL 40 ======================= =========================== SOAL 41 ======================= =========================== SOAL 42 ======================= =========================== SOAL 43 ======================= =========================== SOAL 44 ======================= =========================== SOAL 45 ======================= =========================== SOAL 46 ======================= =========================== SOAL 47 ======================= =========================== SOAL 48 ======================= =========================== SOAL 49 =======================
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T
84
100 SOAL & JAWABAN JAWABAN STACK DAN QUEUE
=========================== SOAL 50 ======================= =========================================== ================
TUGAS
Imam Ciptarjo – 6307130 S TRUKTUR DATA ATA, MUNARWAN, S.T