XOR Linked List

Saturday, 9 August 2014
It is a doubly linked list with an advantage of storage requirements than the previous by storing only one pointer. The pointer stores the XOR of the previous and the next node.
 ...  A        B         C         D         E  ...
         <–>  A⊕C  <->  B⊕D  <->  C⊕E  <-> 
For traverisng the list in the forward direction i.e. if you are in C and you need the address of D you can XOR the location of C with the location of B giving you the location of D.Similarly in the case of moving backward to get the location of B we can XOR the location of C and D. Notice however that we always need two locations to start traversing. Hence even though the storage requirements are reduced a number of fallbacks exist between this and the doubly linked lists.

The disadvantages of using a XOR linked list include :
1. Less space requirement increases code complexity
2. Garbage collection schemes wont work with data structures that donot contain literal pointers
3. XOR of pointers is not defined in some context as in C hence need to convert them to integers and store
4. Ability to delete a node knowing only the address of the node is not present
5. Ability to insert a node knowing only its address is not present

The program below shows a doubly linked list using a single pointer to the next node with add and display functions.
 
#include<iostream>
#include<cstdlib>
using namespace std;

struct Node {
 int data;
 unsigned int next;
}*start,*end,*newNode;

typedef struct Node NODE;

int menu(){
 int choice;
 cout << "1. Add" << endl;
 cout << "2. Display" << endl;
 cout << "3. Exit" << endl;
 cin >> choice;
 return choice;
}

void add(){
 newNode = (NODE *)malloc(sizeof(NODE));
 newNode->next = 0;
 cout << "Enter nodes data:" ;
 cin >> (newNode->data);
 if(start==NULL){
  start = end = newNode;
 }else{
  newNode->next = (unsigned int)end ^ 0;
  end->next = (end->next^0)^(unsigned int)newNode;
  end=newNode;
 }
}

void display(){
 NODE *temp,*curr,*prev;
 prev = curr = temp = NULL;
 for ( curr = start; curr != end ; temp = prev ,\
    prev = curr , curr = ( NODE * ) ( curr -> next ^ (unsigned int)temp ) )
       cout << curr -> data << endl;
    cout << curr -> data << endl;
}

int main()
{
 int choice;
 while(1){
  choice = menu();
  if(choice==3)
   break;
  switch(choice){
  case 1: add();break;
  case 2: display();break;
  }
 }
 return 0;
} 

Creating AVL Tree In Java

Friday, 12 October 2012

 Complete implementation only without the delete method ... If anyone wants it i will post it later in the comments :


import java.io.*;
class Node
{
int data;
Node leftchild;
Node rightchild;
Node parent;
public Node(int d)
{
    data=d;
    leftchild=null;
    rightchild=null;
}
public int get_data()
{
    return data;
}
public Node get_leftchild()
{
    return leftchild;
}
public Node get_rightchild()
{
    return rightchild;
}
public void set_data(int d)
{
    data=d;
}
public void set_leftchild(Node n)
{
    leftchild=n;
}
public void set_rightchild(Node n)
{
    rightchild=n;
}
public void set_parent(Node n)
{
    parent=n;
}
public Node get_parent()
{
    return parent;
}
}

//end of the node class

