stacks and queues
Post on 31-Dec-2015
Embed Size (px)
DESCRIPTIONStacks 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() - PowerPoint PPT Presentation
Stacks and Queues
Chapter 4Stacks and Queues14.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()2Stack 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.
3The delimiters matching algorithm in C++
6A 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.
8Basic 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 false9Push: Add new element to the top of the stackThe input consists of the stack and the new element.Prior to this operation, the stack must exist and must not be fullTop: 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.
Basic Operations on a Stack
1011Operations: 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 stack12In stack ,no search, no adding in arbitrary positions, no sorting, no access to anything beyond the top element.13PushTop Data Top Check for enough room, (no overflow) operation 14PopTop Data Top Check if empty, (no underflow) operation 15Stack topTop Data Top Check if empty, (no underflow) operation 16(a) Physical Top (a) Conceptual countTop HeadData nodes17Stack Linked List ImplementationStack head structurecounttop datanextStack node structureStackcounttopend stack
node datanextend node18Stack AlgorithmsCreate stackalgorithm createStackInitializes metadata for a stack.stack.head = nullstack.count = 0Return end createStack ?count?(a) Before Createtopstackcount0(b) After Createtopstack19Push stackalgorithm pushStack
Insert (push) data into a new node in the liked list.
Postdata have been pushed in stackReturn true if successful, false if memory overflow
If (stack full)successes = falseelseallocate (newptr)newptr->data = datanewptr->next = stack.topstack.top = newptrstack.count = stack.count + 1successes = trueend ifReturn successes end pushStack 20Pop stackalgorithm 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 = falseelsedptr= stack.topdataout = stack.top->datastack.top= stack.top->nextstack.count = stack.count 1Recycle (dptr)successes = trueend ifreturn successes end popStack 21Stack Top algorithm Stacktop This algorithm receives the data from the top of the stack without changing the stack. Postdata have been returned to calling algorithm Return true if data returned, false if underflow
If (stack empty)successes = falseelsedataout = stack.top->datasuccesses = trueend ifreturn successes end Stacktop 22Empty Stackalgorithm emptyStack Determines if stack is empty and returns a Boolean. Postreturns stack statusReturn Boolean, true: stack empty, false: stack contains data
If (stack not empty)result = falseelseresult = trueend ifreturn resultend emptyStack 23Full Stackalgorithm fullStackDetermines if stack is full and returns a Boolean. Postreturns stack statusReturn Boolean, true: stack full, false: stack is empty
If (memory available)result = falseelseresult = trueend ifreturn resultend fullStack 24Stack Countalgorithm StackcountReturns the number of elements currently in stack.
Postreturns stack countReturn integer count of number of elements in stack
end Stackcount 25Destroy Stackalgorithm destroyStackThis algorithm releases all nodes back to dynamic memory
Loop (stack.top not null)temp= stack.topStack.top= stack.top->nextrecycle (temp)end loopStack.count = 0return