Results 1 to 2 of 2
  1. #1
    danver_2009 is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Red face Help on Stack Implementation

    Good Day!

    To all members, will you help me with my problem regarding to my Stack implementation. I try my best to solve but it seems that i don't get it. It took 3 days from now, still I didn't get it right.

    Well, I have my code in List. Will you help me solve my prob.? Can you make a code in it? Can you implement the Stack with its main? If you do so, just try to inherit it to the List code. Thank you. That will be a big help for me. With that I could pass my midterm exam. Thank You...

    [-palms_09-]

    //List implementation


    public class LinkedList
    {
    // reference to the head node.
    private Node head;
    private int listCount;

    // LinkedList constructor
    public LinkedList()
    {
    // this is an empty list, so the reference to the head node
    // is set to a new node with no data
    head = new Node(null);
    listCount = 0;
    }

    public void add(Object data)
    // post: appends the specified element to the end of this list.
    {
    Node temp = new Node(data);
    Node current = head;
    // starting at the head node, crawl to the end of the list
    while(current.getNext() != null)
    {
    current = current.getNext();
    }
    // the last node's "next" reference set to our new node
    current.setNext(temp);
    listCount++;// increment the number of elements variable
    }

    public void add(Object data, int index)
    // post: inserts the specified element at the specified position in this list.
    {
    Node temp = new Node(data);
    Node current = head;
    // crawl to the requested index or the last element in the list,
    // whichever comes first
    for(int i = 1; i < index && current.getNext() != null; i++)
    {
    current = current.getNext();
    }
    // set the new node's next-node reference to this node's next-node reference
    temp.setNext(current.getNext());
    // now set this node's next-node reference to the new node
    current.setNext(temp);
    listCount++;// increment the number of elements variable
    }

    public Object get(int index)
    // post: returns the element at the specified position in this list.
    {
    // index must be 1 or higher
    if(index <= 0)
    return null;

    Node current = head.getNext();
    for(int i = 1; i < index; i++)
    {
    if(current.getNext() == null)
    return null;

    current = current.getNext();
    }
    return current.getData();
    }

    public boolean remove(int index)
    // post: removes the element at the specified position in this list.
    {
    // if the index is out of range, exit
    if(index < 1 || index > size())
    return false;

    Node current = head;
    for(int i = 1; i < index; i++)
    {
    if(current.getNext() == null)
    return false;

    current = current.getNext();
    }
    current.setNext(current.getNext().getNext());
    listCount--; // decrement the number of elements variable
    return true;
    }

    public int size()
    // post: returns the number of elements in this list.
    {
    return listCount;
    }

    public String toString()
    {
    Node current = head.getNext();
    String output = "";
    while(current != null)
    {
    output += "[" + current.getData().toString() + "]";
    current = current.getNext();
    }
    return output;
    }

    private class Node
    {
    // reference to the next node in the chain,
    // or null if there isn't one.
    Node next;
    // data carried by this node.
    // could be of any type you need.
    Object data;


    // Node constructor
    public Node(Object _data)
    {
    next = null;
    data = _data;
    }

    // another Node constructor if we want to
    // specify the node to point to.
    public Node(Object _data, Node _next)
    {
    next = _next;
    data = _data;
    }

    // these methods should be self-explanatory
    public Object getData()
    {
    return data;
    }

    public void setData(Object _data)
    {
    data = _data;
    }

    public Node getNext()
    {
    return next;
    }

    public void setNext(Node _next)
    {
    next = _next;
    }
    }
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    So, what's the problem? Compiler messages? Runtime Errors? Doing something it shouldn't? Not doing something it should?

    I'm sorry, but we are not going to play human compiler and JVM to try and figure out what, exactly, is going wrong. We need you to tell us.

    Also, please post your code again, in code tags this time, so that formatting is correctly preserved.
    Add to masijade's Reputation

Similar Threads

  1. Problem in Calculator implementation using Stack
    By realahmed8 in forum New To Java
    Replies: 1
    Last Post: 12-19-2008, 11:58 PM
  2. Need little Help In Calculater Implementation..
    By realahmed8 in forum New To Java
    Replies: 6
    Last Post: 12-18-2008, 01:39 AM
  3. VietPad 2.0 (.NET implementation)
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-05-2008, 04:32 PM
  4. Graph DPS and BFS implementation
    By hey in forum New To Java
    Replies: 1
    Last Post: 01-09-2008, 09:19 PM
  5. Help with recursive implementation
    By toby in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 05:57 AM

Posting Permissions

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