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.

Slides:



Advertisements
Similar presentations
Ceng-112 Data Structures I Chapter 5 Queues.
Advertisements

One Dimensional Arrays
Data Structures and Algorithms (60-254)
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structure Dr. Mohamed Khafagy.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
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.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
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.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
FIST, Multi Media University Lecture 5 Stack (Array Implementation) Queue (Array Implementation )
Stacks And Queues Chapter 18.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Data Structures & Algorithms
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Advanced Higher Computing Science Stacks Queues and linked lists.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
STACKS & QUEUES for CLASS XII ( C++).
Review Array Array Elements Accessing array elements
Queues.
18 Chapter Stacks and Queues
Data Structure and Algorithms
Chapter 15 Lists Objectives
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
CMSC 341 Lecture 5 Stacks, Queues
Popping Items Off a Stack Lesson xx
Stacks and Queues Prof. Michael Tsai 2017/02/21.
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
CE 221 Data Structures and Algorithms
Data Structures & Algorithms
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
DATA STRUCTURES IN PYTHON
Presentation transcript:

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 Lists (Contiguous and Linked) 4 Stacks and Queues

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 Arrays are easy to declare: int myIntegerArray [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.

Character Arrays 4 Character arrays :names and other text 4 #include 4 void main () 4{4{ 4 char first_name[20]; 4 cin>>first_name; 4 if (first_name[0] == 'J') 4 cout<<"Your name begins with J"<<endl; 4 cout<<first_name<<endl; 4 cout<<first_name[0]<<endl; 4 cout<<first_name[1]<<endl; 4 cout<<first_name[2]<<endl; 4 cout<<first_name[3]<<endl; 4}4}

Character Arrays 4 Character arrays allow us to get name or other text into a single data item that can hold it 4 We can also refer to each individual character by using subscripts as shown in the example source code 4 There are several ways to deal with text strings in C++

Integer Arrays 4 We wish to store the 4-digit pager numbers for all 50 apartments in an apartment complex 4 int pager_numbers[51]; Why? 4 pager_number[1] = 2435; 4 Write the code to print pager number for apartment 32 and to indicate an error if it equals the pager number for apartment 18

Comparison Statement 4 The “if” is used as a comparative statement in C++ 4 The format is : 4 if (condition is true) 4 {some statements to be executed} 4 else 4 {some other statements to be executed}

Comparison Statements 4 We wish to compare the pager number of apartment 32 to that of apartment 18 and if both are equal, we display an error message 4 if (pager_number[32] == pager_number[18]) 4 cout<<“Error; 4 else 4 cout<<“No Error”;

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++) 4 Compare it to the above 4 for (lcv=1; lcv<=100; lcv+=2)

The for Statement 4 “for” statement and while statement can produce identical loops 4 “for” is preferred if the number of iterations is known beforehand. “while” is preferred if the total iterations cannot be computed in advance

Pager Numbers Printout 4 #include 4 void main () 4{4{ 4 int pagers[51]; 4 for (loop=1; loop<51; loop++) 4 cout<<pagers[loop]; 4}4}

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

Adapted for academic use from "Computer Science: An Overview" by J. Brookshear Contiguous Lists 4 Contiguous Lists are stored in consecutive memory locations as shown below:

Adding and Deleting in Contiguous Lists 4 Let us consider a phone book. It can be implemented with an array containing names and phone numbers Fred David Alice Bob Carol Contiguous List of Phone Numbers

Adding and Deleting in Contiguous Lists 4 Deleting an entry is a two-step operation Fred David Alice Carol Deleting an entry from the Contiguous List of Phone Numbers Fred David Alice Carol

Adding and Deleting in Contiguous Lists 4 Adding a new entry can take place towards the end of the list Fred David Alice Carol Fred David Alice Carol Joe Adding an entry to the Contiguous List of Phone Numbers

