import java.util.*;

    public class MyLinkedList  {

    private Node head;

    private class Node {

     private String element;
     Node link;

    }

    public MyLinkedList()  {
        head=null;
    }

    public void insertAtHead(String ele)  {
        Node curr = new Node();
        curr.element = ele;
        curr.link = head;
        head=curr;
    }

    public void insertAtTail(String ele)  {
        Node curr = new Node();
        curr.element = ele;
        curr.link = head;
        head=curr;
    }

    public void removeAtHead(String ele)  {
        Node curr = new Node();
        curr.element = ele;
        curr.link = head;
        head=curr;
    }

    public void removeAtTail(String ele)  {
        Node curr = new Node();
        curr.element = ele;
        curr.link = head;
        head=curr;
    }



    public static void main(String[] args)  {

        MyLinkedList s1 = new MyLinkedList();

    s1.insertAtHead("Java I"); 
    s1.insertAtTail("Java II"); 
    s1.insertAtTail("Data Structure"); 
    System.out.println(s1); 
    System.out.println(s1.removeAtHead ()); 
    System.out.println(s1); 
    s1.insertAtHead("CISC120"); 
    System.out.println(s1); 
    s1.insertAtTail("Android"); 
    System.out.println(s1); 
    System.out.println(s1.removeAtTail ()); 
    System.out.println(s1); 

     for (Node x = s1.head; x!=null; x = x.link)
        System.out.println (x.element + "\t");

        }
        }

1)Next time, you need to ask the right question. Not just dump your code and expect others to look through for you.
2)Your insert & remove methods definitions are exactly the same. The only method that has correct definition is the insertAtHead() method. The rest are wrong.
3)Now, ask the right question.

MyLinkedList.java:56: removeAtHead(java.lang.String) in MyLinkedList cannot be applied to ()
System.out.println(s1.removeAtHead ()); 
                     ^
MyLinkedList.java:62: removeAtTail(java.lang.String) in MyLinkedList cannot be applied to ()
System.out.println(s1.removeAtTail ()); 
                     ^
2 errors

I know the two methods are incorrect, is the insertAtTail incorrect also. I am new to java and this forum. So I'm just getting used to the protocol, so bear with me here. I am unsure how to go about correcting these methods to get the desired output

Those 2 methods are defined as requiring a String as a parameter, but when you use them in the print statements you don't supply a parameter

OK. Now, another problem is that your method does not return anything (void). As a result, you cannot display the result by using System.out.println() or System.out.print() because it requires something inside the ()... What you may do is somewhat as follows:

s1.removeAtTail("something");
System.out.println(s1);

However, the result display will be garbage because it will call default toString() method of the object. You will need to override the method as well inside your class.

//i.e.
@Override
public String toString() {
  String out = "whatever you want, build it in here";
  return out;
}

Edited 4 Years Ago by Taywin

Oh ok thanks.....So I need to add a toString method, I see. and now that I look at it I understand that I need to add "something" as I did in the inserts. I still am unsure on what out should be though, forgive my ignorance...but this was a hugh help so far

OK, I can tell you that the out should build a string from the list from head to tail. If the head is null, you may return either an empty string or a message saying that the list is empty. What you need to understand is how to travesal the linked list... Not sure if you get that concept yet. The reason is shown from your other insert/remove methods (you need to traversal the list in order to correctly insert/remove an element).

Edited 4 Years Ago by Taywin

This article has been dead for over six months. Start a new discussion instead.