Lecture 6 - Elementary Data Structures
Lecture 6 - Elementary Data Structures
• This operation returns true if the stack is empty and false if the
stack is not empty.
Applications of Stacks
• Direct applications
• Page-visited history in a Web browser
• Undo sequence in a text editor
• Chain of method calls in the Java Virtual Machine or C++ runtime
environment
• Stack applications can be classified into four broad categories:
• reversing data,
• pairing data,
• postponing data usage
• backtracking steps. 20
Reversing data items
• We can use a stack to reverse the letters in a word…How?
• Read each letter in the word and push it onto the stack
• When you reach the end of the word, pop the letters off the stack
and print them out.
• Reversing data items requires that a given set of data items be
reordered so that the first and last items are exchanged, with all of
the positions between the first and last also being relatively
exchanged.
• For example, the list (2, 4, 7, 1, 6, 8) becomes (8, 6, 1, 7, 4, 2).
Pairing data items
30
The Enqueue operation
31
The Dequeue operation
• The Dequeue operation deletes the item at the front of the queue.
32
The empty operation
• This operation returns true if the queue is empty and false if the
queue is not empty.
33
Applications of Queues
34
Queue applications cont’d
• Queues are one of the most common of all data processing
structures.
• They are found in virtually every operating system and network
and in countless other areas.
• For example, queues are used ;
• In online business applications such as processing customer requests, jobs and
orders.
• In a computer system, a queue is needed to process jobs and for system
services such as print spools.
Implementing a Queue
37
Implementing Stacks and Queues