//start
class Avl
{
Node root;
public Avl()
{
    root=null;
}

public int max(int a,int b)
{
    if(a>b)
    return a;
    else
    return b;
}

public int get_height(Node n)
{
    if(n==null)
    return 0;
    else
    return max(get_height(n.get_leftchild()),get_height(n.get_rightchild()))+1;
}

public void left_left_rot(Node k2)
{
    Node k1=k2.get_leftchild();
    Node B=k1.get_rightchild();
    Node gp=k2.get_parent();
    if(gp!=null)
    {
        if(gp.get_rightchild()==k2)
        {
            gp.set_rightchild(k1);
            k1.set_parent(gp);
        }
        else
        {
            gp.set_leftchild(k1);
            k1.set_parent(gp);
        }
    }
    else
    {
    k1.set_parent(null);
    }
    k1.set_rightchild(k2);
    if(k1!=null)
    k2.set_parent(k1);
    k2.set_leftchild(B);
    if(B!=null)
    B.set_parent(k2);
}

public void right_right_rot(Node x)
{
    Node y=x.get_rightchild();
    Node B=y.get_leftchild();
    Node gp=x.get_parent();
    if(gp.get_rightchild()==x)
    {
        gp.set_rightchild(y);
        y.set_parent(gp);
        }
    else
    {
        gp.set_leftchild(y);
        y.set_parent(gp);
    }
    y.set_leftchild(x);
    x.set_parent(y);
    x.set_rightchild(B);
    if(B!=null)
    B.set_parent(x);
}

public void left_right_rot(Node k3)
{
    Node k1=k3.get_leftchild();
    Node k2=k1.get_rightchild();
    Node B=k2.get_leftchild();
    Node C=k2.get_rightchild();
    Node par=k3.get_parent();
    if(par.get_leftchild()==k3)
    par.set_leftchild(k2);
    else
    par.set_rightchild(k2);
    if(k2!=null)
    k2.set_parent(par);
    k2.set_leftchild(k1);
    if(k1!=null)
    k1.set_parent(k2);
    k2.set_rightchild(k3);
    if(k3!=null)
    k3.set_parent(k2);
    k1.set_rightchild(B);
    k3.set_leftchild(C);
    if(B!=null)
    B.set_parent(k1);
    if(C!=null)
    C.set_parent(k3);
}

public void right_left_rot(Node k1)
{
    Node k3=k1.get_rightchild();
    Node k2=k3.get_leftchild();
    Node C=k2.get_rightchild();
    Node B=k2.get_leftchild();
    Node par=k1.get_parent();
    if(par.get_leftchild()==k1)
    par.set_leftchild(k2);
    else
    par.set_rightchild(k2);
    if(k2!=null)
    k2.set_parent(par);
    k2.set_rightchild(k3);
    k2.set_leftchild(k1);
    if(k3!=null)
    k3.set_parent(k2);
    if(k1!=null)
    k1.set_parent(k2);
    k1.set_rightchild(B);
    k3.set_leftchild(C);
    if(B!=null)
    B.set_parent(k1);
    if(C!=null)
    C.set_parent(k3);
}
   

public int balance_factor(Node n)
{
    Node n1=n.get_rightchild();
    Node n2=n.get_leftchild();
    int h1,h2;
    h1=get_height(n1);
    h2=get_height(n2);
    return (h2-h1);
}
public Node returnUnbalanced(Node n)
{
    Node unbal=null;
    int checkflag=1;
    if(get_height(n.get_parent())==1)
    return unbal;
    int bf;
    while(n.get_parent()!=null)
    {
        n=n.get_parent();
        bf=balance_factor(n);
        if(checkflag==1)
        if((bf==2)||(bf==-2))
        {
            unbal=n;
            checkflag=0;
        }
    }
    return unbal;
}


   
public void insert(Node n,Node bs)
{
    if(bs==null)
    {
        root=n;
    }
    else
    {
        if(bs.get_data()>n.get_data())
        {
            if(bs.get_leftchild()==null)
            {
                bs.set_leftchild(n);
                n.set_parent(bs);
                Node unbal=returnUnbalanced(n);
                if(unbal!=null)
                {//balance it
                    int bf=balance_factor(unbal);
                    System.out.println("node ::"+unbal.get_data());
                    if(bf==2)
                    {
                        int bf2=balance_factor(unbal.get_leftchild());
                        if(bf2==1)
                        left_left_rot(unbal);
                        if(bf2==-1)
                        left_right_rot(unbal);
                    }
                    if(bf==-2)
                    {
                        int bf2=balance_factor(unbal.get_rightchild());
                        if(bf2==1)
                        right_left_rot(unbal);
                        if(bf2==-1)
                        right_right_rot(unbal);
                    }
                }
            }
            else
            insert(n,bs.get_leftchild());
        }
        else
        {
            if(bs.get_rightchild()==null)
            {
                bs.set_rightchild(n);
                n.set_parent(bs);
                Node unbal=returnUnbalanced(n);
                if(unbal!=null)
                {//perform the req bal op
                    int bf=balance_factor(unbal);
                    System.out.println("node::"+unbal.get_data());
                    if(bf==2)
                    {
                        int bf2=balance_factor(unbal.get_leftchild());
                        if(bf2==1)
                        left_left_rot(unbal);
                        if(bf2==-1)
                        left_right_rot(unbal);
                    }
                    if(bf==-2)
                    {
                        int bf2=balance_factor(unbal.get_rightchild());
                        if(bf2==1)
                        right_left_rot(unbal);
                        if(bf2==-1)
                        right_right_rot(unbal);
                    }
                }
            }
            else
            insert(n,bs.get_rightchild());
        }
    }
}

public void search(int n,Node bs)
{
    if(bs==null)
    System.out.println("not present in tree");
    else
    {
        if(bs.get_data()==n)
        System.out.println("Node is present in Tree");
        else if(bs.get_data()>n)
             search(n,bs.get_leftchild());
             else
             search(n,bs.get_rightchild());
    }
}

public Node findmin(Node bs)
{
    if(bs!=null)
        while(bs.get_leftchild()!=null)
        bs=bs.get_leftchild();
    return bs;
}

public Node findmax(Node bs)
{
    if(bs!=null)
        while(bs.get_rightchild()!=null)
        bs=bs.get_rightchild();
    return bs;
}

public void del(int n,Node bs)
{
    if(bs==null)
    System.out.println("node not found in tree");
    else
    {
        if(bs.get_data()==n)
        {
        //do the three cases
            //case 1.Node has no children;
            if((bs.get_leftchild()==null)&&(bs.get_rightchild()==null))
            {
                Node par=bs.get_parent();
                if(par.get_rightchild()==bs)
                par.set_rightchild(null);
                else
                par.set_leftchild(null);
            }
            //case 2.if Node has 1 child
            else
            {
                if((bs.get_leftchild()==null)||(bs.get_rightchild()==null))
                {
                    //System.out.println("here");
                    Node par=bs.get_parent();
                    Node child;
                    if(bs.get_leftchild()==null)
                    child=bs.get_rightchild();
                    else
                    child=bs.get_leftchild();
                    if(par.get_leftchild()==bs)
                    par.set_leftchild(child);
                    else
                    par.set_rightchild(child);
                }
                else //Node has 2 children
                {
                    Node min_r=findmin(bs.get_rightchild());
                    int tset=min_r.get_data();
                    //min would be that node itself or left leaf
                    Node par=min_r.get_parent();
                    if(par==bs)
                    {
                        bs.set_rightchild(min_r.get_rightchild());
                        bs.set_data(tset);
                    }
                    else
                    {
                        par.set_leftchild(null);
                        bs.set_data(tset);
                    }
                   
                }
            }
        }
        else if(bs.get_data()>n)
            del(n,bs.get_leftchild());
             else
            del(n,bs.get_rightchild());
    }
}

public void preorder(Node bs)
{
    if(bs==null)
    return;
    else
    {
        System.out.print(bs.get_data()+" ");
        preorder(bs.get_leftchild());
        preorder(bs.get_rightchild());
    }
}

public void postorder(Node bs)
{
    if(bs==null)
    return;
    else
    {
        postorder(bs.get_leftchild());
        postorder(bs.get_rightchild());
        System.out.print(bs.get_data()+" ");
    }
}

public void inorder(Node bs)
{
    if(bs==null)
    return;
    else
    {
        inorder(bs.get_leftchild());
        System.out.print(bs.get_data()+" ");
        inorder(bs.get_rightchild());
    }
}
}

