-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa21a96
commit c3b9d85
Showing
3 changed files
with
177 additions
and
0 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
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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} | ||
} | ||
} |