INFSCI 0015 Data Structures Lecture 17: Stack - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

INFSCI 0015 Data Structures Lecture 17: Stack

Description:

Adds new data element to the top of the stack. Removes a data element from ... else. printf('Stack is Emptyn'); else. return; Application: Parenthesis matching ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 20
Provided by: peterbru
Category:

less

Transcript and Presenter's Notes

Title: INFSCI 0015 Data Structures Lecture 17: Stack


1
INFSCI 0015 - Data StructuresLecture 17 Stack
  • Peter Brusilovsky
  • http//www2.sis.pitt.edu/peterb/0015-011/

2
Stacks in Our Life
3
More Stacks
  • A stack is a LIFO structure Last In First Out

4
Basic Operations with Stacks
  • Push
  • Add and item
  • Overflow
  • Pop
  • Remove an item
  • Underflow
  • Stack Top
  • Whats on the Top
  • Could be empty

5
Push
  • Adds new data element to the top of the stack

6
Pop
  • Removes a data element from the top of the stack

7
Stack Top
  • Checks the top element. Stack is not changed

8
(No Transcript)
9
Implementing Stack with Array
  • Stack is stored as an array
  • Max number of elements allocated

10
Data Structure
  • / Stack of integers intstack /
  • struct intstack
  • int stackAry / array of elem. /
  • int count / N of elements /
  • int stackMax / Max N /
  • int top / index of top element /

11
Create Stack
  • static struct intstack createStack (int
    maxElements)
  • struct intstack stack
  • stack (struct intstack ) malloc(sizeof(struct
    intstack))
  • if (stack NULL)
  • return NULL
  • / Head Allocated. Now initialize and allocate
    stack. /
  • stack-gttop -1
  • stack-gtcount 0
  • stack-gtstackMax maxElements
  • stack-gtstackAry (int ) malloc(maxElements
    sizeof(int))
  • if(stack-gtstackAry NULL) / No mem. - free
    all /
  • free(stack)
  • return NULL
  • return stack
  • / createStack /

12
Push
  • static int pushStack(struct intstack stack, int
    dataIn)
  • / full stack check /
  • if (stack-gtcount stack-gtstackMax)
  • return 0
  • (stack-gtcount)
  • (stack-gttop)
  • stack-gtstackArystack-gttop dataIn
  • return 1
  • / pushStack /

13
Pop
  • static int popStack (struct intstack stack, int
    dataOutPtr)
  • if (stack-gtcount 0) / empty stack /
  • return 0
  • else
  • dataOutPtr stack-gtstackArystack-gttop
  • (stack-gtcount)--
  • (stack-gttop)--
  • return 1
  • / else /
  • / popStack /

14
Top
  • / This function retrieves the data from the top
    of the
  • stack without changing the stack.
  • Pre stack is a pointer to the stack
  • Post Returns 1 if success 0 if underflow
  • dataPtr is a pointer to the data on the top
  • /
  • static int stackTop (struct intstack stack, int
    dataOutPtr)
  • if (stack-gtcount gt 0)
  • dataOutPtr stack-gtstackArystack-gttop
  • return 1
  • else
  • return 0
  • / stackTop /

15
Empty?
  • / This function determines if a stack is empty
  • Pre stack is a pointer to the stack
  • Post returns 1 if empty 0 if data are in the
    stack
  • /
  • static int emptyStack (struct intstack stack)
  • return (stack-gtcount 0)
  • / emptyStack /

16
Full?
  • / This function determines if a stack is full.
  • Full is defined as heap full
  • Pre stack is a pointer to a stack head node
  • Post returns 1 if heap is full 0 if heap has
    room
  • /
  • static int fullStack (struct intstack stack)
  • / Statements /
  • return (stack-gtcount stack-gtstackMax)
  • / fullStack /

17
Driver Example 17.1 (1)
  • include "StackIntAr.h"
  • include ltstdio.hgt
  • void main()
  • struct intstack mystack
  • int i
  • int value
  • mystack createStack(10)
  • while(1)
  • printf("Stack manipulation\n\n")
  • printf("1. Push\n")
  • printf("2. Pop\n")
  • printf("3. Top\n")
  • printf("4. End\n\n")
  • scanf("d",i)

18
Driver (2)
  • if(i 1)
  • printf("Value? ")
  • scanf("d",value)
  • if(pushStack(mystack, value))
  • printf("Value d pushed in\n", value)
  • else
  • printf("Stack is Full\n")
  • else if (i 2)
  • if(popStack(mystack, value))
  • printf("Value d popped\n", value)
  • else
  • printf("Stack is Empty\n")
  • else if (i 3)
  • if (stackTop(mystack, value))
  • printf("Value on the top is d\n", value)
  • else
  • printf("Stack is Empty\n")
  • else
  • return

19
Application Parenthesis matching
Write a Comment
User Comments (0)
About PowerShow.com