Está en la página 1de 20

Data Organization:

Creating Order Out Of Chaos


Fundamental Data Structures

2A

15-103 Principles of Computation, Carnegie Mellon QATAR - Stehlik/Cortina

Data Structures
The organization of data is a very important issue for computer scientists. A data structure is a way of storing data in a computer so that it can be used efficiently. Different data structures have different access properties and so choosing the right data structure can play a large role in an algorithms efficiency. A one-dimensional array, or vector, is a very simple data structure for holding a sequence of data. What are its access properties?
15-103 Principles of Computation, Carnegie Mellon QATAR - Stehlik/Cortina 2

One-dimensional Array Example


1 4 2 9 3 4 5 17 23 29 6 7 8 9 10

15-103 Principles of Computation, Carnegie Mellon QATAR - Stehlik/Cortina

Tables
Some data can be organized efficiently in a table (also called a two-dimensional array or matrix) Each cell is denoted M 1 2 3 4 5 with two subscripts, a row and column 1 3 18 43 49 65 index 2 14 30 32 53 75 3 9 28 38 50 73 M[3,4] = 50 4 10 24 37 58 62 5 7 19 40 46 66
15-103 Principles of Computation, Carnegie Mellon QATAR - Stehlik/Cortina 4

Table Example
Algorithm to find the sum of all data values in a matrix. Input M, a 5 X 5 matrix of integers Set sum = 0 For each row r of M do the following: M 1 2 3 For each column c of M do the following: 1 3 18 43 Add M[r,c] to sum 2 14 30 32 Output sum

r 5 4 3 2 1

c 5 4 3 2 1

sum 949 883 837 797 778 771 709 651 614 590 580 507 457 419 391 382 307 254 222 192 178 113 64 21 3 0

3 4 5

4 49 53 9 28 38 50 10 24 37 58 7 19 40 46

5 65 75 73 62 66
5

15-103 Principles of Computation, Carnegie Mellon QATAR - Stehlik/Cortina

Linked Lists
Another linear 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.
15-103 Principles of Computation, Carnegie Mellon QATAR - Stehlik/Cortina 6

A Pointer

Please deliver my mail to Box C.

15-103 Principles of Computation, Carnegie Mellon QATAR - Stehlik/Cortina

Linked List Example


Linked list to store the sequence: [4, 9, 17, 23, 29]
Assume each integer and pointer requires 2 bytes.

Memory Cell Address 100 104

Data Value 9 29 4 23 17

Pointer (next) 124 0 (NULL) 100 104 116

Starting Location of List (head) 112

108 112 116 120 124 128

15-103 Principles of Computation, Carnegie Mellon QATAR - Stehlik/Cortina

Inserting a new data value


One-dimensional Array

1 4

2 9

3 4 5 17 23 29

10

We have to move potentially the entire set of numbers over to make room for the new value, depending on where we want to insert it. (Example: Insert 12)

1 4

2 9

3 4 5 6 12 17 23 29

10

15-103 Principles of Computation, Carnegie Mellon QATAR - Stehlik/Cortina

Inserting a new data value


Linked List (Insert 12)
Memory Cell Address 100 104 Data Value 9 29 4 23 17 12 Pointer (next) 128 0 (null) 100 104 116 124
10

Starting Location of List (head) 112

108 112 116 120 124 128

15-103 Principles of Computation, Carnegie Mellon QATAR - Stehlik/Cortina

Drawing Linked Lists


L = [4, 9, 17, 23, 29]
head

17

23

29

null

Inserting 12:
new head step 2 9 17 12 step 1 23 29 null

15-103 Principles of Computation, Carnegie Mellon QATAR - Stehlik/Cortina

11

Comparison
What are the advantages of using arrays?

What are the advantages of using linked lists?

15-103 Principles of Computation, Carnegie Mellon QATAR - Stehlik/Cortina

12

Stacks

15-103 Principles of Computation, Carnegie Mellon QATAR - Stehlik/Cortina

13

Stacks
A stack is 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 stack Pop remove the top element from the stack

A stack can be implemented using an array or a linked list. Applications: calculators, compilers, programming
15-103 Principles of Computation, Carnegie Mellon QATAR - Stehlik/Cortina 14

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: 34+5*
15-103 Principles of Computation, Carnegie Mellon QATAR - Stehlik/Cortina

end

Hewlett-Packard 12c financial calculator


15

Stack Example
Computing the value of an RPN string
i 1

A 23
i i+1

A[i]

yes

Is x = $?

no
Is x a number?

no
Pop top 2 numbers Perform operation Push result on S

23+ /3= 4 610 20 = 10 202 = S Answer: 2

yes
Push x on S Output Pop S

6 10 4 3 20 23 2

15-103 Principles of Computation, Carnegie Mellon QATAR - Stehlik/Cortina

16

Queues

15-103 Principles of Computation, Carnegie Mellon QATAR - Stehlik/Cortina

17

Queues
A queue is 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 stack operations:


Enqueue put a new element in to the queue Dequeue remove the first element from the queue

A queue can be implemented using an array or a linked list. Applications: printers, simulations, networks
15-103 Principles of Computation, Carnegie Mellon QATAR - Stehlik/Cortina 18

Queues
Josephus Problem
Given a group of n men arranged in a circle such that every mth man will be executed until one remains, find the position of the last survivor. 1. Fill Q with people numbered 1 through n. 2. While Q is not empty do the following: a. Do the following m-1 times: i. Let x = Dequeue Q ii. Enqueue x on Q b. Let x = Dequeue Q 3. Output x as the last survivor.
15-103 Principles of Computation, Carnegie Mellon QATAR - Stehlik/Cortina 19

Queues
Josephus Problem
1. Fill Q with people numbered 1 through n. 2. While Q is not empty do the following: a. Do the following m-1 times: i. Let y = Dequeue Q ii. Enqueue y on Q b. Let x = Dequeue Q 3. Output x as the last survivor. TRACE AFTER STEP 1 and TRACE AFTER EACH EXECUTION OF STEP 2b.
In this example, Josephus needs to be in position 1 to remain alive!
15-103 Principles of Computation, Carnegie Mellon QATAR - Stehlik/Cortina

n = 6, m = 3 Q 123456 45612 1245 512 51 1 (empty)

x 3 6 4 2 5 1
20

También podría gustarte