UNIT 6A Organizing Data: Lists. Data Structure

Similar documents
DATA STRUCTURES USING C

Chapter 3: Restricted Structures Page 1

Module 2 Stacks and Queues: Abstract Data Types

Data Structures and Algorithms Stacks and Queues

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Sequential Data Structures

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Algorithms and Data Structures

10CS35: Data Structures Using C

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Common Data Structures

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Data Structures Using C++ 2E. Chapter 5 Linked Lists

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Linked Lists Linked Lists, Queues, and Stacks

PROBLEMS (Cap. 4 - Istruzioni macchina)

Data Structures and Data Manipulation

PES Institute of Technology-BSC QUESTION BANK

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Introduction to Data Structures and Algorithms

Data Structure [Question Bank]

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

Data Structures and Algorithms

Introduction to Data Structures

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

2) Write in detail the issues in the design of code generator.

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

COMPUTER SCIENCE. Paper 1 (THEORY)

STACKS,QUEUES, AND LINKED LISTS

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Chapter 5 Instructor's Manual

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

Symbol Tables. Introduction

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

West Virginia University College of Engineering and Mineral Resources. Computer Engineering 313 Spring 2010

Universidad Carlos III de Madrid

Base Conversion written by Cathy Saxton

Data Structures UNIT III. Model Question Answer

Data Structures and Algorithms V Otávio Braga

Data Structures. Level 6 C Module Descriptor

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Stacks. Data Structures and Data Types. Collections

Big O and Limits Abstract Data Types Data Structure Grand Tour.

7.1 Our Current Model

Application of Stacks: Postfix Expressions Calculator (cont d.)

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Analysis of a Search Algorithm

St S a t ck a ck nd Qu Q eue 1

Chapter 07: Instruction Level Parallelism VLIW, Vector, Array and Multithreaded Processors. Lesson 05: Array Processors

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

Lecture 12 Doubly Linked Lists (with Recursion)

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Answers to Review Questions Chapter 7

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

Stacks. Linear data structures

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

1. Define: (a) Variable, (b) Constant, (c) Type, (d) Enumerated Type, (e) Identifier.

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

Glossary of Object Oriented Terms

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Collaboration policy. Where to get help (but no code in ) Office hours Lab TAs in Friend 008/009 Bounce ideas (but not code) off classmates

Java Software Structures

Algorithms and Abstract Data Types

Algorithms and Data Structures Written Exam Proposed SOLUTION

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

Module 2 - Multiplication Table - Part 1-1

Node-Based Structures Linked Lists: Implementation

1 The Java Virtual Machine

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Binary Search Trees CMPSC 122

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

3 Abstract Data Types and Search

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Algorithms and Data Structures

Array Abstract Data Type

In this Chapter you ll learn:

Solving Systems of Linear Equations Using Matrices

Unordered Linked Lists

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

Chapter 7D The Java Virtual Machine

Memory Systems. Static Random Access Memory (SRAM) Cell

Here are some examples of combining elements and the operations used:

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

DATA STRUCTURE - QUEUE

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Sample Questions Csci 1112 A. Bellaachia

Analysis of Algorithms, I

6. Standard Algorithms

Transcription:

UNIT 6A Organizing Data: Lists 1 Data Structure The organization of data is a very important issue for computation. A data structureis a way of storing data in a computer so that it can be used efficiently. Choosing the right data structure will allow us to develop certain algorithms for that data that are more efficient. An array(or list) is a very simple data structure for holding a sequence of data. 2 1

Arrays in Memory Typically, array elements are stored in adjacent memory cells. The subscript (or index) is used to calculate an offset to find the desired element. Example: data = [50, 42, 85, 71, 99] Assume integers are stored using 4 bytes (32 bits). Address 100 104 Contents 50 42 If we want data[3], the computer takes 108 85 the address of the start of the array and adds the offset * the size of an array element to find the element we want. 112 116 71 99 Do you see why the first index of an array is 0 now? Location of data[3] is 100 + 3*4 = 112 3 Pros: Arrays: Pros and Cons Access to an array element is fast since we can compute its location quickly. Cons: If we want to insert or delete an element, we have to shift subsequent elements which slows our computation down. We need a large enough block of memory to hold our array. 4 2

Linked Lists Another data structure that stores a sequence of data values is the linked list. Data values in a linked list do not have to be stored in adjacent memory cells. To accommodate this feature, each data value has an additional pointer that indicates where the next data value is in computer memory. In order to use the linked list, we only need to know where the first data value is stored. 5 Linked List Example Linked list to store the sequence: 50, 42, 85, 71, 99 address data next 100 42 148 Assume each integer and pointer requires 4 bytes. Starting Location of List (head) 124 108 99 0 (null) 116 124 50 100 132 71 108 140 148 85 132 156 6 3

Linked List Example To insert a new element, we only need to change a few pointers. Example: Insert 20 after 42. Starting Location of List (head) 124 address data next 100 42 156 108 99 0 (null) 116 124 50 100 132 71 108 140 148 85 132 156 20 148 7 Drawing Linked Lists Abstractly L = [50, 42, 85, 71, 99] head 50 42 85 71 99 null Inserting 20 after 42: head step 2 20 step 1 We link the new node to the list before breaking the existing link. 50 42 85 71 99 null 8 4

Pros: Linked Lists: Pros and Cons Inserting and deleting data does not require us to move/shift subsequent data elements. Cons: If we want to access a specific element, we need to traverse the list from the head of the list to find it which can take longer than an array access. Linked lists require more memory. (Why?) 9 Two-dimensional arrays Some data can be organized efficiently in a table (also called a matrix or 2-dimensional array) Each cell is denoted with two subscripts, a row and column indicator B[2][3] = 50 B 0 1 2 3 4 0 3 18 43 49 65 1 14 30 32 53 75 2 9 28 38 50 73 3 10 24 37 58 62 4 7 19 40 46 66 10 5

2D Arrays in Ruby data = [ [ 1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ] 0 1 2 3 0 1 2 3 4 1 5 6 7 8 2 9 10 11 12 data[0] => [1, 2, 3, 4] data[1][2] => 7 data[2][5] => nil data[4][2] => undefined method '[]' for nil 11 2D Array Example in Ruby Find the sum of all elements in a 2D array def summatrix(table) number of rows in the table sum = 0 for row in 0..table.length-1 do for col in 0..table[row].length-1 do sum = sum + table[row][col] end end return sum end number of columns in the given row of the table 12 6

Tracing the Nested Loop for row in 0..table.length-1 do for col in 0..table[row].length-1 do sum = sum + table[row][col] end end 0 1 2 3 0 1 2 3 4 1 5 6 7 8 2 9 10 11 12 table.length = 3 table[row].length = 4 for every row row col sum 0 0 1 0 1 3 0 2 6 0 3 10 1 0 15 1 1 21 1 2 28 1 3 36 2 0 45 2 1 55 2 2 66 2 3 78 13 Stacks A stackis a data structure that works on the principle of Last In First Out (LIFO). LIFO: The last item put on the stack is the first item that can be taken off. Common stack operations: Push put a new element on to the top of the stack Pop remove the top element from the top of the stack Applications: calculators, compilers, programming 14 7

RPN Some modern calculators use Reverse Polish Notation (RPN) Developed in 1920 by Jan Lukasiewicz Computation of mathematical formulas can be done without using any parentheses Example: ( 3 + 4 ) * 5 = becomes in RPN: 3 4 + 5 * 15 RPN Example Convert the following standard mathematical expression into RPN: (23 3) / (4 + 6) 23 3 4 6 + operand1 operand2 operator operand1 operand2 operator 23 3 4 6 + / operand1 operand2 operator 16 8

Evaluating RPN with a Stack i 0 A 23 3-4 6 + / yes Output Pop S x A[i] i == A.length? no Is x a number? yes Push x on S i i + 1 no Pop top 2 numbers Perform operation Push result on S 17 Example Step by Step RPN: 23 3-4 6 + / Stack Trace: 3 4 4 10 23 23 20 20 20 20 2 6 18 9

Stacks in Ruby You can treat arrays (lists) as stacks in Ruby. stack stack = [] [] stack.push(1) [1] stack.push(2) [1,2] stack.push(3) [1,2,3] x = stack.pop() [1,2] 3 x = stack.pop() [1] 2 x = stack.pop() [] 1 x = stack.pop() nil nil x 19 Queues A queueis a data structure that works on the principle of First In First Out (FIFO). FIFO: The first item stored in the queue is the first item that can be taken out. Common queue operations: Enqueue put a new element in to the rear of the queue Dequeue remove the first element from the front of the queue Applications: printers, simulations, networks 20 10