Write a complete program that will perform the following:
- Push the following sequence of numbers (in order) onto the STACK
1 4 9 16 25 36 49 64 81 100
- After inserting the above items, your program should modify all the elements in the Stack. The modification of the element is based on two rules: (1) if an element is an even number, get its square; (2) if an element is an odd number, multiply it by 3 and add 1.
- After modifying all elements in the Stack, your program should remove the top 3 items.
- Lastly, print Stack elements.
To solve this problem, you should implement the following functions:
/*This function will push each given item onto the Stack*/
void push(int item)
/*This function will remove an item from the Stack. The function will return the value of the popped (removed) item.*/
int pop()
/*This function will modify each element in the Stack. The modification of the element is based on two rules: (1) if an element is an even number, get its square; (2) if an element is an odd number, multiply it by 3 and add 1.*/
void modify()
/*This function will display the elements in the Stack.*/
void printStack()
The output of your program should be:
Original Stack elements:
100-81-64-49-36-25-16-9-4-1-NULL
Modified Stack elements:
10000-244-4096-148-1296-76-256-28-16-4-NULL
Pop(): 10000
Pop(): 244
Pop(): 4096
Remaining Stack elements:
148-1296-76-256-28-16-4-NULL
************************************************** *******
This is my code:
class Stack
{
private int tos;
private int stak[];
Stack(int size)
{
tos=-1;
stak=new int[size];
}
void push(int item)
{
if(tos==stak.length-1)
System.out.println("Stack is Full");
else
{
stak[++tos]=item;
System.out.println(stak[tos]);
}
}
int pop()
{
if(tos<0)
{
System.out.println("Stack Underflow");
return 0;
}
else
return stak[tos--];
}
}
class stack1
{
public static void main(String[] args)
{
Stack s1 = new Stack(10);
s1.push(1);
s1.push(4);
s1.push(9);
s1.push(16);
s1.push(25);
s1.push(36);
s1.push(49);
s1.push(64);
s1.push(81);
s1.push(100);
System.out.println("\nOriginal Stack Elements: ");
for(int i=0; i<10; i++)
System.out.print(s1.pop()+ " ");
}
}
and ang iya output is this ..
1
4
9
16
25
36
49
64
81
100
Original Stack Elements:
100 81 64 49 36 25 16 9 4 1
***
i need this output. unsaon?
Modified Stack elements:
10000-244-4096-148-1296-76-256-28-16-4-NULL
Pop(): 10000
Pop(): 244
Pop(): 4096
Remaining Stack elements:
148-1296-76-256-28-16-4-NULL