You are on page 1of 20

Defining a Stack ADT

In the last lecture, we learned to implement STACKS as an


Abstract Data Type (ADT).

isEmpty

pop
push


Page 1 of 20
/*
* File: Stack.java
*/

public interface Stack {

public void push(int element);


public int pop();
public int stackDepth();
public boolean isEmpty();

Page 2 of 20
Concrete Implementation of the Stack ADT
(Version 2.0)

We now improve the stackADT implementation version


2.0.

The first issue is the size limitation in version 1.0.

public class Stack_v2 implements Stack {


private int elements[] = new int[100];
private int count = 0;


}
Page 3 of 20
New implementation in version 2.0:

public class Stack_v2 implements Stack {


private int elements[] = new int[5];
private int count = 0;
… elements[4]

elements[0]
}

Page 4 of 20
elements[4]

elements[1]

elements[0]

count
2 3 6

6
3
2
Page 5 of 20
*
public void push(int element)
{
if (count == elements.length) {
int tmp[] = new int[elements.length+5];
System.arraycopy(elements, 0, tmp, 0, elements.length);
elements = tmp;
};
elements[count++]=element;
}

Page 6 of 20
Note:

z if (count == elements.length) {
int tmp[] = new int[elements.length+5];
System.arraycopy(elements,0,tmp,0,elements.length);
elements = tmp;
};
2 3 6 4 9

2 3 6 4 9
7 9
4
6 5
3
Page 7 of 20 2
The method System.arraycopy copies an array from the
specified source array, beginning at the specified position,
to the specified position of the destination array.

int p[] = new int[8];


int q[] = new int[10];
… p

System.arraycopy(p, 0, q, 0, 8);
q

Page 8 of 20
The method System.arraycopy copies an array from the
specified source array, beginning at the specified position,
to the specified position of the destination array.

int p[] = new int[8];


int q[] = new int[8];
… p

System.arraycopy(p, 0, q, 0, 5);
q

Page 9 of 20
The method System.arraycopy copies an array from the
specified source array, beginning at the specified position,
to the specified position of the destination array.

int p[] = new int[8];


int q[] = new int[8];
… p

System.arraycopy(p, 1, q, 2, 5);
q

Page 10 of 20
Note:

z if (count == elements.length) {
int tmp[] = new int[elements.length+5];
System.arraycopy(elements,0,tmp,0,elements.length);
elements = tmp;
};
2 3 6

2 3 6 4 9
7 9
4
6 5
3
Page 11 of 20 2
Note:

z elements[count++]=element;

2 3 6 4 9 7
7 9
4
5 6
6
3
2

Page 12 of 20
public int pop()
{
return elements[--count]);
}
4
4
6
3
2
2 3 6 4 …

4 3

Page 13 of 20
public int stackDepth()
{
return count;
}

public boolean isEmpty()


{
return count==0;
}

Page 14 of 20
Concrete Implementation of the Stack ADT
(Version 2.1)

One problem with version 2.0 is that the size of the stack
never reduces. This is not very efficient in terms of
memory usage.

The implementation class version 2.1 is identical to ver-


sion 2.0, except that the function pop is rewritten so that
memory is used in a more efficient way.

Page 15 of 20
/* Version 2.1 */

public int pop()


{
int result;
result = elements[--count];
if (count <= elements.length - 5) {
int tmp[] = new int[elements.length - 5];
System.arraycopy(elements, 0, tmp, 0, tmp.length);
elements = tmp;
};
return(result);
}

Page 16 of 20
Note:

z if (count <= elements.length - 5) {


int tmp[] = new int[elements.length - 5];
System.arraycopy(elements, 0, tmp, 0, tmp.length);
elements = tmp;
};
7
9
4 2 3 6 4 9 7
6
3
2 2 3 6 4 9

Page 17 of 20 6 5
/* Version 2.2 */

public int pop()


{
int result;
if (count==0) throw new HeyException();
result = elements[--count];
if (count <= elements.length - 5) {
int tmp[] = new int[elements.length - 5];
System.arraycopy(elements, 0, tmp, 0, tmp.length);
elements = tmp;
};
return(result);
}

Page 18 of 20
/* Hey Exception */

public class HeyException extends RuntimeException {

public HeyException() {
super("Hey! Exception!");
}

This is probably the most irresponsible way of raising


exception (which we shall use in this course).

Page 19 of 20
Using ADT’s in a Program

Page 20 of 20

You might also like