0% found this document useful (0 votes)
46 views33 pages

Yooo 2

The document discusses several algorithms and data structures including sorting a doubly linked list, segregating a linked list into even and odd elements, detecting loops in a linked list, implementing a minimum stack using two stacks, solving the celebrity problem, calculating stock span, and implementing a priority queue using a doubly linked list. Code implementations are provided for each algorithm or data structure discussed.

Uploaded by

chiraggarg032
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)
46 views33 pages

Yooo 2

The document discusses several algorithms and data structures including sorting a doubly linked list, segregating a linked list into even and odd elements, detecting loops in a linked list, implementing a minimum stack using two stacks, solving the celebrity problem, calculating stock span, and implementing a priority queue using a doubly linked list. Code implementations are provided for each algorithm or data structure discussed.

Uploaded by

chiraggarg032
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
You are on page 1/ 33

1.

Sort the bitonic DLL


import java.util.*;

class node {
public int data;
public node next;
public node prev;
}

class Main {
static node head = null, tail = null; // Renamed 'temp' to 'tail'

public static void push(int value) {


node newnode = new node();
newnode.data = value;
newnode.next = null;
newnode.prev = null;
if (head == null) {
head = newnode;
tail = newnode; // Initialize tail when head is null
} else {
tail.next = newnode;
newnode.prev = tail;
tail = newnode;
}
}

public static void forward() {


node current = head; // Renamed 'temp' to 'current'
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
}

public static void sort() {


if (head == null || head.next == null) return; // Check if list is empty or has only one
node

boolean swapped;
node i, j = null;

do {
swapped = false;
i = head;
while (i.next != j) {
if (i.data > i.next.data) {
swap(i, i.next);
swapped = true;
}
i = i.next;
}
j = i;
} while (swapped);
}

private static void swap(node a, node b) {


int temp = a.data;
a.data = b.data;
b.data = temp;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int n;
while (true) {
n = sc.nextInt();
if (n != -1) {
push(n);
} else {
break;
}
}
forward();
System.out.println();
sort();
forward();
}
}

2Segregate

import java.io.*;
import java.util.*;

public class Solution {


Node head;
class Node
{
int data;
Node next;
Node(int val)
{
data = val;
next = null;
}
}
public void insert(int val)
{
Node newnode = new Node(val);
Node temp = head;
if(head == null)
head = newnode;
else
{
while(temp.next!=null)
{
temp = temp.next;
}
temp.next = newnode;
}
}
public void segOddEven()
{
Node evenstart = null;
Node evenend = null;
Node oddstart = null;
Node oddend = null;
Node currentnode = head;
while(currentnode!=null)
{
int element = currentnode.data;
if(element%2==0)
{
if(evenstart==null)
{
evenstart = currentnode;
evenend = evenstart;
}
else
{
evenend.next = currentnode;
evenend = evenend.next;
}
}
else
{
if(oddstart == null)
{
oddstart = currentnode;
oddend = oddstart;
}
else
{
oddend.next = currentnode;
oddend = oddend.next;
}
}
currentnode = currentnode.next;
}
if (evenstart == null) {
head = oddstart;
} else {
head = evenstart;
evenend.next = oddstart;
}

// Set the next of the last node of the even list to null
if (oddend != null) {
oddend.next = null;
}
}
public void display()
{
Node temp = head;
while(temp!=null)
{
System.out.print(temp.data+" ");
temp = temp.next;
}
}
public static void main(String[] args) {
Solution list = new Solution();
Scanner s = new Scanner(System.in);
int x = s.nextInt();//1
// while(x!=-1)
// {
// list.insert(x);//1
// x = s.nextInt();//2
// }
for(int i=0;i<x;i++){
int m=s.nextInt();
list.insert(m);

}
list.segOddEven();
list.display();
}
}

3-LOOP DETECTION
import java.util.*;

