In: Computer Science
Write an application and perform the following:(NO USER INPUT)
-Create at least three classes such as:
1. listnode- for data and link, constructors
2. Singlelinkedlist-for method definition
3. linkedlist-for objects
-Insert a node at tail/end in a linked list.
-and display all the nodes in the list.
-Delete a node at a position in the list
Note: Add these methods in the same application which we have done in the class.
this is what we've done in the class:
class listnode{
int data;//first part of the node(data)
listnode link;//second part of the node(address)
listnode()
{
data=0;
link=null;
}
listnode(int d,listnode l)//10|null
{
data=d;
link=l;
}
}
class singlelinklist{
void display(listnodes head){
listnodes current=head;
while(current.link!=null){
System.out.print(current.data+"-->");
current=current.link;
}
System.out.print(current.data);
}
public listnodes insert(listnodes head,int data)
{
//create new node
listnodes newnode=new listnodes(data,null);
//link the newnode to the head node
newnode.link=head;
//make newnode the first node
head=newnode;
return head;//return the first node
}
//insert at a position(after or before a node)
public listnodes InsertAtPostion(listnodes head,int data,int
position){
listnodes newnode=new listnodes(data,null);
listnodes previous=head;
int count=1;
while(count<=position-1){//(count<position)
previous=previous.link;
count++;
}
// listnodess current=previous.link;
listnodes current=previous;
current=previous.link;
newnode.link=current;
previous.link=newnode;
return head;
}
public listnodes deletefirst(listnodes head){
listnodes temp=head;//rename
head=head.link;//move head
temp.link=null;//temp alone
return temp;
}
public listnodes deletelast(listnodes head){
if(head==null){
return head;
}
listnodes last=head;
listnodes previoustolast=head;
while(last.link!=null){
previoustolast=last;
last=last.link;
}
previoustolast.link=null;
return last;
}
public int length(listnodes head){
listnodes curr=head;
int c=0;
while(curr!=null){
c++;
curr=curr.link;
}
return c;
}
public boolean find(listnodes head,int searchkey){
listnodes curr=head;
while(curr!=null){
if(curr.data==searchkey)
{
return true;
}
curr=curr.link;
}
return false;
}
}
public class Link {
public static void main(String[] args) {
//craete first node
listnodes head=new listnodes(10,null);
//create an object of class where all the methods are
singlelinklist sl=new singlelinklist();
//insert at postion
sl.InsertAtPostion(head, 30, 1);
//insert at front
listnodes newhead=sl.insert(head,20);
sl.display(newhead);
//delete last
System.out.println(" ");
System.out.println("Delete a node at end");
listnodes l=sl.deletelast(head);
sl.display(l);
System.out.println(" ");
//delete first
System.out.println("\nDelete a node at begining and return head:
\n");
listnodes first=sl.deletefirst(head);
sl.display(first);
System.out.println(" \n");
//find length
System.out.println("length is="+sl.length(head));
System.out.println(" ");
//Search a node
System.out.println("Search for a node");
if(sl.find(head, 10)){
System.out.println("key found");}
else
System.out.println("key not found");
}
}
public ListNode insertLast(ListNode head, int data) {
ListNode newnode = new ListNode(data, null);
if (head == null) {
return head;
}
ListNode last = head;
//traverse till the end.
while (last.link != null) {
last = last.link;
}
//point last node to new node.
last.link = newnode;
//return head
return head;
}
public void displayAllNodes(ListNode head) {
ListNode current = head;
while (current != null) {
//print node and point current to next.
System.out.println(current.data);
current = current.link;
}
}
//delete at a position(after or before a node)
public ListNode deleteAtPosition(ListNode head, int position) {
int currentPosition = 1;
ListNode current = head;
ListNode prev = head;
//move current to node to be deleted.
while (currentPosition < position) {
prev = current;
current = current.link;
currentPosition++;
}
//point previous link to current link. Now prev next is current next, meaning current is deleted.
prev.link = current.link;
return head;
}
Main function :
public static void main(String[] args) {
System.out.println("Creating a new list with node value 3081");
ListNode newHead = new ListNode(3081, null);
SingleLinkList sll = new SingleLinkList();
System.out.println("Inserting 3 numbers one by one in the end.");
sll.insertLast(newHead, 3082);
sll.insertLast(newHead, 3083);
sll.insertLast(newHead, 3084);
System.out.println("Displaying all numbers after 3 insertions.");
sll.displayAllNodes(newHead);
sll.deleteAtPosition(newHead, 2);
System.out.println("After deleting node at position 2 :");
sll.displayAllNodes(newHead);
}