Data Structure and Algorithms
Data Structure and Algorithms
Data Structure and Algorithms
ALGORITHMS
DATA STRUCTURE AND ALGORITHMS
• Lecture Division
Week No.4
• Department
BS CYB/3rd Semester
• Instructor
Engineer Ashna Batool
DEFINITION OF LINKED LIST
Dynamic Size: Unlike arrays, linked lists can grow and shrink in size
dynamically as elements are added or removed.
Non-contiguous Memory: Nodes may not be stored in contiguous memory
locations, allowing for more flexible memory usage.
Efficient Insertions/Deletions: Inserting or deleting nodes can be done
efficiently (in constant time) if the position is known, as it only involves changing
pointers.
TYPES
TYPES OF LINKED LISTS
Singly Linked List: Each node points to the next node, with no reference to the
previous node.
Doubly Linked List: Each node contains references to both the next and the
previous nodes, allowing traversal in both directions.
Circular Linked List: The last node points back to the first node, forming a
circle. This can be singly or doubly linked
SINGLY LINKED LIST
• A singly linked list is a simple data structure where each item (node)
contains data and a reference (or link) to the next item in the list. This
makes it easy to add or remove items without needing to shift the entire
structure, as you might with an array.
DOUBLY LINKED LIST
• A doubly linked list is a type of linked list where each node contains a
reference to both the next node and the previous node, allowing for
bidirectional traversal. This structure makes certain operations easier,
such as inserting and deleting nodes.
CIRCULAR LINKED LIST
• A circular linked list is a variation of the linked list in which the last
node points back to the first node, creating a circular structure. This
allows for continuous traversal of the list without the need to check for
null pointers, as you can keep moving from one node to the next
indefinitely.
OPERATIONS
Linked lists support various operations, each with its own time
complexity. Here are some common operations:
Insertion:
• At the beginning: O(1)
• At the end: O(n) for singly linked lists (O(1)) for doubly linked lists if
you maintain a tail pointer)
• At a specific position: O(n)
OPERATIONS
Deletion:
• From the beginning: O(1)
• From the end: O(n) for singly linked lists (O(1) for doubly linked lists
if you maintain a tail pointer)
• From a specific position: O(n)
OPERATIONS
• Pros:
Dynamic size.
Efficient insertions/deletions.
• Cons:
More memory usage per element due to pointers.
Random access is not possible; traversal is required to find elements.
NEXT WEEK TOPICS
Stacks
• • Basics of Stack data structure
• • Algorithms (Push, Pop, Peek)
• • Implementation using Arrays
• • Implementation using Linked Lists
• • Expression parsing using Stacks (infix, prefix, postfix)