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

Unit 4

Uploaded by

shrutinimbekar05
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Unit 4

Uploaded by

shrutinimbekar05
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

Unit 4 Linked List

1. Introduction to Static and Dynamic Memory Allocation


Static Memory Allocation:
o Memory is allocated at compile-time, and its size is fixed.
o Once allocated, static memory cannot be resized or freed until the
program terminates.
o Advantages: Fast access and simple allocation.
o Disadvantages: Wastes memory if allocated size is more than needed;
lacks flexibility.
o Commonly used for array structures in languages like C.

Dynamic Memory Allocation:


o Memory is allocated at runtime, allowing for flexible memory usage
based on program needs.
o Functions like malloc, calloc, realloc, and free in C allow for dynamic
allocation and deallocation.
o Advantages: Efficient use of memory, as the size can be adjusted
during program execution.
o Disadvantages: Requires careful management to avoid memory leaks
and fragmentation.

2. Linked List: Introduction

A linked list is a fundamental data structure in computer science. It mainly allows


efficient insertion and deletion operations compared to arrays. Like arrays, it is also
used to implement other data structures like stack, queue and deque. Here’s the
comparison of Linked List vs Arrays
 A linked list is a dynamic data structure where elements (nodes) are linked
using pointers.
 Each node typically contains data and a pointer to the next node (or previous
node in doubly linked lists).
 Linked lists grow and shrink dynamically, making them memory-efficient for
certain applications.

Linked List:
 Data Structure: Non-contiguous
 Memory Allocation: Typically allocated one by one to individual elements
 Insertion/Deletion: Efficient
 Access: Sequential
3. Realization of Linked List Using Dynamic Memory Management
 Linked lists use dynamic memory allocation, allowing nodes to be created and
destroyed during runtime.
 Functions like malloc in C or new in C++/Java are commonly used to allocate
memory for new nodes.

4. Operations on Linked Lists


 Create: Initializes a linked list, often by setting the head to NULL.
 Traverse: Visits each node in the list to display or process data.
 Search: Finds a node with a specific value by iterating through the list.
 Insert: Adds a new node at a specific position (beginning, end, or middle).
 Delete: Removes a node from a specific position.
 Sort: Orders nodes based on their values, usually done using algorithms like
bubble sort or merge sort.
 Concatenate: Combines two linked lists into one by linking the end of the
first list to the start of the second.

5. Linked List as an Abstract Data Type (ADT)


 A Linked List ADT defines the standard operations (insert, delete, search,
etc.) without specifying the underlying implementation.
 Allows flexibility in switching between different linked list types or
implementations.
 The List ADT Functions is given below:
 get() – Return an element from the list at any given position.
 insert() – Insert an element at any position of the list.
 remove() – Remove the first occurrence of any element from a non-empty list.
 removeAt() – Remove the element at a specified location from a non-empty list.
 replace() – Replace an element at any position by another element.
 size() – Return the number of elements in the list.
 isEmpty() – Return true if the list is empty, otherwise return false.
 isFull() – Return true if the list is full, otherwise return false.

2. Stack ADT
View of stack
 In Stack ADT Implementation instead of data being stored in each node, the
pointer to data is stored.
 The program allocates memory for the data and address is passed to the stack
ADT.
 The head node and the data nodes are encapsulated in the ADT. The calling
function can only see the pointer to the stack.
 The stack head structure also contains a pointer to top and count of number of
entries currently in stack.
 push() – Insert an element at one end of the stack called top.
 pop() – Remove and return the element at the top of the stack, if it is not empty.
 peek() – Return the element at the top of the stack without removing it, if the stack
is not empty.
 size() – Return the number of elements in the stack.
 isEmpty() – Return true if the stack is empty, otherwise return false.
 isFull() – Return true if the stack is full, otherwise return false.

3. Queue ADT

View of Queue
 The queue abstract data type (ADT) follows the basic design of the stack
abstract data type.
 Each node contains a void pointer to the data and the link pointer to the next
element in the queue. The program’s responsibility is to allocate memory for
storing the data.
 enqueue() – Insert an element at the end of the queue.
 dequeue() – Remove and return the first element of the queue, if the queue is not
empty.
 peek() – Return the element of the queue without removing it, if the queue is not
