[go: up one dir, main page]

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

Lecture 23

Uploaded by

mehulagarwal2004
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)
13 views4 pages

Lecture 23

Uploaded by

mehulagarwal2004
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/ 4

1.

Queue using LL

public class Queue {


//Define the data members
private Node front;
private Node rear;
private int size;

public Queue() {
//Implement the Constructor
front = null;
rear = null;
size = 0;
}

/*----------------- Public Functions of Stack -----------------*/

public int getSize() {


//Implement the getSize() function
return size;
}

public boolean isEmpty() {


//Implement the isEmpty() function
return size == 0;
}

public void enqueue(int data) {


//Implement the enqueue(element) function
Node newNode = new Node(data);
if (front == null) {
front = newNode;
rear = newNode;
} else {
rear.next = newNode;
rear = newNode;
}
size = size + 1;

public int dequeue() {


//Implement the dequeue() function
if (front != null) {
int temp = front.data;
front = front.next;
size = size - 1;
return temp;

} else {
return -1;
}
}

public int front() {


//Implement the front() function
if (front != null) {
return front.data;
} else {
return -1;
}
}
}

2.Stack Using 2 queues


import java.util.*;
public class Stack {
// Define the data members
private Queue<Integer> q1;
private Queue<Integer> q2;
private int size;

public Stack() {
// Implement the Constructor
q1 = new LinkedList<Integer>();
q2 = new LinkedList<Integer>();
size = 0;
}

/*----------------- Public Functions of Stack -----------------*/

public int getSize() {


// Implement the getSize() function
return size;
}

public boolean isEmpty() {


// Implement the isEmpty() function
return size == 0;
}

public void push(int element) {


// Implement the push(element) function
q1.add(element);
size = size + 1;
}

public int pop() {


// Implement the pop() function
if (q1.isEmpty()) {
return -1;
}
while (q1.size() != 1) {
q2.add(q1.remove());
}
int top = q1.remove();

while (!q2.isEmpty()) {
q1.add(q2.remove());
}
size = size - 1;
return top;
}

public int top() {


// Implement the top() function
if (q1.isEmpty()) {
return -1;
}
while (q1.size() != 1) {
q2.add(q1.remove());
}
int top = q1.peek();
q2.add(q1.remove());

Queue<Integer> q = q1;
q1 = q2;
q2 = q;
return top;
}
}

Assignments
1. Reverse Queue
import java.util.LinkedList;
import java.util.Queue;

public class Solution {

public static void reverseQueue(Queue<Integer> input) {


//Your code goes here
if(input.size()==0 || input.size()==1){
return;
}
int temporary = input.remove();
reverseQueue(input);
input.add(temporary);
}

2.Reverse the First K elements in the queue


import java.util.LinkedList;
import java.util.Queue;
public class Solution {

public static Queue<Integer> reverseKElements(Queue<Integer> input, int k) {


//Your code goes here
if (input.size()>k)
{
k=k%input.size();
}
if (k==0 || k==1)
{
return input;
}
reverseQueue(input,k);
for (int i=0;i<input.size()-k;i++)
{
input.add(input.remove());
}
return input;

}
public static void reverseQueue(Queue<Integer> input, int k)
{
//Your code goes here
if (input.size()==0 || input.size()==1 || k==0)
{
return;
}
int temp=input.remove();
reverseQueue(input,k-1);
input.add(temp);
}

You might also like