0% found this document useful (0 votes)
15 views4 pages

SLL

The document contains Java code for a singly linked list (SLL) implementation with various methods for managing nodes. Key functionalities include adding, deleting, and searching for nodes, as well as displaying the list and retrieving the first and last elements. The SNode class represents individual nodes containing data and a reference to the next node.

Uploaded by

yosrayassin04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

SLL

The document contains Java code for a singly linked list (SLL) implementation with various methods for managing nodes. Key functionalities include adding, deleting, and searching for nodes, as well as displaying the list and retrieving the first and last elements. The SNode class represents individual nodes containing data and a reference to the next node.

Uploaded by

yosrayassin04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

public class SNode

{
int data;
SNode next;

public SNode (int data, SNode f)


{
this.data=data;
next = f;
}

public SNode(int data)


{
data=data;
}
}

public class SLL


{
SNode head;

public SLL()
{
head = null;

public boolean isEmpty()


{
return head == null;

public void delAll()


{
head = null;

public void display()


{
if(!isEmpty())
{
SNode p = head;
while (p!=null)
{
System.out.println(p.data);
}

p=p.next;
}
}

public int length()


{
int c= 0;
SNode p = head;
while (p!=null)
{
c++;
}
p=p.next;
return c;
}

public boolean search(int d)


{
if(!isEmpty())
{
SNode p = head;
while (p!=null) {
if (p.data == d)
{
return true;
}
p=p.next;
}
}
return false;
}

public int getFirst ()


{
if(!isEmpty())
{
return head.data;
}
else
return -9999;
}

public int getLast ()


{
if(!isEmpty())
{
SNode p = head;
while (p.next!=null)
{
p=p.next;
}
return p.data;
}
return -9999;
}
public void addFirst (int data)
{
SNode z= new SNode(data);
z.next=head;
head=z;
}

public void addLast (int data)


{
SNode g = new SNode(data);
if(!isEmpty())
{
SNode p = head;
while(p.next!=null)
{
p=p.next;
}
head =g;
}
}

public int addIn (int d, int after)


{
SNode z= new SNode(d);
SNode p = head;
while (p!=null)
{
if(p.data==after)
{
break;
}
else
p=p.next;
}
if (p!=null)
{
z.next=p.next;
p.next=z;
}
else
addLast(d);
return d;
}

public void delFirst()


{
head=head.next;

public void delLast()


{
SNode p =head;
if (!isEmpty())
{
if(p.next!=null)
{
p =head;
}
while (p.next.next!=null)
{
p=p.next;
}
p.next=null;
}
else
head = null;
}

You might also like