Results 1 to 1 of 1
Thread: Java Linked List
- 02-19-2011, 06:43 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
Java Linked List
So I need help with my paste method here. Each time it is called, new Node should be created and be inserted to the left of current. Thank You!
Java Code:import java.util.Scanner; public class List { private Node head; private Node last; private Node current; private Node copy; private Scanner so = new Scanner(System.in); private int total; public List() { head = null; last = null; current = null; copy = null; total = 0; } // adds int to the lined list public void addNum(int n) { if(head == null) { head = new Node(n); } else { Node temp = head; while(temp.getNext() != null) { temp = temp.getNext(); } temp.setNext(new Node(n)); } toString(); } // requests user inputs public void command() { String s = ""; boolean running = true; current = head; while(running) { System.out.println("Command: home forward copy paste cut done ?"); s = so.nextLine(); if(s.toLowerCase().equals("home")) { home(); } else if(s.toLowerCase().equals("forward")) { forward(); } else if(s.toLowerCase().equals("copy")) { copy(); } else if(s.toLowerCase().equals("paste")) { paste(); } else if(s.toLowerCase().equals("cut")) { cut(); } else if(s.toLowerCase().equals("done")) { running = false; } else { System.out.println("Command not recognized, please type a command from above"); } toString(); } } // Copies the current node public void copy() { if(head == null) { return; } else { copy = current; } } // pastes the copied node public void paste() { if(current == null || head == null) { System.out.println("1"); return; } else { if(current == last) { Node temp = new Node(current.getNum()); System.out.println("test 1"); last.setNext(temp); temp.setNext(null); last = temp; } } } // returns last node public Node last() { if(head == null) { return null; } else { Node temp = head; while(temp != null) { last = temp; temp = temp.getNext(); } System.out.println("Last: " + last.getNum()); return last; } } // returns first node public Node home() { if(head == null) { return null; } else { return head; } } // deletes first node public void deleteHead() { if (head == null) return; else { if (head == last) { head = null; last = null; } else { head = head.getNext(); current = head; } } } // deletes last node public void deleteLast() { if(last == null) { return; } else { Node temp = head.getNext(); Node previous = head; while (temp != last) { previous = previous.getNext(); temp = temp.getNext(); } last = previous; last.setNext(null); current = last; } } // cuts the current node public void cut() { if(home() == null) { return; } else if(current == home()) { deleteHead(); } else if(current != last()) { Node temp = home().getNext(); Node previous = home(); while(temp != current) { previous = previous.getNext(); temp = temp.getNext(); } previous.setNext(temp.getNext()); copy = current; current = current.getNext(); } else if(current == last) { deleteLast(); } } // moves the current to the next node public void forward() { if(current == last) { System.out.println("Can not move any further"); return; } else if(head == null) { return; } else if(current == null) { current = home(); } else { current = current.getNext(); System.out.println(current.getNum()); } } // prints the data in linked list public String toString() { String s = ""; Node temp = head; while(temp != null) { s = s + " " + temp.getNum(); temp = temp.getNext(); } System.out.println(s); return s; } public static void main(String[] args) { List gl = new List(); gl.addNum(1); gl.addNum(2); gl.addNum(3); //gl.addNum(4); //gl.addNum(5); //gl.addNum(6); gl.command(); //gl.toString(); } }Java Code:public class Node { private int num; private Node next; public Node(int n) { num = n; next = null; } public int getNum() { return num; } public void setNum(int n) { num = n; } public Node getNext() { return next; } public void setNext(Node n) { next = n; } }
Similar Threads
-
Java Linked List
By nomss in forum New To JavaReplies: 7Last Post: 12-10-2010, 01:02 PM -
Java Linked List
By nomss in forum New To JavaReplies: 8Last Post: 12-09-2010, 04:28 PM -
Linked list inside a linked list
By viperlasson in forum New To JavaReplies: 5Last Post: 07-26-2010, 11:15 PM -
Linked List in java
By zaydz in forum New To JavaReplies: 14Last Post: 06-28-2010, 12:53 PM -
Java double linked list
By Clown in forum New To JavaReplies: 1Last Post: 05-07-2010, 04:04 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks