Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sujan-poudel-03 committed Jun 4, 2023
2 parents 76827c5 + cd97258 commit 725bd66
Show file tree
Hide file tree
Showing 9 changed files with 404 additions and 9 deletions.
9 changes: 0 additions & 9 deletions C/array/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,6 @@ void update(int arr[], int size, int index, int element) {
void main() {
int arr[10] = {1, 3, 5, 7, 9};

<<<<<<< HEAD
// traversal(arr, size);

// insert(arr, size, 2, 10);
delete(arr, size, 2);
// search(arr, size, 7);
// update(arr, size, 2, 10);
=======
int size = 5;
printf("Size of array is : %d \n", size);

Expand All @@ -86,5 +78,4 @@ void main() {
delete (arr, size, 2);
search(arr, size, 7);
update(arr, size, 2, 10);
>>>>>>> ff6a953028974cb83ab15fd202203cd7f2b548d9
}
Binary file removed C/array/array.exe
Binary file not shown.
Binary file removed C/stack/a.exe
Binary file not shown.
Binary file removed C/stack/infix_to_postfix.exe
Binary file not shown.
Binary file removed C/stack/infix_to_prefix.exe
Binary file not shown.
79 changes: 79 additions & 0 deletions Java/queue/CircularQueue.java
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");
}
}
71 changes: 71 additions & 0 deletions Java/queue/LinearQueue.java
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");
}
}
119 changes: 119 additions & 0 deletions Java/stack/InfixToPostfix.java
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");
}
}
Loading

0 comments on commit 725bd66

Please sign in to comment.