10.Elementary data structures Hsu, Lih-Hsing. Computer Theory Lab. Chapter 11P.2 10.1 Stacks and queues Stacks and queues are dynamic set in which element.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Data Structure HKOI training /4/2010 So Pak Yeung.
Ceng-112 Data Structures I Chapter 5 Queues.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
C Programming : Elementary Data Structures 2009/04/22 Jaemin
Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue()
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 6 Dynamic data structures Motivation Common dynamic ADTs Stacks, queues, lists:
Data Structures & Algorithms
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
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.
Data Structures: Lists i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst.
Stacks.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
CS211 Data Structures Sami Rollins Fall 2004.
Data Structures from Cormen, Leiserson, Rivest & Stein.
Stacks and Queues Dr. Andrew Wallace PhD BEng(hons) EurIng
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null
Objectives of these slides:
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Data Structures Winter What is a Data Structure? A data structure is a method of organizing data. The study of data structures is particularly important.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
Stack and Queue.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Chapter 8 Data Abstractions Introduction to CS 1 st Semester, 2015 Sanghyun Park.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Elementary Data Structures Data Structures and Algorithms A. G. Malamos.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
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’
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
Data Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
1 Data Structures - Part II CS215 Lecture #8. Stacks  Last-In-First-Out (LIFO)  Stacks are often used in programming when data will need to be used.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Linear Data Structures
Chapter 10 Elementary data structures Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials.
Lecture 6 Data Structures. Stack top x.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
STACK Data Structure
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Stacks Chapter 3 Objectives Upon completion you will be able to
CPS120: Introduction to Computer Science Nell Dale John Lewis Abstract Data Types.
STACKS & QUEUES for CLASS XII ( C++).
Elementary data structures
Chapter 12 – Data Structures
Stacks and Queues Chapter 4.
Chapter 15 Lists Objectives
CPSC 311 Section 502 Analysis of Algorithm
CS 583 Analysis of Algorithms
Chapter 20: Binary Trees.
Elementary Data Structures
Chapter 21: Binary Trees.
Algorithms Part III. Data Structures
Dynamic Sets (III, Introduction)
Review & Lab assignments
CS6045: Advanced Algorithms
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Presentation transcript:

10.Elementary data structures Hsu, Lih-Hsing

Computer Theory Lab. Chapter 11P Stacks and queues Stacks and queues are dynamic set in which element removed from the set by the DELETE operation is prespecified. In a stack the element deleted from the set is the one most recently inserted; the stack implements a last-in, first- out, or LIFO, policy. Similarly, in a queue, the element deleted is implements a first-in, first- out, or FIFO, policy.

Computer Theory Lab. Chapter 11P.3 An array implementation of a stack S

Computer Theory Lab. Chapter 11P.4 empty, underflows, overflows STACK_EMPTY(S ) 1 if top[S] = 0 2 then return TRUE 3 else return FALSE

Computer Theory Lab. Chapter 11P.5 PUSH(S,x) 1 top[S]  top[S]+1 2 S[top[S]]  x POP(S ) 1 if STACK-EMPTY(S ) 2 then error “underflow” 3 else top[S]  top[s] -1 4 return S[top[S] + 1]

Computer Theory Lab. Chapter 11P.6 An array implementation of a queue Q

Computer Theory Lab. Chapter 11P.7 ENQUEUE(Q,S ) 1Q[tail[Q]]  x 2 if tail[Q]= length[Q] 3then tail[Q]  1 4else tail[Q]  tail[Q]+1

Computer Theory Lab. Chapter 11P.8 DEQUEUE(Q ) 1 x  Q[head[Q]] 2 if head[Q] = length[Q] 3then head[Q]  1 4else head[Q]  head[Q] +1 5 return x

Computer Theory Lab. Chapter 11P Linked lists

Computer Theory Lab. Chapter 11P.10 LIST_SEARCH(L,k) 1 x ≠ head[L] 2 while x ≠ NIL and key[x] ≠ k 3do x ≠ next[x] 4 return x O(n)

Computer Theory Lab. Chapter 11P.11 LIST_INSERT(L,x) 1 next[x]  head[L] 2 if head[L] ≠ NIL 3then prev[head[L]]  x 4 head[L]  x 5 prev[x]  NIL O(1)

Computer Theory Lab. Chapter 11P.12 LIST_DELETE(L,x) (Call LIST_SEARCH first O(n)) 1 if prev[x] ≠ NIL 2then next[prev[x]]  next[x] 3else head[L]  next[x] 4 if next[x] ≠ NIL 5 then prev[next[x]  prev[x] O(1) or O(n)

Computer Theory Lab. Chapter 11P.13 A Sentinel is a dummy object that allows us to simplify boundary conditions,

Computer Theory Lab. Chapter 11P.14 LIST_DELETE ’ (L,x) 1 next[prev[x]]  next[x] 2 prev[next[x]]  prev[x]

Computer Theory Lab. Chapter 11P.15 LIST_SEARCH ’ (L,k) 1 x  next[nil[L]] 2 while x ≠ nil[L] and key[x] ≠ k 3 do x  next[x] 4 return x

Computer Theory Lab. Chapter 11P.16 LIST_INSERT ’ (L,x) 1 next[x]  next[nil[L]] 2 prev[next[nil[L]]]  x 3 next[nil[L]]  x 4 prev[x]  nil[L]

Computer Theory Lab. Chapter 11P Implementing pointers and objects A multiple-array representation of objects

Computer Theory Lab. Chapter 11P.18 A single array representation of objects

Computer Theory Lab. Chapter 11P.19 Allocating and freeing objects--garbage collector

Computer Theory Lab. Chapter 11P.20 Allocate_object( ),LIST_INSERT(L,4),Key(4)=25

Computer Theory Lab. Chapter 11P.21 LIST_DELETE(L,5), FREE_OBJECT(5)

Computer Theory Lab. Chapter 11P.22 ALLOCATE_OBJECT( ) 1if free = NIL 2then error “out of space” 3else x  free 4free  next[x] 5return x

Computer Theory Lab. Chapter 11P.23 FREE_OBJECT(x ) 1 next[x]  free 2 free  x

Computer Theory Lab. Chapter 11P.24 Two link lists

Computer Theory Lab. Chapter 11P Representing rooted trees

Computer Theory Lab. Chapter 11P.26 Binary trees

Computer Theory Lab. Chapter 11P.27 Rooted tree with unbounded branching

Computer Theory Lab. Chapter 11P.28 Other tree representation