Tuesday, 26 March 2013

PriortyQueue



public class Queue {

int [] Q;
int F;
int R;

Queue (int Qsize){
Q= new int[Qsize];
F=0;
R=-1;

}
public void insert (int v){
if (R==-1){
R++;
Q[R]=v;

}
else{
for (int i=F; i<=R&& Q[i]<=v; i++){

}
if(i==R){
R++;
Q[R]=v;

}
else if (i==F){
for(j=R;j>0;j--){
Q[j+1]= Q[j];

}
R++;
Q[i]=v;

}
}
}
}

Thursday, 21 March 2013

CircularQueue


public class CircularQueue {

int[] Q;
int R,F;

CircularQueue(int size)
{
Q=new int[size];
R=size-1;
F=size-1;
}

public boolean isFull(){
if (F==R)
return true;
else
return false;
}
 public boolean isEmpty()
 {
if (R==F)
return true;
else
return false;
 }


public void insertion(int item)
{
if(R==Q.length-1)
R=0;
else
{
R++;
if (isFull())
{
System.out.println("Queue overflow");
R--;
return;
}
}

Q[R]=item;

}

public int Delete()
{
if(isEmpty()){
System.out.println("Queue underflow");
return -1;
}
else{
if(F==Q.length-1)
F=0;
else
F++;
}
    return Q[F];

}

public void display(){
for(int i=0; i<Q.length; i++)
System.out.println(Q[i]);
}



}

////////////////////////////////////////////////////////////////////////



public class Queuedemo {

/**
* @param args
*/
public static void main(String[] args) {
CircularQueue q= new CircularQueue(10);
q.insertion(10);
q.insertion(11);
q.insertion(12);
q.insertion(13);
q.insertion(14);
q.insertion(15);
q.insertion(16);
q.Delete();

q.display();




}

}

///////////////////////////////////////////////////

Tuesday, 19 March 2013

Application Of Stack


LineStack
public class LineStack<T> {
StackNode <T> Top;

public Boolean isEmpty(){
if (Top==null)
return true;
else
return false;


}

public void push (T data) {
StackNode<T> newNode = new StackNode<T> (data);
if (isEmpty()){
Top=newNode;
else
newNode.next=Top;
Top=newNode;

}


}
}

////////////////////////////////


StackNode:

public class StackNode<T>  {

T data;
StackNode<T>next;

StackNode(T d){
data=d;

}

}

/////////////////////////////////////////


public class StackAppDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LineStack<Character> MyStk= new LineStack<Character>()

}

}


//////////////////////////////////////////////////


public class ArithematicExp {

public void infixToPostfix(String Exp){
String Postfix;
LinkStack<T> ExpStk=new LinkStack<Character>;
int i=0;
while (i<Exp.length()){
char ch=Exp.charAt(i);
switch(ch){
case ch='('||'{'||'['
{
}
}
}
i++;
}
}


Thursday, 7 March 2013

