printing the elements by removing middle element of stack
up vote
0
down vote
favorite
Input: First line of input contains a single integer T which denotes the number of test cases. T test cases follows, first line of each test case contains a integer n. Second line consists of n spaced integers.
Output: Print the elements of the stack after deleting the middle element in reverse order.
Input:1
7
1 2 3 4 5 6 7
output is:
7 6 5 3 2 1
actually i am able to do printing in reverse fashion but i don't know how to remove middle element from the stack.please help
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String args)
{
Scanner s=new Scanner(System.in);
int test=s.nextInt();
for(int t=0;t<test;t++)
{
int n=s.nextInt();
int a=new int[n];
for(int i=0;i<n;i++)
a[i]=s.nextInt();
Stack<Integer> stack=new Stack<Integer>();
for(int i=0;i<n;i++)
{
stack.push(a[i]);
}
ListIterator<Integer> lstIterator=stack.listIterator(stack.size());
while(lstIterator.hasPrevious())
{
Integer res=lstIterator.previous();
//what condition should i give so that it would print all the
elements except middle one.
System.out.print(res+" ");
}
System.out.println();
}
}
}
java stack
add a comment |
up vote
0
down vote
favorite
Input: First line of input contains a single integer T which denotes the number of test cases. T test cases follows, first line of each test case contains a integer n. Second line consists of n spaced integers.
Output: Print the elements of the stack after deleting the middle element in reverse order.
Input:1
7
1 2 3 4 5 6 7
output is:
7 6 5 3 2 1
actually i am able to do printing in reverse fashion but i don't know how to remove middle element from the stack.please help
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String args)
{
Scanner s=new Scanner(System.in);
int test=s.nextInt();
for(int t=0;t<test;t++)
{
int n=s.nextInt();
int a=new int[n];
for(int i=0;i<n;i++)
a[i]=s.nextInt();
Stack<Integer> stack=new Stack<Integer>();
for(int i=0;i<n;i++)
{
stack.push(a[i]);
}
ListIterator<Integer> lstIterator=stack.listIterator(stack.size());
while(lstIterator.hasPrevious())
{
Integer res=lstIterator.previous();
//what condition should i give so that it would print all the
elements except middle one.
System.out.print(res+" ");
}
System.out.println();
}
}
}
java stack
1
How do you deal with middle element in case there are even number of elements?
– Pushpesh Kumar Rajwanshi
Nov 9 at 9:37
for that situation i will check for if n is even or odd and if it is even then i will delete the (n/2)-1 position as per the test case.for eg-n=6 and element is 1 2 3 4 5 6 then my output will be 6 5 4 2 1
– RAVI KUMAR SHARMA CSE16
Nov 9 at 9:52
kindly tell me the logic to delete the middle element for the odd no.of elements
– RAVI KUMAR SHARMA CSE16
Nov 9 at 9:52
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Input: First line of input contains a single integer T which denotes the number of test cases. T test cases follows, first line of each test case contains a integer n. Second line consists of n spaced integers.
Output: Print the elements of the stack after deleting the middle element in reverse order.
Input:1
7
1 2 3 4 5 6 7
output is:
7 6 5 3 2 1
actually i am able to do printing in reverse fashion but i don't know how to remove middle element from the stack.please help
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String args)
{
Scanner s=new Scanner(System.in);
int test=s.nextInt();
for(int t=0;t<test;t++)
{
int n=s.nextInt();
int a=new int[n];
for(int i=0;i<n;i++)
a[i]=s.nextInt();
Stack<Integer> stack=new Stack<Integer>();
for(int i=0;i<n;i++)
{
stack.push(a[i]);
}
ListIterator<Integer> lstIterator=stack.listIterator(stack.size());
while(lstIterator.hasPrevious())
{
Integer res=lstIterator.previous();
//what condition should i give so that it would print all the
elements except middle one.
System.out.print(res+" ");
}
System.out.println();
}
}
}
java stack
Input: First line of input contains a single integer T which denotes the number of test cases. T test cases follows, first line of each test case contains a integer n. Second line consists of n spaced integers.
Output: Print the elements of the stack after deleting the middle element in reverse order.
Input:1
7
1 2 3 4 5 6 7
output is:
7 6 5 3 2 1
actually i am able to do printing in reverse fashion but i don't know how to remove middle element from the stack.please help
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String args)
{
Scanner s=new Scanner(System.in);
int test=s.nextInt();
for(int t=0;t<test;t++)
{
int n=s.nextInt();
int a=new int[n];
for(int i=0;i<n;i++)
a[i]=s.nextInt();
Stack<Integer> stack=new Stack<Integer>();
for(int i=0;i<n;i++)
{
stack.push(a[i]);
}
ListIterator<Integer> lstIterator=stack.listIterator(stack.size());
while(lstIterator.hasPrevious())
{
Integer res=lstIterator.previous();
//what condition should i give so that it would print all the
elements except middle one.
System.out.print(res+" ");
}
System.out.println();
}
}
}
java stack
java stack
edited Nov 9 at 9:54
Bas de Groot
532115
532115
asked Nov 9 at 9:28
RAVI KUMAR SHARMA CSE16
215
215
1
How do you deal with middle element in case there are even number of elements?
– Pushpesh Kumar Rajwanshi
Nov 9 at 9:37
for that situation i will check for if n is even or odd and if it is even then i will delete the (n/2)-1 position as per the test case.for eg-n=6 and element is 1 2 3 4 5 6 then my output will be 6 5 4 2 1
– RAVI KUMAR SHARMA CSE16
Nov 9 at 9:52
kindly tell me the logic to delete the middle element for the odd no.of elements
– RAVI KUMAR SHARMA CSE16
Nov 9 at 9:52
add a comment |
1
How do you deal with middle element in case there are even number of elements?
– Pushpesh Kumar Rajwanshi
Nov 9 at 9:37
for that situation i will check for if n is even or odd and if it is even then i will delete the (n/2)-1 position as per the test case.for eg-n=6 and element is 1 2 3 4 5 6 then my output will be 6 5 4 2 1
– RAVI KUMAR SHARMA CSE16
Nov 9 at 9:52
kindly tell me the logic to delete the middle element for the odd no.of elements
– RAVI KUMAR SHARMA CSE16
Nov 9 at 9:52
1
1
How do you deal with middle element in case there are even number of elements?
– Pushpesh Kumar Rajwanshi
Nov 9 at 9:37
How do you deal with middle element in case there are even number of elements?
– Pushpesh Kumar Rajwanshi
Nov 9 at 9:37
for that situation i will check for if n is even or odd and if it is even then i will delete the (n/2)-1 position as per the test case.for eg-n=6 and element is 1 2 3 4 5 6 then my output will be 6 5 4 2 1
– RAVI KUMAR SHARMA CSE16
Nov 9 at 9:52
for that situation i will check for if n is even or odd and if it is even then i will delete the (n/2)-1 position as per the test case.for eg-n=6 and element is 1 2 3 4 5 6 then my output will be 6 5 4 2 1
– RAVI KUMAR SHARMA CSE16
Nov 9 at 9:52
kindly tell me the logic to delete the middle element for the odd no.of elements
– RAVI KUMAR SHARMA CSE16
Nov 9 at 9:52
kindly tell me the logic to delete the middle element for the odd no.of elements
– RAVI KUMAR SHARMA CSE16
Nov 9 at 9:52
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
Do not insert the middle element of input array.
get the middle element index :
int middle = a.length/2;
do not push the middle element in the stack:
Stack<Integer> stack=new Stack<Integer>();
for(int i=0;i<n;i++){
if(i != middle)
stack.push(a[i]);
}
Everything else looks okay. Just make sure to provide meaningful names to variables.
it worked for me..thanks buddy
– RAVI KUMAR SHARMA CSE16
Nov 9 at 9:59
add a comment |
up vote
1
down vote
You can do this with using pop()
method popping returns and removes the top element of the stack so that way you can create and fill a new stack with reverse order, don't need to reverse the iterator, have a look at the code below.
import java.util.ListIterator;
import java.util.Scanner;
import java.util.Stack;
class GFG {
public static void main(String args) {
Scanner s = new Scanner(System.in);
//Define stacks here
Stack<Integer> stack = new Stack<Integer>();
Stack<Integer> new_stack = new Stack<Integer>();
int test = s.nextInt();
for (int t = 0; t < test; t++) {
int n = s.nextInt();
int a = new int[n];
double middle = Math.ceil((double) n / 2);
System.out.println("Middle is : " + middle);
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
}
// add elements to stack
for (int i = 0; i < n; i++) {
stack.push(a[i]);
}
//popping the elements of stack
for (int j = 0; j < n; j++) {
Integer element = stack.pop();
if (j != middle -1) {
new_stack.push(element);
}
}
ListIterator<Integer> lstIterator = new_stack.listIterator(stack.size());
while (lstIterator.hasNext()) {
Integer res = lstIterator.next();
//what condition should i give so that it would print all the elements except middle one.
System.out.print(res + " ");
}
System.out.println();
}
}
}
thanks buddy for the alternate answer.
– RAVI KUMAR SHARMA CSE16
Nov 9 at 10:08
you're welcome.
– Alican Beydemir
Nov 9 at 10:15
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Do not insert the middle element of input array.
get the middle element index :
int middle = a.length/2;
do not push the middle element in the stack:
Stack<Integer> stack=new Stack<Integer>();
for(int i=0;i<n;i++){
if(i != middle)
stack.push(a[i]);
}
Everything else looks okay. Just make sure to provide meaningful names to variables.
it worked for me..thanks buddy
– RAVI KUMAR SHARMA CSE16
Nov 9 at 9:59
add a comment |
up vote
0
down vote
accepted
Do not insert the middle element of input array.
get the middle element index :
int middle = a.length/2;
do not push the middle element in the stack:
Stack<Integer> stack=new Stack<Integer>();
for(int i=0;i<n;i++){
if(i != middle)
stack.push(a[i]);
}
Everything else looks okay. Just make sure to provide meaningful names to variables.
it worked for me..thanks buddy
– RAVI KUMAR SHARMA CSE16
Nov 9 at 9:59
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Do not insert the middle element of input array.
get the middle element index :
int middle = a.length/2;
do not push the middle element in the stack:
Stack<Integer> stack=new Stack<Integer>();
for(int i=0;i<n;i++){
if(i != middle)
stack.push(a[i]);
}
Everything else looks okay. Just make sure to provide meaningful names to variables.
Do not insert the middle element of input array.
get the middle element index :
int middle = a.length/2;
do not push the middle element in the stack:
Stack<Integer> stack=new Stack<Integer>();
for(int i=0;i<n;i++){
if(i != middle)
stack.push(a[i]);
}
Everything else looks okay. Just make sure to provide meaningful names to variables.
answered Nov 9 at 9:36
janardhan sharma
2507
2507
it worked for me..thanks buddy
– RAVI KUMAR SHARMA CSE16
Nov 9 at 9:59
add a comment |
it worked for me..thanks buddy
– RAVI KUMAR SHARMA CSE16
Nov 9 at 9:59
it worked for me..thanks buddy
– RAVI KUMAR SHARMA CSE16
Nov 9 at 9:59
it worked for me..thanks buddy
– RAVI KUMAR SHARMA CSE16
Nov 9 at 9:59
add a comment |
up vote
1
down vote
You can do this with using pop()
method popping returns and removes the top element of the stack so that way you can create and fill a new stack with reverse order, don't need to reverse the iterator, have a look at the code below.
import java.util.ListIterator;
import java.util.Scanner;
import java.util.Stack;
class GFG {
public static void main(String args) {
Scanner s = new Scanner(System.in);
//Define stacks here
Stack<Integer> stack = new Stack<Integer>();
Stack<Integer> new_stack = new Stack<Integer>();
int test = s.nextInt();
for (int t = 0; t < test; t++) {
int n = s.nextInt();
int a = new int[n];
double middle = Math.ceil((double) n / 2);
System.out.println("Middle is : " + middle);
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
}
// add elements to stack
for (int i = 0; i < n; i++) {
stack.push(a[i]);
}
//popping the elements of stack
for (int j = 0; j < n; j++) {
Integer element = stack.pop();
if (j != middle -1) {
new_stack.push(element);
}
}
ListIterator<Integer> lstIterator = new_stack.listIterator(stack.size());
while (lstIterator.hasNext()) {
Integer res = lstIterator.next();
//what condition should i give so that it would print all the elements except middle one.
System.out.print(res + " ");
}
System.out.println();
}
}
}
thanks buddy for the alternate answer.
– RAVI KUMAR SHARMA CSE16
Nov 9 at 10:08
you're welcome.
– Alican Beydemir
Nov 9 at 10:15
add a comment |
up vote
1
down vote
You can do this with using pop()
method popping returns and removes the top element of the stack so that way you can create and fill a new stack with reverse order, don't need to reverse the iterator, have a look at the code below.
import java.util.ListIterator;
import java.util.Scanner;
import java.util.Stack;
class GFG {
public static void main(String args) {
Scanner s = new Scanner(System.in);
//Define stacks here
Stack<Integer> stack = new Stack<Integer>();
Stack<Integer> new_stack = new Stack<Integer>();
int test = s.nextInt();
for (int t = 0; t < test; t++) {
int n = s.nextInt();
int a = new int[n];
double middle = Math.ceil((double) n / 2);
System.out.println("Middle is : " + middle);
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
}
// add elements to stack
for (int i = 0; i < n; i++) {
stack.push(a[i]);
}
//popping the elements of stack
for (int j = 0; j < n; j++) {
Integer element = stack.pop();
if (j != middle -1) {
new_stack.push(element);
}
}
ListIterator<Integer> lstIterator = new_stack.listIterator(stack.size());
while (lstIterator.hasNext()) {
Integer res = lstIterator.next();
//what condition should i give so that it would print all the elements except middle one.
System.out.print(res + " ");
}
System.out.println();
}
}
}
thanks buddy for the alternate answer.
– RAVI KUMAR SHARMA CSE16
Nov 9 at 10:08
you're welcome.
– Alican Beydemir
Nov 9 at 10:15
add a comment |
up vote
1
down vote
up vote
1
down vote
You can do this with using pop()
method popping returns and removes the top element of the stack so that way you can create and fill a new stack with reverse order, don't need to reverse the iterator, have a look at the code below.
import java.util.ListIterator;
import java.util.Scanner;
import java.util.Stack;
class GFG {
public static void main(String args) {
Scanner s = new Scanner(System.in);
//Define stacks here
Stack<Integer> stack = new Stack<Integer>();
Stack<Integer> new_stack = new Stack<Integer>();
int test = s.nextInt();
for (int t = 0; t < test; t++) {
int n = s.nextInt();
int a = new int[n];
double middle = Math.ceil((double) n / 2);
System.out.println("Middle is : " + middle);
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
}
// add elements to stack
for (int i = 0; i < n; i++) {
stack.push(a[i]);
}
//popping the elements of stack
for (int j = 0; j < n; j++) {
Integer element = stack.pop();
if (j != middle -1) {
new_stack.push(element);
}
}
ListIterator<Integer> lstIterator = new_stack.listIterator(stack.size());
while (lstIterator.hasNext()) {
Integer res = lstIterator.next();
//what condition should i give so that it would print all the elements except middle one.
System.out.print(res + " ");
}
System.out.println();
}
}
}
You can do this with using pop()
method popping returns and removes the top element of the stack so that way you can create and fill a new stack with reverse order, don't need to reverse the iterator, have a look at the code below.
import java.util.ListIterator;
import java.util.Scanner;
import java.util.Stack;
class GFG {
public static void main(String args) {
Scanner s = new Scanner(System.in);
//Define stacks here
Stack<Integer> stack = new Stack<Integer>();
Stack<Integer> new_stack = new Stack<Integer>();
int test = s.nextInt();
for (int t = 0; t < test; t++) {
int n = s.nextInt();
int a = new int[n];
double middle = Math.ceil((double) n / 2);
System.out.println("Middle is : " + middle);
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
}
// add elements to stack
for (int i = 0; i < n; i++) {
stack.push(a[i]);
}
//popping the elements of stack
for (int j = 0; j < n; j++) {
Integer element = stack.pop();
if (j != middle -1) {
new_stack.push(element);
}
}
ListIterator<Integer> lstIterator = new_stack.listIterator(stack.size());
while (lstIterator.hasNext()) {
Integer res = lstIterator.next();
//what condition should i give so that it would print all the elements except middle one.
System.out.print(res + " ");
}
System.out.println();
}
}
}
answered Nov 9 at 10:01
Alican Beydemir
226312
226312
thanks buddy for the alternate answer.
– RAVI KUMAR SHARMA CSE16
Nov 9 at 10:08
you're welcome.
– Alican Beydemir
Nov 9 at 10:15
add a comment |
thanks buddy for the alternate answer.
– RAVI KUMAR SHARMA CSE16
Nov 9 at 10:08
you're welcome.
– Alican Beydemir
Nov 9 at 10:15
thanks buddy for the alternate answer.
– RAVI KUMAR SHARMA CSE16
Nov 9 at 10:08
thanks buddy for the alternate answer.
– RAVI KUMAR SHARMA CSE16
Nov 9 at 10:08
you're welcome.
– Alican Beydemir
Nov 9 at 10:15
you're welcome.
– Alican Beydemir
Nov 9 at 10:15
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53223013%2fprinting-the-elements-by-removing-middle-element-of-stack%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
How do you deal with middle element in case there are even number of elements?
– Pushpesh Kumar Rajwanshi
Nov 9 at 9:37
for that situation i will check for if n is even or odd and if it is even then i will delete the (n/2)-1 position as per the test case.for eg-n=6 and element is 1 2 3 4 5 6 then my output will be 6 5 4 2 1
– RAVI KUMAR SHARMA CSE16
Nov 9 at 9:52
kindly tell me the logic to delete the middle element for the odd no.of elements
– RAVI KUMAR SHARMA CSE16
Nov 9 at 9:52