class check
{
public static void main(String[] args)throws IOException
{
    FileReader fr=new FileReader("input.txt");
    BufferedReader br=new BufferedReader(fr);
    InputStreamReader isr=new InputStreamReader(System.in);
    BufferedReader br1=new BufferedReader(isr);
    String line=br.readLine();
    String[] numS=line.split(" ");
    int[] numbers=new int[numS.length];
    //create the bst
    Avl tree=new Avl();
    //input the elements
    for(int i=0;i<numS.length;i++)
    {
        numbers[i]=Integer.parseInt(numS[i]);
        Node n=new Node(numbers[i]);
        tree.insert(n,tree.root);
    }
    //print the preorder
    System.out.println("preorder traversal ::");
    tree.preorder(tree.root);
    System.out.println("\nPostorder traversal::");
    tree.postorder(tree.root);
    System.out.println("\ninorder traversal ::");
    tree.inorder(tree.root);
    //options
    Node nn;
    int d,s;
    while(true)
    {
    System.out.println("\n1.tree height\n2.Search(int)\n3.findmin()\n4.findmax()\n5.preorder()\n6.postorder()\n7.inorder()\n8.exit\n");
    int input=Integer.parseInt(br1.readLine());
    switch(input)
    {
        /*case 1: d=Integer.parseInt(br1.readLine());
            tree.del(d,tree.root);
            break;*/
        case 2: s=Integer.parseInt(br1.readLine());
            tree.search(s,tree.root);
            break;
        case 3: nn=tree.findmin(tree.root);
            System.out.println(nn.get_data());
            break;
        case 4: nn=tree.findmax(tree.root);
            System.out.println(nn.get_data());
            break;
        case 5:tree.preorder(tree.root);
            System.out.println();
            break;
        case 6:tree.postorder(tree.root);
            System.out.println();
            break;
        case 7:tree.inorder(tree.root);
            System.out.println();
            break;
        case 1:System.out.println(tree.get_height(tree.root));
            break;
        case 8:System.exit(1000);
            break;
        default:System.out.println("Not an option");
            break;
    }
    }
}
}

