Dynamic Array: Implementation of Stack

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Advertisements

Today’s Agenda  Stacks  Queues  Priority Queues CS2336: Computer Science II.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
CS261 Data Structures Dynamic Arrays. Pro: only core data structure designed to hold a collection of elements Pro: random access: can quickly get to any.
CS 261- Winter 2009 Dynamic Array Queue and Deque.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
1 Problem Solving Abstraction Oftentimes, different real-world problems can be modeled using the same underlying idea Examples: Runtime storage, Undo operation.
CMPT 225 ADT List using Dynamic arrays A dynamic data structure is one that changes size, as needed, as items are inserted or removed The Java ArrayList.
CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
CS Winter 2011 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing.
Stacks, Queues, and Deques
Stacks, Queues, and Deques
CS 171: Introduction to Computer Science II Stacks Ymir Vigfusson.
EXPANDING STACKS AND QUEUES CS16: Introduction to Data Structures & Algorithms 1 Tuesday, February 10, 2015.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
CS261 Data Structures Dynamic Arrays Part 2: Implementation.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
Data structures Abstract data types Java classes for Data structures and ADTs.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Data Structures & Algorithms
CS 261 Fall 2009 Dynamic Array Introduction (aka Vector, ArrayList)
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Dynamic Array Queue and Deque
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
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.
An Array-Based Implementation of the ADT List
Memory Management.
STACKS & QUEUES for CLASS XII ( C++).
Review Array Array Elements Accessing array elements
18 Chapter Stacks and Queues
Linked List Introduction
G64ADS Advanced Data Structures
Chapter 19 Java Data Structures
MEMORY REPRESENTATION OF STACKS
Chapter 20 Lists, Stacks, Queues, and Priority Queues
CS 1114: Implementing Search
Abstraction A tool (concept) to manage complexity
Data Structures and Algorithms
Dr. Bernard Chen Ph.D. University of Central Arkansas
Abstract Data Types and Stacks
Chapter 12: Data Structures
Cinda Heeren / Geoffrey Tien
October 30th – Priority QUeues
Bag Implementations that Use Arrays
structures and their relationships." - Linus Torvalds
Abstract Data Types (ADTs)
Stacks, Queues, and Deques
Data Representation Methods
More Data Structures (Part 1)
Data Representation Methods
Stacks, Queues, and Deques
Array Based Collections
Data Structures and Algorithms
Introduction to Data Structure
Lists.
Dynamic allocation (continued)
Priority Queues & Heaps
Dynamic Array: Implementation of Queue & Deque
Data Representation Methods
Data Representation Methods
Stacks, Queues, and Deques
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked List Intro CSCE 121.
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
structures and their relationships." - Linus Torvalds
Abstract Data Types Stacks CSCI 240
Abstract Data Types (ADTs)
Presentation transcript:

Dynamic Array: Implementation of Stack CS 261 – Data Structures Dynamic Array: Implementation of Stack

First: Review of Stacks Stack abstraction keeps elements ordered by time of insertion: Reverse order from time of insertion Think of a stack of plates

Stack Functions: Abstractly Abstractly, stack characterized by following operations: init() push(newElement) top() pop() isEmpty() ... And, the LIFO property Other APIs have different names for same stack functions

Stack Interface /* Stack interface. */ int isEmptyDynArr(struct DynArr *v); void pushDynArr(struct DynArr *v, TYPE val); TYPE topDynArr(struct DynArr *v); void popDynArr(struct DynArr *v); /* push, top, pop are all O(1) */

Arrays: Pro’s and Con’s Only core data structure designed to hold a collection of elements Random access: can quickly get to any element  O(1) Fixed size: Maximum number of elements must be specified when created Often don’t know much much space needed until you are done Homogeneity: cannot hold different types in the same array

Dynamic Array (Vector or ArrayList) The dynamic array (called Vector or ArrayList in Java, same thing, different API) gets around this by encapsulating a partially filled array Hide memory management details behind a simple API Is still randomly accessible, but now it grows as necessary

Size and Capacity Unlike arrays, a dynamic array can change its capacity Size (or count) is logical array size: Current number of elements in the dynamic array What the programmer thinks of as the size of the array Managed by an internal data value Capacity is physical array size: # of elements it can hold before it must resize Size Capacity data “Unused” elements

Partially Filled Array data = cnt = 10 cap = 16 Size ( = cnt) Capacity ( = cap)

Adding an element Adding an element to end is usually easy — just increase the (logical) size, and put new value at end What happens when size reaches capacity? Must reallocate new data array - but this detail is hidden from user

Set Capacity: Reallocate and Copy Before reallocation: After reallocation: data = 8 cnt = data = 16 cap = cnt = 8 cap = 8 Must allocate new (larger) array and copy valid data elements Also...don’t forget to free up the old array

(We will be doing this later, doesn’t occur in stack) Adding to Middle Adding an element to middle can also force reallocation (if the current size is equal to capacity) But will ALWAYS require elements to moved up to make space Is therefore O(n) worst case (We will be doing this later, doesn’t occur in stack)

Adding to Middle (cont.) Must make space for new value Careful! Loop from top down while copying data Add at idx  idx  Before After

(We will be doing this later, also not part of stack ADT) Removing an Element Removing an element will also require “sliding over” to delete the value Therefore is O(n) worst case (We will be doing this later, also not part of stack ADT)

Picture of Remove Element Remove also requires loop. This time should it be from top or bottom? Remove idx  Before After

Element Types How to make a general purpose container class? We define element type as symbolic preprocessor constant. Default double. Requires recompiling source for new element types. Not elegant, but workable.

Interface File: dynarr.h #ifndef __DYNARR_H #define __DYNARR_H # define TYPE double # define TYPE_SIZE sizeof(TYPE) # define LT(a, b) ((a) < (b) # define EQ(a, b) ((a) == (b)) ... /* Rest of dynarr.h (on next slide). */ #endif

Interface (cont.) struct DynArr { TYPE *data; /* Pointer to data array. */ int cnt; /* Number of elements in collection. */ int cap; /* Capacity of array. */ }; /* The stack interface. */ void initDynArr(struct DynArr *v, int cap); void pushDynArr(struct DynArr *v, TYPE e); TYPE topDynArr(struct DynArr *v); void popDynArr(struct DynArr *v); int isEmptyDynArr(struct DynArr *v);

Initialization: initDynArr void dynArrInit(struct DynArr *v, int cap) { v->data = (TYPE *)malloc(cap * TYPE_SIZE); assert(v->data != 0); v->cnt = 0; v->cap = cap; }

Clean Up: freeDynArr void freeDynArr(struct DynArr *v) { free(v->data); v->data = 0; v->cap = 0; v->cnt = 0; }

Concretely, in C..using the dynArray Concretely, it’s a bit more complicated: struct DynArr d; initDynArr(&d, 8); pushDynArr(&d, 1.1); if (!isEmptyDynArr(&d)) { TYPE e = topDynArr(&d); popDynArr(&d); }

Your Chance In-class lesson time: finish implementation of simple push and pop, including reallocation Homework assignment deals with reallocation (why doubling is good) Next time we look at linked list implementation of stack