Stack Class OverFlow


  • public class ParenthesisValidation {
    public Boolean paranthesisCheck(String exp) {
    MyStack Mystk = new MyStack(exp.length());
    char ch;
    Boolean Valid = true;
    for (int i = 0; i < exp.length(); i++) {
    ch = exp.charAt(i);
    if (ch == '[' || ch == '{' || ch == '(') {
    Mystk.PUSH(ch);
    } else {
    if (ch == ']' || ch == '}' || ch == ')') {
    if (Mystk.isEmpty()) {
    Valid = false;
    }
    char c = Mystk.POP();
    if(c == '[' && ch == ']' || c == '{' && ch == '}' || c == '(' && ch == ')'){

    }
    }
    }
    }
    return true;
    }
    }
    • Shiraz Sohail

      public class MyStackDemo {
      public static void main(String[] args) {
      MyStack MYstk = new MyStack(10);
      ParenthesisValidation Par = new ParenthesisValidation();
      Par.paranthesisCheck("{(2+3)*3}");

      /* MYstk.PUSH(23);
      MYstk.PUSH(34);
      MYstk.PUSH(66);
      System.out.println(MYstk.POP());
      System.out.println(MYstk.POP());
      System.out.println(MYstk.POP());
      System.out.println(MYstk.POP()); */
      }
      }
      • Shiraz Sohail

        public class MyStack {
        char[] Stack;
        int top;

        MyStack(int s){
        Stack = new char[s];
        top = -1;
        }

        public Boolean isFull(){
        if(top == Stack.length-1)
        return true;
        else
        return false;
        }

        public Boolean isEmpty(){
        if(top == -1)
        return true;
        else
        return false;
        }
        public void PUSH(char d){
        if(isFull()){
        System.out.println("Stack Overflow");
        }
        else{
        Stack[++top] = d;
        }
        }

        public char POP(){
        if(isEmpty()){
        System.out.println("Stack Underflow");
        return ' ';
        }
        else{
        char T=Stack[top];
        top--;
        return T;
        }
        }

        }

      Wednesday, 6 March 2013

      CircularLinkList


      class Node<T>{
      T data;
      Node<T> next;

      Node(T d)
      {
      data=d;

      }
      }
      public class CircularList<T> {
      Node<T> Head;
      Node<T> Tail;
      int TotalNodes;
      public void insert(T d){
      Node<T> newNode=new Node<T>(d);
      TotalNodes++;
      Node<T> T=Head;
      if(Head==null){
      Head=newNode;
      Tail=newNode;
      }
      else{
      newNode.next=T;
      Tail.next=newNode;
      Head=newNode;
      }


      }
      /*
      public int JosephusSolution(){
      Node Temp=Head;
      Node Prev=null;

      int counter;

      for(int i=1; i<TotalNodes;i++){

      counter=(int)(Math.random()*(10)+1); // random counter to avoid baisness

      for(int j=1;j<counter;j++){
      Prev=Temp;
      Temp=Temp.next;
      }

      Prev.next=Temp.next; // update reference for deletion

      if(Temp==Head){ // if the deleted node is Head node
      Tail.next=Head.next;
      Head=Head.next;
      }
      if(Temp==Tail){ // if the deleted node is Tail node
      Tail=Prev;
      }
      //Continue for next deletion
      Temp=Prev.next;
      }// end of outer loop

      return Head.data; // return the Winner

      }
      */
      public void display(){
      Node<T> T=Head;
      System.out.println(T.data);
      T=T.next;
      while(T!=Head)
      {
      System.out.println(T.data);
      T=T.next;
      }
      }

      }

      Tuesday, 5 March 2013

      Stack


      public class DNode()
      {
      DNode prev;
      int data;
      DNode next;

      DNode(int d){
      data=d;
      }
      }

      //////////////////////////////////////////////////////////////////////////

      public class DNode()
      {
      DNode prev;
      int data;
      DNode next;

      DNode(int d){
      data=d;
      }
      }


      public class DoublyLinkList{
      DNode Head;
      public void insert(int d){
      DNode newNode=new DNode(d);
      DNode Temp=Head;

      if (Temp==null)
      {
      Head=newNode;
      }
      else{
      while(Temp.data<newNode.data && Temp.next!=null)\
      {
      Temp=Temp.next;
      }
      if(Temp.next==null) // last insertion
      {
      Temp.next=newNode;
      newNode.prev=Temp;
      }
      else if(Temp==Head) // first insertion
      {
      newNode.next=Temp;
      Temp.prev=newNode;
      Head=newNode;
      }
      else{
      newNode.next=Temp;
      newNode.prev=Temp.prev;
      Temp.prev.next=newNode;
      Temp.prev=newNode;
      }

      }

      }
      public void display(){
      DNode Temp=Head;
      while(Temp != null)
      {
      System.out.print(" " +Temp.data)
      Temp=Temp.next;
      }
      }
      }
      }

      /////////////////////////////////////////////////////////////

      Main Class

      public class doublyLinkListdemo{

      public static void main(string[] args ){
      DoublyLinkList DList= new DoublyLinkList();
      DList.insert(23);
      DList.insert(26);
      DList.insert(28);
      DList.insert(29);


      Dlist.display();

      }
      }