BASIC IMPLIMENTATION OF LINKED LIST IN JAVA

Friday, 24 August 2012
import java.io.*;
class Node
{
    int data;
    Node next;
    public Node(int d)
    {
        next=null;
        data=d;
    }
    public Node(int d,Node n)
    {
        next=n;
        data=d;
    }
    public void set_data(int d)
    {
        data=d;
    }
    public int get_data()
    {
        return data;
    }
    public void set_next(Node d)
    {
        next=d;
    }
    public Node get_next()
    {
        return next;
    }
}

//implimenting linked list class
class Linkedlist
{
    Node head;
    public Linkedlist()
    {
    head=null;
    }
    public void add(int pos,Node n)
    {
    //pos is the position
    //0 is at the beginning
    if(pos==0)
    {
        if(head==null)
        head=n;
        else
        {
        Node temp=new Node(n.get_data(),head);
        head=temp;
        }
    }
    else
    {
        Node temp=head;
        int count=1;
        int flagfound=0;
        while(temp.get_next()!=null)
        {
            if(count==pos)
            {
                flagfound=1;
                break;
            }
            count++;
            temp=temp.get_next();
        }
        if(flagfound==0)
        {
            System.out.println("problem");
        }
        else
        {
            Node temp1=temp.get_next();
            temp.set_next(n);
            n.set_next(temp1);
        }
    }
    }
    //finished add method
    public void del(int d)
    {
        Node temp=head;
        //check if list is empty
        if(head==null)
        {
            System.out.println("List is empty");
            return;
        }
        //if first node
        if(temp.get_data()==d)
        {
        head=head.get_next();
        return;
        }
        //else if not the first node
        int flag=0;
        while(temp.get_next()!=null)
        {
            if(temp.get_next().get_data()==d)
            {
                flag=1;
                break;   
            }
            else
            {
                temp=temp.get_next();
            }
        }
        if(flag==1)
        {
            Node temp2=temp.get_next().get_next();
            temp.set_next(temp2);
        }
        else
        System.out.println("not found");
    }
    //finished del method

    public void search(int d)
    {
        Node temp=head;
        if(head==null)
        {
            System.out.println("List is empty");
            return;
        }
        //if first node
        int count=1;
        if(head.get_data()==d)
        {
            System.out.println("Node found. Node number= "+count);
            return;
        }
        int flag=0;
        while(temp.get_next()!=null)
        {
            temp=temp.get_next();   
            count++;
            if(temp.get_data()==d)
            {
                flag=1;
                System.out.println("Node found. Node number= "+count);
            }
        }
        if(flag==0)
        {
        System.out.println("Not found in list");
        }
       
    }
    //writing update method
   
    public void update(int olddata,int newdata)
    {
        Node temp=head;
                if(head==null)
                {
                        System.out.println("List is empty");
                        return;
                }
                //if first node
                int count=1;
                if(head.get_data()==olddata)
                {
                        System.out.println("Node found. Node number= "+count);
            head.set_data(newdata);
                        return;
                }
                int flag=0;
                while(temp.get_next()!=null)
               {
                        temp=temp.get_next();
                        count++;
                        if(temp.get_data()==olddata)
                        {
                                flag=1;
                temp.set_data(newdata);
                                System.out.println("Node found. Node number= "+count);
                        }
                }
        if(flag==0)
        System.out.println("node not found");
    }
   
    //writing display method
    public void display()
    {
        //if empty
        if(head==null)
        System.out.println("empty list");   
        else
        {
            Node temp=head;
            System.out.println("Printing the list:");
            System.out.print(head.get_data()+", ");
            while(temp.get_next()!=null)
            {
                temp=temp.get_next();
                System.out.print(temp.get_data()+", ");
            }
        }
    }
}

