Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement a single linked list in java by using the insertAsHead(), removeHead(), removeTail(), and display() methods by using TestLinkedLists.java, LinkedList.java, and SingleLink.java Singlelink.java is as

Implement a single linked list in java by using the insertAsHead(), removeHead(), removeTail(), and display() methods by using TestLinkedLists.java, LinkedList.java, and SingleLink.java

Singlelink.java is as follows:

public class SingleLink {

private String data;

private SingleLink next;

public SingleLink(String data) {

this.data = data;

}

protected SingleLink getNext() {

return next;

}

protected void setNext(SingleLink next) {

this.next = next;

}

@Override

public String toString() {

return data;

}

}

LinkList.java is as follows:

public class LinkedList {

SingleLink head;

public LinkedList() {

head = null;

}

public boolean isEmpty() {

// TODO

return true;

}

public void insertAsHead(SingleLink newLink) {

// TODO

}

public SingleLink removeHead() {

// TODO

return null;

}

public SingleLink removeTail() {

// TODO

return null;

}

public void display() {

// TODO

System.out.println();

}

}

TestLinkList.java is as follows:

public class TestLinkedLists {

public static void main(String[] args) {

LinkedList test1 = new LinkedList();

test1.insertAsHead(new SingleLink("Sally"));

test1.insertAsHead(new SingleLink("Johnny"));

test1.insertAsHead(new SingleLink("Leslie"));

test1.display();

System.out.println(test1.removeHead() + "\t\t: RemoveHead()");

test1.display();

System.out.println(test1.removeTail() + "\t\t: RemoveTail()");

test1.display();

System.out.println(test1.removeHead() + "\t\t: RemoveHead()");

test1.display();

System.out.println(test1.removeHead() + "\t\t: RemoveHead()");

System.out.println(test1.removeTail() + "\t\t: RemoveTail()");

System.out.println("Done ");

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions