Microsoft® Small Basic

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Microsoft® Small Basic
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials Stack and Queues.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Stacks  Standard operations: IsEmpty … return true iff stack is empty IsFull … return true iff stack has no remaining capacity Top … return top element.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Stacks A stack is a data structure that only allows items to be inserted and removed at one end We call this end the top of the stack The other end is.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Stacks.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Implementing Stacks Using Arrays CSC 1401: Introduction to Programming with Java Week 14 – Lecture 1 Wanda M. Kunkle.
Summary of lectures (1 to 11)
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Data Structures Data structures permit the storage of related data for use in your program. –Arrays.
More Arrays Length, constants, and arrays of arrays By Greg Butler.
Microsoft® Small Basic The Controls Object Estimated time to complete this lesson: 1 hour.
Microsoft® Small Basic
Microsoft® Small Basic Stacks and Arrays Estimated time to complete this lesson: 1 hour.
Stack Data Structure By : Imam M Shofi. What is stack? A stack is a limited version of an array. A stack is a limited version of an array. New elements,
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Stacks and Queues Introduction to Computing Science and Programming I.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Pointers, Stacks and Memory Addressing Computer Science and Programming Concepts.
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Problem Solving for Programming Session 8 Static Data Structures.
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.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
Understanding Data Types and Collections Lesson 2.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Stacks And Queues Chapter 18.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Microsoft® Small Basic Exploring Shapes Estimated time to complete this lesson: 1 hour.
Visual Basic.NET BASICS Lesson 11 List Boxes, For Next Loops, and Label Settings.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Stacks and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
STACK Data Structure
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
Stacks. What is a Stack? A stack is a type of data structure (a way of organizing and sorting data so that it can be used efficiently). To be specific,
Microsoft® Small Basic Conditions and Loops Estimated time to complete this lesson: 2 hours.
Click to edit Master text styles Stacks Data Structure.
Popping Items Off a Stack Using a Function Lesson xx
Starter Write a program that asks the user if it is raining today.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Tonga Institute of Higher Education
Data Structures – 1D Lists
Presentation transcript:

Microsoft® Small Basic Stacks and Arrays Estimated time to complete this lesson: 1 hour

Stacks and Arrays In this lesson, you will learn how to: Use different operations of the Array object. Use different operations of the Stack object.

Stacks and Arrays Before we discuss the Array and Stack objects, let’s first understand when we might use either of these objects. An array can have multiple dimensions, but a stack has only one dimension. You can directly access any element in an array, but you can access only the top element of a stack. In other words, you must go through all the elements of a stack to access its last element.

The Array Object So far, you have learned about variables that store single values. Now, let’s learn about a special kind of variable that is called an array. An array can store more than one value at the same time. If you want to store the names of five users, you can create five variables, or you can create just one variable to store all five names. You use the indexing method to store multiple values in an array. For example, you can create an array called name as: name[1], name[2], name[3], name[4], and name[5]. Here, 1, 2, 3, 4, and 5 are the indices for the name array. The name[1], name[2]… labels may appear to identify different variables, but they all represent just one variable!

Operations of the Array Object Now, let’s discuss some operations of the Array object, such as IsArray, ContainsIndex, and ContainsValue. You can determine whether the specified variable is an array by using the IsArray operation. You can determine whether an array contains the specified index if you use the ContainsIndex operation. This operation is helpful if you want to determine whether a specific value initializes the array’s index. If any of these operations succeeds, “TRUE” appears in the text window. Otherwise, “FALSE” appears.   You can determine whether the array contains a value that you specify by using the ContainsValue operation. You can use this operation to determine whether the array’s value was stored in the index that you specify.

Operations of the Array Object Let’s see how we can use these operations in a program. In this example, the Subjects array stores the names of five subjects. You can verify whether Subjects is an array by using the IsArray operation. You can also verify whether the index Subjects[4] exists by using the ContainsIndex operation. You can verify whether the value “Math” exists in the Subjects array by using the ContainsValue operation. output Code: Subjects[1] = "English" Subjects[2] = "History" Subjects[3] = "Computers" Subjects[4] = "Science" Subjects[5] = "Math“ TextWindow.WriteLine("Subjects is an array: " + Array.IsArray(Subjects)) TextWindow.WriteLine("Subjects[4] is available: " + Array.ContainsIndex(Subjects, 4)) TextWindow.WriteLine("Math is available: " + Array.ContainsValue(Subjects, "Math")) Array.GetItemCount(Subjects)

Operations of the Array Object Look at this example to learn how to use these operations. The Array object also provides more useful operations, such as:  GetAllIndices GetItemCount In this example, you don’t know the indices for the Employee array, so you use the GetAllIndices operation. Next, you use the GetItemCount operation in a For loop to list the information that is stored in the Employee array. You can use the GetAllIndices operation to get all the indices for the array, in the form of another array. This operation is especially useful when you don’t know the indices of an array. This operation displays an array along with all indices of the specified array. The index of the displayed array starts at 1. You can use the GetItemCount operation to get the total number of items that are stored in the specified array. This operation displays the number of items in the specified array. Code: Employee["Name"] = "John" Employee["City"] = "New York" Employee["Email-ID"] = "John@example.com" Employee["Mobile"] = "06482624" Emplist = Array.GetAllIndices(Employee) For i = 1 To Array.GetItemCount(Employee) TextWindow.WriteLine(Emplist[i] + ":" + Employee[Emplist[i]]) EndFor

The Stack object consists of three operations: You can use the Stack object to store data the same way as you stack plates. This object works on the principle of last-in, first-out (LIFO). For example, if you look down at a stack of plates, you can see only the top plate. To see the next plate, you must remove this top plate. You can’t see a plate in the middle of the stack until you remove the plates above it. The Stack object consists of three operations: PushValue PopValue GetCount Let’s explore each of these operations…

Operations of the Stack Object The Stack object stores data just as a stack of plates. Let’s look at some examples to understand how this object works. Using the PushValue operation is like adding a plate to the top of the stack. By using this operation, you can push a value to the stack that you specify. Using the PopValue operation is like taking a plate from the top of the stack. You can use this operation to pop out a value from the stack that you specify. The PushValue operation does not return anything. The PopValue operation returns the value from the stack. The GetCount operation returns the number of items in a specified stack. The GetCount operation provides the total number of plates in the stack. You can use this operation to determine how many items a stack contains.

Operations of the Stack Object Let’s write a program to better understand these operations. In this example, you use the PushValue operation to push 50 plates into an empty container. Then you take eight plates from the stack by using the PopValue operation. Now, you use the GetCount operation to get the number of plates that remain. You also display the value of the top plate. Code: container = "empty" For i = 0 To 50 Stack.PushValue(container , "plate number " + i) EndFor TextWindow.WriteLine("The number of plates in the container is " + Stack.GetCount(container)) For i = 0 To 8 Stack.PopValue(container) TextWindow.WriteLine("After taking away 8 plates, the total number of plates is " + Stack.GetCount(container)) TextWindow.WriteLine("The top most plate in the container is " + Stack.PopValue(container))

Let’s Summarize… Congratulations! Now you know how to: Use different operations of the Stack object. Use different operations of the Array object.

Show What You Know By using the Array object, write a flight-reservation program that you can use to perform the following actions: Reserve seats for 10 passengers. Display each passenger’s name and seat number. Show how many seats are still available. Solution: TextWindow.WriteLine("Flight Reservations") TotalSeats = 10 For i = 1 To TotalSeats TextWindow.Write("Enter Passenger Name: ") Name[i] = TextWindow.Read() TextWindow.WriteLine("Seat number " + i + " is reserved by " + Name[i]) GetDetails() EndFor Sub GetDetails If Array.GetItemCount(Name) = TotalSeats Then TextWindow.WriteLine("No more seats are available!") Else Array.GetItemCount(Name) AvailableSeats = TotalSeats - Array.GetItemCount(Name) TextWindow.WriteLine("Number of available seats is: " + AvailableSeats) TextWindow.WriteLine("") EndIf EndSub