Abstract Data Types and Stacks

Slides:



Advertisements
Similar presentations
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Advertisements

Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Exercise 6 : Stack 1.Stack is a data structure that supports LIFO (Last In First Out) operations. - In this exercise, you implement Stack data structures.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
CS Winter 2011 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington Last updated: 2/17/
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
CSE373: Data Structures & Algorithms Priority Queues
STACKS & QUEUES for CLASS XII ( C++).
C++ Programming:. Program Design Including
Stacks.
CSE373: Data Structures & Algorithms
Data Structure By Amee Trivedi.
Queues Chapter 4.
G64ADS Advanced Data Structures
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 4 The easy stuff.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Stacks and Queues Chapter 4.
CS 1114: Implementing Search
Data Structures and Algorithms
Abstract Data Types and Stacks
Homework 4 questions???.
Stacks.
Queues Queues Queues.
Cinda Heeren / Geoffrey Tien
Algorithms and Data Structures
Stack and Queue.
CSCE 210 Data Structures and Algorithms
HW-6 Deadline Extended to April 27th
Stacks and Queues.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
CMSC 341 Lecture 5 Stacks, Queues
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Abstract Data Types (ADTs)
Stack ADT & Modularity 2 implementations of the Stack abstract data type: Array Linked List Program design: modularity, abstraction and information hiding.
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Popping Items Off a Stack Lesson xx
CSC 143 Queues [Chapter 7].
Data Structures and Algorithms
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Abstract Data Type Abstract Data Type as a design tool
Chapter 16 Linked Structures
Abstract Data Types and Stacks
Stacks and Queues CSE 373 Data Structures.
Stack.
FIFO Queues CSE 2320 – Algorithms and Data Structures Alexandra Stefan
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Dynamic allocation (continued)
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Abstract Data Types Stacks CSCI 240
Lecture 9: Stack and Queue
Presentation transcript:

Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington Last updated: 2/6/2018

Generalized Queues A generalized queue is an abstract data type that stores a set of objects. Let's use Item to denote the data type of each object. The fundamental operations that it queue supports: void insert(Queue q, Item x): adds object x to set q. Item delete(Queue q): choose an object x, remove that object from q, and return it to the calling function. create – creates a queue destroy – destroys a queue join – joins two queues

Generalized Queues Basic operations: void insert(Queue q, Item x) Item delete(Queue q) delete must choose what item to delete. Last inserted -> Stack / Pushdown Stack / LIFO (Last In First Out) First inserted -> FIFO Queue (First In First Out) Random item. Item with the smallest key (if each item contains a key). -> Priority Queue (Heap)

Pushdown Stack The pushdown stack supports insert and delete as follows: insert push: the insert operation for pushdown stacks. Puts an item "on top of the stack". delete pop: the delete operation for pushdown stacks. Removes the item from the top of the stack (the last item that was pushed in).

Continue with the other operations. Push / Pop – Work sheet push(15) push(20) pop() push(30) push(7) push(25) push(12) Conventions: Value: Push value on stack * : Pop from stack Empty stack 15 20 * 30 Continue with the other operations.

Push / Pop – Answers Know these ‘paper’ conventions. Blackboard test conventions: : empty stack 15:15 push 15 & stack 20:15,20 push 20 & stack *:15 pop & stack push(15) push(20) pop() push(30) push(7) push(25) push(12) Conventions: Push value: value Pop: * 25 12 7 7 7 7 7 20 30 30 30 30 30 30 30 15 15 15 15 15 15 15 15 15 15 Empty stack 15 20 * 30 7 25 * 12 * *

Stack – Output Sequence Conventions: Push value: value Pop: * Operations: 15 20 * 30 7 25 12 The output sequence produced by these operations on a stack is: 20, 25, 12, 7 25 12 7 7 7 7 7 20 30 30 30 30 30 30 30 15 15 15 15 15 15 15 15 15 15 Empty stack 15 20 * 30 7 25 * 12 * *

Exercises Given sequence of operations, show the stack & output: Conventions: letter – push(letter) * - pop() Given sequence of operations, show the stack & output: ROS**T*E*X* ROS**T*E***X* (error) SOM**E*T**H*I*NGTO*D**O*** (a longer example) Given input and output sequence, show the push & pop operations Example: given input: CAT and output ACT, the answer is: CA**T* . (Insert * in the input sequence s.t. that these operations give the desired output.) Input: INSATE, Output: SANETI, Operations Sequence: Output: ANSITE, Operations Sequence: (error)

