/ 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
intStack
Fra : Abdis
Vist : 457 gange
70 point
Dato : 17-03-05 11:44

who can help me to make intstack with the four operation

tak

 
 
Accepteret svar
Fra : molokyle

Modtaget 70 point
Dato : 17-03-05 14:26

Først en Node pladsholder :

Kode
/**
* A Node is an object that holds an int and a link
* to the next Node. It can be used to build linked
* lists of ints.
*/
public class Node {
private int data; // Each node has an int...
private Node link; // ...and a link to the next Node

/**
* Node constructor.
* @param theData the int to store in this Node
* @param theLink a link to the next Node
*/
public Node(int theData, Node theLink) {
data = theData;
link = theLink;
}

/**
* Accessor for the int data stored in this Node.
* @return our int item
*/
public int getData() {
return data;
}

/**
* Accessor for the link to the next Node.
* @return the next Node
*/
public Node getLink() {
return link;
}
}


..og så intStack klassen der gør brug af Node :

Kode
/**
* An IntStack is an object that holds a stack of ints.
*/
public class IntStack {
private Node top = null; // The top Node in the stack

/**
* Test whether this stack has more elements.
* @return true if this stack is not empty
*/
public boolean hasMore() {
return (top != null);
}

/**
* Pop the top int from this stack and return it.
* If the stack is empty we return 0 and leave the
* stack empty.
* @return the popped int or 0 if the stack is empty
*/
public int pop() {
Node n = top;
if (n == null) return 0;
top = n.getLink();
return n.getData();
}

/**
* Push an int on top of this stack.
* @param data the String to add
*/
public void push(int data) {
top = new Node(data,top);
}
}


..source code fra : http://www.webber-labs.com/mpl/source%20code/

</MOLOKYLE>

Godkendelse af svar
Fra : Abdis


Dato : 31-03-05 10:31

Tak for svaret molokyle.
                        

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 : 177423
Tips : 31962
Nyheder : 719565
Indlæg : 6407897
Brugere : 218876

Månedens bedste
Årets bedste
Sidste års bedste