Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stack and Queues. A Different Kind of Structure Some of the data storage structure such as Arrays, linked lists, trees, and so on are appropriate for.

Similar presentations


Presentation on theme: "Stack and Queues. A Different Kind of Structure Some of the data storage structure such as Arrays, linked lists, trees, and so on are appropriate for."— Presentation transcript:

1 Stack and Queues

2 A Different Kind of Structure Some of the data storage structure such as Arrays, linked lists, trees, and so on are appropriate for the kind of data you might find in a database application. They’re typically used for personnel records, inventories, financial data, and so on—data that corresponds to real-world objects or activities. These structures facilitate access to data: They make it easy to insert, delete, and search for particular items Stacks and queues are primarily conceptual aids rather than full-fledged data storage devices. Their lifetime is typically shorter than that of the database-type structures. They are created and used to carry out a particular task during the operation of a program; when the task is completed, they’re discarded.

3 A Different Kind of Structure(cont’) Restricted Access In an array, any item can be accessed, either immediately—if its index number is known—or by searching through a sequence of cells until it’s found. In the data structures of stacks and queues, however, access is restricted: Only one item can be read or removed at a given time (unless you cheat). The interface of these structures is designed to enforce this restricted access. Access to other items is (in theory) not allowed. More Abstract Stacks, queues, and priority queues are more abstract entities than arrays and many other data storage structures. They’re defined primarily by their interface: the permissible operations that can be carried out on them. The underlying mechanism used to implement them is typically not visible to their user. The underlying mechanism for a stack, for example, can be an array, or it can be a linked list.

4 Stacks A stack allows access to only one data item: the last item inserted. If you remove this item, you can access the next-to-last item inserted, and so on. Placing a data item on the top of the stack is called pushing it. Removing it from the top of the stack is called popping it. These are the primary stack operations. A stack is said to be a Last-In-First- Out (LIFO) storage mechanism because the last item inserted is the first one to be removed. Stack Size Stacks are typically small, temporary data structures.

5 The Postal Analogy Process the accumulated mail from the top down This “do the top one first” approach works all right as long as you can easily process all the mail in a reasonable time. If you can’t, there’s the danger that letters on the bottom of the stack won’t be examined for months, and the bills they contain will become overdue. Of course, many people don’t rigorously follow this top-to-bottom approach. They may, for example, take the mail off the bottom of the stack, so as to process the oldest letter first. Or they might shuffle through the mail before they begin processing it and put higher-priority letters on top. In these cases, their mail system is no longer a stack in the computer-science sense of the word. If they take letters off the bottom, it’s a queue; and if they prioritize it, it’s a priority queue.

6

7

8

9

10

11 Queues The word queue is British for line (the kind you wait in). In Britain, to “queue up” means to get in line. In computer science a queue is a data structure that is somewhat like a stack, except that in a queue the first item inserted is the first to be removed (First-In-First-Out, FIFO), while in a stack, as we’ve seen, the last item inserted is the first to be removed (LIFO). A queue works like the line at the movies: The first person to join the rear of the line is the first person to reach the front of the line and buy a ticket. The last person to line up is the last person to buy a ticket(or—if the show is sold out—to fail to buy a ticket).

12

13

14

15

16 Efficiency of Queues As with a stack, items can be inserted and removed from a queue in 1 step. Deques A deque is a double-ended queue. You can insert items at either end and delete them from either end. The methods might be called insertLeft() and insertRight(), and removeLeft() and removeRight(). If you restrict yourself to insertLeft() and removeLeft() (or their equivalents on the right), the deque acts like a stack. If you restrict yourself to insertLeft() and removeRight() (or the opposite pair), it acts like a queue. A deque provides a more versatile data structure than either a stack or a queue and is sometimes used in container class libraries to serve both purposes. However, it’s not used as often as stacks and queues.


Download ppt "Stack and Queues. A Different Kind of Structure Some of the data storage structure such as Arrays, linked lists, trees, and so on are appropriate for."

Similar presentations


Ads by Google