Friday, October 12, 2012

Queue

I will show you how Queue algorithm look like...
and i will divide into two categories....
one use Array and other one use Node....

Array

Algorithm :


Main program :




LinkedList

Algorithm :


Main program :



Sample Running

add 12 :
[12]
add 43 :
[12][43]
add 9 :
[12][43][9]
delete :
[43][9]
front : 43
back : 9
add 12 :
[12]
add 43 :
[12][43]
add 9 :
[12][43][9]
delete :
[43][9]
front : 43
back : 9


you can test it by your self..
i hope you like this souce code...
and please give some comment....
                                                                                                                                                      Created By : Z-man, 2012

8 comments:

  1. i want to ask, what id the question ask about how to:
    1)enqueue 5 string object
    2)dequeue one object
    3)calc total length of string in queue
    4)print in alphabetical order.

    what if it is not string, but char? is it still use the same coding, but just change data type?

    ReplyDelete
    Replies
    1. yes, use same coding....
      because the data type is Object...
      Object can receive any input....
      but when u want to get element in Queue...
      u must convert it first....

      for example, you input an integer in queue...
      then when you want to output in....
      it will look like this...
      System.out.println(String.valueOf(q.front()));

      Delete
  2. btw.. the coding use linked list..

    ReplyDelete
  3. already done that.. but i can't calc total length and print it in alphabetical order... :'(,
    thanks 4 the advice..

    ReplyDelete
    Replies
    1. use must have method...
      public int size(){
      return length;
      }

      to get total length...

      and this is algorithm for print alpa order..

      int length=sList.size();
      Queue temp=new Queue();
      Queue order=new Queue();
      boolean flag=true;// to reduce execute time
      for(int i=0;i<length&&flag;i++){
      flag=false;
      for(int j=0;j<length-1;j++){
      temp.add(sList.front());
      sList.delete();
      if((temp.front()+"").compareTo((sList.front()+""))<0){// if value of diference between 'temp' n 'sList' is negative
      flag=true;
      order.push(temp.front());//top data in 'temp' push in 'order'
      temp.delete();
      }
      else{
      order.add(sList.front());//top data in 'sList' push in 'order'
      sList.delete();
      sList.add(temp.front());//then 'temp' data will re-store into 'sList'
      temp.delete();
      }
      }

      order.add(sList.front());
      sList.delete(); //after this statement 'sList' is empty...
      // *then copy data that store in 'order' into 'sList'
      while(!order.isEmpty()){
      sList.push(order.front());
      order.delete();
      }
      //end of copy
      // 'order' is empty...
      }

      Delete
  4. how to calculate vowels in list of character, eg 5 character.. i use this one, but there is error

    public void calcVowels()
    {
    Node s=qFirst;
    boolean flag=true;

    for(int i=0;i<5&&flag;i++)
    {
    if (s.item =='a' || s.item=='e' ||s.item =='i' || s.item== 'o' ||s.item == 'u') //here is the error message appear
    {
    vowels=vowels+1;
    }
    s=s.link;
    }

    System.out.println("The number of vowels in list is: "+vowels);
    }

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. maybe your s.item is object...
      then what u need to do is....
      convert it first into char....
      how...

      this is the answer....
      char c=String.valueOf(s.item).charAt(0);
      if (c =='a' || c =='e' ||c =='i' || c == 'o' ||c == 'u') {
      vowels++;
      }

      Delete