Linked Lists 4 If the contiguous list becomes large, deleting an entry in the middle of the list becomes very slow 4 It is because of the fact that we have to fill the gaps left after deleting en entry 4 If we wish to maintain the list as sorted, we have to sort it after each addition, causing additional processing overheads

Linked Lists 4 This problem can be solved if we implement the list as a linked list 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 in Linked Lists 4 Pointers are used in C++ and other languages for pointing to other variables 4 A pointer is declared as a variables that can hold the address of another variable 4 When we declare a variable, a memory location is reserved for it by the system 4 For example 4 int my_money; 4 my_money=200;

Pointers 4 Now assume that memory location 0XFF8C is reserved by the system for the variable my_money 4 Location 0XFF8C contains the value Next, we declare a pointer variable my_key 4 int *my_key; 4 It means that my_key will hold the address of an integer variable

Pointers 4 Next, we initialize pointer my_key to point to the variable my_money 4 my_key = &my_money; 0XFF8C200 0XFF8C my_keymy_money y

Pointers 4 Conceptually, my_key points to my_money 0XFF8C200 0XFF8C my_keymy_money y

Pointers 4 Now, there are two ways to access my_money 4 We can refer to it directly 4 We can refer to it through the pointer

Pointers 4 Think about other pointers 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 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 and Queues 4 Stacks 4 Stack Base and Stack Pointer 4 Push operation 4 Pop operation 4 Queues 4 Queue operations enqueue and dequeue 4 Queue examples

Stacks 4 Stack is like a contiguous list 4 In contiguous lists, we add an item at the back and we are allowed to delete an item anywhere in the list 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++; 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++; 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--; 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--; 4}4}

Queues 4 Queues are everywhere!! 4 Post offices, banks, McDonalds, Grocery stores etc. 4 Spell out the queue management rules 4 How to add a person to a queue? 4 How to serve a queue?

Queues 4 Let us implement queues in the computer 4 We can use a fixed size block of memory for this purpose 4 Data items can be added to the queue and deleted from the queue using the same rules as human queues 4 Add at the back 4 Delete at the front

Queue Operation 4 We would like to see an example of queue formation 4 We learn how to set and change front and back indices as per “enqueue” and “dequeue” operations 4 We also learn how to print the elements in a linear queue i.e. a queue implemented in an array without “recycling” emptied locations

Queue Formation 4 A queue can be formed by using an array data structure 4 For example, in C++, a queue can be implemented as an array of characters: 4 int const MAXSIZE=5; 4 int front, back; 4 char letter_queue[MAXSIZE];

Queue Operations 4 Major operations associated with a queue are enqueue and dequeue 4 Let us visualize the letter_queue as defined and see what operations can be done 4 We keep the queue implementation simple (and inefficient). The purpose is to grasp the basic concepts.

An Empty Queue letter_queue front=0 back=0

Queue after adding one element A letter_queue front=0back=1

Queue after adding another element A B letter_queue front=0back=2

Queue after adding third element A B C letter_queue front=0back=3

Queue after deleting one element B C letter_queue front=1back=3

Summary of Operations 4 The “enqueue” operation adds elements to the back of the queue. The back index is incremented AFTER the add operation 4 Thus back index is the NEXT AVAILABLE LOCATION to which an element can be added 4 The “dequeue” operation deletes elements from the front of the queue. The front index is incremented AFTER the delete operation 4 Thus front index is the FRONT ELEMENT in the queue

Listing a Queue 4 Let us assume that this is the “linear” queue and the front and back keep “crawling” forward into the available memory 4 Thus listing the elements of the queue becomes a very simple problem 4 Let us visualize a queue having 4 elements

A Queue With 4 Elements A B C D letter_queue front=0back=4

Exercise to Print the Elements of the Queue 4 Let us write a for loop to print the elements of the queue 4 The declaration of the queue is reproduced below 4 int const MAXSIZE=5; 4 int front, back; 4 char letter_queue[MAXSIZE]; 4 Now front=0 and back=4. All that is needed is a loop that can index into array letter_queue and print the contents.