1 / 7

Department of Computer and Information Science, School of Science, IUPUI

Department of Computer and Information Science, School of Science, IUPUI. CSCI 240. Abstract Data Types Stacks. Dale Roberts, Lecturer IUPUI droberts@cs.iupui.edu. Abstract Data Type Defined. Sedgewick Definition 4.1:

bridgettek
Download Presentation

Department of Computer and Information Science, School of Science, IUPUI

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Department of Computer and Information Science,School of Science, IUPUI CSCI 240 Abstract Data Types Stacks Dale Roberts, Lecturer IUPUI droberts@cs.iupui.edu

  2. Abstract Data Type Defined Sedgewick Definition 4.1: Anabstract data type (ADT) is a data type (a set of values and a collection of operations on those values) that is accessed only through aninterface. We refer to a program that uses an ADT as aclient, and a program that specifies the data type as animplementation. 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. S4 top S3 S2 S1 4 top 1 1 top top 1  3  3 3 Pop (4) Push (4) 6 6 6 Pushdown-stack • Stack model • 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)

  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.

More Related