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();
}
}
}









share|improve this question




















  • 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















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();
}
}
}









share|improve this question




















  • 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













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();
}
}
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












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.






share|improve this answer





















  • it worked for me..thanks buddy
    – RAVI KUMAR SHARMA CSE16
    Nov 9 at 9:59


















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();
}
}
}





share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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.






share|improve this answer





















  • it worked for me..thanks buddy
    – RAVI KUMAR SHARMA CSE16
    Nov 9 at 9:59















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.






share|improve this answer





















  • it worked for me..thanks buddy
    – RAVI KUMAR SHARMA CSE16
    Nov 9 at 9:59













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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












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();
}
}
}





share|improve this answer





















  • 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















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();
}
}
}





share|improve this answer





















  • 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













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();
}
}
}





share|improve this answer












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();
}
}
}






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)