Stack Applications Function execution in computer programs: when a function is called, it enters the calling stack. The function that leaves the calling stack is always the last one that entered (among functions still in the stack). Interpretation and evaluation of symbolic expressions: evaluate expressions like (5+2)*(12-3), or parse C code (as a first step in the compilation process). Search methods. traverse or search a graph

Implementing Stacks A stack can be implemented using: lists or arrays Both implementations are fairly straightforward.

List-Based Stacks … List-based implementation: What is a stack? A stack is essentially a list. push(stack, item) How? : O(1) – wanted (frequent operation for this data structure) pop(stack) O(1) – wanted (frequent operation for this data structure) What type of insert and remove are fast for lists? How many ‘ways’ can we insert in a list? NULL …

List-Based Stacks … List-based implementation: What is a stack? A stack is essentially a list. push(stack, item) How? : inserts that item at the beginning of the list. O(1) pop(stack) How? : removes (and returns) the item at the beginning of the list. NULL …

Implementation Code See files posted on course website: stack.h: defines the public interface. stack_list.c: defines stacks using lists. stack_array.c: defines stacks using arrays.

The Stack Interface See file stack.h posted on the course website. stack newStack(int mx_sz); void destroyStack(stack s); void push(stack s, Item data); Item pop(stack s); //NOTE: type Item, not link int isEmptyStack(stack s);

Defining Stacks typedef struct stack_struct * stack; struct stack_struct { ???; };

Defining Stacks typedef struct stack_struct * stack; struct stack_struct { list items; }; items stack_struct List object length first NULL …

Example stack_struct List object items length first typedef struct stack_struct * stack; struct stack_struct { list items; }; Empty stack. We will insert values in order: 20 7 15 pop() items stack_struct List object length first NULL

Example cont. stack_struct List object items length first typedef struct stack_struct * stack; struct stack_struct { list items; }; Insert values in given order: 20 7 15 pop() items stack_struct List object length 1 first 20 NULL

Example cont. stack_struct List object items length first typedef struct stack_struct * stack; struct stack_struct { list items; }; Insert values in given order: 20 7 15 pop() items stack_struct List object length 2 first 7 20 NULL

Example cont. stack_struct List object items length first typedef struct stack_struct * stack; struct stack_struct { list items; }; Insert values in given order: 20 7 15 pop() items stack_struct List object length 3 first 15 7 20 NULL

Example cont. stack_struct List object items length first typedef struct stack_struct * stack; struct stack_struct { list items; }; items stack_struct List object length pop() returned 15 2 first 7 20 NULL

Creating a New Stack typedef struct stack_struct * stack; struct stack_struct { list items; }; stack newStack(int mx_sz) ??? }

Creating a New Stack stack_struct List object items result length typedef struct stack_struct * stack; struct stack_struct { list items; }; // mx_sz-needed for compatibility with array implementation stack newStack(int mx_sz) { stack result = (stack)malloc(sizeof(*result)); result->items = newList(); //mem allocation return result; } items stack_struct List object result length first NULL

