In: Computer Science
public class IntNode
{
private
int data;
private
IntNode link;
public
IntNode(int data, IntNode link){.. }
public
int getData(
) {.. }
public
IntNode getLink(
) {..
}
public
void setData(int data)
{.. }
public
void setLink(IntNode link) {.. }
}
All questions are based on the above class, and the following declaration.
// Declare an empty, singly linked list with a head and a tail reference.
// you need to make sure that head always points to the head
node and tail always points to the tail node.
IntNode head,
tail;
head = tail =
null;
// 1.) Insert a node with data 2 into this empty list
// 2.) Insert a node with data 4 at the end of the list.
// 3.) Insert a node with data 3 between the first and the last node.
// 4.) Insert a node with data 1 before the first node.
// 5.) delete the node with data 2
// 6.) delete the node with data 1
Program
public class IntNode
{
private int data;
private IntNode link;
public IntNode(int data, IntNode link){
this.data = data;
this.link = link;
}
public int getData(){
return data;
}
public IntNode getLink(){
return link;
}
public void setData(int data){
this.data = data;
}
public void setLink(IntNode link){
this.link = link;
}
}
class SLinkedList{
private IntNode head;
private IntNode tail;
//constructor
public SLinkedList(){
head = null;
tail = null;
}
//method to insert an item to beginning of the
list
public void insertAtBegin(int item)
{
IntNode newnode = new IntNode(item,
head);
if(head==null)
{
head = tail =
newnode;
}
else
{
head =
newnode;
}
}
//method to insert an item to end of the list
public void insertAtEnd(int item)
{
IntNode newnode = new IntNode(item,
null);
if(tail==null)
{
head = tail =
newnode;
}
else
{
tail.setLink(newnode);
tail =
newnode;
}
}
//method to insert an item to the list
public void insert(int item)
{
IntNode newnode = new IntNode(item,
null);
if(head==null)
{
head = tail =
newnode;
}
else
{
IntNode temp =
head;
IntNode pre =
null;
while(temp!=
null && temp.getData()<item)
{
pre = temp;
temp = temp.getLink();
}
pre.setLink(newnode);
newnode.setLink(temp);
}
}
//method to delete an item from the list
public void delete(int item)
{
if(head==null){
System.out.println ("List is empty");
return;
}
IntNode temp = head;
IntNode prev = null;
if(temp.getData()==item)
{
head =
temp.getLink();
System.out.println (item + " has been deleted");
return;
}
while(temp!= null &&
temp.getData()!=item)
{
prev =
temp;
temp =
temp.getLink();
}
if(temp==null)
{
System.out.println (item + " was not found");
return;
}
prev.setLink(temp.getLink());
System.out.println (item + " has
been deleted");
}
//method to print the list
public void printList()
{
if(head==null)
{
System.out.println ("List is empty");
return;
}
System.out.print("List: ");
IntNode temp = head;
while(temp!= null)
{
System.out.print
(temp.getData() + " ");
temp =
temp.getLink();
}
System.out.println ();
}
}
class LinkedListDemo
{
//main method
public static void main (String[] args)
{
//create object of
SLinkedList
SLinkedList list = new
SLinkedList();
// 1.) Insert a node with data 2
into this empty list
list.insert(2);
System.out.println ("After insert
2: ");
list.printList();
// 2.) Insert a node with data 4 at
the end of the list.
list.insertAtEnd(4);
System.out.println ("After insert
4: ");
list.printList();
// 3.) Insert a node with data 3
between the first and the last node.
list.insert(3);
System.out.println ("After insert
3: ");
list.printList();
// 4.) Insert a node with data 1
before the first node.
list.insertAtBegin(1);
System.out.println ("After insert
1: ");
list.printList();
// 5.) delete the node with data
2
list.delete(2);
System.out.println ("After delete
2: ");
list.printList();
// 6.) delete the node with data
1
list.delete(1);
System.out.println ("After delete
1: ");
list.printList();
}
}
Output:
After insert 2:
List: 2
After insert 4:
List: 2 4
After insert 3:
List: 2 3 4
After insert 1:
List: 1 2 3 4
2 has been deleted
After delete 2:
List: 1 3 4
1 has been deleted
After delete 1:
List: 3 4
Solving your question and
helping you to well understand it is my focus. So if you face any
difficulties regarding this please let me know through the
comments. I will try my best to assist you.
Thank you.