Results 1 to 6 of 6
- 04-16-2011, 12:36 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
how to delete/search double linked list
Hello everyone!
I'm trying to create a double linked list with functions like delete last, delete first, add first, add last.
However the search and delete any does not work. I've tried a few different methods which are posted below in the code (as comments).
Any help would be greatly appreciated! :)
PHP Code:public class DoublyLinkedList { private static Node first; private static Node last; public DoublyLinkedList() { first = null; last = null; } public boolean isEmpty(){ return first == null; } public void addToFront(String dd){ Node newNode = new Node(dd); if (isEmpty()) last = newNode; else first.previous = newNode; newNode.next = first; first = newNode; } public void addToEnd(String dd){ Node newNode = new Node(dd); if (isEmpty()) first = newNode; else { last.next = newNode; newNode.previous = last; } last = newNode; } public Node deleteFirst(){ Node temp = first; if (first.next == null) last = null; else first.next.previous = null; first = first.next; return temp; } public Node deleteLast(){ Node temp = last; if (first.next == null) first = null; else last.previous.next = null; last = last.previous; return temp; } /* public static Node search (Node first, String item){ for(int i= 0; i<10;i++){ if (first.next.dData=="26"){ System.out.println("blah"); return first;} first=first.next; } return null; } /*public static Node delete(Node first, String data) { if (first.dData == data) return first.next; Node previous = null; Node current = first; System.out.println("1"); while (current != null) { if (current.dData == "26") { previous.next = current.next; System.out.println("1"); return first; } else { previous = current; current = current.next; } } return first; }*/ /*public Node deleteKey(String key){ Node current = first; while (current.dData != key) { current = current.next; if (current == null) return null; // cannot find it } if (current == first) // found it; first item? first = current.next; else current.previous.next = current.next; if (current == last) // last item? last = current.previous; else // not last current.next.previous = current.previous; return current; // return value }*/ public void print() { System.out.print("List (first to last): "); Node current = first; // start at beginning while (current != null) // until end of list, { current.displayNode(); current = current.next; // move to next Node } System.out.println(""); } public void printFromLast() { System.out.print("List : "); Node current = last; while (current != null){ current.displayNode(); current = current.previous; } System.out.println(""); } public static int count(Node x) { // count number of elements in list // when we start counting at Node x if (x==null) return 0; return 1 + count(x.next); } public static void main(String[] args) { DoublyLinkedList theList = new DoublyLinkedList(); int b = 22; int c = 44; for(int i = 0; i < 6; i++) { b = i+22; c = i+44; String bString=Integer.toString(b); String cString=Integer.toString(c); theList.addToFront(bString); theList.addToEnd(cString); } theList.print(); theList.printFromLast(); System.out.println("The length of the list before deleting = " + count(first)); theList.deleteFirst(); theList.deleteLast(); // theList.deleteKey("25"); //delete(first, "26"); Node found = search(first, "23"); // System.out.println(found.dData); theList.print(); theList.print(); System.out.println("The length of the list = " + count(first)); } } class Node { public String dData; // data item public Node next; // next Node in list public Node previous; // previous Node in list public Node(String d) { dData = d; } public void displayNode(){ System.out.print(dData + " "); } }
- 04-16-2011, 02:07 PM #2
- 04-16-2011, 02:21 PM #3
Last edited by j2me64; 04-16-2011 at 02:36 PM.
- 04-17-2011, 10:01 AM #4
No I mean other. I mean follows. Topic starter say that he want to create some classes, which implement a linked list.
The Linked list this list, where previous node links on next node. This structure does not allow get some node for index.
for example
3 -> 5 -> 0 -> 1
The same way you need create class Node, which contain value and links on previous and next node and class, which store whole elements.
So I don't understand where store whole elements.
Can anyone explain where store whole elements in post above code?Skype: petrarsentev
http://TrackStudio.com
- 04-17-2011, 04:14 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
Thanks alot for the replies! with the comment of jw2me64 and just slowly checking every line I figured out how to solve delete + search methods.
Petr, you mean I can't for example search for a location nr '1' for example or? I cannot for example delete nr '50' in the list of 1 to 100 right? I probably need to add a second variable to the Node class which keeps track of its position in the list. I think that's not too hard, but in this case not required.
- 04-17-2011, 04:37 PM #6
Not I mean other, how you know Java has some collections, which implement interface List. I mean about ArrayList and LinkedList.
Let's imagine follows situation, you need add a node in center a list.
If you use ArrayList you need create new ArrayList for it. but if you use LinkedList you just point that needs node will links on other node it all.Skype: petrarsentev
http://TrackStudio.com
Similar Threads
-
How can you convert a List<String[]> to a List<Double[]>?
By jetnor in forum New To JavaReplies: 8Last Post: 11-04-2011, 08:30 PM -
How to access an element of a linked list inside another linked list?
By smtwtfs in forum New To JavaReplies: 4Last Post: 02-21-2011, 09:34 AM -
Linked list inside a linked list
By viperlasson in forum New To JavaReplies: 5Last Post: 07-26-2010, 11:15 PM -
Java double linked list
By Clown in forum New To JavaReplies: 1Last Post: 05-07-2010, 04:04 PM -
Circular Double Linked List
By theonly in forum Advanced JavaReplies: 3Last Post: 12-06-2009, 05:10 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks