In: Computer Science
class listnodes{
int data;//first part of the node(data)
listnodes link;//second part of the node(address)
listnodes()
{
data=0;
link=null;
}
listnodes(int d,listnodes l)//10|null
{
data=d;
link=l;
}
}
class singlelinkedlist{
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 linkedlist {
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
singlelinkedlist sl=new singlelinkedlist();
//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");
}
}
ON THIS CODE
Write an application and perform the following:
-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
updated singlelinkedlist.java
Add insertLast and deleteAtPosition in this class
class singlelinkedlist {
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 last
public listnodes inserLast(listnodes head , int data) {
listnodes temp=head;
while(temp.link!=null)
temp=temp.link;
listnodes newnode=new listnodes(data,null);
temp.link=newnode;
return head;
}
//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 listnodes deleteAtPosition(listnodes head,int position) {
int i=1;
listnodes temp=head;
if(position==1)
deletefirst(head);
else {
while(position-1>i) {
temp=temp.link;
i++;
}
temp.link=temp.link.link;
}
return head;
}
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;
}
}
updated linkedlist.java
for testing purpose added code here :
import java.util.Scanner;
public class linkedlist {
public static void main(String[] args) {
//craete first node
//create an object of class where all the methods are
singlelinkedlist s1 = new singlelinkedlist();
listnodes head = new listnodes(10,null);
s1.inserLast(head,20);
s1.inserLast(head,30);
s1.inserLast(head,20);
s1.inserLast(head,30);
s1.display(head);
System.out.println("\n==========================");
Scanner sc=new Scanner(System.in);
System.out.println("Enter position from where elements is to be deleted ");
int position=sc.nextInt();
s1.deleteAtPosition(head,position);
s1.display(head);
}
}
Sample outputs :