From c3b9d8535c574fd1b35bc7b8e1cffd90881a3182 Mon Sep 17 00:00:00 2001 From: MartyMiniac Date: Thu, 14 May 2020 16:46:39 +0530 Subject: [PATCH] assignment 6 added --- Semester 2 (DSA)/assignment 6/Node.java | 9 +++ Semester 2 (DSA)/assignment 6/part1.java | 91 ++++++++++++++++++++++++ Semester 2 (DSA)/assignment 6/part2.java | 77 ++++++++++++++++++++ 3 files changed, 177 insertions(+) create mode 100644 Semester 2 (DSA)/assignment 6/Node.java create mode 100644 Semester 2 (DSA)/assignment 6/part1.java create mode 100644 Semester 2 (DSA)/assignment 6/part2.java diff --git a/Semester 2 (DSA)/assignment 6/Node.java b/Semester 2 (DSA)/assignment 6/Node.java new file mode 100644 index 0000000..b2d1980 --- /dev/null +++ b/Semester 2 (DSA)/assignment 6/Node.java @@ -0,0 +1,9 @@ +public class Node +{ + int info; + Node next; + Node(int info) + { + this.info=info; + } +} diff --git a/Semester 2 (DSA)/assignment 6/part1.java b/Semester 2 (DSA)/assignment 6/part1.java new file mode 100644 index 0000000..cb4a1ff --- /dev/null +++ b/Semester 2 (DSA)/assignment 6/part1.java @@ -0,0 +1,91 @@ +import java.util.*; +public class part1 +{ + public static final int MAX=10; + public static void main(String args[]) + { + Scanner in = new Scanner(System.in); + int stack[] = new int[MAX]; + int top=-1; + while(true) + { + System.out.println("***MENU***"); + System.out.println("0: Exit"); + System.out.println("1: Push"); + System.out.println("2: Pop"); + System.out.println("3: Display"); + System.out.println("Enter your choice"); + int choice=in.nextInt(); + switch(choice) + { + case 0: + System.out.println("Exiting Program"); + System.exit(0); + break; + case 1: + top=push(stack,top); + break; + case 2: + top=pop(stack, top); + break; + case 3: + display(stack, top); + break; + default: + System.out.println("Invalid choice"); + } + } + } + public static int push(int S[], int top) + { + if(isFull(top)) + { + System.out.print("Error : Stack OverFlow"); + return top; + } + else + { + top++; + Scanner in = new Scanner(System.in); + System.out.print("Enter the Element to Push : "); + S[top]=in.nextInt(); + return top; + } + } + public static int pop(int S[], int top) + { + if(isEmpty(top)) + { + System.out.println("Error : Stack UnderFlow"); + return top; + } + else + { + top--; + return top; + } + } + public static void display(int S[], int top) + { + if(isEmpty(top)) + { + System.out.println("Stack Empty"); + } + else + { + System.out.println("Elements in the stack"); + for(int i=0; i<=top; i++) + { + System.out.println(S[i]); + } + } + } + public static boolean isFull(int top) + { + return MAX-1==top; + } + public static boolean isEmpty(int top) + { + return top==-1; + } +} diff --git a/Semester 2 (DSA)/assignment 6/part2.java b/Semester 2 (DSA)/assignment 6/part2.java new file mode 100644 index 0000000..c91da8a --- /dev/null +++ b/Semester 2 (DSA)/assignment 6/part2.java @@ -0,0 +1,77 @@ +import java.util.*; +public class part2 +{ + public static void main(String args[]) + { + Scanner in = new Scanner(System.in); + Node top=null; + while(true) + { + System.out.println("****MENU****"); + System.out.println("0:Exit"); + System.out.println("1:Push"); + System.out.println("2:Pop"); + System.out.println("3:Display"); + System.out.println("Enter your choice"); + int choice=in.nextInt(); + switch(choice) + { + case 0: + System.out.println("Exiting Program"); + System.exit(0); + break; + case 1: + top=push(top); + break; + case 2: + top=pop(top); + break; + case 3: + display(top); + break; + default: + System.out.println("Wrong choice"); + } + } + } + public static Node push(Node top) + { + Scanner in = new Scanner(System.in); + System.out.print("Enter the Value to Push :"); + Node n = new Node(in.nextInt()); + n.next=top; + top=n; + return top; + } + public static Node pop(Node top) + { + if(top==null) + { + System.out.println("Error : Stack UnderFlow"); + return null; + } + else + { + Node n=top; + n=n.next; + return n; + } + } + public static void display(Node top) + { + if(top==null) + { + System.out.println("Stack Empty"); + } + else + { + System.out.println("Elements of Stack"); + Node n=top; + while(n!=null) + { + System.out.println(n.info); + n=n.next; + } + } + } +} \ No newline at end of file