class Node {
int num;
Node next;

Node(int val) {
num = val;
next = null;
}
}
class Main {
static Node insertNode(Node head, int val) {
Node newNode = new Node(val);
if(head == null) {
head = newNode;
return head;
}
Node temp = head;
while(temp.next != null)
temp = temp.next;
temp.next = newNode;
return head;
}

static void display(Node head) {


Node temp = head;
while(temp != null) {
System.out.print(temp.num + "->");
temp = temp.next;
}
System.out.println("NULL");
}

static void createCycle(Node head, int a, int b) {


int cnta = 0, cntb = 0;
Node p1 = head;
Node p2 = head;
while(cnta != a || cntb != b) {
if(cnta != a) {
p1 = p1.next;
++cnta;
}
if(cntb != b) {
p2 = p2.next;
++cntb;
}
}
p2.next = p1;
}

static boolean cycleDetect(Node head) {


if(head == null) return false;
Node fast = head;
Node slow = head;

while(fast.next != null && fast.next.next != null) {


fast = fast.next.next;
slow = slow.next;
if(fast == slow) return true;
}
return false;
}

public static void main(String args[]) {


Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Node head = null;
for(int i = 0; i < n; i++) {
int m = sc.nextInt();
head = insertNode(head, m);
}
display(head);
int a = sc.nextInt();
createCycle(head, 1, a); // creating cycle in the list
if(cycleDetect(head))
System.out.println("Cycle detected");
else
System.out.println("Cycle not detected");
}
}

5-Minimum Stack
import java.util.*;

class Mystack {
Stack<Integer> s;
Stack<Integer> a;

Mystack() {
s = new Stack<Integer>();
a = new Stack<Integer>();
}

void getMin() {
if (a.isEmpty())
System.out.println("Stack is Empty");
else
System.out.println("Minimum element: " + a.peek());
}

void peek() {
if (s.isEmpty()) {
System.out.println("Stack is Empty");
return;
}
Integer t = s.peek();
System.out.println("Top most element: " + t);
}

void pop() {
if (s.isEmpty()) {
System.out.println("Stack is Empty");
return;
}
Integer t = s.pop();
System.out.println("Removed element: " + t);
if (t.equals(a.peek()))
a.pop();
}

void push(int x) {
if (s.isEmpty()) {
s.push(x);
a.push(x);
System.out.println("Number Inserted: " + x);
return;
} else {
s.push(x);
System.out.println("Number Inserted: " + x);
}
if (x <= a.peek())
a.push(x);
}
}

public class Main {


public static void main(String args[]) {
Mystack s = new Mystack();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int m = sc.nextInt();
s.push(m);
}
s.getMin();
s.pop();
s.getMin();
s.pop();
s.peek();
}
}
6-The Celebrity problem
import java.util.*;
public class Main {
public static int celebritySolution(int[][] mat) {
Stack<Integer> stk = new Stack<>();
for(int i=0;i<mat.length;i++) {
stk.push(i);
}
while(stk.size() > 1) {
int col = stk.pop();
int row = stk.pop();
if(mat[row][col] == 1)
{
stk.push(col);
}
else {
stk.push(row);
}
}
int x = stk.pop();
for(int j=0;j<mat.length;j++) {
if(j == x) continue;
if(mat[x][j] == 1) {
return -1;
}
}

for(int i=0;i<mat.length;i++) {
if(i == x) continue;
if(mat[i][x] == 0) {
return -1;
}
}
return x;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int matrix[][]=new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
matrix[i][j]=sc.nextInt();
}
}
int res = celebritySolution(matrix);
if(res == -1) {
System.out.println("There is no celebrity in the party");
} else {
System.out.println(res + " is the celebrity in the party");
}
}
}
7-Stock Span problem
import java.util.*;
public class Main {
static void calculate(int arr[], int n, int S[])
{
Stack<Integer> st = new Stack<>();
st.push(0);
S[0] = 1;
for (int i = 1; i < n; i++)
{
while (!st.isEmpty() && arr[st.peek()] <= arr[i])
st.pop();
S[i] = (st.isEmpty()) ? (i + 1) : (i - st.peek());
st.push(i);
}
}
static void printArray(int arr[])
{
System.out.print(Arrays.toString(arr));
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int [n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
int S[] = new int[n];
calculate(arr, n, S);
printArray(S);
}
}

8-Priority Queue using DLL


import java.util.*;
class Main {
static class Node {
int data;
int priority;
Node next, prev;
public Node(int data, int priority) {
this.data = data;
this.priority = priority;
}
}
private static Node head = null;
private static void push(int data, int priority) {
if (head == null) {
Node newNode = new Node(data, priority);
head = newNode;
return; }
Node node = new Node(data, priority);
Node temp = head, parent = null;
while (temp != null && temp.priority >= priority) {
parent = temp;
temp = temp.next;
}
if (parent == null) {

node.next = head;
head.prev = node;
head = node;
}
else if (temp == null) {
parent.next = node;
node.prev = parent;
}
else {
parent.next = node;
node.prev = parent;
node.next = temp;
temp.prev = node;
} }
private static int peek() {
if (head != null) {
return head.data;
}
return -1;
}
private static int pop() {
if (head != null) {
int curr = head.data;
head = head.next;

if (head != null) {
head.prev = null;
} return curr;
}
return -1;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
int data=sc.nextInt();
int pri=sc.nextInt();
push(data, pri);
}
System.out.println(peek());
System.out.println(pop());
System.out.println(pop());
System.out.println(peek());
}
}

