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.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 7: Arrays In this chapter, you will learn about
Stacks, Queues, and Linked Lists
One Dimensional Arrays
Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Character Arrays 4 Integer Arrays 4 Floating Point Number Arrays 4.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Connecting with Computer Science, 2e
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Lists 4 Introduction to Lists 4 Linear Lists 4 Adding and Deleting in linear Lists 4 Linked Lists 4 Pointers in Linked Lists 4 Inserting into a Linked.
Arrays and Other Data Structures 4 Introduction to Arrays 4 Bounds and Subscripts 4 Character Arrays 4 Integer Arrays 4 Floating Point Number Arrays 4.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof.
Ali Abdul Karem Habib Kufa University / mathematics & Science Of Computer.
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Introduction to Data Structures. Definition Data structure is representation of the logical relationship existing between individual elements of data.
Connecting with Computer Science 2 Objectives Learn what a data structure is and how it is used Learn about single and multidimensional arrays and how.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2.
Connecting with Computer Science, 2e Chapter 8 Data Structures.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
INTRODUCTION TO DATA STRUCTURES. INTRODUCTION A data structure is nothing but an arrangement of data either in computer's memory or on the disk storage.
FIST, Multi Media University Lecture 5 Stack (Array Implementation) Queue (Array Implementation )
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Advanced Higher Computing Science Stacks Queues and linked lists.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
CHP-3 STACKS.
Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
Array 10 GB Hard Disk 2 GB 4 GB2 GB 3 GB DATA 4 GB Free Store data in the form of Array (Continuous memory locations) Solution-1: No Solution. Memory Insufficient.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
STACKS & QUEUES for CLASS XII ( C++).
Data Structures Using C++ 2E
Computer Science 210 Computer Organization
Data Structure and Algorithms
Dr. Bernard Chen Ph.D. University of Central Arkansas
Stacks.
Stacks and Queues.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
CMSC 341 Lecture 5 Stacks, Queues
Computer Science 210 Computer Organization
Chapter 9: Pointers.
Popping Items Off a Stack Lesson xx
Stacks Data structure Elements added, removed from one end only
Data Structures & Algorithms
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Presentation transcript:

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 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

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.

4 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 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 ]

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 highs for state 0 12 highs for state 1 etc. Alabama Alaska first row second row STORAGE rows columns

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;

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

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++)

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]);

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

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

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

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

Pointers

4 Web links are also pointers UCLA Server Computer

Inserting into a Linked List Header Fred NE XT Bob New Entry NE XT

Inserting into a Linked List Header Fred NE XT Bob New Entry NE XT

Inserting into a Linked List Header Fred NE XT Bob NE XT

Deleting from a Linked List Header Bob NE XT Alice NE XT Fred NE XT

Deleting from a Linked List Header Bob NE XT Alice NE XT Fred NE XT

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

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

Stack Operation Designated Block for Stack Other Memory

Stack Operation SP SB EMPTY STACK

Stack Operation SP SB Val1 STACK WITH ONE DATA ITEM

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

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

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?

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}

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

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

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

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?

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}

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