0% found this document useful (0 votes)
26 views33 pages

Meeting 2

The document discusses different data structures including lists, stacks, queues, and linked lists. It describes the operations, implementations, and algorithms for each data structure. The document provides details on how each data structure organizes and manages data.

Uploaded by

yousef yahya
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)
26 views33 pages

Meeting 2

The document discusses different data structures including lists, stacks, queues, and linked lists. It describes the operations, implementations, and algorithms for each data structure. The document provides details on how each data structure organizes and manages data.

Uploaded by

yousef yahya
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/ 33

Basic Data Structures

MEETING 2 – QUICK OVERVIEW


Introduction
• The Internet is designed to route information in discrete packets,
which are at most 1500 bytes in length.
• For instance, a video stream is transmitted on the Internet, it must be
subdivided into packets and these packets must each be individually
routed to their destination.
The Need for Data Structures

• Data structures organize data


 more efficient programs.
• More powerful computers  more complex applications.
• More complex applications demand more calculations.
• Complex computing tasks are unlike our everyday experience.
• Any organization for a collection of records can be searched, processed in
any order, or modified.
• The choice of data structure and algorithm can make the difference
between a program running in a few seconds or many days.

3
Selecting a Data Structure
Select a data structure as follows:
1. Analyze the problem to determine the resource constraints a
solution must meet.
2. Determine the basic operations that must be supported. Quantify
the resource constraints for each operation.
3. Select the data structure that best meets these requirements.

4
Abstract Data Types
Abstract Data Type (ADT): a definition for a data type solely in terms of a
set of values and a set of operations on that data type.

Each ADT operation is defined by its inputs and outputs.

Encapsulation: Hide implementation details.

5
Data Structure
• A data structure is the physical implementation of an ADT.
• Each operation associated with the ADT is implemented by one or more subroutines in
the implementation.

• Data structure usually refers to an organization for data in main memory.

• File structure is an organization for data on peripheral storage, such as a disk


drive.
• Data structure is representation of the logical relationship existing between
individual elements of data.
• In other words, a data structure is a way of organizing all data items that
considers not only the elements stored but also their relationship to each
other. 6
Logical vs. Physical Form
Data items have both a logical and a physical form.

Logical form: definition of the data item within an ADT.


• Ex: Integers in mathematical sense: +, -

Physical form: implementation of the data item within a data structure.


• Ex: 16/32 bit integers, overflow.

7
What is Program
• Data structure affects the design of both structural & functional aspects of a program.

Program=algorithm + Data Structure

• You know that a algorithm is a step by step procedure to solve a particular function.

• A Set of Instructions

• Data Structures + Algorithms

• Data Structure = A Container stores Data

• Algorithm = Logic + Control

• That means, algorithm is a set of instruction written to carry out certain tasks & the data structure is the way of
organizing the data with their logical relationship retained.

• To develop a program of an algorithm, we should select an appropriate data structure for that algorithm.

• Therefore algorithm and its associated data structures from a program.


Functions of Data Structures
• The most commonly used operation on data structure are broadly
categorized into following types:
• Create.
• Insert
• Delete Some DS need extra functions
• update • Selection.
• Searching. Insertion needs sometime

• Sorting. Index.
• Key.
• Merging.
• Position.
• Priority.
Common Data Structures
• List
• Set
• Tuple
• Dictionary
• Stack
• Queue this lecture
• Linked List
• Tree
• Heap
through next lectures
• Hash Table
• Priority Queue
Stacks
• A stack is a container of objects that are inserted and removed
according to the last-in first-out (LIFO) principle.
• The name “stack” is derived from the metaphor of a stack of plates in
a spring loaded cafeteria plate dispenser.
• In this case, the fundamental operations involve the “pushing” and
“popping” of plates on the stack.
Pop Pop Pop

Address 4
Address 3 Address 3
Browser Push Address 2 Address 2 Address 2
Address 1 Address 1 Address 1
Stack - Operations
• a stack, S, is a container that supports the following two methods:

• push(o): Insert object o at the top of the stack.


