Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 5th, 2008, 10:27 PM
Registered User
 
Join Date: Mar 2008
Location: , California, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to pmcizhere Send a message via MSN to pmcizhere Send a message via Yahoo to pmcizhere
Default Linked List?

Hello, first post here. I'm in my second semester of Java ever now, and I cannot seem to see what the professor wants in this program. He's tried to explain this to me, but maybe I need it from another person's point of view...He also mentioned that in CharNode each of those methods should be really simple, I've filled them in with what I can, not sure if those are right though!

Code:
class CharNode {
    private char letter;
    private CharNode next;
    public CharNode(char ch, CharNode link) {

    }
    public void setCharacter(char ch) {
        letter = ch;
    }
    public char getCharacter() {
        return letter;
    }
    public void setNext(CharNode next) {
        this.next = next;
    }
    public CharNode getNext() {
        return next;
    }
}
class CharList {

    private CharNode head;
    // member variable pointing to the head of the linked list

    public CharList(CharList l) {
    // copy constructor (is that the right terminology?)

    }
    public CharList(String s) {
    // constructor from a String

        toString();
    }
    public String toString() {
    // for output purposes -- override Object version

    }
    public void addHead(char ch) {
    // create a new node and add it to the head of the list

    }
    public void addTail(char ch) {
    // create a new node and add it to the tail of the list

    }
    public void reverse() {
    // modify the list so it is reversed

    }
    public void removeChar(char ch) {
    // remove all occurrences of the character from the list

    }
    public int length() {
    // how many nodes in the list

    }
}
I know it's a lot of stuff to help with, but there is very little activity on our school's webct and the professor doesn't seem to be online right now...Thanks to anyone who tries to help.

BHM>To start, press any key.
Lagos>Where's the "any" key?_
Reply With Quote
  #2 (permalink)  
Old March 6th, 2008, 08:54 AM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Well, I'm not going to do everything for you, but just to get you started:

Code:
class CharNode {
  // As far as I can tell, you are trying to make a doubly linked list
  // So, you'll need your nodes to have a previous pointer as well 
  // as a next
  private char letter;
  private CharNode next;
  private CharNode previous;

  public CharNode(char ch, CharNode next, CharNode previous) {
    setCharacter(ch);
    setNext(next);
    if(previous != null)
      setPrevious(previous);
  }
  public void setCharacter(char ch) {
    letter = ch;
  }
  public char getCharacter() {
    return letter;
  }
  public void setNext(CharNode next) {
    this.next = next;
  }
  public CharNode getNext() {
    return next;
  }
  public void setPrevious(CharNode previous) {
    this.previous = previous;
  }
  public CharNode getPrevious() {
    return previous;
  }
}

public class CharList {

  private CharNode head;
  // member variable pointing to the head of the linked list

  // Because you've got an addTail, you have a doubly linked list
  // so you'll want a pointer to the tail
  private CharNode tail;

  private int nodeCount; // Number of nodes in list

  public CharList(CharList l) {
    // copy constructor (is that the right terminology?)

    // I'll leave you to do this one.  You'll get each 
    // character from l and addChar it to this CharList
  }
  public CharList(String s) {
    char[] ca = s.toCharArray(); // make the string into a
      // char array
    // loop through char array
    for(int i=0;i<ca.length;i++) {
      this.addHead(ca[i]); // adding chars to our linked list
    }
  }

  // for output purposes -- override Object version
  public String toString() {
    CharNode c = tail; // Start at the tail - this is a FILO list
    StringBuffer sb = new StringBuffer();
    for(int i=0;i<length();i++) {
      sb.append(c.getCharacter());
      if(i<(length()-1))
        sb.append("->");
      c=c.getNext();
    }
    return sb.toString();
  }

  // create a new node and add it to the head of the list
  public void addHead(char ch) {    
    CharNode newHead = new CharNode(ch,head,null);
    if(head!=null) {
      head.setNext(newHead);
      newHead.setPrevious(head);
    }
    head=newHead;
    nodeCount++;
    if(length()==1)
      tail=head;
  }
  public void addTail(char ch) {
    // create a new node and add it to the tail of the list
    CharNode newTail = new CharNode(ch,null,tail);
    if(tail!=null) {
      tail.setPrevious(newTail);
      newTail.setNext(tail);
    }
    tail=newTail;
    nodeCount++;
    if(length()==1)
      tail=head;
  }
  public void reverse() {
    // modify the list so it is reversed

  }
  public void removeChar(char ch) {
    // remove all occurrences of the character from the list

  }
  public int length() {
    // how many nodes in the list
    return nodeCount;
  }

  public static void main (String argv[]) {
    CharList cl=new CharList("abcedfghijklmnopqrstuvwxyz");
    cl.addTail('T');
    cl.addHead('H');
    System.out.println(cl.toString());
  }
}
--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
Reply With Quote
  #3 (permalink)  
Old March 6th, 2008, 09:02 PM
Registered User
 
Join Date: Mar 2008
Location: , California, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to pmcizhere Send a message via MSN to pmcizhere Send a message via Yahoo to pmcizhere
Default

Thanks a lot! What you did fill in has helped me, and the professor got online last night and posted a test program that he wanted us to use (It's a separate program that has a main method), so after a bit of tweaking I should get this thing to work properly. Thanks again. :D

BHM&gt;To start, press any key.
Lagos&gt;Where's the "any" key?_
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked List Implementation shah123 C# 0 April 17th, 2007 09:20 AM
2D Linked List sjf905 C# 0 October 8th, 2006 11:02 PM
C# Object Linked List millsbruce C# 5 July 12th, 2005 04:15 PM
Linked List....!!! amahja56 C++ Programming 4 April 6th, 2004 01:05 PM



All times are GMT -4. The time now is 10:46 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.