0% found this document useful (0 votes)
3 views

Lecture 6 - Elementary Data Structures

The document provides an overview of elementary data structures, focusing on stacks and queues. It defines data structures, explains the characteristics and operations of lists, stacks, and queues, and discusses their implementations using arrays and linked lists. Additionally, it introduces priority queues and highlights their applications and differences from regular queues.

Uploaded by

markkifunye159
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 6 - Elementary Data Structures

The document provides an overview of elementary data structures, focusing on stacks and queues. It defines data structures, explains the characteristics and operations of lists, stacks, and queues, and discusses their implementations using arrays and linked lists. Additionally, it introduces priority queues and highlights their applications and differences from regular queues.

Uploaded by

markkifunye159
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Elementary Data Structures

Stacks & Queues


What is a Data Structure?
• A data structure is a collection of data organized in a specific
fashion.
• A data structure not only stores data, but also supports the
operations for manipulating data in the structure.
• For example, an array is a data structure that holds a collection of
data in sequential order.
• You can find the size of the array, store, retrieve, and modify data in
the array.
• Four Classic Data Structures
• A list is a collection of data stored sequentially.
• It supports insertion and deletion anywhere in the list.
• A stack can be perceived as a special type of the list where insertions and deletions
take place only at the one end, (top of a stack).
• A queue represents a waiting list, where insertions take place at the back (tail) of a
queue and deletions take place from the front (head) of a queue.
• A binary tree is a data structure that supports searching, sorting, inserting, and
deleting data efficiently.
Lists
• A list is a popular data structure to store data in sequential order.
• For example, a list of students, a list of available rooms, a list of cities, and
a list of books, etc. can be stored using lists.
• The common operations on a list are usually the following:
• Retrieve an element from this list.
• Insert a new element to this list.
• Delete an element from this list.
• Find how many elements are in this list.
• Find if an element is in this list.
• Find if this list is empty.
Ways to Implement Lists
• There are two ways to implement a list.
1. To use an array to store the elements.
• The array is dynamically created.
• If the capacity of the array is exceeded, create a new larger array and copy all
the elements from the current array to the new array.
2. The other approach is to use a linked structure.
• A linked structure consists of nodes.
• Each node is dynamically created to hold an element.
• All the nodes are linked together to form a list.
Array Lists

• An array is a fixed-size data structure and once created, its size


cannot be changed.
• Nevertheless, you can still use array to implement dynamic data
structures by creating a new larger array to replace the current
array if the current array cannot hold new elements in the list.
• When inserting a new element into the array, first ensure there is
enough room in the array.
Array Lists
• If not, create a new array with the size twice as much as the
current one, copy the elements from the current array to the new
array.
• The new array now becomes the current array.
Limitations of arrays and Why linked lists?

• Array is simple and easy to use, but it has two limitations:


• Once an array is created, its size cannot be altered (Fixed
size)
• Array provides inadequate support for inserting, deleting,
sorting, and searching operations.
• Linked lists can solve some of these problems
• Linked lists are general purpose storage data structures and
are flexible.
Linked Lists

• A linked list is a collection of data in which each element


contains the location of the next element.
• That is, each element contains two parts:
• Data and link.
• We define an empty linked list to be only a null pointer:
Linked Lists - Types
• A circular linked list, is a list, whose pointer of the last
node points back to the first node.
• A doubly linked list contains the nodes with two pointers.
• One points to the next node and the other points to the
previous node.
• These two pointers are conveniently called a forward pointer
and a backward pointer.
• So, a doubly linked list can be traversed forward and backward.
Linked Lists - Types

• A circular, doubly linked list is a doubly linked list, except


that the forward pointer of the last node points to the
first node and the backward pointer of the first pointer
points to the last node.
Arrays versus linked lists
• Both an array and a
linked list are
representations of a
list of items in
memory.
• The only difference is
the way in which the
items are linked
together.
Stacks
• What is a Stack?
• A stack is a data structure of ordered items such that items can
be inserted and removed only at one end.
• A stack is a restricted linear list in which all additions and
deletions are made at one end, the top.
• If we insert a series of data items into a stack and then remove
them, the order of the data is reversed.
• This reversing attribute is why stacks are known as last in, first out
(LIFO) data structures.
Stacks cont’d
• A stack can be viewed as a special type of list, where the elements
are accessed, inserted, and deleted only from the end, called the top, of
the stack
Stacks cont’d
• Examples of Stacks:
• A heap of Trays, plates, books etc.
• What can we do with a stack?
• There are four basic operations, stack, push, pop and
empty.
• A stack is a LIFO (Last-In/First-Out) data structure
• A stack is sometimes also called a pushdown store.
• What are some applications of stacks?
The stack operation

• The stack operation creates an empty stack.


The push operation
• The push operation inserts an item at the top of the stack.
The pop operation
• The pop operation deletes the item at the top of the stack.
The empty operation

