EE 441 Data Structures Lecture 6 - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

EE 441 Data Structures Lecture 6

Description:

DataType Peek(void) const; //check stack state. int StackEmpty(void) const; ... write Peek as exercise //Stack Empty. int Stack::StackEmpty(void) const ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 17
Provided by: eceg6
Category:

less

Transcript and Presenter's Notes

Title: EE 441 Data Structures Lecture 6


1
EE 441 Data StructuresLecture 6
  • Stacks and Queues

2
Stacks
  • A list of items and a pointer to the "top" item
    in the list.
  • Items can be inserted or removed from the list
    only at the top
  • the list is ordered in the sequence of entry of
    items.
  • Insertions and removals proceed in the "LIFO"
    last-in-first-out order.
  • Functions
  • Push for putting objects into the stack
  • Pop for taking objects out of the stack

3
Stack Class Declaration
  • include ltiostream.hgt
  • include ltstdlib.hgt
  • typedef char DataType
  • const int MaxStackSize50
  • class Stack
  • private
  • DataType stacklistMaxStackSize
  • int top
  • public
  • Stack(void) // constructor to initialize top
  • //modification operations
  • void Push(const DataType item)
  • DataType Pop(void)
  • void ClearStack(void)
  • //just copy top item without modifying stack
    contents
  • DataType Peek(void) const
  • //check stack state

4
Stack Class Implementation
  • StackStack(void)top(-1)
  • //Push
  • void StackPush(const DataType item)//same as
    //Push(DataType item)
  • //can not push if stack has exceeded its limits
  • if (topMaxStackSize-1)
  • cerrltlt"Stack overflow"ltltendl
  • exit(1)
  • // increment top ptr and copy item into list
  • top
  • stacklisttop item
  • //pop
  • DataType StackPop(void)
  • //Peek is the same as Pop, except top is not
    moved
  • DataType StackPeek(void) const
  • // write Peek as exercise
  • //Stack Empty
  • int StackStackEmpty(void) const
  • return top-1 //value is 1 if equal, 0
    otherwise
  • // Stack Full
  • int StackStackFull(void)const
  • return topMaxStackSize-1

