Results 1 to 15 of 15
  1. #1
    zaydz is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default Linked List in java

    i got a homework from my college.

    i have to make an example for Linked List in java so straight to the point, this is my code
    Java Code:
     import javax.swing.*;
    public class  LinkList
    {
    	LinkList Next = new LinkList();
    	LinkList Prev = new LinkList();
    	LinkList Head = new LinkList();
    	LinkList Tail = new LinkList();
    	rmhs mahasiwa = new rmhs();
    
    	LinkList(){
    		rmhs mahasiwa = new rmhs();
    
    		Next = null;
    		Prev = null;
    		Head = null;
    		Tail = null; 
    	 }
    
    	static void AddFirst (){
    		 LinkList P = new LinkList();
    		 
    		 P.mahasiwa.nama = JOptionPane.showInputDialog("Masukan nama Mahasiswa.","isi");
    		 P.mahasiwa.NIM = JOptionPane.showInputDialog("Masukan NIM Mahasiswa.","isi");
    		 
    		 if (Head == null)
    		 {
    			 Head = P;
    			 Tail = P;
    		 }
    		 else
    		 
    		 P.Next = Head;
    		 Head = P;
    		 Tail.Next = Head;
    	 }
    
    	static void AddLast(){
    		LinkList P = new LinkList();
    
    		P.mahasiwa.nama = JOptionPane.showInputDialog("Masukan nama Mahasiswa.","isi");
    		P.mahasiwa.NIM = JOptionPane.showInputDialog("Masukan NIM Mahasiswa.","isi");
    
    		if (Head == null)
    		{
    			Head = P;
    			Tail = P;
    		}
    		else
    
    		Tail.Next = P;
    		P.Next = Head;
    		Tail = P;
    	}
    	
    
    	public static void main(String[] args) 
    	{
    		AddFirst();
    	}
    }
    errr so my lecture said that never use static *thingy* when u want to make an LL. but the error msg said that i have to make those variable to be static. anyone ??

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Static methods can only refer to static members in the same class, so either make those members static (which your professor doesn't like) or make those methods non-static; there's a whole lot more wrong in your class but first you have to fix this ...

    kind regards,

    Jos

  3. #3
    zaydz is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    i already made it non-static at the first. but when im going to call the AddLast Method it have to be static. so i just made it static but its still error..lul

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by zaydz View Post
    i already made it non-static at the first. but when im going to call the AddLast Method it have to be static. so i just made it static but its still error..lul
    You can't make methods or other members static or non-static at will; there has to be a reason for it. You're also confusing the entire list with a single node of a list. Show us your latest code.

    kind regards,

    Jos

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Your problem starts with the main() method calling a static method vs having the main() method create an instance of the class and using a reference to that instance to call the class's methods:

    In main:
    LinkList aLL = new LinkList(); // create an instance
    aLL.AddFirst(); // call the AddFirst method to ....

  6. #6
    zaydz is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    my bad. and i already solved it and made new code thx

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by zaydz View Post
    my bad. and i already solved it and made new code thx
    Why don't you show your final code here? We can criticize it (or compliment it if it's really perfect) and maybe the final result will be better than your individual efforts? You show your code and I'll show mine, deal?

    kind regards,

    Jos

    ps. I only have a skeleton of a singly linked list class yet. ;-)

  8. #8
    zaydz is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    for sure
    this is the abstract data type
    Java Code:
    public class maha 
    {
    	String nama;
    	int NIM;
    	maha next;
    
    	public maha(){
    		this.nama= "";
    		this.NIM= 0;
    		this.next= null;
    	}
    }
    and this is the Link List *not yet finish just add and print, totally different from my first.
    Java Code:
    import javax.swing.*;
    class LinkList 
    {	
    	static maha head;
    	static maha tail;
    	public static void main(String[] args) 
    	{
    		AF();
    		AF();
    		print();
    	}
    
    	public static void AF(){
    		
    		maha P;
    		
    		int IN = Integer.parseInt(JOptionPane.showInputDialog("Masukan NIM mahasiswa.","0"));
    		String N = JOptionPane.showInputDialog("masukan Nama Mahasiswa","LOL");
    
    		maha info = new maha();
    		info.nama= N;
    		info.NIM= IN;
    
    		if (head == null)
    		{
    			head = info;
    			info.next = info;
    			tail = info;
    		}else{
    			P = head;
    			while (info.next != null)
    			{
    				info = info.next;
    			}
    			info.next = null;
    			P.next = info;
    	}
    	}
    
    	public static void print()
    	{
    		maha P;
    		P = head;
    
    		while (P != null)
    		{
    			System.out.println("NIM :"+P.NIM);
    			System.out.println("Nama :"+P.nama+"\n");
    			P =P.next;
    		}
    		System.out.println("");
    	}
    
    
    }
    hehe

    my fren taught mee to do it :)

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Now that you have the logic for how a Linked list should work, try changing it so that a program can have more than one linked list at a time.
    This mean removing all the "static"s.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Cool, here is my implementation of a singly linked list. The list itself hides all nodes from the outer world and it can store all types of data elements by using generics; it also implements the Iterable<T> interface for convenience. Here it is:

    Java Code:
    import java.util.Iterator;
    
    public class MyList<D> implements Iterable<D> {
    	
    	private Node<D> head;
    	
    	private static class Node<D> {
    
    		private D data;
    		private Node<D> next;
    
    		public Node(D data) {
    			this(data, null);
    		}
    		
    		public Node(D data, Node<D> next) {
    			this.data= data;
    			this.next= next;
    		}
    		
    		public String toString() {
    			return data.toString();
    		}
    	}
    	
    	public MyList() { }
    	
    	public MyList(MyList<D> that) {
    		
    		for (D data : that)
    			addLast(data);
    	}
    	
    	private Node<D> last() {
    		
    		Node<D> node;
    		
    		for (node= head; node != null && node.next != null; node= node.next);
    		
    		return node;
    	}
    	
    	public int size() {
    		int size= 0;
    		
    		for (Node<D> node= head; node != null; node= node.next)
    			size++;
    		
    		return size;
    	}
    	
    	public class SLLIterator implements Iterator<D> {
    		
    		private Node<D> node;
    		
    		public SLLIterator() {
    			node= head;
    		}
    		
    		public boolean hasNext() {
    			return node != null;
    		}
    		
    		public D next() {
    			if (node != null) {
    				D data= node.data;
    				node= node.next;
    				return data;
    			}
    			return null;
    		}
    		
    		public void remove() {
    			throw new UnsupportedOperationException("remove");
    		}
    	}
    	
    	public Iterator<D> iterator () { return new SLLIterator(); }
    	
    	public D getFirst() { return (head == null)?null:head.data; }
    	
    	public D getLast() {
    		Node<D> last= last();
    		
    		return (last == null)?null:last.data;
    	}
    	
    	public void addFirst(D data) {
    		
    		head= new Node<D>(data, head);
    	}
    	
    	public void addLast(D data) {
    		
    		Node<D> last= last();
    		
    		if (last == null)
    			addFirst(data);
    		else 
    			last.next= new Node<D>(data);
    	}
    		
    	public boolean contains(D d) {
    		
    		for (D data : this)
    			if (data.equals(d))
    				return true;
    		return false;
    	}
    	
    	public String toString() {
    
    		StringBuilder sb= new StringBuilder();
    		
    		for (D data : this) 
    			sb.append(data).append(' ');
    		return sb.toString();
    	}
    	
    	public static void main(String[] args) {
    
    		MyList<String> list= new MyList<String>();
    		
    		list.addLast("foo");
    		list.addLast("bar");
    		list.addLast("baz");
    		list.addFirst("fubar");
    		
    		System.out.println("contains(fnark): "+list.contains("fnark"));
    		System.out.println("contains(baz): "+list.contains("baz"));
    		System.out.println("first: "+list.getFirst());
    		System.out.println("last: "+list.getLast());
    		System.out.println("size: "+list.size());
    		System.out.println(list);
    		
    		for (String data : list) 
    			System.out.print(data+" ");
    		System.out.println();
    	}
    }
    If you (or anyone) else is interested I can give a method by method explanation and extend this skeleton a bit more. Study it and enjoy.

    kind regards,

    Jos

  11. #11
    zaydz is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    nice code jos. i appreciate it. and imma study it too. thanks

  12. #12
    zaydz is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    anyway u didnt use tail. as the circular one.?

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by zaydz View Post
    anyway u didnt use tail. as the circular one.?
    Nope, my list implementatation is a simple singly linked list; it isn't circular, but feel free to adjust the code.

    kind regards,

    Jos

  14. #14
    zaydz is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    yayy thanks. imma going to make 1 with next and prev i just need 2 type of link list. almost done and tomorrow i'll be ready for the exam :p

    thanks jos u r helpfull

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by zaydz View Post
    yayy thanks. imma going to make 1 with next and prev i just need 2 type of link list. almost done and tomorrow i'll be ready for the exam :p

    thanks jos u r helpfull
    Well, good luck with your exams; so you want to implement a doubly linked list now? You can use my code for it and change it step by step; I'd start with implementing a notion of the 'tail' of the list, not just the 'head' of the list (as it is now). The private method 'last()' can go then, it isn't needed anymore and it'll speed up appending new elements to the list. Go step by step.

    kind regards,

    Jos

Similar Threads

  1. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 11:15 PM
  2. Java double linked list
    By Clown in forum New To Java
    Replies: 1
    Last Post: 05-07-2010, 04:04 PM
  3. Linked List
    By basma in forum JCreator
    Replies: 0
    Last Post: 02-03-2010, 08:34 AM
  4. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 08:53 PM
  5. Linked List help
    By neobie in forum New To Java
    Replies: 8
    Last Post: 12-22-2007, 03:15 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
  •