Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Linked List with PNode

$
0
0
I am trying to implement a linked list that defines a PNode class. The data field of PNode points to a Person object, and the link field points to the next PNode in the list. The user enters in a name and age, and said information is added to the end of the list. Finally, ug the pointer start I want to print out the name and age of the oldest person. Here is what I have, minus the final print statement. Any idea where I have gone wrong with this?

public class Person {

		  private String name;
		  private int age;

		  public Person(String name) {
			setName(name);
		  }

		  public int getAge() {
			return age;
		  }

		  public String getName() {
			return name;
		  }

		  public void setAge (int age) {
			this.age = age;
		  }

		  public void setName(String name) {
			this.name = name;
		  }

}





import java.util.Scanner;


public class PNode {
	private Person object;
	private PNode next;
	

	public PNode(Person person, Object object) {
		
	}

	public static void main(String[] args) {
		  
		  System.out.println("Please enter a person's name and age");
		  Scanner scanner = new Scanner(System.in);
		  
		  PNode start, tail, next;
		  start = null;
		  
		  String name = scanner.next();
		  
		  if (!name.equalsIgnoreCase("DONE")) {
			  start = new PNode(new Person(name), null);
			  tail = start;
			  
			  while (true) {
				  name = scanner.next();
				  if (name.equalsIgnoreCase("DONE")) break;
				  next = new PNode(new Person(name), null);
				  tail.setNext(next);
				  tail = next;
			  }
		  }
	  }

	private void setNext(PNode next) {
		this.next = next;
		
	}
	class LinkedList {
		  public LinkedList() {
		    header = new ListNode(null);
		  }

		  public boolean isEmpty() {
		    return header.next == null;
		  }

		  public void makeEmpty() {
		    header.next = null;
		  }

		  public LinkedListIterator zeroth() {
		    return new LinkedListIterator(header);
		  }

		  public LinkedListIterator first() {
		    return new LinkedListIterator(header.next);
		  }

		  public void insert(Object x, LinkedListIterator p) {
		    if (p != null && p.current != null)
		      p.current.next = new ListNode(x, p.current.next);
		  }

		  public LinkedListIterator find(Object x) {
		    ListNode itr = header.next;

		    while (itr != null && !itr.element.equals(x))
		      itr = itr.next;

		    return new LinkedListIterator(itr);
		  }

		  public LinkedListIterator findPrevious(Object x) {
		    ListNode itr = header;

		    while (itr.next != null && !itr.next.element.equals(x))
		      itr = itr.next;

		    return new LinkedListIterator(itr);
		  }

		  public void remove(Object x) {
		    LinkedListIterator p = findPrevious(x);

		    if (p.current.next != null)
		      p.current.next = p.current.next.next; 
		  }

		  private ListNode header;

		}

		class LinkedListIterator {
		  LinkedListIterator(ListNode theNode) {
		    current = theNode;
		  }

		  public boolean isValid() {
		    return current != null;
		  }

		  public Object retrieve() {
		    return isValid() ? current.element : null;
		  }

		  public void advance() {
		    if (isValid())
		      current = current.next;
		  }

		  ListNode current;
		}

		class ListNode {
		  public ListNode(Object theElement) {
		    this(theElement, null);
		  }

		  public ListNode(Object theElement, ListNode n) {
		    element = theElement;
		    next = n;
		  }

		  public Object element;

		  public ListNode next;
		}

}




Sorry, the variable start points to the first node.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>