35
Arrays and Other Data Structures Introduction to Arrays Bounds and Subscripts Integer Arrays Floating Point Number Arrays Lists (Linked) Stacks

Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

  • View
    244

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Arrays and Other Data Structures

Introduction to Arrays Bounds and Subscripts Integer Arrays Floating Point Number Arrays Lists (Linked) Stacks

Page 2: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Introduction to Arrays

An array is a contiguous block of the same data type. For example, you could have an array of integers (a block of integers), but not a block of integers and floats.

An integer array • int agesOfKids[n]; •Where n is the size of the block indicating the number of integers in this array

Page 3: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Bounds and Subscripts

Array Bounds – "Array bounds" refer to the boundaries in memory which the

array occupies. The beginning of the array (the first) element is considered the lower bound, while the end (or top) is considered to be the upper bound.

Element – An "element" is an individual entity inside the array. Because C

arrays have a lower bound of 0, array[0] refers to the first element.

Array Subscript – The expression inside the [ ... ] is known as an array subscript.

Page 4: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

4

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]

66 64 72 78 85 90 99 105 98 90 88 80row 2,col 7might beArizona’shigh forAugust

EXAMPLE -- To keep monthly high temperatures for all 50 states in one array.

int stateHighs [ 50 ] [12 ] ;

[ 0 ]

[ 1 ]

[ 2 ]

.

. stateHighs [2] [7]

.

[ 48 ]

[ 49 ]

Page 5: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

5

const int NUM_STATES = 50 ;const int NUM_MONTHS = 12 ;int stateHighs [ NUM_STATES ] [ NUM_MONTHS ] ;

In memory, arrays are stored in row order. The first row

is followed by the second row, etc. Base Address

. . .

12 highs for state 0 12 highs for state 1 etc. Alabama Alaska first row second row

8000 8024 8048

STORAGErows columns

Page 6: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Integer Arrays

We wish to store the 4-digit pager numbers for all 50 apartments in an apartment complex

int[] pager_numbers = new int[51]; Location zero is reserved for the building

manager pager_number[0] = 2435;

Page 7: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Integer Arrays

Suppose that we wish to print all pager numbers from apartment#1 to apartment#50 in a nicely formatted list

The best way to do this would be to use the for loop

Page 8: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

The for Statement

“for” is a loop statement that is controlled through a loop control variable

for (lcv=1; lcv<=100; lcv++) The above loop will start with lcv=1 and it

will run until lcv equals 100. The step size is 1 (lcv++)

Page 9: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Pager Numbers Printout in Java

int[] pagers = new int[50]; int loop; for (loop=1; loop<=50; loop++) System.out.println(pagers[loop]);

Page 10: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Lists

Introduction to Lists Contiguous Lists Adding and Deleting in Contiguous Lists Linked Lists Pointers in Linked Lists Inserting into a Linked List Deleting from a Linked List

Page 11: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Introduction to Lists

An organization’s membership list may grow and shrink in size.

Your phone book may also grow and shrink in size as time passes

We need a mechanism to store dynamic lists in the memory

Page 12: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Linked Lists

Linked lists have entries connected with pointers

Deleting an entry can be implemented by re-arranging pointers

So we leave the entries where they are and just re-align the pointers

Page 13: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Pointers

Think about the pointers in your life

My mailing address

My Home

Page 14: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Pointers

Page 15: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Pointers

Web links are also pointers

http://www.ucla.eduUCLA Server

Computer

Page 16: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Inserting into a Linked List

Header

Fred423-3158

NEXT

Bob242-7111

New Entry

NEXT

Page 17: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Inserting into a Linked List

Header

Fred423-3158

NEXT

Bob242-7111

New Entry

NEXT

Page 18: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Inserting into a Linked List

Header Fred423-3158

NEXT

Bob242-7111

NEXT

Page 19: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Deleting from a Linked List

HeaderBob

423-3178

NEXT

Alice242-7111

NEXT

Fred423-3158

NEXT

Page 20: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Deleting from a Linked List

Header

Bob423-3178

NEXT

Alice242-7111

NEXT

Fred423-3158

NEXT

Page 21: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Stacks

Stacks Stack Base and Stack Pointer Push operation Pop operation

Page 22: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Stacks

A stack is a useful data structure that stores values that may be needed in the near future

For example, you may want to return back to a website that you browsed a few moments ago

You may want to undo an operation that you performed in MS-Word

In a stack, we have a fixed size block of memory available in which we can only add and delete at one end

We keep track of both ends of stack with pointers

Page 23: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Stack Operation

Designated Block for

Stack

Other Memory

Page 24: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Stack Operation

SP

SB

EMPTY STACK

Page 25: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Stack Operation

SP

SBVal1

STACK WITH ONE DATA ITEM

Page 26: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Push Operation

SP

SBVal1

We store a data item at the location referenced by SP

Page 27: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Push Operation

SP

SBVal1

We store a data item at the location referenced by SP and then increment SP

Val2

Page 28: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Push Operation

Stack[SP] = New Value SP= SP+1; The stack has a fixed maximum size of N

locations. We cannot bump into other memory

Therefore, we must check before pushing if the stack is full

How?

Page 29: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Push Operation

if (SP == SB+N) cout “sorry!! Stack is full”; else { Stack[SP] = New_Value SP= SP+1; }

Page 30: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Pop Operation

SP

SBVal1

We retrieve a data item from the top of stack. How can we reach top of stack value?

Val2

Val3

Page 31: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Pop Operation

SP

SBVal1

Val3 is top of stack and it is one below the current location referenced by SP

Val2

Val3

Page 32: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Pop Operation

SP

SBVal1

Val3 is popped out and SP is decremented to point to newly vacated location

Val2

Page 33: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Pop Operation

Popped_Value = Stack[SP-1]; SP= SP-1; We cannot pop from an empty stack so we

must check before popping How?

Page 34: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Pop Operation

if (SP == SB) cout “sorry!! Stack is empty”; else { Popped_Value = Stack[SP-1]; SP= SP-1; }

Page 35: Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Integer Arrays 4 Floating Point Number Arrays 4 Lists (Linked) 4 Stacks

Stack Applications

Stacks are very useful in remembering values

Stacks operate similar to the way the office clerks process letters and folders

The current document is on top of stack and it has to be processed first

Stacks help programs remember the place where call to a procedure was issued