Stacks - 2 Nour El-Kadri ITI 1121. 2 Implementing 2 stacks in one array and we don’t mean two stacks growing in the same direction: but two stacks growing.

Slides:



Advertisements
Similar presentations
Chapter 21 Implementing lists: array implementation.
Advertisements

CHP-5 LinkedList.
Part IV: Memory Management
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
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.
Chapter 10 Introduction to Arrays
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
1 Hashing (Walls & Mirrors - end of Chapter 12). 2 I hate quotations. Tell me what you know. – Ralph Waldo Emerson.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
Hash Tables1 Part E Hash Tables  
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Hashing General idea: Get a large array
Pointers Applications
Stacks, Queues, and Deques. 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.
CHP - 9 File Structures. INTRODUCTION In some of the previous chapters, we have discussed representations of and operations on data structures. These.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Chapter 3 Introduction to Collections – Stacks Modified
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Stack and Heap Memory Stack resident variables include:
Chapter 2 ARRAYS.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
1 Hash table. 2 Objective To learn: Hash function Linear probing Quadratic probing Chained hash table.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
Can’t provide fast insertion/removal and fast lookup at the same time Vectors, Linked Lists, Stack, Queues, Deques 4 Data Structures - CSCI 102 Copyright.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Fundamentals of Python: From First Programs Through Data Structures Chapter 13 Collections, Arrays, and Linked Structures.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Stacks Nour El-Kadri CSI Stacks Software stacks are abstract data types (structures) similar to physical stacks, Plates Trays Books PEZ dispenser.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
Chapter 7 Memory Management Eighth Edition William Stallings Operating Systems: Internals and Design Principles.
Stacks – Cont’d Nour El-Kadri ITI Summary We have seen that arrays are efficient data structures. Accessing any element of an array necessitates.
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
An Array-Based Implementation of the ADT List
Review Array Array Elements Accessing array elements
CHP - 9 File Structures.
Arrays (review) CSE 2011 Winter May 2018.
Hash table CSC317 We have elements with key and satellite data
Fundamentals of Programming II Working with Arrays
Java Review: Reference Types
Pointers and Dynamic Variables
Arrays and Linked Lists
Further Data Structures
Arrays .
Introduction to Data Structure
Arrays.
Classes and Objects Object Creation
Stacks – Cont’d Nour El-Kadri CSI 1101.
Presentation transcript:

Stacks - 2 Nour El-Kadri ITI 1121

2 Implementing 2 stacks in one array and we don’t mean two stacks growing in the same direction: but two stacks growing in opposite directions:

3 Implementing 2 stacks in one array How? One array but two distinct instance variables for the two tops: Why? Memory management works like that. Such implementation may reduce the amount of memory that is wasted.

4 Primitive vs. reference

5 Earlier we said that pop consists of: 1. saving the current top value, 2. reset stack[top], 3. decrement top, 4. return the saved value. Is it necessary to set the top value to null?

6 Since a reference from the “stack” to the object exists, this prevents the garbage collector from doing its job.

7 No reference to the object, therefore gc() can do its job.

8 Note: error condition Pre- and post-conditions should be checked and the appropriate Exceptions should be thrown. if (! s.empty()) v = s.pop();... if (! s.isFull()) s.push(v);

9 Properties of the arrays Arrays are accessed by index position, e.g. a[3] designates the fourth position of the array. Access to a position is very fast. We say that indexing is a random access operation in the case of the arrays, which means that the access to any element of an array always takes a constant number of steps, we say the operation necessitates constant time, i.e. the time to access an element is independent of: The size of the array; The number of elements that are in the array; The position of the element that we wish to access (first, last, middle).

10 Access to any element of an array is fast because the elements of an array are stored contiguously in memory. The array starts at some address of the memory, let’s call this the base address, then the first element is stored at this address and the location of the next element depends on the size of an element.

11 All the elements of an array occupy the same amount of space and therefore the address of any element is simply, base address+offset where the offset is index _ size of an element The first element (index = 0) is found at the base address, the second at the base address plus the size of one element, and so on.  No search involved.

12 Fixed size arrays What if the size of an array is not known? Suppose, you were asked to read positive integers from the input until a special value is read (sentinel), say -9, and the values should be stored in a array.

13 How large should you declare this array?*?  Certain programming languages, such as Fortran and Pascal, require you to specify the size of the array at compile time.

14 Solution 1: make it large enough A possible solution would be to create an array that would be suitable for even the largest application. What are the consequences of such actions? If the array is too large this wastes a lot memory. When the array is full the program may be forced to stop.

15 Solution 2: variable size arrays Create an array of a reasonable default capacity. Increase or decrease its size according to the need. This means that the logical size of the array will not correspond to its physical size. Which means that it’s up to the programmer to maintain information about the logical size (the instance variable length of an array refers to its physical size). It’s the responsibility of the programmer to access elements that are below the logical size.

16 Qualify the behavior of the solution as the size of the array increases. All the elements of the array have to be copied. The more elements there are in the array the more copies are needed. Initially, there are only few elements to be copied, but the larger the array the more copies are needed. Insertion and resizing are related to one another. Once the logical size of the array equals its physical size every subsequent insertion necessitates resizing the array, which has a cost that is proportional to the number of elements in the array.

17 A more practical solution consists of doubling the size of the array whenever the logical size of the array equals its physical size. What have been achieved? Not all insertions require resizing the array, hence copying its elements. What has been lost? Memory efficiency.  for some applications, the logical size of the array can also decrease, in which cases, the physical size of the array could also be decreased whenever the number of elements is below a certain threshold.

18 1. is the array big enough? 2. where do we start coping the elements?  implement remove(int pos)