Stacks A stack is a data structure that is similar in spirit to a pile of cafeteria trays. Think about the trays in the dining halls: when the dining staff.

Slides:



Advertisements
Similar presentations
Stack & Queues COP 3502.
Advertisements

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.
Data Structures & Algorithms
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Implementing Stacks Using Arrays CSC 1401: Introduction to Programming with Java Week 14 – Lecture 1 Wanda M. Kunkle.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
Data Structures Data structures permit the storage of related data for use in your program. –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.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
Stacks and Queues Introduction to Computing Science and Programming I.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
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.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
CMSC 202 Stacks and Queues. What’s a Queue? A queue is a linear collection of homogeneous data in which items added to the queue must be placed at the.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
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.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
CHP-3 STACKS.
Stack Data Structure By Marwa M. A. Elfattah. Stack - What A stack is one of the most important non- primitive linear data structure in computer science.
Stacks Stack Abstract Data Type (ADT) Stack ADT Interface
Stacks II David Lillis School of Computer Science and Informatics
Elementary Data Structures
Review Array Array Elements Accessing array elements
Pointers and Linked Lists
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
MEMORY REPRESENTATION OF STACKS
Dr. Bernard Chen Ph.D. University of Central Arkansas
Hassan Khosravi / Geoffrey Tien
Queues Queues Queues.
Stacks.
Stack and Queue APURBO DATTA.
Visit for more Learning Resources
Stack and Queue.
Stack Data Structure, Reverse Polish Notation, Homework 7
HW-6 Deadline Extended to April 27th
Queues A queue is a data structure that is similar in spirit to a fair line. As you can see in this photo, the first dog in line can expect to be the first.
CMSC 341 Lecture 5 Stacks, Queues
Principles of Computing – UFCFA3-30-1
Pointers and Linked Lists
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS1S467 GUI Programming LECTURE 11 Collections
Stacks, Queues, and Deques
More Data Structures (Part 1)
Stacks and Queues.
Stacks.
Stacks, Queues, and Deques
Data Structures – Stacks and Queus
Tonga Institute of Higher Education
Data Structures and Algorithms for Information Processing
Abstract Data Type Abstract Data Type as a design tool
Stacks.
Stacks and Queues Prof. Michael Tsai 2017/02/21.
More Data Structures (Part 1)
Visit for more Learning Resources
Stacks and Queues: Concepts and Implementations
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Stacks, Queues, and Deques
Stacks Written by J.J. Shepherd.
Stacks and Queues.
Lecture 9: Stack and Queue
Presentation transcript:

Stacks A stack is a data structure that is similar in spirit to a pile of cafeteria trays. Think about the trays in the dining halls: when the dining staff put trays out before meals, they pile them from the bottom to the top, and then you take the top-most tray when you arrive. The last tray that the staff put on the piles is the first one taken from the pile.

push pop LIFO Similarly, with stacks, the last element inserted will be the first element retrieved. We'll refer to this pattern of insertion and retrieval as "last-in, first-out," or LIFO for short. A stack's two primary operations are called push and pop. push places a new element on the top of the stack (like a dining hall's tray or a function's stack frame), and pop retrieves the topmost element from the stack, decrementing the stack's size in the process. Like queues (and unlike arrays), stacks typically don't allow access to elements in the middle.

char* strings[CAPACITY]; int size; } stack; typedef struct { char* strings[CAPACITY]; int size; } stack; This is one way to define a stack for char*s. CAPACITY is a constant and strings is a statically-sized array of char*s that you'll use for storing the char* elements. size stores the number of elements currently in the stack. You'll need to adjust it appropriately so that you can track the location of the "top" of the stack. Why is this design suboptimal? It imposes a limit on the size of the stack.

[5] push TODOs: [4] [3] [2] [1] [0] size < CAPACITY? store element at [size] size++ [4] [3] To push an element onto the stack, first make sure that the array isn't full by comparing size to CAPACITY. If size < CAPACITY, store the element in the next available open slot, which should be at index [size]. [2] [1] [0]

[5] pop TODOs: [4] [3] [2] [1] [0] size > 0? size-- return [size] To pop an element off the stack, first make sure that there are elements in the array by checking whether size > 0. If size > 0, decrement size and return the last element in the array, which should be at index [size]. [2] [1] [0]