0% found this document useful (0 votes)
5 views4 pages

Queue Implementation in Java Lab

The document contains code for implementing a queue data structure using both an array and a linked list in Java. It includes methods for enqueueing, dequeueing, and displaying the queue contents. The code demonstrates basic operations and error handling for both implementations.

Uploaded by

Arman Babar Ali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Queue Implementation in Java Lab

The document contains code for implementing a queue data structure using both an array and a linked list in Java. It includes methods for enqueueing, dequeueing, and displaying the queue contents. The code demonstrates basic operations and error handling for both implementations.

Uploaded by

Arman Babar Ali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Data Structures (Lab)

Slot: Monday 2:30 till 5:20


Lab # 8 Tasks

Submitted by Arman Babar Ali (68312)

CODES:
QUEUE USING ARRAY:
public class Main {
public static void main(String[] args)
{
queuearray obj = new queuearray(10);
[Link](100);
[Link](200);
[Link](300);
[Link]();
}

class queuearray{
int size, front, rear;
int capacity;
int queue[];

queuearray(int x)
{
[Link] = x;
queue = new int[capacity];
front = 0;
rear = -1;
size = 0;
}

void enqueue(int x)
{
if (size == capacity)
{
[Link]("queue is full");
return;
}
rear = (rear+1) % capacity;
queue[rear] = x;
size++;
[Link]("value inserted");

void dequeue(int x)
{
if (size == 0)
{
[Link]("queue is empty");
return;
}
int removed = queue[front];
front = (front+1) % capacity;
size--;
[Link](removed +"deleted");
}

void display()
{
if (size == 0)
{
[Link]("queue is empty");
return;
}
for(int i=0; i < size; i++)
{
[Link](queue[(front+i)%capacity]+ " ");
}
[Link]();
}
}

QUEUE USING LINKED LIST:


public class queuelinked
{
public static void main(String[] args)
{
queuelinkedlist obj = new queuelinkedlist();
[Link](10);
[Link](20);
[Link](30);
[Link]();
[Link]();
[Link](40);
[Link]();
}

class node
{
int data;
node next;

node(int data)
{
[Link] = data;
[Link] = next;
}
}

class queuelinkedlist
{
node front, rear;
queuelinkedlist()
{
front=rear=null;
}

void enqueue(int x)
{
node new_node = new node(x);
if (rear == null)
{
front=rear=new_node;
[Link](x+" inserted successfully");
}
[Link] = new_node;
rear = new_node;
[Link](x+" inserted successfully");
}

void dequeue()
{
if (front == null)
{
[Link]("Queue underflow");
return;
}
int removed = [Link];
front = [Link];
if(front == null)
{
rear = null;
[Link](removed + " deleted successfully");
}
}

void display()
{
if (front == null)
{
[Link]("Queue underflow");
return;
}
node temp = front;
while(temp != null)
{
[Link]([Link] + " ");
temp = [Link];
}
}

You might also like