0% found this document useful (0 votes)
14 views38 pages

Unit No 04 Linked List

The document discusses static and dynamic memory allocation in programming, highlighting their characteristics, advantages, and disadvantages. It explains linked lists as a dynamic data structure that allows efficient insertion and deletion of elements, and introduces Generalized Linked Lists (GLL) for representing complex data structures. Key operations and functions for memory management in C/C++ are also covered, along with the benefits of using GLL for polynomial representation.

Uploaded by

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

Unit No 04 Linked List

The document discusses static and dynamic memory allocation in programming, highlighting their characteristics, advantages, and disadvantages. It explains linked lists as a dynamic data structure that allows efficient insertion and deletion of elements, and introduces Generalized Linked Lists (GLL) for representing complex data structures. Key operations and functions for memory management in C/C++ are also covered, along with the benefits of using GLL for polynomial representation.

Uploaded by

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

Unit no 4

Introduction to Static and Dynamic


Memory Allocation
• Memory allocation is a critical concept in computer programming that
determines how memory is reserved and managed for variables and
data structures during the execution of a program. In programming,
there are two primary types of memory allocation:
Static Memory Allocation
Dynamic Memory Allocation
1. Static Memory Allocation
• In static memory allocation, the memory for variables and data
structures is allocated at compile time (before the program runs) and
remains fixed throughout the program's execution.
Characteristics:
• Compile-time Allocation: The amount of memory required is
determined by the compiler at the time of compilation.
• Fixed Size: The size of memory allocated is constant and cannot be
changed during program execution.
• Lifetime: Variables with static memory allocation exist throughout the
entire lifetime of the program.
Advantages:
• Fast Access: Since the memory location is fixed, accessing variables is
fast and does not involve runtime allocation overhead.
• Simpler to Use: There's no need for explicit memory management like
allocation or deallocation.
Disadvantages:
• Memory Wastage: If you over-allocate memory (e.g., for an array), it
can lead to memory wastage, as unused memory remains reserved.
• Inflexibility: Once allocated, the memory size cannot be adjusted
during runtime, which can be inefficient when dealing with dynamic
data.
2. Dynamic Memory Allocation
• In dynamic memory allocation, the memory is allocated at runtime
(while the program is executing) based on the program's
requirements. The size of memory can be adjusted dynamically,
making it more flexible for handling data structures whose size
changes during program execution.
Characteristics:
• Runtime Allocation: Memory is allocated as needed during the
execution of the program.
• Flexible Size: Memory can be allocated and deallocated as needed,
allowing for resizing of data structures dynamically.
Advantages:
• Efficient Use of Memory: Memory is allocated only when required,
reducing memory wastage.
• Flexibility: Data structures like linked lists, trees, or resizable arrays
can be implemented since memory can grow or shrink dynamically.
Disadvantages:
• Memory Management Overhead: The programmer must explicitly
manage memory (allocate and deallocate), which increases the
complexity of the code.
• Potential Memory Leaks: If memory is allocated but not properly
deallocated, it can lead to memory leaks, where the program uses
more memory than necessary over time.
Common Functions for Dynamic
Memory Allocation (C/C++):
• malloc(): Allocates a block of memory and returns a pointer to it.
• calloc(): Allocates memory for an array of elements, initializes them to
zero, and returns a pointer.
• realloc(): Resizes previously allocated memory.
• free(): Deallocates previously allocated memory.
Example
int *arr;
arr = (int*) malloc(100 * sizeof(int)); // Dynamically allocate memory
for an array of 100 integers

// Use the allocated memory

free(arr); // Deallocate the memory to avoid memory leak