9-Stack permutations
import java.util.*;
class Main {
static Boolean check(int ip[], int op[], int n)
{
Stack<Integer> s = new Stack<Integer>();
int j = 0;
for (int i = 0; i < n; i++) {
s.push(ip[i]);
while (!s.isEmpty() && s.peek() == op[j]) {
s.pop();
j++;
}
}
if (s.isEmpty()) {
return true; }
return false;
}
public static void main(String args[])
{
Scanner sc=new Scanner (System.in);
int n=sc.nextInt();
int input[]=new int[n];
int output[]=new int[n];
for(int i=0;i<n;i++)
{
input[i]=sc.nextInt();
}
for(int i=0;i<n;i++)
{
output[i]=sc.nextInt();
}
if (check(input, output, n))
System.out.println("Yes");
else
System.out.println("Not Possible");
}
}

Max Sliding Window


import java.util.*;
public class Main {
static void maximum(int arr[], int N, int K)
{
int j, max;
for (int i = 0; i <= N - K; i++) {
max = arr[i];
for (j = 1; j < K; j++) {
if (arr[i + j] > max)
max = arr[i + j];
}
System.out.print(max + " ");
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
int K = sc.nextInt();
maximum(arr,n, K);
}
}

10-MERGE SORT IN DLL


import java.util.*;
// MERGE FUNCTION
class Solution {
public
static Node merge(Node head1,
Node head2) {
Node merged = new Node(-1);
Node temp = merged;
while (head1 != null && head2 != null) {
if (head1.data < head2.data) {
temp.next = head1;
if (temp.data != -1)
head1.prev = temp;
head1 = head1.next;
} else {
temp.next = head2;
if (temp.data != -1)
head2.prev = temp;
head2 = head2.next;
}
temp = temp.next;
}
while (head1 != null) {
temp.next = head1;
head1.prev = temp;
head1 = head1.next;
temp = temp.next;
}
while (head2 != null) {
temp.next = head2;
head2.prev = temp;
head2 = head2.next;
temp = temp.next;
}
return merged.next;
}
// 2.FIND THE MID POINT
public
static Node find_mid(Node head) {
Node slow = head, fast = head.next;
while (slow != null && fast !=
null) {
fast = fast.next;
if (fast == null)
break;
slow = slow.next;
fast = fast.next;
}
return slow;
}
public
static Node mergesort(Node head) {
if (head.next == null) {
return head;
}
Node mid = find_mid(head);
Node head1 = head;
Node head2 = mid.next;
mid.next = null;
head2.prev = null;
head1 = mergesort(head1);
head2 = mergesort(head2);
return merge(head1, head2);
}
}
// 3.NODE
class Node {
int data;
Node next;
Node prev;
Node(int data) {
this.data = data;
next = null;
prev = null;
}
}
// 4.LINKED LIST
class LinkedList {
Node head;
void add(int data) {
Node new_node = new Node(data);
if (head == null) {
head = new_node;
return;
}
Node curr = head;
while (curr.next != null)
curr = curr.next;
curr.next = new_node;
new_node.prev = curr;
}
}
// 5.MAIN FUNCTION
class Main {
public
static void main(String[] args) {

Scanner input = new Scanner(System.in);


int n = input.nextInt();
LinkedList a = new LinkedList();
for (int i = 0; i < n; i++) {
a.add(input.nextInt());
}
Solution Obj = new Solution();
a.head = Obj.mergesort(a.head);
Node h = a.head;
while (h != null) {
System.out.print(h.data + " ");
h = h.next;
}
System.out.println("");
}
}

11- SORT WITH SPACES


import java.util.Arrays;

public class Main {


public static void mergeSort(int[] arr,
int low, int high) {
if (low < high) {
int mid = (low + high) / 2;
mergeSort(arr, low, mid);
mergeSort(arr, mid + 1, high);
merge(arr, low, mid, high);
}
}

public static void merge(int[] arr, int low, int mid, int high) {
int n1 = mid - low + 1;
int n2 = high - mid;
int[] left = new int[n1];
int[] right = new int[n2];
for (int i = 0; i < n1; i++) {
left[i] = arr[low + i];
}
for (int j = 0; j < n2; j++) {
right[j] = arr[mid + 1 + j];
}
int i = 0, j = 0, k = low;
while (i < n1 && j < n2) {
if (left[i] <= right[j]) {
arr[k] = left[i];
i++;
} else {
arr[k] = right[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = left[i];
i++;
k++;
}
while (j < n2) {
arr[k] = right[j];
j++;
k++;
}
}
public static void main(String[] args) {
int[] arr = { 7, 2, 1, 6, 8, 5 };
mergeSort(arr, 0, arr.length - 1);
System.out.println("Sorted Array: " + Arrays.toString(arr));
}
}

12-TOWER OF HANOI
import java.util.Stack;

public class Main {


static void towerOfHanoi(int numDisks, char source, char auxiliary, char destination) {

Stack < Integer > sourceStack = new


Stack < > ();
Stack < Integer > auxiliaryStack = new
Stack < > ();
Stack < Integer > destinationStack = new
Stack < > ();
// Initialize the source rod with disks
for (int i = numDisks; i >= 1; i--) {
sourceStack.push(i);
}
// Total number of moves required
int totalMoves = (int) Math.pow(2,
numDisks) - 1;
// Determine the order of pegs for odd/even number of disks
char temp;
if (numDisks % 2 == 0) {
temp = auxiliary;
auxiliary = destination;
destination = temp;
}
// Perform iterative Tower of Hanoi
for (int move = 1; move <=
totalMoves; move++) {
if (move % 3 == 1) {
moveDisk(sourceStack,
destinationStack, source, destination);
} else if (move % 3 == 2) {
moveDisk(sourceStack,
auxiliaryStack, source, auxiliary);
} else if (move % 3 == 0) {
moveDisk(auxiliaryStack,
destinationStack, auxiliary, destination);
}
}
}

static void moveDisk(Stack < Integer >


sourceStack, Stack < Integer > destinationStack,
char source, char destination) {
if (!sourceStack.isEmpty() &&
(destinationStack.isEmpty() || sourceStack.peek() < destinationStack.peek())) {
destinationStack.push(sourceStack.pop());
System.out.println("Move disk " +
destinationStack.peek() + " from " + source + "to " + destination);
}
else {
sourceStack.push(destinationStack.pop());
System.out.println("Move disk " +
sourceStack.peek() + " from " +
destination + " to " + source);
}
}

public static void main(String[] args) {


int numDisks = 3; // Change the
char source = 'A';
char auxiliary = 'B';
char destination = 'C';
towerOfHanoi(numDisks, source,
auxiliary, destination);
}
}
DLL again
import java.util.*;

class Solution {

static Node front, rear;

// Linked List Node


static class Node {
int info;
int priority;
Node prev, next;
}

// Function to insert a new Node


static void push(Node fr, Node rr, int n, int p)
{
Node news = new Node();
news.info = n;
news.priority = p;

// If linked list is empty


if (fr == null) {
fr = news;
rr = news;
news.next = null;
}
else {
// If p is less than or equal front
// node's priority, then insert at
// the front.
if (p <= (fr).priority) {
news.next = fr;
(fr).prev = news.next;
fr = news;
}

// If p is more rear node's priority,


// then insert after the rear.
else if (p > (rr).priority) {
news.next = null;
(rr).next = news;
news.prev = (rr).next;
rr = news;
}

// Handle other cases


else {

// Find position where we need to


// insert.
Node start = (fr).next;
while (start.priority > p)
start = start.next;
(start.prev).next = news;
news.next = start.prev;
news.prev = (start.prev).next;
start.prev = news.next;
}
}
front = fr;
rear = rr;
}

// Return the value at rear


static int peek(Node fr) { return fr.info; }
static boolean isEmpty(Node fr) { return (fr == null); }

// Removes the element with the


// least priority value from the list
static int pop(Node fr, Node rr)
{
Node temp = fr;
int res = temp.info;
(fr) = (fr).next;
if (fr == null)
rr = null;

front = fr;
rear = rr;
return res;
}

// Driver code
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();

for(int i=0;i<n;i++){
int m=sc.nextInt();
int a=sc.nextInt();
push(front,rear,m,a);
}
System.out.println(pop(front, rear));
System.out.println(peek(front));
}
}

You might also like