i just learn about single linkedlist and i am trying to create a program.
public class linklis {
public static void main(String[] args) {
LinkedList ab= new LinkedList();
ab.add("1");
ab.add("2");
ab.add("4");
ab.add("7");
System.out.println("the list" + ab);
System.out.println("remove 2nd element: " + ab.Delete(2));
System.out.println(" - print linkedlist size: " + ab.size());
System.out.println("the list" + ab);
}
}
class LinkedList {
private Node head;
private int cursor;
public LinkedList(){
head=-1;
cursor=-1;
}
public void add(int newMember){
Node newData=new Node(newMember);
newData.setLink(head);
head=newData;
}
public boolean empty(){
return head=null;
}
public int size(){
return size;
}
public void setData(integer newdata){
data=newdata;
}
public void setLink (node newlink){
link=newlink;
}
public int getData(){
return data;
}
public int getLink(){
return link;
}
public boolean Delete(int key){
Node found=search(key);
if (found==-1)
{
System.out.println("nothing to delete");
return false;
}
else
{
if (found==head)
{
head=found.getLink();
System.out.println("keydeleted");
}
return true;
}
}
public String toString(){
String outString = "";
if (!empty ())
{
resetCursor ();
outString += (cursor + " ") ;
while (hasNext ())
outString += (getNext () + " ");
}
return outString;
}
}