• The empty operation checks the status of the stack.

• 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

• We often need to pair some characters in an expression.


• For example, when we write a mathematical expression in a computer
language, we often need to use parentheses to change the precedence
of operators.
• The following two expressions are evaluated differently
because of the parentheses in the second expression:
Pairing data items

• When we type an expression with a lot of parentheses, we often


forget to pair the parentheses.
• One of the duties of a compiler is to do the checking for us.
• The compiler uses a stack to check that all opening parentheses
are paired with a closing parentheses.
Implementing a Stack

• There are two ways we can implement a stack:


• Using an array
• Using a linked list
• Implementing a stack using an array is fairly easy.
• The bottom of the stack is at data[0]
• The top of the stack is at data[numItems-1]
• push onto the stack at data[numItems]
• pop off of the stack at data[numItems-1]
Implementing a Stack
• Implementing a stack using a linked list isn’t that bad
either…
• Store the items in the stack in a linked list
• The top of the stack is the head node, the bottom of the stack
is the end of the list
• push by adding to the front of the list
• pop by removing from the front of the list
Queues
• A queue is a linear list in which data can only be inserted at one end,
called the rear, and deleted from the other end, called the front.
• A data structure of ordered items such that items can be inserted only
at one end and removed at the other end.
• A queue represents a waiting list.
• A queue can be viewed as a special type of list, where the elements are
inserted into the end (tail) of the queue, and are accessed and deleted
from the beginning (head) of the queue.
Queues
• What can we do with a queue?
• Enqueue - Add an item to the queue
• Dequeue - Remove an item from the queue
• These restrictions ensure that the data is processed through the
queue in the order in which it is received.
• In other words, a queue is a first in, first out (FIFO) data structure.
• What are some applications of queues?
• Round-robin scheduling in processors
• Input / Output processing
• Queuing of packets for delivery in networks
Queue examples

• A line at the supermarket, banks, print jobs, etc.


Operations on queues

• Although we can define many operations for a queue,


four are basic:
• Queue,
• Enqueue,
• Dequeue and
• Empty,
29
The queue operation

• The queue operation creates an empty queue.

30
The Enqueue operation

• The Enqueue operation inserts an item at the rear of the queue.

31
The Dequeue operation
• The Dequeue operation deletes the item at the front of the queue.

32
The empty operation

• The empty operation checks the status of the queue.

• This operation returns true if the queue is empty and false if the
queue is not empty.
33
Applications of Queues

• They can be classified either as;


• Direct applications
• Waiting lines
• Access to shared resources (e.g., printer)
• Multiprogramming

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

• Just like a stack, we can implement a queue in two ways:


• Using an array
• Using a linked list
• Using an array to implement a queue is significantly harder than
using an array to implement a stack.
• Why?
• Unlike a stack, where we add and remove at the same end, in a queue we
add to one end and remove from the other.
Implementing a Queue

• Implementing a queue using a linked list is still easy:


• Front of the queue is stored as the head node of the linked list,
rear of the queue is stored as the tail node.
• Enqueue by adding to the end of the list
• Dequeue by removing from the front of the list.

37
Implementing Stacks and Queues

• Use an array list to implement Stack


• Since the insertion and deletion operations on a stack are made only at
the end of the stack, using an array list to implement a stack is more
efficient than a linked list.

• Use a linked list to implement Queue


• Since deletions are made at the beginning of the list, it is more efficient
to implement a queue using a linked list than an array list. 38
Priority Queue
• A regular queue is a first-in and first-out data structure.
• Elements are appended to the end of the queue and are removed from
the beginning of the queue.
• In a priority queue, elements are assigned with priorities.
• When accessing elements, the element with the highest priority is
removed first.
• A priority queue has a largest-in, first-out behavior.
• For example, the emergency room in a hospital assigns patients with priority
numbers; the patient with the highest priority is treated first.
Priority Queues
• Sometimes we may want certain things to get to go to the front
of the line.
• Examples:
• Frequent fliers on airlines
• Real-time video and audio packets in networks
• In a priority queue, each item stored in the queue has a priority
associated with it.
• When we call enqueue, we pass the item to be enqueued and the
priority associated with that item.
Implementing a PQ
• There are several ways in which we might implement a priority
queue:
• Use an array of ordinary queues, one for each priority.
• Queues[0] is the queue for priority 0, queues[1] is the queue for
priority 1
• Use a sorted linked list
• The list should be sorted according the priorities of the items
contained
• Which approach is better???
• Brain tester: Where might a computer’s operating system use queues?
Review

• A stack is a LIFO data structure


• A queue is a FIFO data structure
• Both queues and stacks can be implemented using either linked
lists or arrays
• A priority queue is a queue in which the ordering of the items is
determined by the priorities assigned to them.
43

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy