Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

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

3 Introduction to Arrays 4 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. 4 An integer array int agesOfKids [n]; Where n is the size of the block indicating the number of integers in this array

4 Bounds and Subscripts 4 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. 4 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. 4 Array Subscript –The expression inside the [... ] is known as an array subscript.

5 4 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 66 64 72 78 85 90 99 105 98 90 88 80 row 2, col 7 might be Arizona’s high for August 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 ]

6 5 const int NUM_STATES = 50 ; const int NUM_MONTHS = 12 ; int stateHighs [ NUM_STATES ] [ NUM_MONTHS ] ; 4 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 800080248048 STORAGE rows columns

7 Integer Arrays 4 We wish to store the 4-digit pager numbers for all 50 apartments in an apartment complex 4 int[] pager_numbers = new int[51]; 4 Location zero is reserved for the building manager 4 pager_number[0] = 2435;

8 Integer Arrays 4 Suppose that we wish to print all pager numbers from apartment#1 to apartment#50 in a nicely formatted list 4 The best way to do this would be to use the for loop

9 The for Statement 4 “for” is a loop statement that is controlled through a loop control variable 4 for (lcv=1; lcv<=100; lcv++) 4 The above loop will start with lcv=1 and it will run until lcv equals 100. The step size is 1 (lcv++)

10 Pager Numbers Printout in Java 4 int[] pagers = new int[50]; 4 int loop; 4 for (loop=1; loop<=50; loop++) 4 System.out.println(pagers[loop]);

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

12 Introduction to Lists 4 An organization’s membership list may grow and shrink in size. 4 Your phone book may also grow and shrink in size as time passes 4 We need a mechanism to store dynamic lists in the memory

13 Linked Lists 4 Linked lists have entries connected with pointers 4 Deleting an entry can be implemented by re-arranging pointers 4 So we leave the entries where they are and just re-align the pointers

14 Pointers 4 Think about the pointers in your life My mailing address My Home

15 Pointers

16 4 Web links are also pointers http://www.ucla.edu UCLA Server Computer

17 Inserting into a Linked List Header Fred 423-3158 NE XT Bob 242-7111 New Entry NE XT

18 Inserting into a Linked List Header Fred 423-3158 NE XT Bob 242-7111 New Entry NE XT

19 Inserting into a Linked List Header Fred 423-3158 NE XT Bob 242-7111 NE XT

20 Deleting from a Linked List Header Bob 423-3178 NE XT Alice 242-7111 NE XT Fred 423-3158 NE XT

21 Deleting from a Linked List Header Bob 423-3178 NE XT Alice 242-7111 NE XT Fred 423-3158 NE XT

22 Stacks 4 Stacks 4 Stack Base and Stack Pointer 4 Push operation 4 Pop operation

23 Stacks 4 A stack is a useful data structure that stores values that may be needed in the near future 4 For example, you may want to return back to a website that you browsed a few moments ago 4 You may want to undo an operation that you performed in MS-Word 4 In a stack, we have a fixed size block of memory available in which we can only add and delete at one end 4 We keep track of both ends of stack with pointers

24 Stack Operation Designated Block for Stack Other Memory

25 Stack Operation SP SB EMPTY STACK

26 Stack Operation SP SB Val1 STACK WITH ONE DATA ITEM

27 Push Operation SP SB Val1 4 We store a data item at the location referenced by SP

28 Push Operation SP SB Val1 4 We store a data item at the location referenced by SP and then increment SP Val2

29 Push Operation 4 Stack[SP] = New Value 4 SP= SP+1; 4 The stack has a fixed maximum size of N locations. We cannot bump into other memory 4 Therefore, we must check before pushing if the stack is full 4 How?

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

31 Pop Operation SP SB Val1 4 We retrieve a data item from the top of stack. How can we reach top of stack value? Val2 Val3

32 Pop Operation SP SB Val1 4 Val3 is top of stack and it is one below the current location referenced by SP Val2 Val3

33 Pop Operation SP SB Val1 4 Val3 is popped out and SP is decremented to point to newly vacated location Val2

34 Pop Operation 4 Popped_Value = Stack[SP-1]; 4 SP= SP-1; 4 We cannot pop from an empty stack so we must check before popping 4 How?

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

36 Stack Applications 4 Stacks are very useful in remembering values 4 Stacks operate similar to the way the office clerks process letters and folders 4 The current document is on top of stack and it has to be processed first 4 Stacks help programs remember the place where call to a procedure was issued


Download ppt "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."

Similar presentations


Ads by Google