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
i want to ask, what id the question ask about how to:
ReplyDelete1)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?
yes, use same coding....
Deletebecause 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()));
btw.. the coding use linked list..
ReplyDeletealready done that.. but i can't calc total length and print it in alphabetical order... :'(,
ReplyDeletethanks 4 the advice..
use must have method...
Deletepublic 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...
}
how to calculate vowels in list of character, eg 5 character.. i use this one, but there is error
ReplyDeletepublic 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);
}
This comment has been removed by the author.
Deletemaybe your s.item is object...
Deletethen 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++;
}