KEMBAR78
Circularlinkedlist | PDF | Information Technology | Computer Data
0% found this document useful (0 votes)
40 views10 pages

Circularlinkedlist

This document describes a Java program that implements a circular linked list data structure. It includes methods to create the list, display the list, insert nodes at the beginning, end, or after/before a specified node, delete nodes from the head, tail, or after/before a specified node, traverse the list, find a specified node, and replace a node's data. The main method runs an interactive menu that allows the user to call these methods and manipulate the circular linked list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views10 pages

Circularlinkedlist

This document describes a Java program that implements a circular linked list data structure. It includes methods to create the list, display the list, insert nodes at the beginning, end, or after/before a specified node, delete nodes from the head, tail, or after/before a specified node, traverse the list, find a specified node, and replace a node's data. The main method runs an interactive menu that allows the user to call these methods and manipulate the circular linked list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Circular linker list

Anuj Gupta 20BCE7055


package circularlinkerlist;
import java.util.Scanner;
class Node{
int data;
Node next;
}
class SLL{
static Node head=null,tail=null;
static Scanner sc=new Scanner(System.in static void create()
{

System.out.println("Enter total number of nodes");


int n=sc.nextInt();
System.out.println("Enter "+n+" data values");
for(int i=0;i<n;i++)
{
Node temp=new Node();

temp.data=sc.nextInt();
temp.next=null;
if(head==null)
head=tail=temp;
else{
if(tail.next=null) {
tail.next=temp;
tail=temp;
tail.next=head;}
}
}
}

static void display()


{
if(head==null)
System.out.println("List is Empty");
else
{
Node temp;
temp=head;
do {
System.out.println(" "+temp.data);
temp=temp.data;
}
while(temp!=head)
}
}
static void Ins_Begin()
{
Node temp=new Node();
System.out.println("Enter data");
temp.data=sc.nextInt();
temp.next=null;
if(head==null)
head=tail=temp;
else
{
temp.next=head;
head=temp;
tail.next=head;
}
}

static void Ins_End()


{
Node temp=new Node();
System.out.println("Enter data");
temp.data=sc.nextInt();
temp.next=null;
if(head==null)
head=tail=temp;
else
{
tail.next=temp;
tail=temp;
tail.next=head;
}
}

static void Ins_aft(int p)


{
Node c=head;
int i=1;
if(c.data!=p) {
c=c.next;
}
else
{i=i+1}
while(c!=head)
{
if(c.data==p) {
break;
}
c=c.next;
i++;
}
Node d=head;
int count=1;
Node temp=new Node();
System.out.println("Enter data");
temp.data=sc.nextInt();
if(i==1) {
temp.next=d.next;
d.next=temp;
}
else {d=d.next;
while(d!=head)
{
if(count==i-1)
break;
d=d.next;
count++;
}
if(d!=null)
{
temp.next=d.next;
d.next=temp;
if(tail==d)
tail=temp;
tail.next=head;
}
else
{
System.out.println("position not found");
}
}
}

static void Ins_bfr(int p)


{
Node c=head;
int i=1;
if(c.data!=p) {
c=c.next;
}
else
{i=i+1}
while(c!=head)
{
if(c.data==p) {
break;
}
c=c.next;
i++;
}
Node d=head;
int count=1;
Node temp=new Node();
System.out.println("Enter data");
temp.data=sc.nextInt();
if(i==1) {
head.next=temp.next;
head=temp
tail.next=head;
}
else {d=d.next;
while(d!=head)
{
if(count==i-1)
break;
d=d.next;
count++;
}
if(d!=null)
{
temp.next=d.next;
d.next=temp;
if(tail==d)
tail=temp;
tail.next=head;
}
else
{
System.out.println("position not found");
}
}
}

static void del_head()


{
Node temp=head;
if(head==tail)
head=tail=null;
else
{
head=head.next;
temp.next=null;
temp=null;
tail.next=head;
}

static void del_tail()


{
Node temp=head;
if(head==tail)
head=tail=null;
else
{
while(temp.next!=tail)
{
temp=temp.next;
}
tail=temp;
tail.next=head;
}
}

static void del_aft(int p)

{
Node c=head;
int i=1;
if(c.data!=p) {
c=c.next;
}
else
{i=i+1}
while(c!=head)
{
if(c.data==p) {
break;
}
c=c.next;
i++;
}
Node d=head,prev=null;
if(i=1) {
head=head.next;
tail.next=head;}
else {
d=d.next;
int count=1;
while(d!=head)
{
if(count==i+1)
break;
prev=d;
d=d.next;
count++;
}
if(d!=null)
{
if(head==tail)
head=tail=null;
else
{
prev.next=c.next;
if(d==tail)
tail=prev;
tail.next=head;
d=null;
}
}
else
{
System.out.println("Invalid position");
}
}
}

static void del_bfr(int p)

{
Node c=head;
int i=1;
if(c.data!=p) {
c=c.next;
}
else
{i=i+1}
while(c!=head)
{
if(c.data==p) {
break;
}
c=c.next;
i++;
}
Node d=head,prev=null;
if(i=1) {
head=head.next;
tail.next=head;}
else {
d=d.next;
int count=1;
while(d!=head)
{
if(count==i-1)
break;
prev=d;
d=d.next;
count++;
}
if(d!=null)
{
if(head==tail)
head=tail=null;
else
{
prev.next=c.next;
if(d==tail)
tail=prev;
tail.next=head;
d=null;
}
}
else
{
System.out.println("Invalid position");
}
}
}

static void del_gvn(int p)

{
Node c=head;
int i=1;
if(c.data!=p) {
c=c.next;
}
else
{i=i+1}
while(c!=head)
{
if(c.data==p) {
break;
}
c=c.next;
i++;
}
Node d=head,prev=null;
if(i=1) {
head=head.next;
tail.next=head;}
else {
d=d.next;
int count=1;
while(d!=head)
{
if(count==i)
break;
prev=d;
d=d.next;
count++;
}
if(d!=null)
{
if(head==tail)
head=tail=null;
else
{
prev.next=c.next;
if(d==tail)
tail=prev;
tail.next=head;
d=null;
}
}
else
{
System.out.println("Invalid position");
}
}
}
static void transverse()
{
if(head==null)
System.out.println("List is Empty");
else
{
Node temp;
temp=head;
do {
System.out.println(" "+temp.data);
temp=temp.data;
}
while(temp!=head)
}
}

static void find_gvn(int key)


{
org.w3c.dom.Node c= head;
int i=1;
if(c.data==head) {
System.out.println(c.data+" found at "+i+" ");
}
c=head.next;
else {
while(c!=head) {
if(c.data==key) {
System.out.println(c.data+" found at "+i+" ");
break;
}
c=c.next;
}
}

static void replace(key,p) {


Node c =head;
int i=1;
while(c!=null) {
if(c.data==key) {
c.data=null;
c.data=p;
break;
}
i++;
c=c.next;
}
if(c.data==head)
head=p;
tail.next=head;
else if(c.data==tail)
tail=p;
tail.next=head;
}
}
}

public static void main(String args[]){

create();
display_fwd();
int ch,p;
while(true)
{
System.out.println("Enter choice \n 1.Insert at Begining \n 2. Insert at
End\n 3. Insert after a given node \n 4.Insert before a given node 5.Delete Head \n
6.Delete Tail \n 7. Delete after the given node \n 8.Delete before the given node\n
9.Delete a given node\n 10. Transverse list\n 11. Find a specific node\n 12.
Replace a specific node\n 13. Exit");
ch=sc.nextInt();
switch(ch)
{
case 1: Ins_Begin();
display();
break;

case 2: Ins_end();
display();
break;

case 3: System.out.println("Enter the data after which you want to


insert node");
int p=sc.nextInt();
Ins_aft(p);
display();
break;

case 4: System.out.println("Enter the data before which you want to


insert node");
int p=sc.nextInt();
Ins_bfr(p);
display();
break;

case 5: del_head()
display();
break;

case 6: del_end()
display();
break;

case 7: System.out.println("Enter the data after which you want to


delete node");
int p=sc.nextInt();
del_aft(p);
display();
break;
case 8: System.out.println("Enter the data after which you want to
delete node");
int p=sc.nextInt();
del_bfr(p);
display();
break;

case 9: System.out.println("Enter the data you want to delete


node");
int p=sc.nextInt();
del_gvn(sc.nextInt);
display();
break;

case 10: traverse();


break;

case 11: find_gvn();


break;

case 12: System.out.println("Enter the recent data of that node you


want to replace ");
int key=sc.nextInt();
System.out.println("Enter the new data of that node you
wnat to replace");
int p=sc.nextInt();
replace(key,p);
display();
break;

case 13: Exit(0);


break;

default System.out.println("Invalid choice");


break;
}
}
}

You might also like