Wednesday, 22 May 2013

44. STACK USING LINKED LIST

Code:
import java.util.Scanner;
public class StackLL
{
    Node start=new Node();
    Node top=new Node();
    Node curr=new Node();
    int ch,num;

    StackLL()
    {
        num=0;
        start=null;
        top=null;
    }

    void Push(int val)
    {
        Node t=new Node();
        t.info=val;
        t.next=null;
        if(num++==0)
            start=t;
        else
            top.next=t;
        top=t;

    }

    void Pop()
    {
        curr=start;
        if(num==0)
            System.out.println("Stack is Empty");
        else
        {
            if(top==start)
            {
                start=null;
                top=null;
                num=0;
            }
            else
            {
                while(curr.next!=top)
                    curr=curr.next;
                top=curr;
            }
        }
    }

    void Display()
    {
        curr=start;
        System.out.print("Bottom -> ");
        while(curr!=null)
        {
            System.out.print(curr.info + " -> ");
            curr=curr.next;
        }
        System.out.println("Top");
    }

    public static void main(String args[])
    {
        int ch,val;
        StackLL s=new StackLL();
        Scanner in = new Scanner(System.in);
        while(true)
        {
            System.out.println("\n-----------------------------------");
            System.out.println("Enter your Choice");
            System.out.println("1 : Push\t2 : Pop\t3 : Display\t4 : Exit");
            ch=in.nextInt();
            switch(ch)
            {
                case 1:
                {
                    System.out.print("Enter a Number = ");
                    val=in.nextInt();
                    s.Push(val);
                    break;
                }
                case 2:
                {
                    s.Pop();
                    break;
                }
                case 3:
                {
                    s.Display();
                    break;
                }
                case 4:
                    System.exit(0);
                    break;
                default : continue;
            }
        }
    }

}

Need the code??

43. QUEUE USING LINKED LIST

Code:
import java.util.Scanner;

public class Node
{
    int info;
    Node next,prev;
    Node left,right;
}

public class QueueLL
{
    Node start=new Node();
    Node end=new Node();
    Node curr=new Node();
    int ch,num;
    Object obj=new Object();

    QueueLL()
    {
        start=null;
        end=null;
        num=0;
    }

    void Add(int val)
    {

        Node t=new Node();
        t.info=val;
        if(num++==0)
            start=t;
        else
           t.next=end;
        end=t;
    }

    void Del()
    {
        if(num==0)
            System.out.println("Queue is Empty");
        else
        {
            if(start==end)
            {
                start=null;
                end=null;
                num=0;
            }
            else
            {
                curr=end;
                while(curr.next!=start)
                    curr=curr.next;
                start=curr;
                start.next=null;
            }
        }
    }

    void Display()
    {
        curr=end;
        System.out.print("Back -> ");
        while(curr!=null)
        {
            System.out.print(curr.info + " -> ");
            curr=curr.next;
        }
        System.out.println("Front");
    }

    public static void main(String args[])
    {
        int ch,val;
        QueueLL q=new QueueLL();
        Scanner in = new Scanner(System.in);
        while(true)
        {
            System.out.println("Enter your Choice");

            System.out.println("-------------------------------------");
            System.out.println("1 : Add\t2 : Del\t3 : Display\t4 : Exit");
            ch=in.nextInt();
            switch(ch)
            {
                case 1:
                {
                    System.out.print("Enter a Number = ");
                    val=in.nextInt();
                    q.Add(val);
                    break;
                }
                case 2:
                {
                    q.Del();
                    break;
                }
                case 3:
                {
                    q.Display();
                    break;
                }
                case 4:
                    System.exit(0);
                    break;
                default : continue;
            }
        }
    }

}

Need the code??

42. PRIORITY QUEUE USING HEAP

Code:
#include<iostream.h>
#include<process.h>
#include<math.h>
#include<iomanip.h>
#include<malloc.h>
struct Heap
{
int capacity;
int size;
int *element;
};

