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

Lec 3

Here are the key steps to insert a new node at the head of a singly linked list: 1. Allocate a new node and store the element to be inserted in it. 2. Make the next pointer of the new node point to the current head node. 3. Update the head pointer to point to the new node. 4. Return or update the head pointer variable. This allows constant time insertion at the head of the list while maintaining the linked list structure.

Uploaded by

Mado Saeed
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)
36 views

Lec 3

Here are the key steps to insert a new node at the head of a singly linked list: 1. Allocate a new node and store the element to be inserted in it. 2. Make the next pointer of the new node point to the current head node. 3. Update the head pointer to point to the new node. 4. Return or update the head pointer variable. This allows constant time insertion at the head of the list while maintaining the linked list structure.

Uploaded by

Mado Saeed
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

M269

Algorithms, Data Structures


and Computability
Agenda
• Introduction to Data Structures.
• ADT vs. DS.
• Python Built-in Data Structures (Lists, Tuples, Sets, Dict).
• Famous Data Structures:
❑Stack.
❑Queue.
❑Linked Lists && Doubly Linked Lists.
Data Types & Data Structures

• Applications/programs read data, store data temporarily, process it and


finally output results.
• What is data? Numbers, Characters, etc.

Application/ Output
Input
Program data
data
3
Introduction to Data Structures
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.

5
Efficiency

A solution is said to be efficient if it solves the problem within its resource


constraints.
• Space
• Time
• The cost of a solution is the amount of resources that the solution
consumes… expressed in terms of big O notation

6
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.

7
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.

8
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. 9
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.

10
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 Some DS need extra functions
• Delete
• Selection.
• update
• Searching. Insertion needs sometime
• Index.
• Sorting.
• Key.
• Merging.
• Position.
• Priority.
Common Data Structures
Video tutorial: (in Python)

• List https://www.youtube.com/watch?v=R-HLU9Fl5ug
Interactive tutorial: (in Python)
• Set https://www.programiz.com/python-programming/list
• Tuple https://www.programiz.com/python-programming/set
https://www.programiz.com/python-programming/tuple
• Dictionary https://www.programiz.com/python-programming/dictionary
• Stack
• Queue this lecture

• Linked List
• Tree
• Heap through next lectures
• Hash Table
• Priority Queue
Stack Data Structure

Visualize the Stack:


https://visualgo.net/en/list
The Stack ADT

• The Stack ADT stores arbitrary • Auxiliary stack operations:


objects • object top(): returns the last inserted
• Insertions and deletions follow element without removing it
the last-in first-out scheme • integer len(): returns the number of
• Think of a spring-loaded plate elements stored
dispenser • boolean is_empty(): indicates
• Main stack operations: whether no elements are stored
• push(object): inserts an element
• object pop(): removes and returns
the
15 last inserted element
Example
Applications of Stacks

• Direct applications
• Page-visited history in a Web browser
• Undo sequence in a text editor
• Chain of method calls in a language that supports recursion
• Indirect applications
• Auxiliary data structure for algorithms
• Component of other data structures
List-based Stack

• A simple way of implementing the Stack ADT uses a list


• We add elements from left to right
• A variable keeps track of the index of the top element


S
0 1 2 t
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)
List-based Stack in Python
Queue Data Structure

Visualize the Queue:


https://visualgo.net/en/list
The Queue ADT
• The Queue ADT stores arbitrary
objects
• Insertions and deletions follow the • Auxiliary queue operations:
first-in first-out scheme
• object first(): returns the
• Insertions are at the rear of the element at the front without
queue and removals are at the front removing it
of the queue
• integer len(): returns the
• Main queue operations: number of elements stored
• enqueue(object): inserts an element • boolean is_empty(): indicates
at the end of the queue
whether no elements are stored
• object dequeue(): removes and
returns the element at the front of
the queue
Example

23
Applications of Queues
• Direct applications
• Waiting lists, bureaucracy
• Access to shared resources (e.g., printer)
• Multiprogramming
• Indirect applications
• Auxiliary data structure for algorithms
• Component of other data structures
Queue in Python

• Use the following three instance variables:


• _data: is a reference to a list instance with a fixed capacity.
• _size: is an integer representing the current number of elements stored in the
queue (as opposed to the length of the data list).
• _front: is an integer that represents the index within data of the first element
of the queue (assuming the queue is not empty).
Queue in Python, Beginning
Queue in Python, Continued
Linked List Data Structure

Visualize the Queue:


https://visualgo.net/en/list
Singly Linked List

A singly linked list is a concrete next


data structure consisting of a
sequence of nodes, starting
from a head pointer
Each node stores node
elem
◼ element
head
◼ link to the next node

A B C D
Inserting at the Head

1. Allocate a new node


2. Insert new element
3. Have new node point to old head
4. Update head to point to new node
Removing at the Head

1. Update head to point to


next node in the list
2. Allow garbage collector
to reclaim the former
first node
Inserting at the Tail

1. Allocate a new node


2. Insert new element
3. Have new node
point to null
4. Have old last node
point to new node
5. Update tail to point
to new node
Removing at the Tail

Removing at the tail of


a singly linked list is not
efficient!
There is no constant-
time way to update the
tail to point to the
previous node
Stack as a Linked List
We can implement a stack with a singly linked list
The top element is stored at the first node of the list
The space used is O(n) and each operation of the Stack ADT takes
O(1) time
nodes
t

Linked Lists elements


Linked-List Stack in Python
Queue as a Linked List
We can implement a queue with a singly linked list
◼The front element is stored at the first node
◼The rear element is stored at the last node
The space used is O(n) and each operation of the Queue ADT takes O(1)
time r
nodes
f

Linked Lists
elements
Linked-List Queue in Python

Linked Lists 37
Doubly Linked List Data Struture
Visualize the Doubly linked List:
Doubly Linked List https://visualgo.net/en/list

• A doubly linked list provides a natural prev next


implementation of the Node List ADT
• Nodes implement Position and store:
• element
• link to the previous node
elem node
• link to the next node
• Special trailer and header nodes

header nodes/positions trailer

Doubly-Linked Lists
elements
Insertion
• Insert a new node, q, between p andpits successor.

A B C

A B q C

X
p q

A B X C
Deletion
• Remove a node, p, from a doubly-linked list.
p

A B C D

A B C p

A B C
Doubly-Linked List in Python
Thank You

You might also like