class check
{
    public static void main(String[] args)throws IOException
    {
        InputStreamReader isr=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(isr);
        //create a linkedlist
        Linkedlist ll=new Linkedlist();
        while(true)
        {
        System.out.println("\nWelcome to world of linked lists.Choose an option :");
        System.out.println("1. add(data,pos);");
        System.out.println("2. delete(int d)");
        System.out.println("3. search(int d);");
        System.out.println("4. update(int old,int new)");
        System.out.println("5. display");
        System.out.println("6. exit");
        int n1,n2;
        int ch=Integer.parseInt(br.readLine());
        switch(ch)
        {
        case 1:System.out.println("Enter the data and then pos");
            n1=Integer.parseInt(br.readLine());
            n2=Integer.parseInt(br.readLine());
            Node nn=new Node(n1);
            ll.add(n2,nn);
            break;
        case 2:System.out.println("Enter data");
            n1=Integer.parseInt(br.readLine());
            ll.del(n1);
            break;
        case 3:System.out.println("Enter data");
             n1=Integer.parseInt(br.readLine());
            ll.search(n1);
            break;
        case 4:System.out.println("Enter old data and then new data");
             n1=Integer.parseInt(br.readLine());
             n2=Integer.parseInt(br.readLine());
            ll.update(n1,n2);
            break;
        case 5:ll.display();
            break;
        case 6:System.exit(500);
        default: System.out.println("wrong option try again");
        }
        }
    }
}   

Implimentation of doubly linked lists in C

Sunday, 11 March 2012
this is my implimentation :
#include
#include
struct node{
struct node *prev;
struct node *next;
int info;
}*start;

int length()
{
if(start==NULL)
return 0;
int count=1;
struct node *q;
q=start;
while(q->next!=NULL)
{
count++;
q=q->next;
}
return count;
}

void add_beg(int num)
{
struct node *tmp;
if(start==NULL)
{
tmp->next=NULL;
tmp->prev=NULL;
tmp->info=num;
start=tmp;
}
else{
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=num;
tmp->next=start;
start->prev=tmp;
tmp->prev=NULL;
start=tmp;
}
}
void display()
{
struct node *q;
if(start==NULL)
{
printf("List is empty\n");
return;
}
q=start;
printf("List is :\n");
while(q!=NULL)
{
printf("%d ", q->info);
q=q->next;
}
printf("\n");
}/*End of display() */

void add_after(int num,int loc)
{
struct node *q,*tmp;
int i;
q=start;
if(loc==length())
{
while(q->next!=NULL)
q=q->next;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=num;
tmp->next=NULL;
tmp->prev=q;
q->next=tmp;
return;
}
printf("hi");
for(i=0;inext;
if(q==NULL)
{
printf("list does not have that many elements");
return;
}
}
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=num;
tmp->prev=q;
q->next->prev=tmp;
tmp->next=q->next;
q->next=tmp;
}

void del(int num)
{
struct node *q;
q=start;
while(q->next!=NULL)
q=q->next;
if(q->info==num)
{
q->prev->next=NULL;
free(q);
return;
}
else
{q=start;}
while(q!=NULL)
{
if(q->info==num)
{
//delete q
q->prev->next=q->next;
q->next->prev=q->prev;
free(q);
return;
}
q=q->next;
}
printf("element not in in list");
}
void rev()
{
struct node *p1,*p2;
p1=start;
p2=p1->next;
p1->next=NULL;
p1->prev=p2;
while(p2!=NULL)
{
p2->prev=p2->next;
p2->next=p1;
p1=p2;
p2=p2->prev; /*next of p2 changed to prev */
}
start=p1;
}/*End of rev()*/



int main()
{
int choice,n,m,po,i;
start=NULL;
while(1)
{
printf("1.Add at begining\n");
printf("2.Display\n");
printf("3.exit\n");
printf("4.add after\n");
printf("5.delete\n");
printf("6.length\n");
printf("7.reverse\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the element : ");
scanf("%d",&m);
add_beg(m);
break;
case 2:display();
break;
case 3:return 0;
case 4:printf("Enter the element : ");
scanf("%d",&m);
printf("Enter the position after which this element is inserted : ");
scanf("%d",&po);
add_after(m,po);
break;
case 5:printf("Enter the element for deletion : ");
scanf("%d",&m);
del(m);
break;
case 6:printf("%d\n",length());
break;
case 7:rev();
break;
default:printf("hello option");
}
}
return 0;
}
Copyright @ 2013 code-craft. Designed by Templateism | MyBloggerLab