Jumat, 21 Maret 2014

Queue Using Linked list


#include<string.h>
#include<windows.h>
#include<stdio.h>


struct tnode{
int number;
tnode *next;

}*curr,*head=NULL;
struct tnode *front = 0;
struct tnode *tail = 0;

void add(){
int x;

printf("Input x : ");
scanf("%d", &x); fflush(stdin);

struct tnode *node = (struct tnode *)malloc(sizeof(struct tnode));
node->number = x;
node->next = 0;
if (front==NULL){

front = tail = node;
}
else{

tail->next = node;
tail = node;
}
}

void view(){
if (front == 0)
printf("Kosong ");
else{
while (front!=NULL){
printf("%d", front->number);
front = front->next;
}
} getchar();
}

void hapusAll(){
struct tnode *del = front;
if (front == 0){
printf("Queue kosong");
}else{
front = front->next;
free(del);
}
}

void main(){
int pilih;
do{
system("cls");
printf("\t\t Queue using linked list\n");
printf("\t\t   =by singgihchandra=\n\n");
printf("1. enqueue\n");
printf("2. dequeue\n");
printf("3. display\n");
printf("4. exit\n");
printf("pilihan anda:");
scanf("%d",&pilih);fflush(stdin);
if(pilih<1||pilih>5){
printf("pilih antara 1..4");
getchar();
}else{
switch(pilih){
case 1:add();break;
case 2:hapusAll();break;
case 3:view();break;
}
}
}while(pilih!=4);


getchar();
}

Tidak ada komentar: