Information Science 2

Similar documents
1 P age DS & OOPS / UNIT II

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.


EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

cs Java: lecture #6

Lecture 4 Stack and Queue

PA3 Design Specification

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Linear Data Structures

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

Data Structure. IBPS SO (IT- Officer) Exam 2017

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

Information Science 2

CS350 - Exam 1 (100 Points)

Lecture Data Structure Stack

Dynamic Data Structures

Ashish Gupta, Data JUET, Guna

Linked List. April 2, 2007 Programming and Data Structure 1

3. Fundamental Data Structures

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Overview of Data. 1. Array 2. Linked List 3. Stack 4. Queue

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

infix expressions (review)

Fundamentals of Data Structure

Data Structure. Recitation VII

Stack and Queue. Stack:

.:: UNIT 4 ::. STACK AND QUEUE

Linked Lists in C and C++

Stacks and Queues. CSE 373 Data Structures Lecture 6

CS301 - Data Structures Glossary By

University of Palestine. Final Exam 2 nd semester 2014/2015 Total Grade: 50

ITI Introduction to Computing II

Linked Lists and Abstract Data Structures A brief comparison

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Insert. SEMCOM Page 1 of 11

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113)

12 Abstract Data Types

8. Fundamental Data Structures

Cpt S 122 Data Structures. Data Structures

CS 206 Introduction to Computer Science II

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

CS 206 Introduction to Computer Science II

STACKS AND QUEUES. Problem Solving with Computers-II

CSE 230 Intermediate Programming in C and C++

LIFO : Last In First Out

Queues Fall 2018 Margaret Reid-Miller

Postfix (and prefix) notation

Linked lists (6.5, 16)

Elementary Data Structures. Stacks, Queues, & Lists Amortized analysis Trees

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Lecture 10 Notes Linked Lists

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

Chapter 18: Stacks And Queues

Data Structures. Jia-Liang Lu. Slides based on the course notes of C.A. Shaffer

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

a) State the need of data structure. Write the operations performed using data structures.

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

Department of Computer Science and Technology

Lecture 10 Notes Linked Lists

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list.

Final Exam Data Structure course. No. of Branches (5)

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Logistics. Half of the people will be in Wu & Chen The other half in DRLB A1 Will post past midterms tonight (expect similar types of questions)

What is an algorithm?

Jump Tables A jump table is essentially a list of places in the code to jump to. This can be thought of in C as an array of function pointers. In Asse

V.S.B ENGINEERING COLLEGE DEPARTMENT OF INFORMATION TECHNOLOGY I IT-II Semester. Sl.No Subject Name Page No. 1 Programming & Data Structures-I 2

Sequence Abstract Data Type

table1 = [ [1, 2, 3, 4], [4, 5, 6, 7], [8, 9,10,11] ] we get: >>> sumcol(table1,0) 13 >>> sumcol(table1,1) 16

1. Attempt any three of the following: 15

Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists

III Data Structures. Dynamic sets

Introduction to Programming in C Department of Computer Science and Engineering

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

CS 171: Introduction to Computer Science II. Linked List. Li Xiong

Queues. Lesson 4. CS 32: Data Structures Dept. of Computer Science

Chapter 18: Stacks And Queues

Data Structures -- Introduction

Your Topic Goes Here Queue

CS 216 Exam 1 Fall SOLUTION

INSTITUTE OF AERONAUTICAL ENGINEERING

Programming. Lists, Stacks, Queues

1 Deletion in singly linked lists (cont d) 1 Other Functions. 1 Doubly Linked Lists. 1 Circular lists. 1 Linked lists vs. arrays

Foundations of Data Structures

[2:3] Linked Lists, Stacks, Queues

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

Stacks, Queues and Hierarchical Collections

Sample Question Paper

Transcription:

Information Science 2 - Basic Data Structures- Week 02 College of Information Science and Engineering Ritsumeikan University

Today s class outline l Basic data structures: Definitions and implementation issues - Singly and doubly linked lists - Stack - Queue l Quiz 2

Linked lists: Overview l Arrays and node-based data structures l Linked lists - The idea - Main operations - Single vs. double linked 3

Simplest data structure Array l The array is a fundamental building block for data structures: - Objects are stored in contiguous memory locations and referenced by an index - There is little memory-management overhead - Look-up operations are usually very fast - Operations that require rearrangement (insert, remove, etc.) can be slow 4

Nodes l A node is a small block of memory, referenced via its address, which may point to other nodes via addresses Node with one possible link Node data (element) Node with two possible links Node data (element) Pointer Pointer Pointer - A pointer is a variable that contains an address number - The node s pointers can have addresses of other nodes or may point to NULL, a special address for nothing 5

Node-based structures: Physical vs. Logical l When we draw pictures of node-based data structures, the positions of nodes in the picture have nothing to do with their positions in memory - For example, if a structure is stored like this (physical structure): - Then we might re-draw it like this (logical structure): 6

Linked lists: The idea l A linked list is a collection of nodes linked via pointers (references) Head NULL pointer 3 1 5 4 5 2 - The head of the list generally stores a pointer (reference) to the first node, or NULL if the list is empty. It can also store other information (data type, etc.) - There must be some way of identifying the last node. Typically, it is done by making its pointer (reference) NULL 7

