forked from sujan-poudel-03/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/sujan-poudel-03/DSA
- Loading branch information
Showing
9 changed files
with
404 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package Java.queue; | ||
|
||
public class CircularQueue { | ||
public static int MAX_SIZE = 5; | ||
public static int[] items = new int[MAX_SIZE]; | ||
public static int front = -1; | ||
public static int rear = -1; | ||
|
||
public static void main(String[] args) { | ||
// Fails because front = -1 | ||
dequeue(); | ||
|
||
enqueue(1); | ||
enqueue(2); | ||
enqueue(3); | ||
enqueue(4); | ||
enqueue(5); | ||
|
||
// Fails to enqueue because front == 0 && rear == SIZE - 1 | ||
enqueue(6); | ||
|
||
display(); | ||
dequeue(); | ||
|
||
display(); | ||
|
||
enqueue(7); | ||
display(); | ||
|
||
// Fails to enqueue because front == rear + 1 | ||
enqueue(8); | ||
} | ||
|
||
public static void enqueue(int value) { | ||
if (isFull()) { | ||
System.out.println("Queue is full"); | ||
} else { | ||
if (front == -1) front = 0; | ||
rear = (rear + 1) % MAX_SIZE; | ||
items[rear] = value; | ||
System.out.println("Inserted -> " + value); | ||
} | ||
} | ||
|
||
public static void dequeue() { | ||
if (isEmpty()) { | ||
System.out.println("Queue is empty"); | ||
} else { | ||
int element = items[front]; | ||
|
||
if (front == rear) { | ||
front = rear = -1; | ||
} else { | ||
front = (front + 1) % MAX_SIZE; | ||
} | ||
|
||
System.out.println("Deleted -> " + element); | ||
} | ||
} | ||
|
||
public static boolean isEmpty() { | ||
return front < 0; | ||
} | ||
|
||
public static boolean isFull() { | ||
return (front == rear + 1) || (front == 0 && rear == MAX_SIZE - 1); | ||
} | ||
|
||
public static void display() { | ||
if (isEmpty()) { | ||
System.out.println("Queue is empty!"); | ||
} else { | ||
for (int i = front; i <= rear; i++) { | ||
System.out.printf("%d ", items[i]); | ||
} | ||
} | ||
System.out.println("\n"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package Java.queue; | ||
|
||
public class LinearQueue { | ||
public static int MAX_SIZE = 5; | ||
public static int[] items = new int[MAX_SIZE]; | ||
public static int front = -1; | ||
public static int rear = -1; | ||
|
||
public static void main(String[] args) { | ||
//dequeue is not possible on empty queue | ||
dequeue(); | ||
|
||
//enqueue 5 elements | ||
enqueue(1); | ||
enqueue(2); | ||
enqueue(3); | ||
enqueue(4); | ||
enqueue(5); | ||
|
||
// 6th element can't be added to because the queue is full | ||
enqueue(6); | ||
|
||
display(); | ||
|
||
//dequeue removes element entered first i.e. 1 | ||
dequeue(); | ||
|
||
//Now we have just 4 elements | ||
display(); | ||
} | ||
|
||
public static void enqueue(int value) { | ||
if (isFull()) { | ||
System.out.println("Queue is full"); | ||
} else { | ||
if (front == -1) front = 0; | ||
rear++; | ||
items[rear] = value; | ||
System.out.println("Inserted -> " + value); | ||
} | ||
} | ||
|
||
public static void dequeue() { | ||
if (isEmpty()) { | ||
System.out.println("Queue is empty"); | ||
} else { | ||
System.out.println("Deleted -> " + items[front]); | ||
front++; | ||
if (front > rear) front = rear = -1; | ||
} | ||
} | ||
|
||
public static boolean isEmpty() { | ||
return front < 0; | ||
} | ||
|
||
public static boolean isFull() { | ||
return rear == MAX_SIZE - 1; | ||
} | ||
|
||
public static void display() { | ||
if (isEmpty()) { | ||
System.out.println("Queue is empty!"); | ||
} else { | ||
for (int i = front; i <= rear; i++) { | ||
System.out.printf("%d ", items[i]); | ||
} | ||
} | ||
System.out.println("\n"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package Java.stack; | ||
|
||
import java.util.Scanner; | ||
|
||
public class InfixToPostfix { | ||
public static int MAX_SIZE = 100; | ||
public static int top = -1; | ||
public static char[] stack = new char[MAX_SIZE]; | ||
|
||
public static boolean isFull() { | ||
return top == MAX_SIZE - 1; | ||
} | ||
|
||
public static boolean isEmpty() { | ||
return top < 0; | ||
} | ||
|
||
public static void push(char c) { | ||
if (!isFull()) { | ||
stack[++top] = c; | ||
} else { | ||
System.out.println("Could not insert data, stack is full"); | ||
} | ||
} | ||
|
||
public static char pop() { | ||
if (!isEmpty()) { | ||
return stack[top--]; | ||
} else { | ||
System.out.println("Could not insert data, stack is full"); | ||
return '\0'; | ||
} | ||
} | ||
|
||
public static int precedence(char c) { | ||
switch(c) { | ||
case '+': | ||
case '-': | ||
return 1; | ||
case '*': | ||
case '/': | ||
return 2; | ||
case '^': | ||
return 3; | ||
default: | ||
return 0; | ||
} | ||
} | ||
|
||
public static void infixToPostfix(char[] infix, char[] postfix) { | ||
int i, j, infixLength = infix.length; | ||
|
||
i = j = 0; | ||
|
||
while(i < infixLength) { | ||
char current = infix[i]; | ||
|
||
if (Character.isLetterOrDigit(current)) { | ||
postfix[j++] = current; | ||
} else if (current == '(') { | ||
push(current); | ||
} else if (current == ')') { | ||
while(top != -1 && stack[top] != '(') { | ||
postfix[j++] = pop(); | ||
} | ||
|
||
if (top != -1 && stack[top] == '(') { | ||
pop(); | ||
} | ||
} else { | ||
while(precedence(current) <= precedence(stack[top+1])) { | ||
if (top == -1) break; | ||
postfix[j++] = pop(); | ||
} | ||
|
||
push(current); | ||
} | ||
i++; | ||
} | ||
|
||
while(top != -1) { | ||
postfix[j++] = pop(); | ||
} | ||
|
||
postfix[j-1] = '\0'; | ||
} | ||
|
||
public static void main(String[] args) { | ||
char[] infix = new char[MAX_SIZE]; | ||
char[] postfix = new char[MAX_SIZE]; | ||
|
||
Scanner scanner = new Scanner(System.in); | ||
|
||
System.out.println("Enter an infix expression:"); | ||
String expr = scanner.nextLine(); | ||
|
||
for (int i = 0; i < expr.length(); i++) { | ||
infix[i] = expr.charAt(i); | ||
} | ||
|
||
infixToPostfix(infix, postfix); | ||
|
||
System.out.println("\nInfix expression:"); | ||
traversal(infix); | ||
System.out.println("\nPostfix expression:"); | ||
traversal(postfix); | ||
|
||
scanner.close(); | ||
} | ||
|
||
public static void traversal(char[] arr) { | ||
int size = arr.length; | ||
for (int i = 0; i < size; i++) { | ||
System.out.printf("%c", arr[i]); | ||
} | ||
|
||
System.out.println("\n"); | ||
} | ||
} |
Oops, something went wrong.