Implementing stacks Prof. Ramin Zabih

Slides:



Advertisements
Similar presentations
Data Structures: A Pseudocode Approach with C
Advertisements

Linked lists Prof. Noah Snavely CS1114
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
Linked lists and memory allocation Prof. Noah Snavely CS1114
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Important Problem Types and Fundamental Data Structures
Storage allocation issues Prof. Ramin Zabih
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Breadth-first and depth-first traversal Prof. Noah Snavely CS1114
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
COMP 103 Course Review. 2 Menu  A final word on hash collisions in Open Addressing / Probing  Course Summary  What we have covered  What you should.
Homework 6 Shortest Paths!. The Basic Idea You have seen BFS and DFS tree searches. These are nice, but if we add the concept of “distance”, we can get.
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.
Final Exam Review COP4530.
Data Structure By Amee Trivedi.
Prof. Ramin Zabih Implementing queues Prof. Ramin Zabih
Top 50 Data Structures Interview Questions
CSCI-255 LinkedList.
Data Structure Interview Question and Answers
Chapter 15 Lists Objectives
CS 315 Data Structures B. Ravikumar Office: 116 I Darwin Hall Phone:
CS 1114: Implementing Search
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
UNIT-3 LINKED LIST.
Basic Data Structures.
Csc 2720 Instructor: Zhuojun Duan
Hashing Exercises.
structures and their relationships." - Linus Torvalds
Chapter 1.
Data Structures and Algorithms for Information Processing
CSE 326: Data Structures: Midterm Review
Lecture 15 CSE 331 Sep 29, 2014.
Graphs Representation, BFS, DFS
Data Structures – Stacks and Queus
Introduction to Data Structures
Lesson 6. Types Equality and Identity. Collections.
Lectures on Graph Algorithms: searching, testing and sorting
Graphs.
Graphs.
CSE 214 – Computer Science II B-Trees
CSE 373 Data Structures Lecture 16
Richard Anderson Autumn 2016 Lecture 5
Advanced Implementation of Tables
Lecture 16 CSE 331 Oct 8, 2012.
CSE 12 – Basic Data Structures
Richard Anderson Winter 2009 Lecture 6
Data Structures & Algorithms
EE 312 Final Exam Review.
Graphs.
Graphs.
Linked lists Prof. Noah Snavely CS1114
Graphs.
Important Problem Types and Fundamental Data Structures
Richard Anderson Winter 2019 Lecture 6
Graphs.
Analysis and design of algorithm
Richard Anderson Winter 2019 Lecture 5
Prof. Ramin Zabih Graph Traversal Prof. Ramin Zabih
Richard Anderson Autumn 2015 Lecture 6
DATA STRUCTURES IN PYTHON
Lecture-Hashing.
Presentation transcript:

Implementing stacks Prof. Ramin Zabih

2 Administrivia  Assignment 3 is out, due this+next Friday  Prelim! –Options: Thursday 10/4 or 10/11 The other date will have Quiz 4  First optional evening lecture: –Bobby Kleinberg on graphs –Wednesday evening 10/3 (exact time TBD) –RPC Auditorium (RPC205)  A final note about big-O notation –Linear vs quadratic algorithms

3 Depth versus breadth  In DFS, you only need to keep track of the vertices “above” you –I.e., on the path to the root  In BFS, you need to store all the vertices at the same level –I.e., same distance to the root  In a binary tree with 100 levels, this is the difference between storing 100 vertices and storing vertices! –So, why ever do BFS??

4 Computers and arrays  The notion of “constant time” operations relies on a model of the hardware  Computer memory is a large array –We will call it M  In constant time, a computer can: –Read any element of M –Change any element of M to another element –Perform any simple arithmetic operation  This is more or less what the hardware manual for an x86 describes

5 Data structures in memory  This week we will focus on implementing various data structures using the array M  We have a contract we wish to fulfill –Example: stack contract If we push X onto S and then pop S, we get back X, and S is as before  We want to fulfill this contract using the memory array M  We’ll look at a related data structure that is even more useful than a stack

6 Linked lists  You can add items to it, and you can process the items you added  Unlike a stack, you don’t need to pop it in order to get at all the items  Adding items takes constant time  Getting each item takes linear time –Example: searching the list for an element  You can also delete an element –This is always the hard part of any data structure, and the source of most bugs

7 Linked lists 8413

8 Linked lists as memory arrays  We’ll implement linked lists using M –This is very close to what the hardware does More details in CS316  A linked list contains “cells”  A value, and where the next cell is –We will represent cells by a pair of adjacent array entries

9 A few details  I will draw odd numbered entries in blue and even ones in red –Odd entries are values Number interpreted as list elements –Even ones are “cells” Number interpreted as index of the next cell AKA location, address, or pointer  The first cell is M(1) and M(2), for now  The last cell has 0, i.e. pointer to M(0) –Also called a “null pointer”

10 Example X 789

11 Access every item in a list  Start at the first cell, M(1) and M(2)  Access the first value, M(1)  The next cell is at location v=M(2)  If v = 0, we are done  Otherwise, access the next value, M(v)  The next cell is at location v’ = M(v+1)  Keep on going until the next cell is at location k, such that M(k+1) = 0

12 Adding a header  We can represent the linked list just by the initial cell, but this is problematic –Think about deleting the last cell! –More subtly, it’s also a problem for insertion  Instead, we will have a few entries in M that are not cells, but instead hold some information about the list –Specifically, a pointer to the first element –Having a counter is also useful at times

13 Insert and delete  To insert an element, we create a new cell and splice it into the list –Various places it could go –Need to update header info, and the cell (if any) before the new one  To delete an element we simply need to update the header and to change some pointers –Basically, need to “skip over” deleted element

14 Linked list with header  Initial list:  Insert (end):  Insert (start):  Delete (end):  Delete (start): XXX E S XXX XXX 789