Stacks: Array Data Structure and Stack

Topics: Array data structure, Stack, Linked list Pages: 7 (1126 words) Published: March 8, 2013
L10: Stacks

UCCD1024 Data Structures

What you will learn today?
 What are stacks?
 Why study stacks?  How to implement stack using linked list?  How to apply the stack concept in application? 

This is our emphasis here.

 Read Textbook 1: Chapter 7.
L10: Stack Jan 2013 2

What are stacks?
 It is a special kind of list.  The operations of insert (called push) and delete (called pop) are allowed only at one end (i.e., top).  It has the property of LIFO: Last In First Out.  Less general than list and easier to implement because no insertion and deletion at random locations. L10: Stack Jan 2013

item4 item3 item2 Take this Insert here item1 one out
without moving other items?

X

without removing item4?
3

X

What you will learn today?
 What are stacks?

 Why study stacks?
 How to implement stack using linked list?  How to apply the stack concept in application? 

This is our emphasis here.

L10: Stack Jan 2013

4

Why study stacks?
 A lot of problems have the LIFO characteristics.  With the data structure of stacks that support LIFO, one can easily create LIFO operations for them.  Problems that need LIFO:  moving a stack of boxes (the removal order is LIFO)

1 2 3 4 5 6

7

L10: Stack Jan 2013

5

Why study stacks?
 A lot of problems have the LIFO characteristics.  With the data structure of stacks that support LIFO, one can easily create LIFO operations for them.  Problems that need LIFO:  moving a stack of boxes (the removal order is LIFO),  reversing a list of objects,  can employ the operation of 2 LIFO stacks  programming function call will use the system stack for its need L10: Stack of LIFO ordering. Jan 2013

1 2 3 4 5 6

7

6

Why study stacks?
 A lot of problems have the LIFO characteristics.  With the data structure of stacks that support LIFO, one can easily create LIFO operations for them.  Problems that need LIFO:  Check proper pairing of (, ), {, }, [, ] in your program, e.g. check the following. 

y = { x + [ ( u – b ) / 4] }

L10: Stack Jan 2013

Towers of Hanoi (see later lecture) Dungeon & Dragon (see next page) 7

Why study stacks?
 Example of Dungeon & Dragon After visiting rooms D, B and C as shown below, one of the direct approaches to get back to surface is “exit the last room (in history) that you have got in”. This is LIFO (Last room to get in, first room to get out) approach.

room
tunnel
L10: Stack Jan 2013

8

What you will learn today?
 What are stacks?
 Why study stacks?

 How to implement stack using linked list?
 How to apply the stack concept in application?

This is our emphasis here.

L10: Stack Jan 2013

9

How to implement stacks using List?
 The implementation of stack can be based on either linked list or array list.  Since we has learnt linked list only, we will use linked list implementation as shown on next page.  The most important operations that we need to model are the  Push: insert new item as the first node, i.e. same as insert(1, newItem) from List definition.  Pop: remove the first node, i.e. same as remove(1).

item4 item3 item2 item1
10

L10: Stack Jan 2013

Push (insert new item as the first node only)
10 20 item 30 next 40 50

Pop (delete the first node only)

A list becomes a LIFO stack if we allow the push and pop operations only on one end of this list L10: Stack Jan 2013

item4 item3 item2 item1
11

 The implementation of stack can make use of “has-a” relationship (see below) to reuse all the functions defined for List, i.e. this will save implementation time.  Line 2 below defines Stack to have a List component. 1 2 3 4 5 6 7 8 template class Stack {// Here Stack has a component of List private: List list; public: bool empty(); int size(); bool push(T); // insert item bool pop(T&); // remove item }; 12

L10:...