5
Stack Example
  • Example Write a program that uses a stack to
    test a given character string and decides if the
    string is a palindrome (i.e. reads the same
    backwards and forwards, e.g. "kabak", " a man a
    plan a canal panama", etc.)
  • Algorithm
  • first get rid of all blanks in the string
  • push the whole string character-wise, into a
    stack
  • pop out characters one by one, comparing with the
    characters of the original de-blanked string

6
Stack Example
  • void main()
  • // create stack object to store string in reverse
    order.
  • Stack S
  • char palstring 80 , deblankedstring 80 , c
  • int i0 // string pointer
  • bool ispalindrometrue //we'll stop if false
  • // get input
  • cin.getline(palstring,80,'\n')
  • //remove blanks
  • Deblank(palstring,deblankedstring)// A,the
    arrayA pointer to A
  • //push character onto stack
  • i0
  • while(deblankedstring i !NULL)
  • S.Push(deblankedstring i )
  • Assuming that the stack
  • declaration and implementation
  • are in "astack.h
  • include astack.h"
  • include ltiostream.hgt
  • void Deblank(char s, char t)
  • while (s!NULL)
  • if (s!' ')
  • ts
  • t
  • s
  • tNULL //append NULL to newstring

7
Templates
  • We use typedef to define the data type for the
    stack items for the stack class
  • Stack class can be used only with that type of
    data in the program
  • We want to use the same class or function
    definition for different types of items
  • Create different objects within the same class
    but with different data types
  • Define a general function that works on different
    data types
  • Link the data type with the object or function
    call and not with the whole program
  • A single function or class definition gives rise
    to a family of functions or classes that are
    compiled from the same source code, but operate
    on different types.
  • Stack ltfloatgt A
  • Stackltchargt B
  • C allows this through Templates

8
Templates
  • template ltclass T1, class T2, classTngt indicates
    that T1,T2, ..Tn are classes that will be used
    with a specific class upon creation of an object
  • Note here that any operation in the template
    class or function must be defined for any
    possible data types in the template
  • template ltclass Tgt
  • int SeqSearch(T list? ?, int n, T key)
  • / T is a type that will be specified when
    SeqSearch is called /
  • for (int i0 iltn i)
  • if (list?i?key)
  • return i
  • return -1
  •  
  • void main()
  • int A10, Aindex
  • float M10, fkey4.5, Mindex
  • AindexSeqSearch(A,10,25) /search for int 25 in
    A /
  • MindexSeqSearch(M,100,fkey) / search for float
    4.5 in M /

9
A general stack
  • template ltclass Tgt
  • StackltTgtStack(void)top(-1)
  • template ltclass Tgt
  • //Push
  • void StackltTgtPush(const T item)//same as
    //Push(int item)
  • //can not push if stack has exceeded its limits
  • if (topMaxStackSize-1)
  • cerrltlt"Stack overflow"ltltendl
  • exit(1)
  • // increment top ptr and copy item into list
  • top
  • stacklisttop item
  • template ltclass Tgt
  • const int MaxStackSize50
  • template ltclass Tgt
  • class Stack
  • private
  • T stacklistMaxStackSize
  • int top
  • public
  • Stack(void) // constructor to initialize top
  • //modification operations
  • void Push(const T item)
  • T Pop(void)
  • void ClearStack(void)
  • //just copy top item without modifying stack
    contents
  • T Peek(void) const
  • //check stack state
  • int StackEmpty(void) const
  • int StackFull(void) const

10
Example Arithmetic expression evaluation
  • Arithmetic expressions
  • 32513 Correct
  • 32525 Incorrect
  • Parentheses ((111)/2-4 )36
  • Operator precedence Low to High
  • ( ? -1
  • ) ? 0
  • ,- ? 1
  • , / ? 2
  • exp? 3
  • A ( starts a new expression which must be
    calculated before others currently being
    calculated? 4
  • Rule for checking operator-operand matching
  • rank (state variable)
  • Initialize rank to 0
  • For each operand add 1
  • For each binary operator subtract 1
  • For unary operators and parentheses no action
  • For each term the rank should be 0 or 1
  • For the full expression it must be 1

11
Example Arithmetic expression evaluation
  • Two stacks
  • Operator stack
  • Operand stack
  • Algorithm
  • Input operand push it on operand stack
  • Input operator Pop all operators which have
    stack precedence higher than or equal to the
    precedence of current operators
  • As operators are popped execute the operation on
    top stack operands, push result back.
  • e.g. input is ) Pop and evaluate all operators
    that are in the stack and have stack precedence
    greater than or equal to the input precedence of
    )
  • (Note that as stack precedence of ( is
  • - 1, this loop stops when ( is found and
    popped.)
  • At end of expression flush the operator stack
    evaluating operations.
  • The rank must be 1.
  • If rank lt1 ?operand missing.
  • While clearing stack if ( found ? ) missing.
  • Final result left on stack top
  • CHECK Preiss book stack applications for a
    different example

12
Example
  • e.g. (35)/2e3

Finished Flush stack
13
Queues
  • A queue is a data structure that stores elements
    in a list and permits data access only at the two
    ends of the list.
  • An element is inserted only at the rear end of
    the list and deleted from only the front end of
    the list.
  • Elements are removed in the same order in which
    they are stored and hence a queue provides
    FIFO(first-in/first-out) or FCFC(first-come/first-
    served) ordering

14
Queues
  • 1. initially count0, front0, rear0

5. delete
0 1 2 3
A
B
C
c2
r
not accessible any more
f
c0
f,r
2. insert A
A
6. insert D
B
C
D
c1
r
f
3. insert B
c3
f
r
A
B
NOTE CIRCULAR OPERATION i.e., move rear front
forward rear(rear1)MaxQSize front(front1)Ma
xQSize etc.
r
f
c2
4. insert C
A
B
C
c3
r
f
15
A General Queue
  • // Queue constructor
  • //initialize queue front, rear, count
  • template ltclass Tgt
  • QueueltTgtQueue(void) front(0), rear (0),
    count(0)
  • //Queue Operations
  • // Qinsert insert item into the queue
  • template ltclass Tgt
  • void QueueltTgtQinsert(const T item)
  • // terminate if queue is full
  • if (countMaxQSize)
  • cerrltlt"Queue overflow!" ltltendl
  • exit(1)
  • //increment count, assign item to qlist and
    update rear
  • count
  • include ltiostream.hgt
  • include ltstdlib.hgt
  • const int MaxQSize50
  • template ltclass Tgt
  • class Queue
  • private
  • // queue array and its parameters
  • int front, rear, count
  • T qlist MaxQSize
  • public

16
References
  • Data Structures with C, William H. Ford,William
    R. Topp
  • http//www.brpreiss.com/books/opus4/
  • http//vision1.eee.metu.edu.tr/vision/LectureNote
    s/EE441/ch1/classes.html
  • http//www.research.att.com/bs/glossary.html
Write a Comment
User Comments (0)
About PowerShow.com