LAB # 11 Queues Implementation: Object
LAB # 11 Queues Implementation: Object
LAB # 11
Queues Implementation
Object:
FIFO queue implementation using double end.
Sample Program 1:
Code:
package Lab11;
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
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;
QueueArray(int c)
{
front = rear = 0;
capacity = c;
queue = new int[capacity];
}
else {
queue[rear] = data;
rear++;
}
return;
}
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.");
rear--;
}
return;
}
}
SWE-203L: Data Structures and Algorithms SSUET/QR/114
Output: