Chapter 5. ADTs Stack and Queue
|
|
- Clyde Wilson
- 5 years ago
- Views:
Transcription
1 Chapter 5 ADTs Stack and Queue
2 Stacks of Coins and Bills
3 Stacks of Boxes and Books TOP OF THE STACK TOP OF THE STACK
4 Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of stack items can take place only at the top of the stack A stack is a LIFO last in, first out structure
5 Stack ADT Operations MakeEmpty -- Sets stack to an empty state IsEmpty -- Determines whether the stack is currently empty IsFull -- Determines whether the stack is currently full Push (ItemType newitem) -- Throws exception if stack is full; otherwise adds newitem to the top of the stack Pop -- Throws exception if stack is empty; otherwise removes the item at the top of the stack ItemType Top -- Throws exception if stack is empty; otherwise returns a copy of the top item
6 ADT Stack Operations Transformers Push Pop Observers IsEmpty IsFull change state observe state
7 class StackType { public: StackType( ); bool IsFull () const; bool IsEmpty() const; void Push( ItemType item ); void Pop(); private: ItemType Top(); private: int top; ItemType items[max_items]; }; What are the pre and post conditions?
8 // File: StackTypecpp #include "StackTypeh" #include <iostream> StackType::StackType( ) { top = -1; } bool StackType::IsEmpty() const { return(top == -1); } bool StackType::IsFull() const { return (top = = MAX_ITEMS-1); }
9 void StackType::Push(ItemType newitem) { if( IsFull() ) throw FullStack(): top++; items[top] = newitem; } void StackType::Pop() { if( IsEmpty() ) throw EmptyStack(); top--; } ItemType StackType::Top() { if (IsEmpty()) throw EmptyStack(); return items[top]; }
10 Class Interface Diagram (Memory reversed to better illustrate concept) StackType class StackType IsEmpty IsFull Push Pop Top Private data: top [MAX_ITEMS-1] [ 2 ] [ 1 ] items [ 0 ]
11 Tracing Client Code Private data: top letter [MAX_ITEMS-1] [ 2 ] [ 1 ] items [ 0 ] char letter = ; StackType charstack; charstackpush(letter); charstackpush( C ); charstackpush( S ); if ( charstackisempty( )) charstackpop( ); charstackpush( K ); while (charstackisempty( )) { letter = charstacktop(); charstackpop(0)}
12 Tracing Client Code letter Private data: top -1 [MAX_ITEMS-1] [ 2 ] [ 1 ] items [ 0 ] char letter = ; StackType charstack; charstackpush(letter); charstackpush( C ); charstackpush( S ); if ( charstackisempty( )) charstackpop( ); charstackpush( K ); while (charstackisempty( )) { letter = charstacktop(); charstackpop(0)}
13 Tracing Client Code letter Private data: top 0 [MAX_ITEMS-1] [ 2 ] [ 1 ] items [ 0 ] char letter = ; StackType charstack; charstackpush(letter); charstackpush( C ); charstackpush( S ); if ( charstackisempty( )) charstackpop( ); charstackpush( K ); while (charstackisempty( )) { letter = charstacktop(); charstackpop(0)}
14 Tracing Client Code letter Private data: top 1 [MAX_ITEMS-1] [ 2 ] items [ 0 ] [ 1 ] C char letter = ; StackType charstack; charstackpush(letter); charstackpush( C ); charstackpush( S ); if ( charstackisempty( )) charstackpop( ); charstackpush( K ); while (charstackisempty( )) { letter = charstacktop(); charstackpop(0)}
15 Tracing Client Code letter Private data: top 2 [MAX_ITEMS-1] items [ 0 ] [ 2 ] S [ 1 ] C char letter = ; StackType charstack; charstackpush(letter); charstackpush( C ); charstackpush( S ); if ( charstackisempty( )) charstackpop( ); charstackpush( K ); while (charstackisempty( )) { letter = charstacktop(); charstackpop(0)}
16 Tracing Client Code letter Private data: top 2 [MAX_ITEMS-1] items [ 0 ] [ 2 ] S [ 1 ] C char letter = ; StackType charstack; charstackpush(letter); charstackpush( C ); charstackpush( S ); if ( charstackisempty( )) charstackpop( ); charstackpush( K ); while (charstackisempty( )) { letter = charstacktop(); charstackpop(0)}
17 Tracing Client Code letter Private data: top 1 [MAX_ITEMS-1] items [ 0 ] [ 2 ] S [ 1 ] C 0 char letter = ; StackType charstack; charstackpush(letter); charstackpush( C ); charstackpush( S ); if ( charstackisempty( )) charstackpop( ); charstackpush( K ); while (charstackisempty( )) { letter = charstacktop(); charstackpop(0)}
18 Tracing Client Code letter Private data: top 2 [MAX_ITEMS-1] items [ 0 ] [ 2 ] K [ 1 ] C char letter = ; StackType charstack; charstackpush(letter); charstackpush( C ); charstackpush( S ); if ( charstackisempty( )) charstackpop( ); charstackpush( K ); while (charstackisempty( )) { letter = charstacktop(); charstackpop(0)}
19 Tracing Client Code letter Private data: top 2 [MAX_ITEMS-1] items [ 0 ] [ 2 ] K [ 1 ] C char letter = ; StackType charstack; charstackpush(letter); charstackpush( C ); charstackpush( S ); if ( charstackisempty( )) charstackpop( ); charstackpush( K ); while (charstackisempty( )) { letter = charstacktop(); charstackpop(0)}
20 Tracing Client Code letter Private data: top 2 K [MAX_ITEMS-1] items [ 0 ] [ 2 ] K [ 1 ] C char letter = ; StackType charstack; charstackpush(letter); charstackpush( C ); charstackpush( S ); if ( charstackisempty( )) charstackpop( ); charstackpush( K ); while (charstackisempty( )) { letter = charstacktop(); charstackpop(0)}
21 Tracing Client Code letter Private data: top 1 [MAX_ITEMS-1] items [ 0 ] K [ 2 ] K [ 1 ] C char letter = ; StackType charstack; charstackpush(letter); charstackpush( C ); charstackpush( S ); if ( charstackisempty( )) charstackpop( ); charstackpush( K ); while (charstackisempty( )) { letter = charstacktop(); charstackpop(0)}
22 Tracing Client Code letter Private data: top 1 K [MAX_ITEMS-1] items [ 0 ] [ 2 ] K [ 1 ] C char letter = ; StackType charstack; charstackpush(letter); charstackpush( C ); charstackpush( S ); if ( charstackisempty( )) charstackpop( ); charstackpush( K ); while (charstackisempty( )) { letter = charstacktop(); charstackpop(0)}
23 Tracing Client Code letter Private data: top 0 C [MAX_ITEMS-1] items [ 0 ] [ 2 ] K [ 1 ] C char letter = ; StackType charstack; charstackpush(letter); charstackpush( C ); charstackpush( S ); if ( charstackisempty( )) charstackpop( ); charstackpush( K ); while (charstackisempty( )) { letter = charstacktop(); charstackpop(0)}
24 Tracing Client Code letter Private data: top 0 C [MAX_ITEMS-1] items [ 0 ] [ 2 ] K [ 1 ] C char letter = ; StackType charstack; charstackpush(letter); charstackpush( C ); charstackpush( S ); if ( charstackisempty( )) charstackpop( ); charstackpush( K ); while (charstackisempty( )) { letter = charstacktop(); charstackpop(0)}
25 Tracing Client Code letter Private data: top -1 [MAX_ITEMS-1] items [ 0 ] [ 2 ] K [ 1 ] C char letter = ; StackType charstack; charstackpush(letter); charstackpush( C ); charstackpush( S ); if ( charstackisempty( )) charstackpop( ); charstackpush( K ); while (charstackisempty( )) { letter = charstacktop(); charstackpop(0)}
26 End of Trace letter Private data: top -1 [MAX_ITEMS-1] items [ 0 ] [ 2 ] K [ 1 ] C char letter = ; StackType charstack; charstackpush(letter); charstackpush( C ); charstackpush( S ); if ( charstackisempty( )) charstackpop( ); charstackpush( K ); while (charstackisempty( )) { letter = charstacktop(); charstackpop(0)}
27 Another Stack Implementation One advantage of an ADT is that the implementation can be changed without the program using it knowing about it The dynamic array implementation of the stack has a weakness -- the maximum size of the stack is passed to the constructor as parameter Instead we can dynamically allocate the space for each stack element as it is pushed onto the stack
28 ItemType is char class StackType StackType Top IsEmpty IsFull Private data: topptr C Push Pop ~StackType
29 ItemType is float class StackType StackType Top IsEmpty IsFull Push Pop ~StackType Private data: topptr
30 ItemType is string class StackType StackType Top IsEmpty IsFull Private data: topptr cat dog Push Pop ~StackType
31 letter Tracing Client Code char letter = ; StackType mystack; mystackpush(letter); mystackpush( C ); mystackpush( S ); If (mystackisempty() ) { letter = mystacktop( ); mystackpop(); } mystackpush( K );
32 letter Private data: topptr NULL Tracing Client Code char letter = ; StackType mystack; mystackpush(letter); mystackpush( C ); mystackpush( S ); If (mystackisempty() ) { letter = mystacktop( ); mystackpop(); } mystackpush( K );
33 letter Private data: topptr Tracing Client Code char letter = ; StackType mystack; mystackpush(letter); mystackpush( C ); mystackpush( S ); If (mystackisempty() ) { letter = mystacktop( ); mystackpop(); } mystackpush( K );
34 letter Private data: topptr C Tracing Client Code char letter = ; StackType mystack; mystackpush(letter); mystackpush( C ); mystackpush( S ); If (mystackisempty() ) { letter = mystacktop( ); mystackpop(); } mystackpush( K );
35 letter Private data: topptr Tracing Client Code S C V char letter = ; StackType mystack; mystackpush(letter); mystackpush( C ); mystackpush( S ); If (mystackisempty() ) { letter = mystacktop( ); mystackpop(); } mystackpush( K );
36 letter Private data: topptr Tracing Client Code S C char letter = ; StackType mystack; mystackpush(letter); mystackpush( C ); mystackpush( S ); If (mystackisempty() ) { letter = mystacktop( ); mystackpop(); } mystackpush( K );
37 letter Private data: topptr S C Tracing Client Code char letter = ; StackType mystack; mystackpush(letter); mystackpush( C ); mystackpush( S ); If (mystackisempty() ) { letter = mystacktop( ); mystackpop(); } mystackpush( K );
38 letter Private data: topptr S C Tracing Client Code char letter = ; StackType mystack; mystackpush(letter); mystackpush( C ); mystackpush( S ); If (mystackisempty() ) { letter = mystacktop( ); mystackpop(); } mystackpush( K );
39 letter Private data: topptr C Tracing Client Code char letter = ; StackType mystack; mystackpush(letter); mystackpush( C ); mystackpush( S ); If (mystackisempty() ) { letter = mystacktop( ); mystackpop(); } mystackpush( K );
40 letter Private data: topptr C Tracing Client Code char letter = ; StackType mystack; mystackpush(letter); mystackpush( C ); mystackpush( S ); If (mystackisempty() ) { letter = mystacktop( ); mystackpop(); } mystackpush( K );
41 letter Private data: topptr Tracing Client Code char letter = ; StackType mystack; mystackpush(letter); mystackpush( C ); mystackpush( S ); If (mystackisempty() ) { letter = mystacktop( ); mystackpop(); } mystackpush( K );
42 letter Private data: topptr Tracing Client Code char letter = ; StackType mystack; mystackpush(letter); mystackpush( C ); mystackpush( S ); If (mystackisempty() ) { letter = mystacktop( ); mystackpop(); } mystackpush( K );
43 letter Private data: topptr char letter = ; StackType mystack; mystackpush(letter); mystackpush( C ); K mystackpush( S ); If (mystackisempty() ) { letter = mystacktop( ); mystackpop(); } mystackpush( K ); Tracing Client Code
44 // DYNAMICALLY LINKED IMPLEMENTATION OF STACK Struct NodeType; //Forward declaration class StackType { public: //Identical to previous implementation private: NodeType* topptr; }; Struct NodeType { ItemType info; NodeType* next; };
45 Adding newitem to the stack newitem B newitem = B ; NodeType* location; location = new NodeType<char>; location->info = newitem; location->next = topptr; topptr = location; topptr X C L
46 Adding newitem to the stack newitem B newitem = B ; NodeType* location; location = new NodeType<char>; location->info = newitem; location->next = topptr; topptr = location; topptr X C L location
47 newitem B Adding newitem to the stack newitem = B ; NodeType* location; location = new NodeType<char>; location->info = newitem; location->next = topptr; topptr = location; topptr X C L location
48 Adding newitem to the stack newitem B newitem = B ; NodeType* location; location = new NodeType<char>; location->info = newitem; location->next = topptr; topptr = location; topptr X C L location B
49 newitem B Adding newitem to the stack newitem = B ; NodeType* location; location = new NodeType<char>; location->info = newitem; location->next = topptr; topptr = location; topptr X C L location B
50 Adding newitem to the stack newitem B newitem = B ; NodeType* location; location = new NodeType<char>; location->info = newitem; location->next = topptr; topptr = location; topptr X C L location B
51 Implementing Push void StackType::Push ( ItemType newitem ) // Adds newitem to the top of the stack { if (IsFull()) throw FullStack(); NodeType* location; location = new NodeType<ItemType>; location->info = newitem; location->next = topptr; topptr = location; } Do we need IsFull?
52 item Deleting item from the stack NodeType* tempptr; item = topptr->info; tempptr = topptr; topptr = topptr->next; delete tempptr; topptr B X C L tempptr
53 Deleting item from the stack item B NodeType* tempptr; item = topptr->info; tempptr = topptr; topptr = topptr->next; delete tempptr; topptr B X C L tempptr
54 item B Deleting item from the stack NodeType* tempptr; item = topptr->info; tempptr = topptr; topptr = topptr->next; delete tempptr; topptr B X C L tempptr
55 item B Deleting item from the stack NodeType* tempptr; item = topptr->info; tempptr = topptr; topptr = topptr->next; delete tempptr; topptr B X C L tempptr
56 item B Deleting item from the stack NodeType<ItemType>* tempptr; item = topptr->info; tempptr = topptr; topptr = topptr->next; delete tempptr; topptr X C L tempptr
57 Implementing Pop / Top void StackType::Pop() // Remove top item from Stack { if (IsEmpty()) throw EmptyStack(); else { NodeType* tempptr; tempptr = topptr; topptr = topptr ->next; delete tempptr; } } ItemType StackType::Top() // Returns a copy of the top item in the stack { if (IsEmpty()) throw EmptyStack(); else return topptr->info; }
58 Implementing IsFull bool StackType::IsFull() const // Returns true if there is no room for another // ItemType on the free store; false otherwise { NodeType* location; try { location = new NodeType; delete location; return false; } catch(std::bad_alloc exception) { return true; } }
59 Example of a Queue
60 Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the rear), and elements are removed from the other end (the front) A queue is a FIFO first in, first out structure
61 Queue ADT Operations MakeEmpty -- Sets queue to an empty state IsEmpty -- Determines whether the queue is currently empty IsFull -- Determines whether the queue is currently full Enqueue (ItemType newitem) -- Adds newitem to the rear of the queue Dequeue (ItemType& item) -- Removes the item at the front of the queue and returns it in item
62 ADT Queue Operations Transformers MakeEmpty Enqueue Dequeue Observers IsEmpty IsFull change state observe state
63 Public Interface of QueType typedef char ItemType; class QueType { public: QueType(int max); // max is the size of the queue QueType(); // Default size of 500 ~QueType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType item); void Dequeue(ItemType& item);
64 class QueType SIMPLE ARRAY IMPLEMENTATION QueType ~QueType Enqueue Dequeue Private Data: front 1 rear 4 maxque 5 items RESERVED C X J items [0] [1] [2] [3] [4]
65 What is the private part of class QueType for this design? What troubles do we encounter?
66 class QueType DYNAMIC ARRAY IMPLEMENTATION QueType ~QueType Enqueue Dequeue Private Data: front 1 rear 4 maxque 5 items RESERVED C X J items [0] [1] [2] [3] [4]
67 What is the private part of class QueType for this design? Does this design have the same problems as the previous design?
68 class QueType QueType ~QueType Enqueue Private Data: qfront qrear C Z T Dequeue
69 What is the private part of class QueType for this design?
70 CountedQueType inherits from QueType // DERIVE CLASS CountedQueType FROM BASE CLASS QueType class CountedQueType : QueType; { public: CountedQueType( ); void Enqueue( ItemType newitem ); void Dequeue( ItemType& item ); int GetLength( ) const; // Returns number of items on the counted queue private: int length; };
71 What additional fields does CountedQueType need? What functions must be changed?
72
Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.
5 Linked Structures Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of stack items can take place only at the
More informationADTs Stack and Queue. Outline
Chapter 5 ADTs Stack and Queue Fall 2017 Yanjun Li CS2200 1 Outline Stack Array-based Implementation Linked Implementation Queue Array-based Implementation Linked Implementation Comparison Fall 2017 Yanjun
More informationPropedéutico de Programación
Propedéutico de Programación Coordinación de Ciencias Computacionales Semana 4, Tercera Parte Dra. Pilar Gómez Gil Versión 1.1 01.07.08 http://ccc.inaoep.mx/~pgomez/cursos/programacion/ Capítulo 5 ADT
More informationADT Sorted List Operations
6 Lists Plus ADT Sorted List Operations Transformers MakeEmpty InsertItem DeleteItem Observers IsFull LengthIs RetrieveItem Iterators ResetList GetNextItem change state observe state process all class
More informationADTs Stack and Queue. Outline
Chapter 5 ADTs Stack and Queue Fall 2017 Yanjun Li CS2200 1 Outline Stack Array-based Implementation Linked Implementation Queue Array-based Implementation Linked Implementation Comparison Fall 2017 Yanjun
More informationData Structures and Algorithms for Engineers
04-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University
More informationStack. Data structure with Last-In First-Out (LIFO) behavior. Out
Stack and Queue 1 Stack Data structure with Last-In First-Out (LIFO) behavior In Out C B A B C 2 Typical Operations Pop on Stack Push isempty: determines if the stack has no elements isfull: determines
More informationWentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.
Lecture 11 1 More Data Structures In this lecture we will use a linked list to implement two abstract data types (ADT) An ADT provides the interface, or what a data structure does We can then use code
More informationFrom Data Structures to Abstract Data Types (ADTs)
From Data Structures to Abstract Data Types (ADTs) 1 Data Collections As our programs become more sophisticated, we need assistance : to organize large amounts of data to manage relationships among individual
More informationExtra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)
CPSC 122 Study Guide 3: Final Examination The final examination will consist of three parts: Part 1 covers mostly C++. For this, see study guides 1 and 2, exams 1 and 2, and part of exam 3, and quizzes
More informationWhat is a List? elements, with a linear relationship. first) has a unique predecessor, and. unique successor.
5 Linked Structures What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. That is, each list element (except the first) has a unique predecessor,
More informationCS 308 Data Structures Spring Dr. George Bebis Final Exam
CS 308 Data Structures Spring 2003 - Dr. George Bebis Final Exam Duration: noon - 2:00 pm Name: 1. True/False (3 pts each) To get credit, you must give brief reasons for your answers!! (1.1) T F An inorder
More informationChapter 18: Stacks And Queues
Chapter 18: Stacks And Queues 18.1 Introduction to the Stack ADT Introduction to the Stack ADT Stack: a LIFO (last in, first out) data structure Examples: plates in a cafeteria return addresses for function
More informationCS 216 Exam 1 Fall SOLUTION
CS 216 Exam 1 Fall 2004 - SOLUTION Name: Lab Section: Email Address: Student ID # This exam is closed note, closed book. You will have an hour and fifty minutes total to complete the exam. You may NOT
More informationLists, Stacks and Queues in C. CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4
Lists, Stacks and Queues in C CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4 Outline Structure Linked List Overview Implementation Stack Overview Implementation Queue Overview Implementation 2
More informationChapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Chapter 18: Stacks And Queues Copyright 2009 Pearson Education, Inc. Copyright Publishing as Pearson 2009 Addison-Wesley Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18.1 Introduction to
More informationName CPTR246 Spring '17 (100 total points) Exam 3
Name CPTR246 Spring '17 (100 total points) Exam 3 1. Linked Lists Consider the following linked list of integers (sorted from lowest to highest) and the changes described. Make the necessary changes in
More informationChapter 18: Stacks And Queues
Chapter 18: Stacks And Queues 18.1 Introduction to the Stack ADT Introduction to the Stack ADT Stack a LIFO (last in, first out) data structure Examples plates in a cafeteria return addresses for function
More informationStacks. Revised based on textbook author s notes.
Stacks Revised based on textbook author s notes. Stacks A restricted access container that stores a linear collection. Very common for solving problems in computer science. Provides a last-in first-out
More informationLecture 3: Stacks & Queues
Lecture 3: Stacks & Queues Prakash Gautam https://prakashgautam.com.np/dipit02/ info@prakashgautam.com.np 22 March, 2018 Objectives Definition: Stacks & Queues Operations of Stack & Queues Implementation
More informationCSC 1052 Algorithms & Data Structures II: Stacks
CSC 1052 Algorithms & Data Structures II: Stacks Professor Henry Carter Spring 2018 Recap Abstraction allows for information to be compartmentalized and simplifies modular use Interfaces are the Java construction
More informationData Structure. Recitation VII
Data Structure Recitation VII Recursion: Stack trace Queue Topic animation Trace Recursive factorial Executes factorial(4) Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4)
More informationCS6202 - PROGRAMMING & DATA STRUCTURES I Unit IV Part - A 1. Define Stack. A stack is an ordered list in which all insertions and deletions are made at one end, called the top. It is an abstract data type
More informationCS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)
CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) An interesting definition for stack element The stack element could be of any data type struct stackelement int etype; union int ival;
More informationCS24 Week 4 Lecture 2
CS24 Week 4 Lecture 2 Kyle Dewey Overview Linked Lists Stacks Queues Linked Lists Linked Lists Idea: have each chunk (called a node) keep track of both a list element and another chunk Need to keep track
More information.:: UNIT 4 ::. STACK AND QUEUE
.:: UNIT 4 ::. STACK AND QUEUE 4.1 A stack is a data structure that supports: Push(x) Insert x to the top element in stack Pop Remove the top item from stack A stack is collection of data item arrange
More informationAn Introduction to Queues With Examples in C++
An Introduction to Queues With Examples in C++ Prof. David Bernstein James Madison University Computer Science Department bernstdh@jmu.edu Motivation Queues are very straightforward but are slightly more
More informationADT Unsorted List. Outline
Chapter 3 ADT Unsorted List Fall 2017 Yanjun Li CS2200 1 Outline Abstract Data Type Unsorted List Array-based Implementation Linked Implementation Comparison Fall 2017 Yanjun Li CS2200 2 struct NodeType
More informationSTACKS AND QUEUES. Problem Solving with Computers-II
STACKS AND QUEUES Problem Solving with Computers-II 2 Stacks container class available in the C++ STL Container class that uses the Last In First Out (LIFO) principle Methods i. push() ii. iii. iv. pop()
More informationCPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues
CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues Alan J. Hu (Slides borrowed from Steve Wolfman) Be sure to check course webpage! http://www.ugrad.cs.ubc.ca/~cs221 1 Lab 1 is available.
More informationCPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues
CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues Alan J. Hu (Slides borrowed from Steve Wolfman) Be sure to check course webpage! http://www.ugrad.cs.ubc.ca/~cs221 1 Lab 1 available very
More informationChapter 9 STACK, QUEUE
Chapter 9 STACK, QUEUE 1 LIFO: Last In, First Out. Stacks Restricted form of list: Insert and remove only at front of list. Notation: Insert: PUSH Remove: POP The accessible element is called TOP. Stack
More informationl Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)
Final Exam Exercises Chapters 1-7 + 11 Write C++ code to: l Determine if a number is odd or even CS 2308 Fall 2016 Jill Seaman l Determine if a number/character is in a range - 1 to 10 (inclusive) - between
More informationChapter 4. ADT Sorted List
Chapter 4 ADT Sorted List Sorted Type Class Interface Diagram SortedType class MakeEmpty IsFull GetLength GetItem PutItem DeleteItem Private data: length info [ 0 ] [ 1 ] [ 2 ] [MAX_ITEMS-1] currentpos
More informationMore Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;
More Group HW The following code is contained in the file ex1stck.h. Fill in the blanks with the C++ statement(s) that will correctly finish the method. Each blank may be filled in with more than one statement.
More informationWhat can we do with Coin dispenser?
/7/ Outline Part Stacks Stacks? Applications using Stacks Implementing Stacks Relationship between Stacks and Recursive Programming Instructor: Sangmi Pallickara (sangmi@cscolostateedu) Department of Computer
More informationData Structures (INE2011)
Data Structures (INE2011) Electronics and Communication Engineering Hanyang University Haewoon Nam Lecture 4 1 Stacks Insertion and deletion are made at one end () Last input first output (LIFO) Inserting
More informationObject Oriented Programming COP3330 / CGS5409
Object Oriented Programming COP3330 / CGS5409 Intro to Data Structures Vectors Linked Lists Queues Stacks C++ has some built-in methods of storing compound data in useful ways, like arrays and structs.
More informationCS 2604 Homework 1 Greatest Hits of C++ Summer 2000
Instructions: This homework assignment covers some of the basic C++ background you should have in order to take this course. I. Pointers Opscan forms will be passed out in class and will be available at
More information! A data type for which: ! An ADT may be implemented using various. ! Examples:
Stacks and Queues Unit 6 Chapter 19.1-2,4-5 CS 2308 Fall 2018 Jill Seaman 1 Abstract Data Type A data type for which: - only the properties of the data and the operations to be performed on the data are
More informationCh. 18: ADTs: Stacks and Queues. Abstract Data Type
Ch. 18: ADTs: Stacks and Queues CS 2308 Fall 2011 Jill Seaman Lecture 18 1 Abstract Data Type A data type for which: - only the properties of the data and the operations to be performed on the data are
More informationList, Stack and Queue Implementation
Roy Chan CSC2100B Data Structures Tutorial 2 (Version 2) January 21, 2009 1 / 39 1 CSC2100B Online Judge Score 2 Structure 3 Linked List Overview Implementation 4 Stack Overview Implementation 5 Queue
More informationLinked List. April 2, 2007 Programming and Data Structure 1
Linked List April 2, 2007 Programming and Data Structure 1 Introduction head A linked list is a data structure which can change during execution. Successive elements are connected by pointers. Last element
More informationCS 2604 Homework 2 Solution for Greatest Hits of C++ Fall 2000
Instructions: This homework assignment covers some of the basic C++ background you should have in order to take this course. I. Pointers Opscan forms will be passed out in class. Write your name and code
More informationC Data Structures Stacks. Stack. push Adds a new node to the top of the stack
1 12 C Data Structures 12.5 Stacks 2 Stack New nodes can be added and removed only at the top Similar to a pile of dishes Last-in, first-out (LIFO) Bottom of stack indicated by a link member to NULL Constrained
More informationIntroduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18
Stacks and Queues Week 9 Gaddis: Chapter 18 CS 5301 Fall 2015 Jill Seaman Introduction to the Stack Stack: a data structure that holds a collection of elements of the same type. - The elements are accessed
More informationdouble d0, d1, d2, d3; double * dp = new double[4]; double da[4];
All multiple choice questions are equally weighted. You can generally assume that code shown in the questions is intended to be syntactically correct, unless something in the question or one of the answers
More informationITI Introduction to Computing II
ITI 1121. Introduction to Computing II Queues ArrayQueue Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 10, 2014 Abstract These lecture notes are meant to be looked
More informationData Structures using OOP C++ Lecture 9
Stack A stack is an ordered group of homogeneous items or elements. The removal of existing items and the addition of new items can take place only at the top of the stack. The stack may be considered
More informationSCJ2013 Data Structure & Algorithms. Queue. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi
SCJ2013 Data Structure & Algorithms Queue Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi Course Objectives At the end of the lesson students are expected to be able to: Understand queue concepts and
More information8/28/12. Outline. Part 2. Stacks. Linear, time ordered structures. What can we do with Coin dispenser? Stacks
8/8/ Part Stacks Instructor: Sangmi Pallickara (sangmi@cscolostateedu) Department of Computer Science Linear, time ordered structures Data Structures that reflects a temporal relationship Order of removal
More informationCSC 1052 Algorithms & Data Structures II: Queues
CSC 1052 Algorithms & Data Structures II: Queues Professor Henry Carter Spring 2018 Recap Recursion solves problems by solving smaller version of the same problem Three components Applicable in a range
More informationDATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.
DATA STRUCUTRES A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently. An algorithm, which is a finite sequence of instructions, each of which
More informationStacks and Queues. Gregory D. Weber. CSCI C243 Data Structures
Stacks and Queues Gregory D. Weber CSCI C243 Data Structures Principal Points 1. The Stack interface: how to declare it, what it does. 2. Generics: why they were added to Java, how to use them. 3. The
More informationData Structures Week #3. Stacks
Data Structures Week #3 Stacks Outline Stacks Operations on Stacks Array Implementation of Stacks Linked List Implementation of Stacks Stack Applications October 5, 2015 Borahan Tümer, Ph.D. 2 Stacks (Yığınlar)
More informationLecture Data Structure Stack
Lecture Data Structure Stack 1.A stack :-is an abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example a deck of cards
More information8/19/2014. Most algorithms transform input objects into output objects The running time of an algorithm typically grows with input size
1. Algorithm analysis 3. Stacks 4. Queues 5. Double Ended Queues Semester I (2014) 1 Most algorithms transform input objects into output objects The running time of an algorithm typically grows with input
More informationADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S
Stacks Stacks & Queues A linear sequence, or list, is an ordered collection of elements: S = (s 1, s 2,..., s n ) Stacks and queues are finite linear sequences. A Stack is a LIFO (Last In First Out) list.
More informationStack and Queue. Stack:
Stack and Queue Stack: Abstract Data Type A stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle. In the pushdown stacks only two operations
More informationMULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR
STUDENT IDENTIFICATION NO MULTIMEDIA COLLEGE JALAN GURNEY KIRI 54100 KUALA LUMPUR FIFTH SEMESTER FINAL EXAMINATION, 2014/2015 SESSION PSD2023 ALGORITHM & DATA STRUCTURE DSEW-E-F-2/13 25 MAY 2015 9.00 AM
More informationQueues. CITS2200 Data Structures and Algorithms. Topic 5
CITS2200 Data Structures and Algorithms Topic 5 Queues Implementations of the Queue ADT Queue specification Queue interface Block (array) representations of queues Recursive (linked) representations of
More informationSeptember 19,
September 19, 2013 1 Problems with previous examples Changes to the implementation will require recompilation & relinking of clients Extensions will require access to the source code Solutions Combine
More informationDiscussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:
Discussion 2C Notes (Week 3, January 21) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Abstraction In Homework 1, you were asked to build a class called Bag. Let
More informationCSCD 326 Data Structures I Stacks
CSCD 326 Data Structures I Stacks 1 Stack Interface public interface StackInterface { public boolean isempty(); // Determines whether the stack is empty. // Precondition: None. // Postcondition: Returns
More informationBBM 201 DATA STRUCTURES
BBM 201 DATA STRUCTURES Lecture 5: Stacks and Queues 2017 Fall Stacks A list on which insertion and deletion can be performed. Based on Last-in-First-out (LIFO) Stacks are used for a number of applications:
More informationData Structures. BSc in Computer Science University of New York, Tirana. Assoc. Prof. Marenglen Biba 1-1
Data Structures BSc in Computer Science University of New York, Tirana Assoc. Prof. Marenglen Biba 1-1 General info Course : Data Structures (3 credit hours) Instructor : Assoc. Prof. Marenglen Biba Office
More informationStack Implementation
Stack Implementation (In Java Using BlueJ) What is BlueJ? BlueJ is a Java integrated development environment (IDE) which has been designed specifically for learning object oriented programming in Java.
More informationAshish Gupta, Data JUET, Guna
Introduction In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station etc. The person who come first, he/she
More informationAnother common linear data structure similar to. new items enter at the back, or rear, of the queue. Queue is an ADT with following properties
Queues FIFO queue ADT Examples using queues reading character string in order recognize palindromes Queue implementations LL pointer based List ADT based array based tradeoffs 1 ADT queue operations Create
More informationStacks and Queues
Stacks and Queues 2-25-2009 1 Opening Discussion Let's look at solutions to the interclass problem. Do you have any questions about the reading? Do you have any questions about the assignment? Minute Essays
More informationIntroduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. Week 9. elements of the same type.
Stacks and Queues Week 9 Gaddis: Chapter 18 (8th ed.) Gaddis: Chapter 19 (9th ed.) CS 5301 Fall 2018 Jill Seaman Introduction to the Stack Stack: a data structure that holds a collection of elements of
More informationcsci 210: Data Structures Stacks and Queues
csci 210: Data Structures Stacks and Queues 1 Summary Topics Stacks and Queues as abstract data types ( ADT) Implementations arrays linked lists Analysis and comparison Applications: searching with stacks
More informationQueue with Array Implementation
Queue with Array Implementation SCSJ2013 Data Structures & Algorithms Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi Faculty of Computing Objectives Queue concepts and applications. Queue structure and
More information[CS302-Data Structures] Homework 2: Stacks
[CS302-Data Structures] Homework 2: Stacks Instructor: Kostas Alexis Teaching Assistants: Shehryar Khattak, Mustafa Solmaz, Bishal Sainju Fall 2018 Semester Section 1. Stack ADT Overview wrt Provided Code
More informationStacks and Queues. CSE 373 Data Structures Lecture 6
Stacks and Queues CSE 373 Data Structures Lecture 6 Readings and References Reading Sections 3.3 and 3.4 10/11/02 Stacks and Queues - Lecture 6 2 Stacks A list for which Insert and Delete are allowed only
More informationCT 229 Object-Oriented Programming Continued
CT 229 Object-Oriented Programming Continued 24/11/2006 CT229 Summary - Inheritance Inheritance is the ability of a class to use the attributes and methods of another class while adding its own functionality
More information! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade
Final Exam Review Final Exam Mon, May 5, 2:00PM to 4:30PM CS 2308 Spring 2014 Jill Seaman Closed book, closed notes, clean desk Comprehensive (covers entire course) 30% of your final grade I recommend
More informationCS200: Queues. Prichard Ch. 8. CS200 - Queues 1
CS200: Queues Prichard Ch. 8 CS200 - Queues 1 Queues First In First Out (FIFO) structure Imagine a checkout line So removing and adding are done from opposite ends of structure. 1 2 3 4 5 add to tail (back),
More informationQueues. Gaddis 18.4, Molly A. O'Neil CS 2308 :: Spring 2016
Queues Gaddis 18.4, 18.6 Molly A. O'Neil CS 2308 :: Spring 2016 The Queue ADT A queue is an abstract data type that stores a collection of elements of the same type The elements of a queue are accessed
More information8. Fundamental Data Structures
172 8. Fundamental Data Structures Abstract data types stack, queue, implementation variants for linked lists, [Ottman/Widmayer, Kap. 1.5.1-1.5.2, Cormen et al, Kap. 10.1.-10.2] Abstract Data Types 173
More informationThe Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.
The Stack ADT Stacks Set of objects in which the location an item is inserted and deleted is prespecified Stacks! Insert in order! Delete most recent item inserted! LIFO - last in, first out Stacks 2 The
More informationSCHOOL OF COMPUTER AND COMMUNICATION ENGINEERING. EKT224: ALGORITHM AND DATA STRUCTURES (Stack, Queue, Linked list)
ASSIGNMENT 2 SCHOOL OF COMPUTER AND COMMUNICATION ENGINEERING EKT224: ALGORITHM AND DATA STRUCTURES (Stack, Queue, Linked list) Date: 30/9/2015 Due Date: 30/10/2015 Early Bird Date: 23/10/2015 QUESTION
More informationCMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC
CMSC 341 Lecture 6 Templates, Stacks & Queues Based on slides by Shawn Lupoli & Katherine Gibson at UMBC Today s Topics Data types in C++ Overloading functions Templates How to implement them Possible
More informationAbstract Data Types. Abstract Data Types
Abstract Data Types Wolfgang Schreiner Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria Wolfgang.Schreiner@risc.jku.at http://www.risc.jku.at Wolfgang Schreiner
More information! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:
Ch. 8: ADTs: Stacks and Queues Abstract Data Type A data type for which: CS 8 Fall Jill Seaman - only the properties of the data and the operations to be performed on the data are specific, - not concerned
More informationAssociate Professor Dr. Raed Ibraheem Hamed
Associate Professor Dr. Raed Ibraheem Hamed University of Human Development, College of Science and Technology Computer Science Department 2015 2016 1 What this Lecture is about: Stack Structure Stack
More information"apple" "grape" "grape" "grape" "apple"
Test 1: CPS 100 Owen Astrachan and Dee Ramm February 21, 1997 Name: Honor code acknowledgment (signature) Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 Problem 6 TOTAL: value 10 pts. 9 pts. 21 pts.
More informationStacks. Access to other items in the stack is not allowed A LIFO (Last In First Out) data structure
CMPT 225 Stacks 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 called the bottom Access to other
More informationCS302 - Data Structures using C++
CS302 - Data Structures using C++ Topic: Queues and Priority Queues Kostas Alexis Implementations of the ADT Queue Like stacks, queues can have Array-based or Link-based implementation Can also use implementation
More information1 P age DS & OOPS / UNIT II
UNIT II Stacks: Definition operations - applications of stack. Queues: Definition - operations Priority queues - De que Applications of queue. Linked List: Singly Linked List, Doubly Linked List, Circular
More informationStack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5
What is Stack? Stack 1. Stack is LIFO Structure [ Last in First Out ] 2. Stack is Ordered List of Elements of Same Type. 3. Stack is Linear List 4. In Stack all Operations such as Insertion and Deletion
More informationDay 6. COMP1006/1406 Summer M. Jason Hinek Carleton University
Day 6 COMP1006/1406 Summer 2016 M. Jason Hinek Carleton University today s agenda assignments Assignment 3 is due on Monday a quick look back abstract classes and interfaces casting objects abstract data
More informationData Structure - Stack and Queue-
Data Structure - Stack and Queue- Hanyang University Jong-Il Park STACK Stack ADT List that insertions and deletions can be performed at the end of the list Operations Push(X, S): insert X in the list
More informationLinear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013
Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every
More informationStacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.
Stacks 1 Stacks The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle. 2 Main stack operations Insertion and removal are defined by: push(e): inserts
More information15. Stacks and Queues
COMP1917 15s2 15. Stacks and Queues 1 COMP1917: Computing 1 15. Stacks and Queues Reading: Moffat, Section 10.1-10.2 Overview Stacks Queues Adding to the Tail of a List Efficiency Issues Queue Structure
More informationCPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists!
CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists! Winter 2013 Instructor: Hassan Khosravi Problems with Arrays and Vectors With arrays and vectors you are allocated a large space.
More informationSTACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:
STACKS A stack is a linear data structure for collection of items, with the restriction that items can be added one at a time and can only be removed in the reverse order in which they were added. The
More informationData Abstraction and Specification of ADTs
CITS2200 Data Structures and Algorithms Topic 4 Data Abstraction and Specification of ADTs Example The Reversal Problem and a non-adt solution Data abstraction Specifying ADTs Interfaces javadoc documentation
More informationQueues. A queue is a special type of structure that can be used to maintain data in an organized way.
A queue is a special type of structure that can be used to maintain data in an organized way. This data structure is commonly implemented in one of two ways: as an array or as a linked list. In either
More information