typedef struct Heap *pqueue;
pqueue initialize(int max)
{
pqueue heap;
if(max<3)
{
cout<<”\nPriority Queue is too Small\n”;
exit(0);
}
heap = new Heap;
if(heap==NULL)
{
cout<<”\nOut of Space\n”;
exit(0);
}
heap->elements=new int[((max+1) * sizeof(int))];
if(heap->elements==NULL)
{
cout<<”\n Out of Space\n”;
exit(0);
}
heap->capacity = max;
heap->size=0;
heap->elements[0]=0; //minData
return heap;
}
int isempty(pqueue heap)
{
return (heap->size==0);
}
int isfull(pqueue heap)
{
return(heap->size==heap->capacity);
}
void insert(int x, pqueue heap)
{
int i;
if(isfull(heap))
{
cout<<”\nHeap is Full\n”;
return;
}
for(i=++heap->size; heap->elements[i/2] > x; i/=2)
heap->elements[i]=heap->elements[i/2];
heap->elements[i]=x;
}
int deletemin(pqueue heap)
{
int i, child, min, last;
if(isempty(heap))
{
cout<<”\nHeap is Empty\n”;
return heap->elements[0];
}
min=heap->elements[1];
last=heap->elements[heap->size--];
for(i=1; i*2<=heap->size; i=child)
{
child = i*2;
if(child!=heap->size && heap->elements[child+1]<heap->elements[child])
child++;
if(last>heap->elements[child])
heap->elements[i]=heap->elements[child];
else
break;
}
heap->elements[i]=last;
return min;
}
void main()
{
int size,ch,ele,child;
pqueue heap;
cout<<”\nWorking with Priority Queues (or) Heaps\n”;
cout<<”\nEnter the Size of the Heap…”;
cin>>size;
heap = initialize(size);
do
{
cout<<”\n\t\tMenu\n”;
cout<<”\n1. Insert”;
cout<<”\n2. Delete”;
cout<<”\n3. Display”;
cout<<”\n4. Exit”;
cout<<”\nEnter Your Choice…”;
cin>>ch;
switch(ch)
{
case 1:
cout<<”\nEnter an Element…”;
cin>>ele;
insert(ele, heap);
break;
case 2:
ele=deletemin(heap);
cout<<”\nDeleted Element is “<<ele<<endl;
break;
case 3:
int space,level=1;
int c=1;
for(int i=1; i<heap->size; i++)
{
for(int j=1;j<=level;j++)
{
if(c<=heap->size)
{
space=(int) (heap->size*2)/c;
cout<<setw(space)<<”  “<<heap->elements[c++]<<setw(space)<<”  “;
}
else
break;
}
if (level==1)
level=2;
else if (level>=2)
level=level*2;
cout<<”\n”;
}
break;
default:
cout<<”\n Exit “;
}
}while(ch<4);

}

Need the code??

41. SINGLY LINKED LIST IN JAVA

Code:
import java.util.Scanner;
public class SingleLL
{
    Node start=new Node();
    Node end=new Node();
    Node curr=new Node();
    int num;

    SingleLL()
    {
        start=null;
        end=null;
        num=0;
    }

 
    void AddBeg(int val)
    {
        Node t=new Node();
        t.info=val;
        if(num++==0)
            end=t;
        else
            t.next=start;
        start=t;
    }

    void AddEnd(int val)
    {

        Node t=new Node();
        t.info=val;
        t.next=null;
        if(num++==0)
            start=t;
        else
            end.next=t;
        end=t;
    }

    void DelBeg()
    {
        if(num==0)
            System.out.println("Linked List is Empty");
        else
        {
            if(start==end)
            {
                start=null;
                end=null;
                num=0;
            }
            else
                start=start.next;
        }
    }

    void DelEnd()
    {
        if(num==0)
            System.out.println("Linked List is Empty");
        else
        {
            if(start==end)
            {
                start=null;
                end=null;
                num=0;
            }
            else
            {
                curr=start;
                while(curr.next!=end)
                    curr=curr.next;
                end=curr;
                end.next=null;
            }
        }
    }
  
    void DelEle(int val)
    {
        if(num==0)
            System.out.println("Linked List is Empty");
        else
        {
            curr=start;
            Node prev=new Node();
            if(end==start && start.info==val)
            {
                start=end=null;
                num=0;
            }
            else if(start.info==val)
                start=start.next;
            else if(end.info==val)
            {
                System.out.println("HEllo");
                curr=start;
                while(curr.next!=end)
                    curr=curr.next;
                end=curr;
                end.next=null;
            }
            else
            {
                curr=start;
                while(curr.info!=val)
                {
                    prev=curr;
                    curr=curr.next;
                }
                prev.next=curr.next;
            }          
        }
    }

    void Display()
    {
        curr=start;
        System.out.print("Start -> ");
        while(curr!=null)
        {
            System.out.print(curr.info + " -> ");
            curr=curr.next;
        }
        System.out.println("End");
    }

    void Search(int val)
    {
        Loop:
        {
        for(curr=start;curr!=null;curr=curr.next)
        {
            if(curr.info==val)
            {
                System.out.println("Element Found");
                break Loop;
            }
        }
        System.out.println("Element Not Found");
        }
    }

    public static void main(String args[])
    {
        int ch,val;
        SingleLL sl=new SingleLL();
        Scanner in = new Scanner(System.in);
        while(true)
        {
            System.out.println("-----------------------------------");
            System.out.println("Enter your Choice");
            System.out.println("1 : Add Beg  2 : Add End  3 : Del Beg  4 : Del End  5 : Del Ele  6 : Display  7 : Search  8 : Exit");
            ch=in.nextInt();
            switch(ch)
            {

case 1:
                {
                    System.out.print("Enter Number = ");
                    val=in.nextInt();
                    sl.AddBeg(val);
                    break;
                }
                case 2:
                {
                    System.out.print("Enter Number = ");
                    val=in.nextInt();
                    sl.AddEnd(val);
                    break;
                }
                case 3:
                {
                    sl.DelBeg();
                    break;
                }
                case 4:
                {
                    sl.DelEnd();
                    break;
                }
                case 5:
                {
                    System.out.print("Enter Number To Delete = ");
                    val=in.nextInt();
                    sl.DelEle(val);
                    break;
                }
                case 6:
                {
                    sl.Display();
                    break;
                }
                case 7:
                    System.out.print("Enter Number = ");
                    val=in.nextInt();
                    sl.Search(val);
                    break;
                case 8:
                    System.exit(0);
                    break;
                default : continue;
            }
        }
    }  

}

Need the code??