260 likes | 356 Views
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()
E N D
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() • isEmpty() • Push(el) • Pop() • topEl()
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.
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.
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
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.
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
In stack , • no search, • no adding in arbitrary positions, • no sorting, • no access to anything beyond the top element.
Check for enough room, (no overflow) Data Push Top Top operation
Check if empty, (no underflow) Data Top Pop Top operation
Check if empty, (no underflow) Data Top Top Stack top operation
Head count Top Top Data nodes (a) Physical (a) Conceptual
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
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
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
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
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
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
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
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
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