top of page
Writer's pictureAdmin

Coding Nomor Antrian pakai Queue Di prgram C

Cara bikin nomor antrian pakai Queue di bahasa prgram C yaittu :


Coding :

#include<stdio.h> #include<stdlib.h> #include<time.h> struct queue{ struct queue *next; int up; }*head, *tail, *curr, *temp; void insert (int x){ struct queue *temp; curr = (struct queue*) malloc (sizeof(queue)); curr -> up = x; if (head == NULL){ head = tail = curr; } else if (curr -> up < head -> up){ curr -> next = head; head = curr; } else if (curr -> up > tail -> up){ tail -> next = curr; tail = curr; } else { temp = head; while (temp -> next -> up < curr -> up){ curr = curr -> next; } curr -> next = temp -> next; temp -> next = curr; } tail -> next = NULL; } void Delete (int x){ curr = head; if (head == tail){ head = tail = NULL; free(curr); } else{ curr = head; head = head -> next; free(curr); } } void view (){ int f = 0; curr = head; while (curr != NULL){ f++; printf ("Queue to %d : Number. %d\n", f, curr -> up); curr = curr -> next; } } int main (){ int a, x, y=0; while (y >= 0){ y++; printf ("BANK OF ME\n"); printf ("=================\n"); printf ("1. Take the Queue\n"); printf ("2. Serve Customers\n"); printf ("3. See Queue\n"); printf ("4. Exit\n"); printf ("Enter Your Choices : "); scanf ("%d", &a); if (y == 1){ if (a == 1){ srand (time(NULL)); x = rand(); insert(x); printf ("You are in a queue number : %d\n", x); } if (a == 2){ printf ("There is no queue!\n"); y = 0; } if (a == 3){ printf ("There is no queue!\n"); } if (a == 4){ break; } } if (y >= 2){ if (a == 1){ x = x + 1; insert(x); printf ("You are in a queue number : %d\n", x); } if (a == 2){ if (head != NULL){ printf ("Serve customers with a queue number : %d\n",

head -> up); Delete(x); } else { printf ("No queues\n"); y = 0; } } if (a == 3){ if (head != NULL){ printf ("\n"); view(); printf ("\n"); } else { printf ("No queues\n"); y = 0; } } if (a == 4){ break; } } } }

Input dan Output :


23 views0 comments

Recent Posts

See All

Comments


bottom of page