/ Forside/ Teknologi / Udvikling / Java / Spørgsmål
Login
Glemt dit kodeord?
Brugernavn

Kodeord


Reklame
Top 10 brugere
Java
#NavnPoint
molokyle 3688
Klaudi 855
strarup 740
Forvirret 660
gøgeungen 500
Teil 373
Stouenberg 360
vnc 360
pmbruun 341
10  mccracken 320
stack
Fra : Abdis
Vist : 535 gange
20 point
Dato : 09-02-05 21:59

I want someone who can give me an emplemented of four stac operations I mean the code, becouse the last day I get alot of tips but there is still something missing .


thnks

 
 
Accepteret svar
Fra : justuniverse

Modtaget 30 point
Dato : 09-02-05 22:41

Abdis> What excactly are you missing?

Kommentar
Fra : justuniverse


Dato : 09-02-05 23:19

Another method in the IntStack implementation should be the peak-method.

In the class IntStack: (Shown here http://www.kandu.dk/dk/spg/60141)

you can add this method:
Kode
private int[] structure;
private int count;

//supposed to let you see the element last added (by the put()-method
//without making any changes
public int peek() {
return structure[count-1];
}


By having this method you can choose to "pop" if a certain number is the last.

Lets say you put the integers {3, 5, 7, 2} and for some reason you want only to hold odd numbers in your stack, but you want to be sure not to pop odd numbers:

Kode
public static void main(String[] args) {
IntStack stack = new IntStack();

stack.put(3);
stack.put(5);
stack.put(7);
stack.put(2);

while(stack.peek()%2 != 0) {
stack.pop;
}
}


After this test, there should only be the numbers {3, 5, 7} which is all odd numbers.
x%y produces the remainders of x divided by y. Any numbers for x divided by 2 will give 0 remainders if the number is even (like 2, 4, 6, 8...)
and the remainder will not be 0 (!=) if x is an odd number.

In this testcase we are lucky that only the last number is an even number. There are no operations (for the ADT of a Stack) to traverse the stack, but it is ofcourse possible by using another Stack.
In this example, the goal is to hold only odd numbers in stack1 at the end:

Kode
public static void main(String[] args) {
IntStack stack1 = new IntStack();
IntStack stack2 = new IntStack();

for(int i = 1; i<= 10; i++) {
stack1.put(i);
}

//Now stack1 contains {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.
//The first number to be popped is 10 because it was the last number added by "put()"

while(stack1.size() > 0) {
if(stack1.peek()%2 != 0) {
stack2.put(stack1.pop());
}
else {
stack1.pop();
}
}

//Now stack2 holds all the odd numbers, but in the wrong order.
//The first one to pop is 1. It should be 9, so I put them back:

while(stack2.size() > 0) {
stack1.put(stack2.pop());
}
}


Now stack1 holds {1, 3, 5, 7, 9} and the fist one to pop is 9...
(at least I think... I didnt try, but you could try )
In your test you could show by writing System.out.println(stack1);

By the way... change the toString()-method. Should be as follows:
Kode
public String toString() {
StringBuffer buf = new StringBuffer();
int i = structure.length - 1;

buf.append("Index " + i + " = ");
for (i; i >= 1 ; i--) { //It´s a Stack so the list is supposed to be reversed....
buf.append(structure[i] + ", ");
}
buf.append(structure[i]) // to prevent a ", " as the last print....

return buf.toString();
}


Godkendelse af svar
Fra : Abdis


Dato : 10-02-05 20:58

Tak for svaret justuniverse.
                        

Du har følgende muligheder
Eftersom du ikke er logget ind i systemet, kan du ikke skrive et indlæg til dette spørgsmål.

Hvis du ikke allerede er registreret, kan du gratis blive medlem, ved at trykke på "Bliv medlem" ude i menuen.
Søg
Reklame
Statistik
Spørgsmål : 177547
Tips : 31968
Nyheder : 719565
Indlæg : 6408797
Brugere : 218887

Månedens bedste
Årets bedste
Sidste års bedste