Department of Artificial Intelligence & Data Science
Lecture Handouts
Course Code & Name: 24AI14301 & Data Structures and Algorithms
Semester/Year
Branch : B.Tech – AI&DS Sec A&B&C
II / I
Staff Name : Mrs.M.Chithra.,AP/AI&DS, Mr.R.Sathiskumar.,AP/AI&DS
Mrs.S.Sowndarya.,AP/AI&DS
Unit : II Lecture No. 9 Date of Lecture:
Topic of Lecture: Application of Linked list
Introduction
Linked lists are versatile data structures that are widely used in various applications due to their dynamic
nature and efficient memory management. Unlike arrays, linked lists allow for the insertion and deletion
of elements without the need for shifting other elements, making them ideal for certain tasks. Below are
some key applications of linked lists
Prerequisite knowledge for Complete learning of Topic:
The basic knowledge of C Language Fundamentals
Basic Knowledge of Application of linked list.
Data structure and algorithms.
Detailed Content of the Lecture:
Description: In applications that require dynamic memory allocation, linked lists are often used to
manage free memory efficiently. This is particularly useful when the memory size is not known in
advance or changes during runtime.
How Linked List Helps: Each node in a linked list can dynamically allocate memory as needed, and
when the memory is freed, it can be easily deallocated by adjusting the pointers, reducing memory
fragmentation.
Application of linked list
Example:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int isEmpty(struct Node* top) {
return top == NULL;
void push(struct Node** top, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Memory allocation failed!\n");
return;
newNode->data = data;
newNode->next = *top;
*top = newNode;
printf("Pushed %d onto the stack\n", data);
int pop(struct Node** top) {
if (isEmpty(*top)) {
printf("Stack is empty! Cannot pop.\n");
return -1;
struct Node* temp = *top;
int poppedValue = temp->data;
*top = (*top)->next; // Update top to the next node
free(temp); // Free the memory of the popped node
return poppedValue;
int peek(struct Node* top) {
if (isEmpty(top)) {
printf("Stack is empty! Cannot peek.\n");
return -1;
return top->data;
void display(struct Node* top) {
if (isEmpty(top)) {
printf("Stack is empty!\n");
return;
}
struct Node* temp = top;
printf("Stack: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
printf("NULL\n");
int main() {
struct Node* stack = NULL; // Initialize an empty stack
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
push(&stack, 40);
display(stack); // Output: 40 -> 30 -> 20 -> 10 -> NULL
printf("Peek: %d\n", peek(stack)); // Output: 40
printf("Popped: %d\n", pop(&stack)); // Output: 40
printf("Popped: %d\n", pop(&stack)); // Output: 30
display(stack); // Output: 20 -> 10 -> NULL
printf("Popped: %d\n", pop(&stack)); // Output: 20
printf("Popped: %d\n", pop(&stack)); // Output: 10
printf("Popped: %d\n", pop(&stack)); // Output: Stack is empty! Cannot pop;
return 0;
Output:
Pushed 10 onto the stack
Pushed 20 onto the stack
Pushed 30 onto the stack
Pushed 40 onto the stack
Stack: 40 -> 30 -> 20 -> 10 -> NULL
Peek: 40
Popped: 40
Popped: 30
Stack: 20 -> 10 -> NULL
Popped: 20
Popped: 10
Stack is empty! Cannot pop.
Outcome / Application: Students will be able to
Can use application of the linked lists
Video Content / Details of website for further learning:
1. https://youtu.be/2DnfWj13mQ0?si=n-qzyTQJEuQL6myI
2. https://youtu.be/6wXZ_m3SbEs?si=O-KuGSErCLhL4aES
Important Books/Journals for further learning including the page nos.:
1. ReemaThareja, ―Data Structures Using C, Second Edition , Oxford University Press,
2011
2. Thomas H. Cormen, Charles E. Leiserson, Ronald L.Rivest, Clifford Stein, Introduction to
Algorithms", Second Edition, Mcgraw Hill, 2002.
Subject Teacher Verified by HOD