Introduction to Linked Lists
• A Linked List is a linear data structure in which elements are stored in
nodes, and each node contains a reference (or pointer) to the next
node in the sequence. Unlike arrays, linked lists do not store data in
contiguous memory locations. This structure allows for efficient
insertion and deletion of elements, as the list can dynamically grow
and shrink as needed.
Key Components of a Linked List:
1. Node: The fundamental building block of a linked list. Each node
contains
• Data: The value stored in the node (e.g., an integer, string, etc.).
• Pointer (next): A reference to the next node in the list.
2. Head: A Pointer to the first node in the linked list .if the list is empty ,
the head points to the null.
Basic Structure of a Node (C/C++):
Realization of Linked List Using
Dynamic Memory Management
• Since the size of a linked list can grow and shrink dynamically during
runtime, it is often implemented using dynamic memory allocation.
Dynamic memory management allows allocating memory for each
node when needed and releasing it when no longer required, thus
optimizing memory usage.
Key Operations for Dynamic Memory
Management:
• malloc() (in C) or new (in C++): Allocates memory for a new node
• free() (in C) or delete (in C++): Deallocates memory when a node is no
longer needed.
• Example of Node Creation Using Dynamic Memory:
Linked List Structure in C/C++:
Operations on Linked List
• A linked list supports various fundamental operations that allow
inserting, deleting, and accessing elements. Here are the common
operations:
1. Insertion:
• At the Beginning: Insert a new node at the start of the list.
• At the End: Insert a new node at the end of the list.
• In the Middle: Insert a new node at a specific position in the list.
Example (Insertion at the
Beginning):
2. Deletion:
• At the Beginning: Remove the first node in the list.
• At the End: Remove the last node in the list.
• In the Middle: Remove a node from a specific position
Example (Deletion at the
Beginning):
Linked List as an Abstract Data Type
(ADT)
• A Linked List can be seen as an Abstract Data Type (ADT) that
provides a set of operations to store and manage data. As an ADT, the
linked list defines a logical structure (how data is conceptually stored
and accessed) and hides its implementation details (how the
operations are performed).
Abstract Data Type (ADT) Definition:
• An ADT specifies what operations are supported on a data structure,
not how they are implemented. In the case of a linked list, the key
operations include:
Insert
Delete
Traverse
Search
reverse
Why Linked List as an ADT?
• Data Abstraction: A linked list abstracts the way data is stored and
only exposes operations for insertion, deletion, and access.
• Modular Design: The operations on a linked list are encapsulated,
meaning the internal details of the linked list (such as pointers) are
hidden from the user. Users interact with the linked list via its defined
operations.
• Flexibility: Linked lists provide flexibility in handling dynamic data
where the size may vary during runtime.
Advantages of Linked Lists
• Dynamic Size: Unlike arrays, linked lists can grow or shrink at runtime
as needed.
• Efficient Insertions/Deletions: Insertion and deletion operations are
faster than arrays, especially when modifying elements at the
beginning or middle of the list (no shifting of elements required).
• Memory Utilization: Linked lists only use memory for the actual
number of elements, avoiding the need to pre-allocate memory.
Disadvantages of Linked Lists
• Extra Memory Overhead: Each node requires extra memory for the
pointer(s), which can lead to higher memory usage compared to
arrays.
• Sequential Access: Unlike arrays, random access to elements in a
linked list is not possible, requiring traversal from the head to access
an element.
• Cache Inefficiency: Since nodes may not be stored in contiguous
memory locations, cache performance may be lower compared to
arrays.
Types of Linked Lists
• Singly Linked List: Each node points to the next node in the list, and
the last node points to null.
• Doubly Linked List: Each node contains two pointers, one to the next
node and one to the previous node, allowing traversal in both
directions.
• Circular Linked List: The last node points back to the first node,
forming a circle. It can be singly or doubly linked.
Generalized Linked List (GLL)
Concept
• A Generalized Linked List (GLL) is an extension of the traditional
linked list where elements of the list can either be atoms (single data
elements) or sub-lists (nested lists). This concept provides flexibility
for representing complex data structures where elements are
themselves lists or collections.
• n a GLL, each node can point to either a data element (like in a
traditional list) or another list (referred to as a sub-list).
Key Components of a
Generalized Linked List (GLL):
• Atom: A single data element (similar to traditional linked lists).
• Sub-list: A node that points to another linked list, representing a
nested list.
Node Structure in GLL:
• Indicator field: To differentiate between an atom and a sub-list.
• Data field (for atom): Holds the value if the node is an atom.
• Pointer to sub-list (if not an atom): Points to another GLL if the node
represents a sub-list.
• Next pointer: Points to the next node in the list
Generalized Linked List Example
Structure:
• Let’s consider the following GLL:
(a, (b, c), d, (e, f))
This list contains:
An atom a
A sub-list (b, c)
An atom d
Another sub-list (e, f)
Node Structure in C/C++:
Representation of Polynomial Using
Generalized Linked List (GLL)
Advantages of Using GLL for
Polynomial Representation:
• Dynamic Representation: Polynomials can grow or shrink dynamically
without predefined size.
• Nested Representation: Variables with multiple powers can be
handled elegantly using sub-lists.
• Efficient Operations: Operations like addition, subtraction, or
differentiation can be efficiently implemented using GLL traversal and
manipulation.

You might also like