Sunteți pe pagina 1din 8

Leksion 08

Rradhet. Karakteristikat dhe ndrtimi.


Zbatimi me ane te listave lineare njedrejtimore.
Zbatimi me ane te vektoreve.
Veorit dhe struktura e tyre.

FIRST IN FIRST OUT = FIFO

Rradha zbaton logjiken e rradhes perpara sporteleve te nje banke, kinemaje ose
aeroporti. Personi qe vjen i pari perfiton i pari
sherbimin.

Zbatimi me ane te listave lineare.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{
int Data;
struct Node* next;
}*rear, *front;

void delQueue()
{
struct Node *temp, *var=rear;
if(var==rear)
{
rear = rear->next;
free(var);
}
else
printf("\nQueue Empty");
}

void push(int value)


{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (front == NULL)
{
front=temp;
front->next=NULL;
rear=front;
}
else
{
front->next=temp;
front=temp;
front->next=NULL;
}
}

void display()
{
struct Node *var=rear;
if(var!=NULL)
{
printf("\nElements are as: ");
while(var!=NULL)
{
printf("\t%d",var->Data);
var=var->next;

}
printf("\n");
}
else
printf("\nQueue is Empty");
}

int main()
{
int i=0;
front=NULL;
printf(" \n1. Push to Queue");
printf(" \n2. Pop from Queue");
printf(" \n3. Display Data of Queue");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
{
int value;
printf("\nEnter a valueber to push into Queue: ");
scanf("%d",&value);
push(value);
display();
break;
}

case 2:
{
delQueue();
display();
break;
}
case 3:
{
display();
break;
}
case 4:
{
exit(0);
}
default:
{
printf("\nwrong choice for operation");
}
}
}
}

Zbatimi me ane te tabelave njepermasore(vektoreve).

#include<stdio.h>
#include<conio.h>
#define MAX 4

void insert(int);
int del();
int queue[MAX], rear=0, front=0;
void display();
int main()
{
char ch , a='y';
int choice, token;
printf("1.Insert");
printf("\n2.Delete");
printf("\n3.show or display");
do
{
printf("\nEnter your choice for the operation: ");
scanf("%d",&choice);
switch(choice)
{
case 1: insert(token);
display();
break;

case 2:
token=del();
printf("\nThe token deleted is %d",token);
display();
break;

case 3:
display();
break;

default:
printf("Wrong choice");
break;
}
printf("\nDo you want to continue(y/n):");
ch=getch();
}
while(ch=='y'||ch=='Y');
getch();
}

void display()
{
int i;
printf("\nThe queue elements are:");
for(i=rear;i<front;i++)
{
printf("%d ",queue[i]);
}
}

void insert(int token)


{
if(front==MAX)
{
printf("\nQueue full");
return;
}
else

{
printf("\nEnter the token to be inserted:");
scanf("%d",&token);
queue[front]=token;
front=front+1;
return;
}
}

int del()
{
int t;
if(front==rear)
{
printf("\nQueue empty");
return 0;
}
rear=rear+1;
t=queue[rear-1];
return t;
}

S-ar putea să vă placă și