diff --git a/Competitive Coding/Data Structures/Queue/queue.class b/Competitive Coding/Data Structures/Queue/queue.class new file mode 100644 index 000000000..d4831508b Binary files /dev/null and b/Competitive Coding/Data Structures/Queue/queue.class differ diff --git a/Competitive Coding/Data Structures/Queue/queue.java b/Competitive Coding/Data Structures/Queue/queue.java new file mode 100644 index 000000000..6b20f68ec --- /dev/null +++ b/Competitive Coding/Data Structures/Queue/queue.java @@ -0,0 +1,132 @@ +package Data_Structures; +import java.util.*; +class Queue +{ + private int q[], front, rear, size; + + + /***** Constructor to initialize queue *****/ + queue(int n) + { //Begin constructor + size = n; + front = -1; + rear = -1; + q = new int[size]; + + for(int i = 0; i "); + + for(int i = front; i <= rear; i++) + System.out.print(q[i]+"\t"); + + System.out.println("<--- rear"); + + } + } //End display() + + + + + public static void main(String args[]) + { //Begin main() + + Scanner sc = new Scanner(System.in); + System.out.println("Enter the size of the queue"); + int n = sc.nextInt(); + + queue q1 = new queue(n); + + int choice; + do + { //Begin do + System.out.println("\nEnter:\n1. Insert\n2. Delete\n3. Display"); + choice = sc.nextInt(); + switch(choice) + { //Begin switch + case 1: System.out.println("\nEnter an element to insert"); + int element = sc.nextInt(); + q1.insert(element); + break; + + case 2: q1.delete(); + break; + + case 3: q1.display(); + break; + + default: System.out.println("Wrong choice. Exitting."); + break; + } //End switch + } //End do + while(choice >= 1 && choice <= 3); + } //End main() +} //End class diff --git a/Competitive Coding/Data Structures/Stack/stack.class b/Competitive Coding/Data Structures/Stack/stack.class new file mode 100644 index 000000000..5a35d9f53 Binary files /dev/null and b/Competitive Coding/Data Structures/Stack/stack.class differ diff --git a/Competitive Coding/Data Structures/Stack/stack.java b/Competitive Coding/Data Structures/Stack/stack.java new file mode 100644 index 000000000..cbb27d6f0 --- /dev/null +++ b/Competitive Coding/Data Structures/Stack/stack.java @@ -0,0 +1,130 @@ +package Data_Structures +import java.util.*; +class stack +{ //Begin class + + int top = -1, size, s[]; + + + /*****Constructor to initialise the stack *****/ + stack(int n) + { //Begin constructor + size = n; + + s = new int[size]; + for(int i = 0; i < size; i++) + { //Begin for + s[i] = 0; + } //End for + } //End constructor + + + + /***** Fucntion to insert/push item to stack *****/ + private void push(int item) + { //Begin push() + + if(top == (size-1)) //Checks if stack is full + { //Begin if + System.out.println("Stack overflow"); + } //End if + + else //Element is entered to the stack when there is place + { //Begin else + top++; + s[top] = item; + } //End else + + } //End push() + + + + /***** Function to delete/pop an item (topmost element) from stack *****/ + private void pop() + { //Begin pop() + + if(top == -1) //Checks if stack is empty + { //Begin if + System.out.println("Stack underflow"); + } //End if + + else + { //Begin else + System.out.println("Item deleted is:\t"+s[top]); + top--; + } //End else + } //End pop() + + + /***** Function to peep (show the topmost element in stack) *****/ + private void peep() + { //Begin peep() + if(top == -1) + System.out.println("No element to peep"); + + else + System.out.println("Peep: top ---> "+s[top]); + } //End peep() + + + + /***** Function to display the elements of stack *****/ + private void display() + { //Begin display() + + if(top == -1) + { //Begin if + System.out.println("No elements in stack"); + } //End if + + else + { //Begin else + + System.out.print("Elements of the stack: top---> "); + for(int i=top; i>=0; i--) + { + System.out.print(s[i]+"\t"); + } + System.out.println(); + } //End else + } //End display() + + + /***** main fucntion *****/ + public static void main(String args[]) + { //Begin main() + + Scanner sc = new Scanner(System.in); + System.out.println("Enter the size of the stack"); + int n = sc.nextInt(); + + stack st = new stack(n); + + int choice; + do + { //Begin do + System.out.println("\nEnter:\n1. Push\n2. Pop\n3. Peep\n4. Display"); + choice = sc.nextInt(); + switch(choice) + { //Begin switch + case 1: System.out.println("\nEnter an element to push"); + int element = sc.nextInt(); + st.push(element); + break; + + case 2: st.pop(); + break; + + case 3: st.peep(); + break; + + case 4: st.display(); + break; + + default: System.out.println("Wrong choice. Exitting!"); + break; + } //End switch + } //End do + while(choice >= 1 && choice <= 4); + } //End main() +} //End class