import java.util.Scanner;
class SLLNode
{
int data;
SLLNode next;
}
class SLL
{
SLLNode head=null,tail=null;
public void create(int val)
{
SLLNode newnode = new SLLNode();
newnode.data=val;
newnode.next=null;
if(head == null)
head=tail=newnode;
else
{
tail.next=newnode;
tail=newnode;
}
}
public void display()
{
if(head == null)
System.out.println("no SLL");
else
{
SLLNode c=head;
while(c != null)
{
System.out.print(c.data+"->");
c=c.next;
}
}
}
public void insertBeg(int val)
{
SLLNode newnode = new SLLNode();
newnode.data=val;
newnode.next=null;
if(head == null)
head=tail=newnode;
else
{
newnode.next=head;
head=newnode;
}
}
public void insertMiddle(int val,int pos)
{
SLLNode newnode = new SLLNode();
newnode.data=val;
newnode.next=null;
SLLNode c = head;
for(int i=1;i<pos-1;i++)
c=c.next;
newnode.next=c.next;
c.next=newnode;
}
public void delEnd()
{
SLLNode c = head;
while(c.next != null)
c=c.next;
System.out.println("deleted element = "+c.data);
c.next=null;
}
public void delMiddle(int pos)
{
SLLNode c = head;
for(int i=1;i<pos-1;i++)
c=c.next;
System.out.println("deleted element = "+c.next.data);
c.next=c.next.next;
}
public void delBeg()
{
if(head != null)
{
System.out.println("deleted element = "+head.data);
head=head.next;
}
}
}
public class SLLDemo
{
public static void main(String args[])
{
SLL ob = new SLL();
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("1. InsertEnd/Create 2.InserBeg 3. InsertMiddle");
System.out.println("4. DeleteEnd 5.DeleteBeg 6. DeleteMiddle");
System.out.println("7. Display 8.Exit");
int choice=sc.nextInt();
switch(choice)
{
case 1:
case 2:
case 3:
System.out.println("");
int choice1=sc.nextInt();
System.out.println("Enter data to be inserted");
int val = sc.nextInt();
if(choice == 1)
ob.create(val);
else if(choice == 2)
ob.insertBeg(val);
else
{
System.out.println("Enter position");
int pos=sc.nextInt();
ob.insertMiddle(val,pos);
}
break;
case 4: ob.delEnd();
break;
case 5: ob.delBeg();
break;
case 6:
System.out.println("Enter position");
int pos=sc.nextInt();
ob.delMiddle(pos);
break;
case 7:
ob.display();
break;
default:
System.exit(0);
}
}
}
}
===========================================
import java.util.Scanner;
class SLLNode
{
int data;
SLLNode next;
}
class SLL
{
SLLNode head=null,tail=null;
public void create(int val)
{
SLLNode newnode = new SLLNode();
newnode.data=val;
newnode.next=null;
if(head == null)
head=tail=newnode;
else
{
tail.next=newnode;
tail=newnode;
}
}
public void display()
{
if(head == null)
System.out.println("no SLL");
else
{
SLLNode c=head;
while(c != null)
{
System.out.print(c.data+"->");
c=c.next;
}
}
}
public void insertBeg(int val)
{
SLLNode newnode = new SLLNode();
newnode.data=val;
newnode.next=null;
if(head == null)
head=tail=newnode;
else
{
newnode.next=head;
head=newnode;
}
}
public void insertMiddle(int val,int pos)
{
SLLNode newnode = new SLLNode();
newnode.data=val;
newnode.next=null;
SLLNode c = head;
for(int i=1;i<pos-1;i++)
c=c.next;
newnode.next=c.next;
c.next=newnode;
}
}
public class SLLDemo
{
public static void main(String args[])
{
SLL ob = new SLL();
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("1. Insert 2. Display 3.Exit");
int choice=sc.nextInt();
switch(choice)
{
case 1:
System.out.println("1. InsertAtEnd 2.InsertAtBeg 3. InsertAtMiddle");
int choice1=sc.nextInt();
System.out.println("Enter data to be inserted");
int val = sc.nextInt();
switch(choice1)
{
case 1: ob.create(val);
break;
case 2: ob.insertBeg(val);
break;
case 3: System.out.println("Enter position");
int pos=sc.nextInt();
ob.insertMiddle(val,pos);
break;
}
break;
case 2: ob.display();
break;
default:
System.exit(0);
}
}
}
}
class SLLNode
{
int data;
SLLNode next;
}
class SLL
{
SLLNode head=null,tail=null;
public void create(int val)
{
SLLNode newnode = new SLLNode();
newnode.data=val;
newnode.next=null;
if(head == null)
head=tail=newnode;
else
{
tail.next=newnode;
tail=newnode;
}
}
public void display()
{
if(head == null)
System.out.println("no SLL");
else
{
SLLNode c=head;
while(c != null)
{
System.out.print(c.data+"->");
c=c.next;
}
}
}
public void insertBeg(int val)
{
SLLNode newnode = new SLLNode();
newnode.data=val;
newnode.next=null;
if(head == null)
head=tail=newnode;
else
{
newnode.next=head;
head=newnode;
}
}
public void insertMiddle(int val,int pos)
{
SLLNode newnode = new SLLNode();
newnode.data=val;
newnode.next=null;
SLLNode c = head;
for(int i=1;i<pos-1;i++)
c=c.next;
newnode.next=c.next;
c.next=newnode;
}
public void delEnd()
{
SLLNode c = head;
while(c.next != null)
c=c.next;
System.out.println("deleted element = "+c.data);
c.next=null;
}
public void delMiddle(int pos)
{
SLLNode c = head;
for(int i=1;i<pos-1;i++)
c=c.next;
System.out.println("deleted element = "+c.next.data);
c.next=c.next.next;
}
public void delBeg()
{
if(head != null)
{
System.out.println("deleted element = "+head.data);
head=head.next;
}
}
}
public class SLLDemo
{
public static void main(String args[])
{
SLL ob = new SLL();
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("1. InsertEnd/Create 2.InserBeg 3. InsertMiddle");
System.out.println("4. DeleteEnd 5.DeleteBeg 6. DeleteMiddle");
System.out.println("7. Display 8.Exit");
int choice=sc.nextInt();
switch(choice)
{
case 1:
case 2:
case 3:
System.out.println("");
int choice1=sc.nextInt();
System.out.println("Enter data to be inserted");
int val = sc.nextInt();
if(choice == 1)
ob.create(val);
else if(choice == 2)
ob.insertBeg(val);
else
{
System.out.println("Enter position");
int pos=sc.nextInt();
ob.insertMiddle(val,pos);
}
break;
case 4: ob.delEnd();
break;
case 5: ob.delBeg();
break;
case 6:
System.out.println("Enter position");
int pos=sc.nextInt();
ob.delMiddle(pos);
break;
case 7:
ob.display();
break;
default:
System.exit(0);
}
}
}
}
import java.util.Scanner;
class SLLNode
{
int data;
SLLNode next;
}
class SLL
{
SLLNode head=null,tail=null;
public void create(int val)
{
SLLNode newnode = new SLLNode();
newnode.data=val;
newnode.next=null;
if(head == null)
head=tail=newnode;
else
{
tail.next=newnode;
tail=newnode;
}
}
public void display()
{
if(head == null)
System.out.println("no SLL");
else
{
SLLNode c=head;
while(c != null)
{
System.out.print(c.data+"->");
c=c.next;
}
}
}
public void insertBeg(int val)
{
SLLNode newnode = new SLLNode();
newnode.data=val;
newnode.next=null;
if(head == null)
head=tail=newnode;
else
{
newnode.next=head;
head=newnode;
}
}
public void insertMiddle(int val,int pos)
{
SLLNode newnode = new SLLNode();
newnode.data=val;
newnode.next=null;
SLLNode c = head;
for(int i=1;i<pos-1;i++)
c=c.next;
newnode.next=c.next;
c.next=newnode;
}
}
public class SLLDemo
{
public static void main(String args[])
{
SLL ob = new SLL();
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("1. Insert 2. Display 3.Exit");
int choice=sc.nextInt();
switch(choice)
{
case 1:
System.out.println("1. InsertAtEnd 2.InsertAtBeg 3. InsertAtMiddle");
int choice1=sc.nextInt();
System.out.println("Enter data to be inserted");
int val = sc.nextInt();
switch(choice1)
{
case 1: ob.create(val);
break;
case 2: ob.insertBeg(val);
break;
case 3: System.out.println("Enter position");
int pos=sc.nextInt();
ob.insertMiddle(val,pos);
break;
}
break;
case 2: ob.display();
break;
default:
System.exit(0);
}
}
}
}
1 comment: