0% found this document useful (0 votes)
53 views9 pages

LAB # 11 Queues Implementation: Object

The document describes a lab assignment on implementing queues using linked lists. It includes sample code for a queue using a linked list with a Node class containing data and next pointers. The code shows enqueue and dequeue methods as well as a print method. The output of running the sample code is also shown.

Uploaded by

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

LAB # 11 Queues Implementation: Object

The document describes a lab assignment on implementing queues using linked lists. It includes sample code for a queue using a linked list with a Node class containing data and next pointers. The code shows enqueue and dequeue methods as well as a print method. The output of running the sample code is also shown.

Uploaded by

Abdul Nafay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

SWE-203L: Data Structures and Algorithms SSUET/QR/114

LAB # 11
Queues Implementation
Object:
FIFO queue implementation using double end.

Sample Program 1:
Code:
package Lab11;

public class QueueLinkedList {

class Node{
int data;
Node next;
Node(int key){
this.data = key;
this.next=null;
}
}
Node front, rear;
public QueueLinkedList() {
this.front = null;
this.rear = null;
}
void enqueue(int key) {
Node temp = new Node(key);
if (this.rear == null) {
this.front = this.rear = temp;
return;
}
this.rear.next = temp;
this.rear = temp;
}
Node dequeue() {
if (this.front == null)
return null;
Node temp = this.front;
this.front = this.front.next;
if (this.front == null)
this.rear = null;
return temp;
}
public static void printList(QueueLinkedList list) {
Node currentNode = list.front;
SWE-203L: Data Structures and Algorithms SSUET/QR/114

System.out.print("Linked List Elements: ");


while (currentNode != null)
{System.out.print(currentNode.data + " ");
currentNode = currentNode.next;
}
System.out.println();
}
public static void main(String[] args) {
QueueLinkedList q = new QueueLinkedList();
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
printList(q);
System.out.println("Dequeued item is " +
q.dequeue().data);
printList(q);
}
}

Output:
SWE-203L: Data Structures and Algorithms SSUET/QR/114

Lab Tasks:
1. Write a program to implement queue and perform all operations on it.

Code:
import java.util.*;
public class Lab11 {
static void push(Stack st, String a) {
st.push(new String(a));
System.out.println("push(" + a + ")");
System.out.println("stack: " + st);
}
static void pop(Stack st) {
System.out.print("pop: ");
String a = (String) st.pop();
System.out.println(a);
System.out.println("stack: " + st);
}
public static void main(String args[]) {
Stack st = new Stack();
System.out.println("stack: " + st);
push(st, "Nafay");
push(st, "Asghar");
push(st, "Rumaisa");
push(st, "Ayesha");
push(st, "Shahnawaz");
push(st, "Narmeen");
push(st, "Daniyal");
push(st, "Hamzah");
push(st, "Talha");
push(st, "Moazzam");
push(st, "Nouman");
push(st, "Shahzaib");
push(st, "Maham");
push(st, "Huda");
push(st, "Mahnoor");
push(st, "Hijab");
push(st, "Areeba");
push(st, "Hussnain");
push(st, "Areesha");
push(st, "Haseeb");
pop(st);
pop(st);
pop(st);
pop(st);
pop(st);
pop(st);
pop(st);
pop(st);
pop(st);
pop(st);
pop(st);
pop(st);
pop(st);
pop(st);
SWE-203L: Data Structures and Algorithms SSUET/QR/114

pop(st);
pop(st);
pop(st);
pop(st);
pop(st);
pop(st);
try {
pop(st);
} catch (EmptyStackException e) {
System.out.println("empty stack");
}
}
}

Output:
SWE-203L: Data Structures and Algorithms SSUET/QR/114
SWE-203L: Data Structures and Algorithms SSUET/QR/114

Home Tasks:
1. Write a program that processes a group of input lines. Each input line contains an
'A' for arrival or a 'D' for departure, and a license plate number. Cars are assumed
to arrive and depart in the order specified by the input. The program should:
a) Print a message whenever a car arrives or departs
b) When a car arrives, the message should specify whether or not there is a room
for the car in the garage. If there is no room, the car leaves without entering the garage.
c) When a car departs, the message should include the number of times that the
car was moved out of the garage to allow other cars to depart.
Code:
package Lab11;

public class HomeTask {


static class QueueArray {
private static int front, rear, capacity;
private static int queue[];

QueueArray(int c)
{
front = rear = 0;
capacity = c;
queue = new int[capacity];
}

static void queueEnqueue(int data)


{
System.out.println("Car Arrived..");
// check queue is full or not
if (capacity == rear) {
System.out.printf("\nNo room for more
cars.\n");
return;
}

else {
queue[rear] = data;
rear++;
}
return;
}

static void queueDequeue()


{
SWE-203L: Data Structures and Algorithms SSUET/QR/114

if (front == rear) {
System.out.printf("\nThere are no cars. Empty
Queue\n");
return;
}

else {
System.out.println("Car Departured..");
int i;
for (i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
System.out.println("The car moved "+(i+1)+"
times.");

// store 0 at rear indicating there's no


element
if (rear < capacity)
queue[rear] = 0;

rear--;
}
return;
}

static void queueDisplay()


{
int i;
if (front == rear) {
System.out.printf("\nGarage is Empty\n");
return;
}

// traverse front to rear and print elements


for (i = front; i < rear; i++) {
System.out.printf(" %d <-- ", queue[i]);
}
System.out.println();
return;
}

static void queueFront()


{
if (front == rear) {
System.out.printf("\nQueue is Empty\n");
return;
}
SWE-203L: Data Structures and Algorithms SSUET/QR/114

System.out.printf("\nFront Element is: %d",


queue[front]);
return;
}
}

public static void main(String[] args){


QueueArray garage = new QueueArray(4);
garage.queueEnqueue(123);
garage.queueDisplay();
garage.queueEnqueue(456);
garage.queueEnqueue(789);
garage.queueEnqueue(101);
garage.queueDisplay();
garage.queueEnqueue(102);
garage.queueDisplay();
garage.queueDequeue();
garage.queueDisplay();
garage.queueDequeue();
garage.queueDisplay();
garage.queueDequeue();
garage.queueDisplay();
garage.queueDequeue();
garage.queueDisplay();
garage.queueDequeue();

}
SWE-203L: Data Structures and Algorithms SSUET/QR/114

Output:

You might also like