Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abstract Data Types Stacks CSCI 240

Similar presentations


Presentation on theme: "Abstract Data Types Stacks CSCI 240"— Presentation transcript:

1 Abstract Data Types Stacks CSCI 240
Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Stacks Dale Roberts, Lecturer IUPUI

2 Abstract Data Type Defined
Sedgewick Definition 4.1: An abstract data type (ADT) is a data type (a set of values and a collection of operations on those values) that is accessed only through an interface. We refer to a program that uses an ADT as a client, and a program that specifies the data type as an implementation. The key distinction that makes a data type abstract is the word only. Client programs never access data except through the operations provided in the interface. We say that the interface is opaque. The client cannot see the implementation through the interface. This is also called encapsulation.

3 Point ADT Interface // point.h class POINT { private:
// Implementation-dependent code public: POINT(); float distance(POINT) const; }; We can use different implementations (point.cpp) that have the same interface without changing any code in the client programs that use the ADT.

4 Pushdown-stack Stack model push pop
A stack is a list with the restriction that insertion & deletion can be performed only at the end (or top) of the list. Only the top node is accessible: New nodes can be added and removed only at the top Last-in, first-out (LIFO) Bottom of stack indicated by a link member to NULL push Adds a new node to the top of the stack pop Removes a node from the top Stores the popped value Returns true if pop was successful A stack can be empty, “pop” from an empty stack is an error A stack can never be full (assuming infinite memory) top S4 S3 S2 S1 top 4 1 3 6 Push (4) Pop (4)

5 Pushdown-stack ADT interface
template <class Item> class STACK { private: // Implementation-dependent code public: STACK(int); int empty() const; void push(Item item); Item pop(); };

6 Array implementation of pushdown stack
template <class Item> class STACK { private: Item *s; int N; public: STACK(int maxN) { s = new Item[maxN]; N = 0; } int empty() const { return N == 0; } void push(Item item) { s[N++] = item; } Item pop() { return s[--N]; } };

7 Linked-list implementation of a pushdown stack
template <class Item> class STACK { private: struct node { Item item; node* next; node(Item x, node* t) { item = x; next = t; } }; typedef node *link; link head; public: STACK(int) { head = 0; } int empty() const { return head == 0; } void push(Item x) { head = new node(x, head); } Item pop() { Item v = head->item; link t = head->next; delete head; head = t; return v; } We can implement the push and pop operations for the pushdown stack ADT in constant time, using either arrays or linked lists.


Download ppt "Abstract Data Types Stacks CSCI 240"

Similar presentations


Ads by Google