Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6078329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:49:16+00:00 2026-05-23T10:49:16+00:00

I tried to print the stack elements of the Linked List Implementation. But to

  • 0

I tried to print the stack elements of the Linked List Implementation.

But to print in the order of the stack itself I need a static variable in java.

public void display()
{
<STATIC> <here I need> LinkedListImp temp = this;

    while(temp.next!=null)
    {
     temp=temp.next;
     display();
     }

    System.out.println("\n\t"+ temp.element +"\n");;
}

But While declaring like this, I am getting an error.

I have implemented the display() in interface concept. Hence I cannot have display(LinkedListImp temp).

interface StackMethods
{
    int pop();
    void push(int numberint);
    void display();
}

For example,
If the elements of stack is 1 then 2 then 3.
I didnt want the output as 1 2 3 or 1 (newline) 2 (newline) 3.
Rather I want as 3 (newline)
2 (newline)
1 (though it is not required to demonstrate a real stack)

Is there any other way to implement this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:49:16+00:00Added an answer on May 23, 2026 at 10:49 am

    If you want the value of temp not to be dependent on an instance of display()‘s parent class (LinkedListImp?), then you would need a static class variable. In Java the static keyword marks a variable that belongs to an entire class, not an individual instance. Static in Java creates a variable that is also known as a “class variable.” By definition a class variable can’t be local. To learn more about static variables see what the documentation has to say or check out this StackOverflow question that has the spec in an answer.

    But it looks like what you are trying to do is use a class’s instance, which means you don’t want a static variable. You absolutely want the value tied to the class.

    To make it work, though, you need braces around both statements in your while loop. Otherwise you will get a program that loops through all the elements of the linked list and prints out just the last one. This is because in Java if a block statement (if, else, for, while, etc.) is not followed by braces it treats only the next line as the contents of the block.

    public void display()
    {
        LinkedListImp temp = this;
        while(temp.next!=null)
        {
            System.out.println("\n\t"+ temp.element +"\n");
            temp=temp.next;
        }
    }
    

    To reverse the order here with a loop I’d use a StringBuilder and build up a string.

    public void display()
    {
        LinkedListImp temp = this;
        StringBuilder result = new StringBuilder();
        while(temp.next!=null)
        {
            result.insert(0, "\n\t"+ temp.element +"\n"); // put the result at the front
            temp=temp.next;
        }
        System.out.println(result.toString());
    }
    

    Based on your edit you have added a recursive call to the method, but this is not necessary with the loop. If you are doing recursion, remove the loop. In that case recursion acts as a loop. In that case simply print out the item after calling display with the next item for reverse order, or before for standard order.

    public display() {
       doDisplay(this);
    }
    
    private void doDisplay(LinkedListImpl item) {
        if(item.next) // implicit != null
        {
            doDisplay(item.next);
        }
        System.out.println("\n\t" + temp.element + "\n");  // this line goes before
                                                           // the if statement for
                                                           // regular ordering
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've seen some code such as: out.println(print something); I tried import java.lang.System; but it's
in c, i tried to print out address of variable and address of some
Is it possible to print full list without using cycle? I tried: Console.WriteLine([1;2;3;4;5]) and
I'd like to print what's behind an &myVariable. I tried NSLog(&myIntVar); but it won't
What's the DB2 Version of TSQL Print? I've tried Print 'TableName:X' And Select 'X'
I tried running a python script: print Hello, World! And I get this error:
I tried to implement XOR swap in python. x,y= 10,20 x,y,x = x^y,x^y,x^y print('%s
I tried to allocate 17338896 elements of floating point numbers as follows (which is
I've tried to design an XML schema, but I keep getting stuck. My XML
Sorry if this isn't Stack Overflow worthy but I'm stumped. Here is my code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.