1 / 26

Stacks and Queues

Stacks and Queues. Chapter 4. 4.1 Stacks . A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called LIFO. A stack is defined in terms of operations that change its status and operations that check this status.: Clear()

makara
Download Presentation

Stacks and Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stacks and Queues Chapter 4

  2. 4.1 Stacks • A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. • Its called LIFO. • A stack is defined in terms of operations that change its status and operations that check this status.: • Clear() • isEmpty() • Push(el) • Pop() • topEl()

  3. Stack is useful when data have to be stored and then retrieved in reverse order. • An application example of the stack is in matching delimiters in a program. • parentheses, square brackets, curly brackets, and comment delimiters. • Ex: a= b + (c-d) * (e-f) • The program could have nested delimitiers.

  4. The delimiters matching algorithm in C++

  5. A stack is a last in, first out (LIFO) abstract data type and data structure . A stack can have any abstract data type as an element ,but is characterized by only two fundamental operations: push and pop. • Elements are removed from the stack in the reverse order to the order of their addition :therefore, the lower elements are those that have been on the stack the longest.

  6. Basic Operations on a Stack • InitializeStack: • Initializes the stack to an empty state. • DestroyStack: • Removes all the elements from the stack, leaving the stack empty. • IsEmptyStack: • Checks whether the stack is empty. If empty, it returns true; otherwise, it returns false. • IsFullStack: • Checks whether the stack is full. If full, it returns true; otherwise, it returns false

  7. Basic Operations on a Stack • Push: • Add new element to the top of the stack • The input consists of the stack and the new element. • Prior to this operation, the stack must exist and must not be full • Top: • Returns the top element of the stack. • Prior to this operation, the stack must exist and must not be empty. • Pop: • Removes the top element of the stack. • Prior to this operation, the stack must exist and must not be empty.

  8. Operations: • initialize • destroy • build from given data (set of elements) • check if it is empty • get the total size of the stack • add an element to the top of the stack [PUSH] • delete an element from the top of the stack [POP] • get the data from the element at the top of the stack • update the data of the top element • print the data of the top element • print the entire stack

  9. In stack , • no search, • no adding in arbitrary positions, • no sorting, • no access to anything beyond the top element.

  10. Check for enough room, (no overflow) Data Push Top Top operation

  11. Check if empty, (no underflow) Data Top Pop Top operation

  12. Check if empty, (no underflow) Data Top Top Stack top operation

  13. Head count Top Top Data nodes (a) Physical (a) Conceptual

  14. Stack Linked List Implementation Stack count <integer> top <node pointer> end stack node data <datatype> next <node pointer> end node count top Stack head structure data next Stack node structure

  15. stack ? ? count top (a) Before Create stack 0 count top (b) After Create Stack Algorithms • Create stack algorithm createStack Initializes metadata for a stack. stack.head= null • stack.count = 0 • Return end createStack

  16. algorithm pushStack Insert (push) data into a new node in the liked list. Postdata have been pushed in stack Return true if successful, false if memory overflow • If (stack full) • successes = false • else • allocate (newptr) • newptr->data = data • newptr->next = stack.top • stack.top = newptr • stack.count = stack.count + 1 • successes = true • end if • Return successes • end pushStack • Push stack

  17. algorithm popStack This algorithm pops the item on the top of the stack and returns it to the user Postdata have been returned to calling algorithm Return true if successful, false if underflow • If (stack empty) • successes = false • else • dptr = stack.top • dataout = stack.top->data • stack.top = stack.top->next • stack.count = stack.count – 1 • Recycle (dptr) • successes = true • end if • return successes • end popStack • Pop stack

  18. Stack Top algorithm Stacktop This algorithm receives the data from the top of the stack without changing the stack. Post data have been returned to calling algorithm Return true if data returned, false if underflow • If (stack empty) • successes = false • else • dataout = stack.top->data • successes = true • end if • return successes • end Stacktop

  19. Empty Stack algorithm emptyStack Determines if stack is empty and returns a Boolean. Post returns stack status Return Boolean, true: stack empty, false: stack contains data • If (stack not empty) • result = false • else • result = true • end if • return result • end emptyStack

  20. Full Stack algorithm fullStack Determines if stack is full and returns a Boolean. Post returns stack status Return Boolean, true: stack full, false: stack is empty • If (memory available) • result = false • else • result = true • end if • return result • end fullStack

  21. Stack Count algorithm Stackcount Returns the number of elements currently in stack. Post returns stack count Return integer count of number of elements in stack • return (stack.count) • end Stackcount

  22. Destroy Stack algorithm destroyStack This algorithm releases all nodes back to dynamic memory • Loop (stack.top not null) • temp = stack.top • Stack.top = stack.top->next • recycle (temp) • end loop • Stack.count = 0 • return • end destroyStack

More Related