Destroying a Stack stack_struct List object items result length first typedef struct stack_struct * stack; struct stack_struct { list items; }; void destroyStack(stack s) { destroyList(s->items); // free memory free(s); } items stack_struct List object result length 3 15 7 20 NULL first

Pushing an Item typedef struct stack_struct * stack; struct stack_struct { list items; }; void push(stack s, Item data) { link L = newLink(data, NULL); insertAtBeginning(s->items, L); }

Popping an Item Stack List s items length first Item pop(stack s) { typedef struct stack_struct * stack; struct stack_struct { list items; }; Item pop(stack s) { link top = removeFirst(s->items); return linkItem(top); } linkItem(top) returns the item in the node top What is wrong with this definition of pop? Item d = pop(s); top = d = items Stack List s length 3 15 7 20 NULL first

Popping an Item typedef struct stack_struct * stack; struct stack_struct { list items; }; Item pop(stack s) { link top = removeFirst(s->items); return linkItem(top); } What is wrong with this definition of pop? Memory leak!!!

Popping an Item Stack List s items length first Item pop(stack s) { if (isStackEmpty(s)) ERROR. No item to remove!!! link top = removeFirst(s->items); Item item = linkItem(top); free(top); return item; } items Stack List s length 3 15 7 20 NULL first

Abstract Datatypes (ADT) ADT (Abstract Data Type) is a data type (a set of values and a collection of operations on those values) that can only be accessed through an interface. Client is a program that uses an ADT. E.g.: walmart.c Will have: #include “list.h” Implementation is a program that specifies the data type and the operations for it. E.g.: list.c Function definitions and struct definitions (or class definitions) Interface a list of operations available for that datatype. E.g.: list.h (notice, a header file, not a c file) It will contain headers of functions and typedef for data types, It is opaque: the client can not see the implementation through the interface.

WHY use a Stack ADT? Why should we have a Stack interface, when all we do is use list operations? Why not use a list directly? Protection: from performing unwanted operations (e.g. an insert at a random position in the list) Flexibility: To modify the current implementation To use another stack implementation Isolates the dependency of the Stack implementation on the List interface: if the list INTERFACE (.h file) is modified we will only need to go and change the STACK implementation (.c file), not all the lines of code where a stack operation is done in all the client programs. It makes the stack behavior explicit: what we can do and we can not do with a stack.

“Explicitly, why is this better than just using a list where I only insert and delete from the beginning?” Keep in mind that your goal here is to implement a stack datatype, for others to use, not for your own one-time usage. Directly providing a list, gives access to operations not allowed for a stack like reversing it, or removing an item other than the ‘top’ one. Any client code that includes the stack.h file, can only use the functions provided in that file so they can only call: pop, push, isEmpty, newStack, destroyStack. They can not reverse the list because they do not have access to the stack_struct definition so they can not get the list object. A function in the stack_list.cpp file can call any list function but that is the point where you focus on implementing a stack and so you should not do operations that are not allowed. Notice that even in this file, you cannot access fields of the list_struct directly. It has to go through function calls (e.g. you cannot write my_list->length, but you can write getLength(my_list) ). The file stack_list.cpp is a client for list.cpp and so it does not have access to the underlying list representation.

Array-Based Implementation of Stacks

Array-based Stacks Array-based implementation: What is a stack? What will hold the data of the stack? push(stack, item) How? : O(1) - can we get this? pop(stack)

Array-based Stacks Array-based implementation: See stack_array.c What is a stack? What will hold the data of the stack? An array. push(stack, item) How? : ‘insert’ at the end of the array. O(1) - Yes pop(stack) How? : ‘remove’ from the end of the array. See stack_array.c

Array-based Stacks index max_size -1 push(5) push(8) push(20) pop() 55 top -1

Array-based Stacks 5 index max_size -1 push(5) push(8) push(20) pop() 55 top 5

Array-based Stacks 8 5 index max_size -1 push(5) push(8) push(20) pop() 55 top 1 8 5

Array-based Stacks 20 8 5 index max_size -1 push(5) push(8) push(20) pop() 55 top 20 2 8 5

Array-based Stacks 8 5 index max_size -1 push(5) push(8) push(20) pop() 55 top 1 8 5

Defining Stacks Using Arrays typedef struct stack_struct * stack; struct stack_struct { Item * items; int top; int max_size; }; 55 Stack items 8 top 1 max_size 5

Creating a New Stack Stack struct stack_struct { Item * items; int top; int max_size; }; stack newStack(int mx_sz) { stack res = (stack)malloc(sizeof(*res)); res->items = (Item*)malloc(mx_sz*sizeof(Item)); res->max_size = mx_sz; res->top = -1; return res; } 55 Stack items top 1 Do not use an array for items (e.g. items[100])! See the Victim-TAB example showing the difference between Stack and Heap memory. max_size

Destroying a Stack void destroyStack(stack s) { struct stack_struct { Item * items; int top; int max_size; }; void destroyStack(stack s) { free(s->items); // s->items is an array free(s); }

Pushing an Item void push(stack s, Item data) { struct stack_struct { Item * items; int top; int max_size; }; void push(stack s, Item data) { if (s->top == s->max_size - 1) ERROR. No more room ( the array is full)!!! return; s->top = s->top + 1; s->items[s->top] = data; }

Popping an Item Item pop(stack s) { if (isStackEmpty(s)) struct stack_struct { Item * items; int top; int max_size; }; Item pop(stack s) { if (isStackEmpty(s)) //ERROR. No data to pop!!! return 0; // return zeroItem(); Item item = s->items[s->top]; s->top = s->top - 1; return item; }