Skip to content

Commit

Permalink
assignment 6 added
Browse files Browse the repository at this point in the history
  • Loading branch information
MartyMiniac committed May 14, 2020
1 parent aa21a96 commit c3b9d85
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Semester 2 (DSA)/assignment 6/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Node
{
int info;
Node next;
Node(int info)
{
this.info=info;
}
}
91 changes: 91 additions & 0 deletions Semester 2 (DSA)/assignment 6/part1.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
77 changes: 77 additions & 0 deletions Semester 2 (DSA)/assignment 6/part2.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}

0 comments on commit c3b9d85

Please sign in to comment.