CONTOH PROGRAM QUEUE MENGGUNAKAN BAHASA C
A.QUEUE
Contoh program di bawah ini merupakan queue yang menggunakan pointer.
#include<conio.h>
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
node *link;
};
node *front=NULL;
node *rear=NULL;
void insertion()
{
node *temp;
temp=(node *)malloc(sizeof (node));
printf("Masukan data= ");
scanf("%d",&temp->data);
temp->link=NULL;
if(front==NULL)
{
front=temp;
rear=temp;
}
else
{
rear->link=temp;
rear=rear->link;
}
}//end insertion
void deletion()
{
node *temp;
if(front==NULL)
{
printf("Cannot insert queue is empty\n");
}
else
{
temp=front;
front=front->link;
printf("%d\n",temp->data);
free(temp);
if(front==NULL)
{
rear=NULL;
}
}
}//end delete
void display()
{
node *temp;
if(front==NULL)
{
printf("cannot display queue is empty\n");
}
else
{
temp=front;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->link;
}
}
}//end display
int pilihan(int pilih)
{
printf("Masukan pilihan anda= ");
scanf("%d",&pilih);
return pilih;
}
void daftar()
{
printf("Daftar pilihan\n");
printf("\t\t1).Insertion\n");
printf("\t\t2).Deletion\n");
printf("\t\t3).Display\n");
printf("\t\t4).search value\n");
}
void searchvalue()
{
node *temp;
int nilai=0;
int i=0;
int j=0;
if(front==NULL)
{
printf("cannot find queue is empty\n");
}
else
{
printf("Masukan nilai yang ingin di cari= ");
scanf("%d",&nilai);
temp=front;
while(temp!=NULL)
{
j++;
if(nilai == temp->data)
{
i++;
printf("Nilai %d di temukan di queue di urutan= %d \n",nilai,j);
}
temp=temp->link;
}//end while
printf("Nilai %d di temukan di queue sebanyak= %d \n",nilai,i);
}//end else
}//end search value
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
node *link;
};
node *front=NULL;
node *rear=NULL;
void insertion()
{
node *temp;
temp=(node *)malloc(sizeof (node));
printf("Masukan data= ");
scanf("%d",&temp->data);
temp->link=NULL;
if(front==NULL)
{
front=temp;
rear=temp;
}
else
{
rear->link=temp;
rear=rear->link;
}
}//end insertion
void deletion()
{
node *temp;
if(front==NULL)
{
printf("Cannot insert queue is empty\n");
}
else
{
temp=front;
front=front->link;
printf("%d\n",temp->data);
free(temp);
if(front==NULL)
{
rear=NULL;
}
}
}//end delete
void display()
{
node *temp;
if(front==NULL)
{
printf("cannot display queue is empty\n");
}
else
{
temp=front;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->link;
}
}
}//end display
int pilihan(int pilih)
{
printf("Masukan pilihan anda= ");
scanf("%d",&pilih);
return pilih;
}
void daftar()
{
printf("Daftar pilihan\n");
printf("\t\t1).Insertion\n");
printf("\t\t2).Deletion\n");
printf("\t\t3).Display\n");
printf("\t\t4).search value\n");
}
void searchvalue()
{
node *temp;
int nilai=0;
int i=0;
int j=0;
if(front==NULL)
{
printf("cannot find queue is empty\n");
}
else
{
printf("Masukan nilai yang ingin di cari= ");
scanf("%d",&nilai);
temp=front;
while(temp!=NULL)
{
j++;
if(nilai == temp->data)
{
i++;
printf("Nilai %d di temukan di queue di urutan= %d \n",nilai,j);
}
temp=temp->link;
}//end while
printf("Nilai %d di temukan di queue sebanyak= %d \n",nilai,i);
}//end else
}//end search value
int main()
{
int pilih;
char jawab;
daftar();
do
{
pilih=pilihan(pilih);
switch(pilih)
{
case 1:
{
insertion();
break;
}
case 2:
{
deletion();
break;
}
case 3:
{
display();
break;
}
case 4:
{
searchvalue();
break;
}
}
printf("Apakah anda ingin mengulang= ");
scanf(" %c",&jawab);
}
while(jawab=='y');
getch();
{
int pilih;
char jawab;
daftar();
do
{
pilih=pilihan(pilih);
switch(pilih)
{
case 1:
{
insertion();
break;
}
case 2:
{
deletion();
break;
}
case 3:
{
display();
break;
}
case 4:
{
searchvalue();
break;
}
}
printf("Apakah anda ingin mengulang= ");
scanf(" %c",&jawab);
}
while(jawab=='y');
getch();
return 0;
}
}
Seperti ini outputnya:
ok,, semoga bermanfaat !!!
klo pakai char gimana programnya??
BalasHapusmisalnya
char nama[]
char nim[]
char jurusan[]
programnya gimana jadinya????
artikelnya bermanfaat kak, ini sya jga punya artikel tentang Queue dalam Bahasa C, smoga dpt saling melengkapi
BalasHapusQueue (Antrian) dalam Bahasa C
Kumpulan Materi Bahasa C, Lengkap dengan contoh program