Linked lists: Main operations l The main operations are to add (insert) a node and to delete a node l The operations require two steps: Finding the correct location Doing the work to add / delete the node l There are three possible positions of a node in the list: The front The end Somewhere in the middle 8

Doubly linked list l In a singly linked list, a node stores a data element and one pointer (reference). In a doubly linked list, each node stores: - Data-carrying element - Link to the next node - Link to the previous node prev node element l A doubly linked list also has special nodes called header and trailer nodes header nodes/positions next trailer elements (e.g. pictures) 9

Links: Doubly or singly? l Advantages of doubly linked lists: Can be traversed in either direction (may be essential for some programs) Some operations, such as deleting and inserting before a node, become easier l Disadvantages of doubly linked lists: Requires more space List manipulations are slower (because more links must be changed) Greater chance of having bugs (because more links must be manipulated) 10

Stacks: Overview l The concept of a stack l Main operations on a stack: - Push, pop - Initialize - Check status (empty or full) l Implementation issues l Application examples 11

What is a stack? l A stack is a linear data structure that stores a set of elements in a particular order: It realizes a FILO (First-In, Last-Out) interface l Sometimes it is called a push-down stack l Real life examples: spring-loaded coin dispensers plate dispensers at a cafeteria 12

Push and Pop operations l Plate dispensers are stacks: - Operation Push(item) adds item to top of stack - Operation Pop(item) removes item from top of stack and returns it - Number of items in the stack can vary from 0 to some N, which determines its storage capacity 13

A typical sequence of operations on a stack l Initialize the stack l Determine stack size l Check the status: Is the stack empty or full? l Push an item onto the top of the stack l l Pop an item off the top of the stack l l Clear the stack l 14

Implementation l Array-based stack implementation: Allocate array of some size = maximum number of elements in stack Bottom stack element stored at index 0 Current index tells which element is the top Increment index when element is pushed, decrement when popped Top index 0 l Linked list-based stack implementation: Allocate a node for each stack element Nodes are chained together by reference to next node in the chain : 2 1 15

Stack application example l Determining well-formed expressions with parenthesis Well-Formed Expressions Ill-Formed Expressions ( x x x [ ] ) ( x x x [ ) ( ) [ ] { } ( ( { ( x x x x x ) ( ) } { x x x x x ) ( ) } l Check the expression: ( x { x [ ] } x ) 16

Application example (cont-d) l Check the expression: ( x { x [ ] } x ) '(' : Push ( 'x' : Ignore '{' : Push { 'x' : Ignore '[' : Push [ ']' : Get top of stack, opensymbol='[' Pop Compare if '[' matches ']' '}' : Get top of stack, opensymbol='{' Pop Compare if '{' matches '}' 'x' : Ignore ')' : Get top of stack, opensymbol='(' Pop Compare if '(' matches ')' '\n' : Print expression is well-formed 17

Another example: 1+2 3+4 l l l 1 : push 1 on number stack + : push + on operation stack 2 : push 2 on number stack l : because has higher precedence than +, push onto operation stack l 3 : push 3 onto number stack l + : because + has lower precedence than pop 3, 2, and compute 2 3=6, and push 6 onto number stack push + onto operation stack l 4 : push 4 onto number stack l expression end : pop 4, 6 and +, compute 6+4=10, push 10; pop 10, 1, and +, compute 1+10=11, push 11 l 11 (at the top of the stack) is the answer 18

Queues: Overview l The concept of a queue l Main operations on a queue: - enqueue, dequeue l Implementation issues 19

What is a queue? l A queue is a linear data structure that stores a set of elements in a particular order: It realizes a FIFO (First In First Out) interface l Real life examples: Check-out in a supermarket Printing with network printers 20

Main operations on a queue l enqueue to add a new item at the rear l dequeue to remove an item from the front l queueempty to check whether the queue is empty or not l queuefull to check whether the queue is full or not l queuesize returns the number of items in the queue l queuepeek returns the front item C B A rear front 21

l Array-based queue implementation: Implementation D C B A Max_Size rear front After dequeue(a): n-1 3 2 n-1 3 2 D C B 1 0 1 0 Max_Size rear front l Problem: Once rear is at the end of this array, and there is a new item coming in, what can we do? 22

Implementation (cont-d) l A simple and elegant solution is to wrap the array around to create a circular array (queue): n-1 3 2 1 0 C B A front=0 1 0 A 2 B C 3 rear=3 rear=3 front=0 n-1 l Circular array with size n can at most hold n - 1 items 23

Circular queue operations enqueue rear = (rear + 1) MOD n dequeue front = (front + 1) MOD n 1 2 B C 3 rear=3 front=1 1 2 B C 3 0 A 0 A n-1 n-1 l l If (rear == front) then queueempty=true If ((rear + 1) MOD n == front) then queuefull=true 24

Implementing a queue using enqueue(2, 8, 4, 7) a linked list rear front dequeue rear front enqueue(3) rear front 25

Queue application example l Check-out line using a queue: 26

Summary l We have briefly surveyed three types of basic data structures singly and doubly linked lists stacks (FILO) queues (FIFO) l You need to clearly understand the concepts of these data structures, and also to be able to give examples of stack and queue applications 27

l Read these slides again l Receive self-preparation assignments and study them thoroughly Homework l Learn the English terms new for you 28

Next class l Applications of basic data structures - basics of Graph Theory - binary trees - (data) networks 29