BMC205 DSAA Unit1 Linked List Notes
BMC205 DSAA Unit1 Linked List Notes
Self-Referential Structure
One or more of structure’s components is a pointer to the itself type.
struct node {
int data;
struct node *nextPtr;
}
Linked List
A linked list is a collection of “nodes” connected together via links. These nodes consist of
the data to be stored and a pointer to the address of the next node within the linked list. In the
case of arrays, the size is limited to the definition, but in linked lists, there is no defined size.
A linked list is a linear data structure, in which the elements are not stored at contiguous
memory locations.
There are following types of linked lists −
• Singly Linked List − It is the simplest type of linked list in which every node
contains some data and a pointer to the next node of the same data type. The node
contains a pointer to the next node means that the node stores the address of the next
node in the sequence. A single linked list or a one-way list allows the traversal of
• Header Linked List − Header node is not the actual node of the list. A header linked
list is a type of linked list that has a header node at the beginning of the list. In a
header linked list, HEAD points to the header node instead of the first node of the list.
The header node does not represent an item in the linked list. This data part of this
node is generally used to hold any global information (as count) about the entire
linked list. The next part of the header node points to the first node in the list.
Example,
Input:
P1 = 5x2 + 4x1 + 2x0
P2 = 5x1 + 5x0
Output:
5x2 + 9x1 + 7x0
struct Node{
int coeff;
int pow;
struct Node *next;
};
Explanation − For adding two polynomials that are represented by a linked list, we check
values at the exponent value of the node. For the same values of exponent, we will add the
coefficients. Then return the final polynomial.
Algorithm
Input − polynomial p1 and p2 represented as a linked list.
Step 1: loop around all values of linked list and follow step 2 & 3.
Step 2: if the value of a node’s exponent is greater, copy this node to result node and head
towards the next node.
Step 3: if the values of both node’s exponent is same, add the coefficients and then copy the
added value with node to the result.
Step 4: Print the resultant node.
Garbage Collection:-
The operating system of a computer may periodically collect all the deleted space
onto the free-storage list. Any technique which does this collection is called garbage
collection. Garbage collection usually lakes place in two steps. First the computer runs
through all lists, tagging those cells which are currently in use, and then the computer runs
through the memory, collecting all untagged space onto the free-storage list. The garbage
collection may take place when there is only some minimum amount of space or no space at
all left in the free-storage list, or when the CPU is idle and has time to do the collection.
Generally speaking, the garbage collection is invisible to the programmer.
Stack: A stack is a linear data structure that follows the Last In, First Out (LIFO) principle,
i.e., the item which is added at last is removed first. The best analogy for a stack is either a
pile of coins or a pile of books kept one above the other. You can add or remove an item from
the top only. It can be implemented using an array and linked list.
A stack is represented using nodes of a linked list. Each node consists of two parts: data and
next (storing the address of the next node). The data part of each node contains the assigned
value, and the next points to the node containing the next item in the stack. The top refers to
the topmost node in the stack. Both the push() and pop() operations are carried out at the
front/top of the linked list.
1. Push Function: Adding or inserting a new element to a stack is known as the Push() operation in
the stack. Elements can only be pushed at the top of the stack.
• Create a new node using dynamic memory allocation and assign value to the node.
• Check if stack is empty or not, i.e, (top == NULL).
• If it is empty, then set the next pointer of the node to NULL.
• If it is not empty, the newly created node should be linked to the current top element of the
stack, i.e.,
• Make sure that the top of the stack should always be pointing to the newly created node.
2. Pop Function: Removing or deleting an element from a stack is known as the Pop()
operation in the stack. Elements are popped from the top of the stack. There should be at least one
element in the stack to perform the pop() operation.
• Check if stack is empty or not, i.e, (TOP == NULL).
• If it is not empty, then create a temporary node and set it to top. Now, create another
variable and copy the data of top element to this variable.
• Now, make top point to the next node.
• Delete the temporary node.
• If it is empty, then print Stack Underflow.
Queue: A queue is a linear data structure that follows the First In, First Out (FIFO) principle,
which means that the element which is inserted first in the queue will be the first one to be
removed from the queue. A good example of a queue is a queue of customers purchasing a
train ticket, where the customer who comes first will be served first. It can be implemented
using an array and linked list.
Operation on Linked Queue: Each node of a linked queue consists of two fields: data and
next (storing address of next node). The data field of each node contains the assigned value,
and the next points to the node containing the next item in the queue.
A linked queue consists of two pointers, i.e., the front pointer and the rear pointer. The front
pointer stores the address of the first element of the queue, and the rear pointer stores the
address of the last element of the queue.
Queue support operations like enqueue (insertion) and dequeue (deletion). Insertion is
performed at the rear end, whereas deletion is performed at the front end of the queue. If front
and rear both points to NULL, it signifies that the queue is empty.
1. Enqueue Function: It adds an element to the end of the queue. The last element can be tracked
using the rear pointer.
• First, build a new node with given data.
• Check if the queue is empty or not.
• If a queue is empty then, a new node is assigned to the front and rear.
• Else make next of rear as new node and rear as a new node.
2. Dequeue Function: It always removes the first element of the queue. For dequeue, the queue
must contain at least one element, else underflow conditions will occur.
• Check if queue is empty or not.
• If the queue is empty, then dequeue is not possible.
• Else store front in temp
• And make next of front as the front.
• Delete temp, i.e, free(temp).