• pop(): Remove from the stack and return the top object on the
stack, that is, the most recently inserted element still in the stack; an
error occurs if the stack is empty.
Stack Implementation
• A Simple Array-Based Implementation
• A stack is easily implemented with an N-element array S, with
elements stored from S[0] to S[t], where t is an integer that gives
the index of the top element in S.
• We must specify some maximum size N for our stack.
Performance and Limitations
• Performance
• Let n be the number of elements in the stack
• The space used is O(n)
• Each operation runs in time O(1) (amortized in the
case of a push)
Stack – Algorithm push(o)
if t + 1 = N then
return that a stack-full error has occurred
t←t+1
S[t] ← o
return
O(1)
Stack – Algorithm pop()
if t < 0 then
return that a stack-empty error has occurred
e ← S[t]
S[t] ← null
t←t−1
O(1)
return e
Queues
• A queue is a container of objects that are inserted and removed
according to the first-in first-out (FIFO) principle.
• elements enter the queue at the rear and are removed from the front.
Queues – Operations
• A queue supports the following two fundamental methods:
• enqueue(o): Insert object o at the rear of the queue.
• dequeue(): Remove and return from the queue the object at the
front; an error occurs if the queue is empty.
Queue – Implementation
• A Simple Array-Based Implementation
• We use an array, Q, with capacity N, for storing its elements.
• Since the main rule for a queue is that we insert and delete objects
according to the FIFO principle.
• We must decide how we are going to keep track of the front and rear of
the queue.
• Two variables are defined f and r, which meaning:
• f is an index to the cell of Q storing the first element of the, unless the queue
is empty (in which case f = r).
• r is an index to the next available array cell in Q.
Queue – Algorithm dequeue()
if f = r then
return an error condition that the queue is empty
temp ← Q[f]
Q[f] ← null
f ← (f + 1) mod N
return temp
Queue – Algorithm enqueue(o)

if (N − f + r) mod N = N − 1 then
return an error condition that the queue is full
Q[r] ← o
r ← (r + 1) mod N
return
Lists
• Stacks and queues store elements according to a linear sequence
determined by update operations that act on the “ends” of the
sequence.
• Lists maintain linear orders while allowing for accesses and updates
in the “middle.”
Lists – Index-Based Lists
• Suppose we are given a linear sequence, S, that contains n elements.
• We can uniquely refer to each element e of S using an integer in the
range [0, n−1].
• We define the index (or rank) of an element e in S to be the number of
elements that are before e in S.
• So, the first element in a sequence is at index 0 and the last element
is at index n − 1.
Index-Based Lists – Operations
• get(r): Return the element of S with index r; an error condition occurs
if r < 0 or r > n− 1.
• set(r, e): Replace with e the element at index r and return it; an error
condition occurs if r < 0 or r > n− 1.
• add(r, e): Insert a new element e into S to have index r; an error
condition occurs if r < 0 or r > n.
• remove(r): Remove from S the element at index r; an error condition
occurs if r < 0 or r > n− 1.
Index-Based Lists – Algorithm add(r, e)
if n = N then
return “Array is full.”
if r < n then
for i ← n − 1, n − 2, . . . , r do
A[i + 1] ← A[i] // make room for the new element
A[r] ← e
n←n+1
Index-Based Lists – Algorithm remove(r)
e ← A[r] // e is a temporary variable
if r < n− 1 then
for i ← r, r + 1, . . . , n − 2 do
A[i] ← A[i + 1] // fill in for the removed element
n←n−1
return e
Lists – Linked Lists
• A linked list as a container of elements that stores each element at a
node position and that keeps these positions arranged in a linear
order relative to one another.
Linked Lists – Operations
• first(): Return the position of the first element of S; an error occurs if S
is empty.
• last(): Return the position of the last element of S; an error occurs if S
is empty.
• before(p): Return the position of the element of S preceding the one
at position p; an error occurs if p is the first position.
• after(p): Return the position of the element of S following the one at
position p; an error occurs if p is the last position.
Linked Lists – Operations
• insertBefore(p, e): Insert a new element e into S before position p in S.
• insertAfter(p, e): Insert a new element e into S after position p in S.
• remove(p): Remove from S the element at position p.
Linked Lists – Algorithm insertAfter(p, e)
Inserting an element e after a position p in a linked list
Create a new node v
v.element ← e
v.prev ← p // link v to its predecessor
v.next ← p.next // link v to its successor
(p.next).prev ← v // link p’s old successor to v
p.next ← v // link p to its new successor, v
return v // the position for the element e
Linked Lists – Algorithm insertBefore(p, e)
Linked Lists – Algorithm remove(p)
Removing an element e stored at a position p in a linked list
t ← p.element // a temporary variable to hold the return value
(p.prev).next ← p.next // linking out p
(p.next).prev ← p.prev
p.prev ← null // invalidating the position p
p.next ← null
return t
•To Be Continue

You might also like