You are on page 1of 2

public class MySinglyLinkedCircularList {

static class Node


{
int getData;
Node getNext;
}
static Node addToEmpty(Node last, int data)
{
if (last != null)
return last;
Node node = new Node();
node.getData = data;
last = node;
last.getNext = last;
return last;
}

static Node addBegin(Node last, int data)


{
if (last == null)
return addToEmpty(last, data);

Node node = new Node();

node.getData = data;
node.getNext = last.getNext;
last.getNext = node;

return last;
}

static Node addEnd(Node last, int data)


{
if (last == null)
return addToEmpty(last, data);

Node node = new Node();

node.getData = data;
node.getNext = last.getNext;
last.getNext = node;
last = node;

return last;
}

static Node addAfter(Node last, int data, int item)


{
if (last == null)
return null;

Node temp, p;
p = last.getNext;
do
{
if (p.getData == item)
{
temp = new Node();
temp.getData = data;

This study source was downloaded by 100000851302356 from CourseHero.com on 08-30-2022 04:33:33 GMT -05:00

https://www.coursehero.com/file/106719559/MySinglyLinkedCircularListjava/
temp.getNext = p.getNext;
p.getNext = temp;

if (p == last)
last = temp;
return last;
}
p = p.getNext;
} while(p != last.getNext);

System.out.println(item + " not present in the list.");


return last;

static void traverse(Node last)


{
Node p;
if (last == null)
{
System.out.println("List is empty.");
return;
}
p = last.getNext;
do
{
System.out.print(p.getData + " ");
p = p.getNext;

}
while(p != last.getNext);

}
public static void main(String[] args)
{
Node add = null;
System.out.println("Singly Linked Circular List");
add = addToEmpty(add, 1);
add = addBegin(add, 2);
add = addBegin(add, 3);
add = addBegin(add, 4);
traverse(add);
}
}

This study source was downloaded by 100000851302356 from CourseHero.com on 08-30-2022 04:33:33 GMT -05:00

https://www.coursehero.com/file/106719559/MySinglyLinkedCircularListjava/
Powered by TCPDF (www.tcpdf.org)

You might also like