Results 1 to 11 of 11

Thread: Java stack

  1. #1
    lluucckk is offline Member
    Join Date
    Jun 2015
    Posts
    4
    Rep Power
    0

    Default Java stack

    Hello there. I`m new in Java programming and i have for school something to do asap.
    Compute the height of a stack. Then determine whether that value is found in the stack element .
    If someone can help me i`ll be so grateful. THANKS

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Java stack

    Submit your code, ask a question, and we might be able to help. But we don't provide solutions to homework here.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    lluucckk is offline Member
    Join Date
    Jun 2015
    Posts
    4
    Rep Power
    0

    Default Re: Java stack

    I have this code

    package stack;
    public class Stack {

    public Node l;
    public Stack(){
    l=null;}
    public Node insert(int i){
    Node aux=new Node(i);
    aux.next=l;
    l=aux;
    return l;}
    public int height(){
    int height=0;
    Node first = null;
    while(first!=null)
    height=height + first.info;
    return height;}
    public void check(){
    int height = 0;
    Node i;
    for(i=l;i!=null;i=i.next)
    {
    if(height==l.info)
    System.out.println("The value is in the stack");
    else
    System.out.println("The value isn`t in the stack");
    }
    }
    public void displaystack(){
    Node i;
    for(i=l;i!=null;i=i.next)
    i.displaystack();
    }


    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Java stack

    Ok, What happens when you compile and execute that code?
    Where is the main() method for executing the code?

    Please edit your post and wrap the code in code tags to preserve its formatting.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Java stack

    This might be an interesting read:

    Rubber Duck Problem Solving
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Java stack

    That's a process we used to call using a "cardboard programmer". In the process of explaining a problem to another programmer, you'd often solve it yourself, well before the other programmer understood what the problem was.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    lluucckk is offline Member
    Join Date
    Jun 2015
    Posts
    4
    Rep Power
    0

    Default Re: Java stack

    I have 4 classes :
    1.Link

    package java;
    public class Link {
    public double dData;
    public Link next;
    public Link(double dd)
    { dData=dd; }
    public void displayLink()
    { System.out.println(dData + " "); }

    }

    2.LinkList

    package java;
    public class LinkList {

    private Link first;
    public LinkList()
    { first=null; }
    public boolean isEmpty()
    { return (first==null); }
    public void insertFirst(double dd)
    {
    Link newLink=new Link(dd);
    newLink.next=first;
    first=newLink;
    }
    public double deleteFirst()
    {
    Link temp=first;
    first=first.next;
    return temp.dData;
    }
    public void displayList()
    {
    Link current=first;
    while(current !=null)
    {
    current.displayLink();
    current=current.next;
    }
    System.out.println("");
    }
    }

    3.LinkStack

    package java;

    class LinkStack {
    public LinkList theList;
    public LinkStack()
    {theList=new LinkList();}
    public void push(double j)
    {
    theList.insertFirst(j);
    }
    public double pop()
    {return theList.deleteFirst();}
    public boolean isEmpty()
    {return (theList.isEmpty() );}
    public void displayStack()
    {
    System.out.print("Stiva (varf->baza): ");
    theList.displayList();
    }
    }

    4.LinkStackApp //this is the main class

    package java;

    class LinkStackApp {
    public static void main(String[] args)
    {
    LinkStack theStack=new LinkStack();
    theStack.push(20);
    theStack.push(40);
    theStack.displayStack();
    theStack.push(60);
    theStack.push(80);
    theStack.displayStack();
    theStack.pop();
    theStack.pop();
    theStack.displayStack();
    }
    }

  8. #8
    lluucckk is offline Member
    Join Date
    Jun 2015
    Posts
    4
    Rep Power
    0

    Default Re: Java stack

    At this point it gives me this message
    Attachment 6840

    I use Jbuilder 5

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Java stack

    Please edit your post and wrap the code in code tags to preserve its formatting.
    Be sure the code has proper indentations to show the logic and nesting of statements.

    Also copy the full text of the error message and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Java stack

    Quote Originally Posted by Norm View Post
    That's a process we used to call using a "cardboard programmer". In the process of explaining a problem to another programmer, you'd often solve it yourself, well before the other programmer understood what the problem was.
    I remember an annual review I had years ago (must have been the 90s as it was BAe) where one of the points raised about my work was that I didn't ask enough questions. I replied that there was little point me asking a question if I didn't actually know what the question was in the first place and, by the time I'd got the problem I was having into some sort of order so that I had a reasonable question to ask I had (9 time out of 10) actually answered it. At best I just needed confirmation.

    I got a rather blank look at that...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Java stack

    You have been told twice to use code tags. Read about them here --> BB Code List - Java Programming Forum - Learn Java Programming

    And please go thru the Forum Rules before making any more posts.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 2
    Last Post: 08-23-2013, 05:15 AM
  2. stack Java help
    By judemartin99 in forum New To Java
    Replies: 1
    Last Post: 05-01-2013, 04:09 AM
  3. Replies: 2
    Last Post: 01-14-2012, 08:56 PM
  4. Java Stack and Queue
    By jeanjiang in forum New To Java
    Replies: 3
    Last Post: 04-25-2011, 02:19 PM
  5. Using java.util.Stack
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-20-2007, 05:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •