Chapter 5 ADTs Stack and Queue. Stacks of Boxes and Books. TOP OF THE STACK. TOP OF THE STACK. 2. Definition of Stack. Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements),
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.
Definition of Stack • Logical (or ADT) level:A stack is an ordered group of homogeneous items (elements), • in which the removal and addition of stack items can take place only at the top of the stack. • A stack is a LIFO “last in, first out” structure. • One advantage of an ADT is that the implementation can be changed without the program using it knowing about it. 3
Stack ADT Operations • MakeEmpty -- Sets stack to an empty state. • IsEmpty -- Determines whether the stack is currently empty. • IsFull-- Determines whether the stack is currently full. • Push (ItemTypenewItem) -- Throws exception if stack is full; otherwise adds newItem to the top of the stack. • Pop -- Throws exception if stack is empty; otherwise removes the item at the top of the stack. • ItemType Top -- Throws exception if stack is empty; otherwise returns a copy of the top item 4
ADT Stack Operations Transformers • Push • Pop Observers • IsEmpty • IsFull • Top change state observe state 5
‘X’‘C’‘L’ topPtr Adding newItem to the stack newItem ‘B’ newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location; 43
‘X’‘C’‘L’ topPtr Adding newItem to the stack newItem ‘B’ newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location; location 44
‘X’‘C’‘L’ topPtr Adding newItem to the stack ‘B’ newItem newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location; location 45
‘X’‘C’‘L’ topPtr Adding newItem to the stack newItem ‘B’ newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location; ‘B’ location 46
‘X’‘C’‘L’ topPtr Adding newItem to the stack newItem ‘B’ newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location; ‘B’ location 47
topPtr Adding newItem to the stack newItem ‘B’ newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location; ‘X’‘C’‘L’ ‘B’ location 48
Implementing Push void StackType::Push ( ItemTypenewItem ) // Adds newItem to the top of the stack. { if (IsFull()) throw FullStack(); NodeType* location; location = new NodeType<ItemType>; location->info = newItem; location->next = topPtr; topPtr = location; } Do we need IsFull? 49
topPtr Deleting item from the stack item NodeType* tempPtr; topPtr = topPtr->next; delete tempPtr; ‘B’‘X’‘C’‘L’ tempPtr 50