Implement Stack data structure using Array.
Implement Stack data structure using Array.
import java.util.*;
class Stack1
{
int s[],max,top;
public static void main()
{
int ch,size;
Scanner sc =new Scanner(System.in);
System.out.println("Enter the size of the stack");
size=sc.nextInt();
Stack1 obj=new Stack1();
obj.assign(size);
while(true)
{
System.out.println("1:PUSH\n2:POP\n3:DISPLAY\n4:EXIT");
System.out.println("Enter your choice");
ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println("Enter an element to push in the stack");
int v=sc.nextInt();
obj.push(v);
break;
case 2: System.out.println("Element deleted from stack is: "+obj.pop());
break;
case 3: System.out.println("STACK ELEMENTS ARE:");
obj.display();
break;
case 4: System.exit(1);
break;
default:System.out.println("Invalid input");
break;
}
}
}
void assign(int x)
{
max=x;
s=new int[max];
top=-1;
}
void push(int v)
{
if(top==max-1)
{
System.out.println("stack overflow");
}
else
{
top=top+1;
s[top]=v;
}
}
int pop()
{
if(top==-1)
{
System.out.println("stack underflow");
return -999;
}
else
{
int d;
d=s[top];
top=top-1;
return d;
}
}
void display()
{
for(int i=top;i>=0;i--)
System.out.println(s[i]);
// System.out.println("size of stack:"+s.length);
}
}
OUTPUT---
Enter the size of the stack
4
1:PUSH
2:POP
3:DISPLAY
4:EXIT
Enter your choice
1
Enter an element to push in the stack
1
1:PUSH
2:POP
3:DISPLAY
4:EXIT
Enter your choice
2
Element deleted from stack is: 1
1:PUSH
2:POP
3:DISPLAY
4:EXIT
Enter your choice
1
Enter an element to push in the stack
2
1:PUSH
2:POP
3:DISPLAY
4:EXIT
Enter your choice
2
Element deleted from stack is: 2
1:PUSH
2:POP
3:DISPLAY
4:EXIT
Enter your choice
1
Enter an element to push in the stack
3
1:PUSH
2:POP
3:DISPLAY
4:EXIT
Enter your choice
4
import java.util.*;
class Stack1
{
int s[],max,top;
public static void main()
{
int ch,size;
Scanner sc =new Scanner(System.in);
System.out.println("Enter the size of the stack");
size=sc.nextInt();
Stack1 obj=new Stack1();
obj.assign(size);
while(true)
{
System.out.println("1:PUSH\n2:POP\n3:DISPLAY\n4:EXIT");
System.out.println("Enter your choice");
ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println("Enter an element to push in the stack");
int v=sc.nextInt();
obj.push(v);
break;
case 2: System.out.println("Element deleted from stack is: "+obj.pop());
break;
case 3: System.out.println("STACK ELEMENTS ARE:");
obj.display();
break;
case 4: System.exit(1);
break;
default:System.out.println("Invalid input");
break;
}
}
}
void assign(int x)
{
max=x;
s=new int[max];
top=-1;
}
void push(int v)
{
if(top==max-1)
{
System.out.println("stack overflow");
}
else
{
top=top+1;
s[top]=v;
}
}
int pop()
{
if(top==-1)
{
System.out.println("stack underflow");
return -999;
}
else
{
int d;
d=s[top];
top=top-1;
return d;
}
}
void display()
{
for(int i=top;i>=0;i--)
System.out.println(s[i]);
// System.out.println("size of stack:"+s.length);
}
}
OUTPUT---
Enter the size of the stack
4
1:PUSH
2:POP
3:DISPLAY
4:EXIT
Enter your choice
1
Enter an element to push in the stack
1
1:PUSH
2:POP
3:DISPLAY
4:EXIT
Enter your choice
2
Element deleted from stack is: 1
1:PUSH
2:POP
3:DISPLAY
4:EXIT
Enter your choice
1
Enter an element to push in the stack
2
1:PUSH
2:POP
3:DISPLAY
4:EXIT
Enter your choice
2
Element deleted from stack is: 2
1:PUSH
2:POP
3:DISPLAY
4:EXIT
Enter your choice
1
Enter an element to push in the stack
3
1:PUSH
2:POP
3:DISPLAY
4:EXIT
Enter your choice
4
Comments
Post a Comment