Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Thought for the Day

I would rather regret the things I have done


than the things I have not.
– Lucille Ball
Using Generic Classes
• Must specify the actual type to be used
GenericList<Student> classList =
new GenericList<Student>();
. . .
• Compiler checks that only Student
objects are placed in classList
• Compiler can also infer type on the right
GenericList<Student> classList =
new GenericList<>();
Using Generic Classes
• Type casts no longer required

GenericList<Student> classList =
new GenericList<>();
classList.add(new Student());
. . .
Student st = classList.get(k);
Generics
• There are some more advanced features
– Will see some later
– Others described in documentation
Summary of List ADTs in
Chapter 4
• Lists of integers:
– IntegerVector – based on an array
– IntegerList – based on a linked-list

• Generic lists:
– ObjectList – using polymorphism
– GenericList – using Java generic features
The java.util Package
• This standard Java package contains similar
classes:
– Vector and ArrayList: generic, array-
based ADTs, with “automatic growth”
– LinkedList: similar to our GenericList
ADT
Chapter 5: Stacks and Queues
• Objectives
– To consider stack, queue and deque ADTs
– To look at their implementation
• using arrays and linked lists
– To look at some of their applications
– To study new implementation techniques for
linked lists: doubly-linked lists, circularly-
linked lists and list header nodes
Introduction
• Stacks, Queues
– specialised lists
– restricted ways of adding and removing
elements

Stack
Queue
Introduction (cont.)
• Deques (Double-Ended Queues)
– More general behaviour: add and remove from
either end

Deque
Stacks
• Elements added and removed at one end
– the top

• LIFO (Last In, First Out) list

• Add: “push”
• Remove: “pop” Stack
The Stack Interface
• We will use a generic interface to specify
the requirements for our stacks
public interface Stack<T>
{ public void push (T item);
// Push the new item onto a stack
public T pop ();
// Pop item off top of stack
public T top ();
// Return a copy of top item
public boolean isEmpty ();
// Return TRUE if no items on stack
} // interface Stack
Implementing a Stack:
Array-Based Approach
• Very simple
– One index to keep track of top item

• Empty stack:
top -1

0 1 2 3 4 max
stack ...
Pushing Elements
– Increment index
– Store item

top 0
2
1

0 1 2 3 4 max
stack G e o ...
Popping Elements
– Return top element
– Decrement index

Return ’o’
top 2
1

0 1 2 3 4 max
stack G e o ...
Array Implementation
• Usual space problems:
– Too big: waste memory
– Too small: run out of space
The ArrayStack Class
• Class Diagram

ArrayStack
data, topIndex
push, pop, top, isEmpty

Methods as required by the Stack interface


The ArrayStack Class
public class ArrayStack<T> implements Stack<T>
{ private T[] data; // Array of data
private int topIndex; // Top element

@SuppressWarnings("unchecked")
public ArrayStack (int initSize)
{ data = (T[])new Object[initSize];
topIndex = -1;
} // Constructor
public ArrayStack ()
{ this(100); Very similar to
} // Constructor IntegerVector
...
} // class ArrayStack
The push and pop Operations
• Very simple in Java
– use the decrement and increment operators
public void push (T item)
// Push the new item onto an ArrayStack
{ if (topIndex >= data.length-1)
throw new …Exception…();
data[++topIndex] = item;
} // push
public T pop () Order is important
// Pop item off top of stack
{ if (topIndex < 0)
throw new …Exception…();
return data[topIndex--];
} // pop
top and isEmpty

public T top ()
// Return a copy of top item
{ if (topIndex < 0)
throw new …Exception…();
return data[topIndex];
} // top

public boolean isEmpty ()


// Return TRUE if no items on stack
{ return topIndex < 0;
} // isEmpty

You might also like