//File header.h
#include (<)stdio.h(>)
#include (<)stdlib.h(>)
#include (<)conio.h(>)
#define maxel 5
typedef int address;
typedef char infotype;
typedef struct{
infotype T[maxel];
address head;
address tail;
}queue;
void createempty(queue *q);
int isempty(queue q);
int isfull(queue q);
int isoneelmn(queue q);
void add(queue *q, infotype val);
void del(queue *q);
void printqueue(queue q);
//File definition.c
#include "antrian.h"
void createempty(queue *q)
{
(*q).head=(*q).tail=-1;
}
int isempty(queue q)
{
return(q.head==-1&&q.tail==-1);
}
int isfull(queue q)
{
return((q.head(<)q.tail && q.tail-q.head==maxel-1) || (q.head(>)q.tail && q.head-q.tail==1));
}
int isoneelmn(queue q)
{
return(q.head==q.tail && q.head!=-1);
}
void add(queue *q, infotype val)
{
if(!isfull((*q)))
{
if(isempty((*q)))
{
(*q).head=(*q).tail=0;
(*q).T[(*q).head]=val;
}
else
{
if((*q).tail==maxel-1)
(*q).tail=0;
else
(*q).tail++;
(*q).T[(*q).tail]=val;
}
}
else
printf("Antrian Penuh");
}
void del(queue *q)
{
infotype temp=(*q).T[(*q).head];
if(!isempty(*q))
{
if(isoneelmn((*q)))
createempty(&(*q));
else
{
if((*q).head==maxel-1)
(*q).head=0;
else
(*q).head++;
}
printf("Elemen yang dihapus: %c",temp);
}
else
printf("Antrian Kosong");
}
void printqueue(queue q)
{
address i;
if(q.head<=q.tail)
{
for(i=q.head;i<=q.tail;i++)
printf("%c",q.T[i]);
}
else
{
for(i=q.head;i(<)maxel;i++)
printf("%c",q.T[i]);
for(i=0;i<=q.tail;i++)
printf("%c",q.T[i]);
}
}
//File main.c
#include "antrian.h"
void main()
{
queue q;
char val;
int pil;
createempty(&q);
do
{
system("cls");
printf("head = %d , tail = %d\n",q.head,q.tail);
puts("\nMENU\n");
puts("1.Add Queue\n");
puts("2.Del Queue\n");
puts("3.Print All Queue\n");
puts("0.Keluar\n");
puts("Pilihan : ");scanf("%d",&pil);
switch(pil)
{
case 1:
//printf("Masukkan data : ");scanf("%s",&val);
printf("Masukkan data : ");scanf(" %c",&val);
add(&q,val);
getch();
break;
case 2:
del(&q);
getch();
break;
case 3:
printf("Isi dari antrian : ");
printqueue(q);
getch();
break;
case 0:
printf("Bye");
break;
default:
printf("Nomor yang anda masukkan salah");
getch();
break;
}
}while(pil!=0);
}
Subscribe to:
Post Comments (Atom)


No comments:
Post a Comment