linked list new version.
class Sll{ // user defined data structure
Node head;
Node tail;
int size;
void size(){
System.out.println("length of list is="+size);
}
void deleteAtIndex(int ix){
if(ix<0 || ix>=size)
throw new IndexOutOfBoundsException("invalid index");
if(ix==0){
deleteAtStart();
return;
}
if(ix==size-1){
deleteAtEnd();
return;
}
Node x=head;
for(int i=0; i<ix-1; i++){
x=x.next;
}
x.next=x.next.next;
size--;
}
void deleteAtEnd(){
if(head==null)
throw new IllegalStateException("list is already empty");
if(head==tail){
head=tail=null;
size--;
return;
}
Node x=head;
while(x.next!=tail){
x=x.next;
}
tail=x;
x.next=null;
size--;
}
void deleteAtStart(){
if(head==null)
throw new IllegalStateException("list is already empty");
if(head==tail){
head=tail=null;
size--;
return;
}
head=head.next;
size--;
}
void setElementAtIndex(int ix, int val){
if(ix<0 || ix>=size)
throw new IndexOutOfBoundsException("invalid index");
if(ix==0){
head.val=val;
return;
}
if(ix==size-1){
tail.val=val;
return;
}
Node x=head;
for(int i=0; i<ix; i++){
x=x.next;
}
x.val=val;
}
int getElementAtIndex(int ix){
if(ix<0 || ix>=size)
throw new IndexOutOfBoundsException("invalid index");
if(ix==0)
return head.val;
if(ix==size-1)
return tail.val;
Node x=head;
for(int i=0; i<ix; i++){
x=x.next;
}
return x.val;
}
void insertAtIndex(int ix,int val){
if(ix<0 || ix>size)
throw new IndexOutOfBoundsException("invalid index");
if(ix==0){
insertAtStart(val);
return;
}
if(ix==size){
insertAtEnd(val);
return;
}
Node temp = new Node(val);
Node x=head;
for(int i=0; i<ix-1; i++){
x=x.next;
}
temp.next=x.next;
x.next=temp;
size++;
}
void insertAtStart(int val){
Node temp=new Node(val);
if(head==null){
head=tail=temp;
size++;
return;
}
temp.next=head;
head=temp;
size++;
}
void insertAtEnd(int val){
Node temp = new Node(val);
if(head==null){
head=tail=temp;
size++;
return;
}
tail.next=temp;
tail=temp;
size++;
}
void print(){
Node temp=head;
while(temp!=null){
System.out.print(temp.val+" ");
temp=temp.next;
}
System.out.println();
}
}
public class Implementation {
public static void main(String[] args) {
Sll l = new Sll();
}
}
===================================================================================
class Node {
int val;
Node next;
Node(int val){
this.val=val;
}
}
public class ListNodeClass {
public static void printRevRec(Node head){
if(head==null)
return;
printRevRec(head.next);
System.out.print(head.val+" ");
}
public static void Print(Node head){
Node temp=head;
while(temp!=null){
System.out.print(temp.val+" ");
temp=temp.next;
}
System.out.println();
}
public static void printRec(Node head){
if(head==null)
return;
System.out.print(head.val+" ");
printRec(head.next);
}
public static void main(String[] args) {
Node a = new Node(10);
Node b = new Node(20);
Node c = new Node(30);
Node d = new Node(40);
Node e = new Node(50);
a.next=b; b.next=c;
c.next=d; d.next=e;
// System.out.print(a.val+" ");
// System.out.print(b.val+" ");
// System.out.print(c.val+" ");
// System.out.print(d.val+" ");
// System.out.print(e.val+" ");
// System.out.println(a.val);
// System.out.println(a.next.val);
// System.out.println(a.next.next.val);
// System.out.println(a.next.next.next.val);
// System.out.println(a.next.next.next.next.val);
//printRec(a);
//Print(a);
printRevRec(a);
}
}
Comments
Post a Comment