empty.
 size() – Return the number of elements in the queue.
 isEmpty() – Return true if the queue is empty, otherwise return false.
 isFull() – Return true if the queue is full, otherwise return false.

6. Types of Linked Lists


Singly Linked List:
o Each node has data and a pointer to the next node.
o Can only be traversed in one direction (forward).

Circular Linked List:


o Similar to a singly linked list, but the last node points back to the first
node.
o Allows circular traversal of the list, useful in certain applications like
round-robin scheduling.

Doubly Linked List:


o Each node contains data, a pointer to the next node, and a pointer to the
previous node.
o Can be traversed both forward and backward.
o More memory-intensive but useful for applications requiring reverse
traversal.

Doubly Circular Linked List:


o A doubly linked list where the last node points back to the first, and the
first node points to the last.
o Combines the benefits of circular traversal and bidirectional access.

7. Primitive Operations on Linked Lists


 Create: Initialize an empty linked list.
 Traverse: Visit and process each node in sequence.
 Search: Locate a node with a particular value by iterating through the list.
 Insert:
o At Beginning: Add a node at the start of the list.

To insert a new node at the front, we create a new node and point
its next reference to the current head of the linked list. Then, we update
the head to be this new node. This operation is efficient because it only
requires adjusting a few pointers.

Algorithm:
 Make the first node of Linked List linked to the new node
 Remove the head from the original first node of Linked List
 Make the new node as the Head of the Linked List.

o At End: Add a node at the end of the list.

Inserting at the end involves traversing the entire list until we reach the last node.
We then set the last node’s next reference to point to the new node, making the new
node the last element in the list.
Algorithm:
 Go to the last node of the Linked List
 Change the next pointer of last node from NULL to the new node
 Make the next pointer of new node as NULL to show the end of Linked List

o At Position: Insert a node at a specified position.


If we want to insert a new node after a specific node, we first locate that node. Once
we find it, we set the new node’s next reference to point to the node that follows the
given node. Then, we update the given node’s next to point to the new node. This
requires traversing the list to find the specified node.

Approach:
 Initialize a pointer curr to traverse the list starting from head.
 Loop through the list to find the node with data equal to key.
 If not found then return from function.
 Create a new node, say new_node initialized with the given data.
 Make the next pointer of new_node as next of given node.
 Update the next pointer of given node point to the new_node.

 Delete:
o From Beginning: Remove the first node.
o From End: Remove the last node.
o At Position: Delete a node at a specified position.
 Sort: Arrange nodes in ascending or descending order.
 Concatenate: Link the end of one list to the beginning of another.

8. Polynomial Manipulations: Polynomial Addition


 A polynomial can be represented as a linked list where each node represents a
term with a coefficient and exponent.
 Polynomial Addition:
o Traverse two polynomials (linked lists) and combine terms with the
same exponent.
o If exponents match, add coefficients; if they don’t, retain the unique
terms.
o Store the result as a new linked list representing the sum of the two
polynomials.

9. Generalized Linked List (GLL) Concept


 A Generalized Linked List (GLL) is an extension of a standard linked list
where each node can store an atomic data element or a pointer to another
GLL.
 Allows for multi-level lists, enabling hierarchical data representation.
 Applications: Useful in representing complex data structures like hierarchical
file systems or mathematical expressions.

A Generalized Linked List L, is defined as a finite sequence of n>=0


elements, l1, l2, l3, l4, …, ln, such that li are either item or the list of items.
Thus L = (l1, l2, l3, l4, …, ln) where n is total number of nodes in the list.

To represent a list of items there are certain assumptions about the node
structure.
 Flag = 1 implies that down pointer exists
 Flag = 0 implies that next pointer exists
 Data means the item.
 Down pointer is the address of node which is down of the current node
 Next pointer is the address of node which is attached as the next node

Why Generalized Linked List? Generalized linked lists are used because
although the efficiency of polynomial operations using linked list is good
but still, the disadvantage is that the linked list is unable to use multiple
variable polynomial equation efficiently. It helps us to represent multi-
variable polynomial along with the list of elements.

10. Representation of Polynomial Using GLL


 In a GLL representation, each polynomial term (coefficient and exponent) can
be represented as nodes, with additional links connecting like terms across
different levels.
 Enables efficient representation of polynomials with multiple variables or
complex mathematical expressions.

You might also like