Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington Last updated: 2/17/2016 1.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Stack & Queues COP 3502.
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.
1 Abstract Data Types. Objectives To appreciate the concept and purpose of abstract data types, or ADTs To understand both the abstract behavior and the.
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 CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
CS Winter 2011 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing.
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.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
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.
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.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
Click to edit Master text styles Stacks Data Structure.
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++).
Stack ADT (Abstract Data Type) N …
Stacks.
CSE373: Data Structures & Algorithms
Data Structure By Amee Trivedi.
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.
COSC160: Data Structures: Lists and Queues
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
Data Structures – Week #3
Stack and Queue.
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
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
Popping Items Off a Stack Lesson xx
CSC 143 Queues [Chapter 7].
ITEC 2620M Introduction to Data Structures
Programming Abstractions
Data Structures and Algorithms
CSC 143 Stacks [Chapter 6].
Abstract Data Type Abstract Data Type as a design tool
Abstract Data Types and Stacks
Stacks and Queues CSE 373 Data Structures.
Stack.
FIFO Queues CSE 2320 – Algorithms and Data Structures Alexandra Stefan
Dynamic allocation (continued)
Stacks, Queues, and Deques
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Abstract Data Types Stacks CSCI 240
Lecture 9: Stack and Queue
Abstract Data Types (ADTs)
Abstract Data Types and Stacks
Presentation transcript:

Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington Last updated: 2/17/2016 1

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.

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 such a queue must support are: – 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 3

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 – First inserted -> FIFO Queue – Random item. – Item with the smallest key (if each item contains a key). -> Priority Queue (Heap) 4

Push and Pop The pushdown stack supports insert and delete as follows: – insert push: This is what we call the insert operation when we talk about pushdown stacks. It puts an item "on top of the stack". – delete pop: This is what we call the delete operation when we talk about pushdown stacks. It removes the item that was on top of the stack (the last item to be pushed, among all items still on the stack). 5

Examples of Push and Pop push(15) push(20) pop() push(30) push(7) push(25) pop() push(12) pop() 6 15

Examples of Push and Pop push(15) push(20) pop() push(30) push(7) push(25) pop() push(12) pop()

Examples of Push and Pop push(15) push(20) pop() – returns 20 push(30) push(7) push(25) pop() push(12) pop() 8 15

Examples of Push and Pop push(15) push(20) pop() push(30) push(7) push(25) pop() push(12) pop()

Examples of Push and Pop push(15) push(20) pop() push(30) push(7) push(25) pop() push(12) pop()

Examples of Push and Pop push(15) push(20) pop() push(30) push(7) push(25) pop() push(12) pop()

Examples of Push and Pop push(15) push(20) pop() push(30) push(7) push(25) pop() – returns 25 push(12) pop()

Examples of Push and Pop push(15) push(20) pop() push(30) push(7) push(25) pop() push(12) pop()

Examples of Push and Pop push(15) push(20) pop() push(30) push(7) push(25) pop() push(12) pop() – returns 12 pop()

Examples of Push and Pop push(15) push(20) pop() push(30) push(7) push(25) pop() push(12) pop() pop() – returns

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 16

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

List-Based Stacks List-based implementation: – What is a stack? A stack is essentially a list. – push(stack, item) How? : O(??) – pop(stack) How? : O(??) 18

List-Based Stacks List-based implementation: – What is a stack? A stack is essentially a list. – push(stack, item) How? : O(1) – ideally !!!! (frequent operation for this data structure) – pop(stack) How? : O(1) – ideally !!!! (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? 19 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. O(1) 20 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. 21

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 Item isStackEmpty(stack s); 22

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

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

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

Defining Stacks typedef struct stack_struct * stack; struct stack_struct { list items; }; 26 items Stack object length 1 List object first 20 NULL Insert values in given order: pop()

Defining Stacks typedef struct stack_struct * stack; struct stack_struct { list items; }; 27 items Stack object length 2 List object first 7 20 NULL Insert values in given order: pop()

Defining Stacks typedef struct stack_struct * stack; struct stack_struct { list items; }; 28 items Stack object length 3 List object first NULL Insert values in given order: pop()

Defining Stacks typedef struct stack_struct * stack; struct stack_struct { list items; }; 29 items Stack object length 2 List object first 7 20 NULL pop() returned 15

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

Creating a New Stack 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 alloc return result; } 31

Destroying a Stack typedef struct stack_struct * stack; struct stack_struct { list items; }; void destroyStack(stack s) { ??? } 32

Destroying a Stack typedef struct stack_struct * stack; struct stack_struct { list items; }; void destroyStack(stack s) { destroyList(s->items); free(s); } 33

Pushing an Item typedef struct stack_struct * stack; struct stack_struct { list items; }; void push(stack s, Item content) { ??? } 34

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); } 35

Popping an Item typedef struct stack_struct * stack; struct stack_struct { list items; }; Item pop(stack s) { ??? } 36

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); } 37 What is wrong with this definition of pop?

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); } 38 What is wrong with this definition of pop? Memory leak!!!

Popping an Item typedef struct stack_struct * stack; struct stack_struct { list items; }; 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; } 39

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 ops (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 when 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. 40

“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 the list, or removing any item in it. 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 stack_list.cpp is a client for list.cpp and so it does not have access to the list representation. 41

Array-Based Implementation of Stacks 42

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) How? : O(1) - can we get this? 43

Array-based Stacks Array-based implementation: – 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. O(1) - Yes See stack_array.c 44

Array-based Stacks max_size -1 0 top index

Array-based Stacks max_size top index push(5) 5

Array-based Stacks max_size top index push(5) push(8) 5 8

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

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

Defining Stacks Using Arrays typedef struct stack_struct * stack; struct stack_struct { Item * items; int top; int max_size; };

Creating a New Stack struct stack_struct { Item * items; int top; int max_size; }; stack newStack(int mx_sz) { stack result = (stack)malloc(sizeof(*result)); result->items = (Item*)malloc(mx_sz * sizeof(Item)); result->max_size = mx_sz; result->top = -1; return result; } 51

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

Pushing an Item 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; } 53

Popping an Item struct stack_struct { Item * items; int top; int max_size; }; ??? pop(stack s) { ??? } 54

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