Module 3-1
Module 3-1
Table of contents
Module 3
Textbook 2: Ch. 9.1 to 9.2, 9.3 (Only 9.3.1 to 9.3.5, 9.3.11 to 9.3.12), 9.4 to 9.5.
1
BY DR GANESH V BHAT DEPT ECE @ CEC
INTRODUCTION TO DATA STRUCTURES
Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at a
contiguous location; the elements are linked using pointers.
2
BY DR GANESH V BHAT DEPT ECE @ CEC
INTRODUCTION TO DATA STRUCTURES
STEP 1: IF POINTER = NULL (GO STEP 7 MEANS EXIT OTHERWISE FOLLOW STEP2)
STEP 2: SET NEW_NODE = POINTER
STEP 3: SET POINTER = POINTER → NEXT
STEP 4: SET NEW_NODE → DATA= VALUE
STEP 5: SET NEW_NODE →NEXT =HEAD
STEP 6: SET HEAD = NEW_NODE
STEP 7: RETURN
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node *next;
}*head;
void Insert_B(int);
void Display();
void main(void){
int count=5,val,i;
head =NULL;
for(i=0;i<count;i++){
printf("Enter a bigning value\n");
scanf("%d",&val);
Insert_B(val); }
Display();
}
void Display(){
struct Node *p;
p=head;
while(p!=NULL){
printf("%d",p->data);
3
BY DR GANESH V BHAT DEPT ECE @ CEC
INTRODUCTION TO DATA STRUCTURES
p=p->next;
} }
4
BY DR GANESH V BHAT DEPT ECE @ CEC
INTRODUCTION TO DATA STRUCTURES
5
BY DR GANESH V BHAT DEPT ECE @ CEC
INTRODUCTION TO DATA STRUCTURES
6
BY DR GANESH V BHAT DEPT ECE @ CEC
INTRODUCTION TO DATA STRUCTURES
• If current_Node is not NULL, then element is found hence return index otherwise -1.
In a singly circular linked list, the address of the last node’s next pointer rather than being
NULL is pointed towards the address of the head node.
Advantages
• Any node can be starting point and we can still traverse the whole list
• It can also deem to be useful for implementation as queues as rather than maintaining
the Front and Rear, we can use a circular linked list and just maintain the pointer to the
first inserted node and it can simply be the next node to the last node.
• Circular linked lists are commonly used in OS’es for the task that requires repeatedly to
be executed by OS.
Disadvantages
• Doubly Linked List is faster in getting previous node information due to previous pointer.
• Doubly Linked List can traverse in both forward and backward direction.
• Finding end of list and loop control is harder (no NULL to mark beginning and end).
• The entire list can be traversed starting from any node (traverse means visit every node
just once).
7
BY DR GANESH V BHAT DEPT ECE @ CEC
INTRODUCTION TO DATA STRUCTURES
#include <stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node *next;
}*head,*tail;
void Create(int x[],int num);
void Display ();
void Insert (int x, int pos);
void main (void){
int A[6]={1,2,3,4,5,6};
Create(A,6);
Display();
Insert(9,3);
Display();
}
void Create(int x[], int num){
int i;
struct Node *temp;
head =(struct Node*)malloc(sizeof(struct Node));
head->data=x[0];
head->next=head;
tail=head;
for (i=1;i<num;i++){
temp=(struct Node*)malloc(sizeof(struct Node));
temp->data=x[i];
temp->next=head;
tail->next=temp;
tail=tail->next; }
}
void Display(void){
struct Node *p;
p=head;
do{
8
BY DR GANESH V BHAT DEPT ECE @ CEC
INTRODUCTION TO DATA STRUCTURES
printf("%d\n ",p->data);
p=p->next;
} while (head!=p);
}
9
BY DR GANESH V BHAT DEPT ECE @ CEC
INTRODUCTION TO DATA STRUCTURES
Push / Pop operation at the beginning of the Linked List is much faster than end of the list
insertion and deletion as Pushing a new node at the beginning of a linked list takes place in
constant time. When inserting the new node the number of traversals that occur is equal to 1,
i.e time complexity of inserting a new node at the beginning of the linked list is O(1).
10
BY DR GANESH V BHAT DEPT ECE @ CEC
INTRODUCTION TO DATA STRUCTURES
#include <stdio.h>
#include <stdlib.h>
// Stack structure
typedef struct {
Node *top; // Pointer to the top node of the stack
} Stack;
11
BY DR GANESH V BHAT DEPT ECE @ CEC
INTRODUCTION TO DATA STRUCTURES
int main() {
Stack stack;
initStack(&stack); // Initialize the stack
Node list[MAX];
12
BY DR GANESH V BHAT DEPT ECE @ CEC
INTRODUCTION TO DATA STRUCTURES
Dynamic/Linked Allocation
Definition: Linked allocation dynamically allocates memory for each node during runtime. Each
node contains data and a pointer to the next node.
Characteristics
Memory Management: Memory is allocated during runtime using functions like malloc or calloc.
Size: Dynamic size; the list can grow or shrink based on the requirements.
Flexibility: Highly flexible as memory is allocated and deallocated dynamically.
Memory Utilization: Efficient as memory is used only when required. No predefined limits.
Performance: May be slower due to dynamic memory management overhead, but provides
adaptability for varying sizes.
Implementation: Uses pointers to link the nodes in memory.
Example:
typedef struct Node {
int data;
struct Node *next;
} Node;
Comparison Table
Aspect Static Allocation Linked Allocation
Memory
Compile-time (fixed) Runtime (dynamic)
Allocation
Flexible, grows/shrinks as
Size Fixed, predefined
needed
Memory
May waste memory Efficient, no memory wastage
Utilization
13
BY DR GANESH V BHAT DEPT ECE @ CEC
INTRODUCTION TO DATA STRUCTURES
Review Questions
What is a linked list? What are its various types?
Explain the representation of a linked list in memory with the help of an illustration.
Explain the typical operations that are performed on a linked list.
Explain the key advantages and disadvantages of linked lists.
What is a circular linked list? How is it different from a normal linked list?
What is a doubly linked list? Why is it used?
Write the algorithm for searching an element in a singly linked list.
Write the algorithm for inserting an element in a circular linked list.
What is a stack? Explain with examples.
Briefly describe the LIFO principle.
What is a top pointer? Explain its significance.
What are the different application areas of stack data structure?
Give any four real-life examples that principally resemble the stack data structure.
Explain the logical representation of stacks in memory with the help of an example.
Explain push and pop operations with the help of examples.
What will happen if we keep on pushing elements into a stack one after another?
What will happen if we continue to pop out elements from a stack one after
another?
How are stacks implemented?
What is the advantage of linked implementation of stacks over array
implementation?
What role does dynamic memory management techniques play in linked
implementation of stacks?
Briefly explain the overflow and underflow conditions along with their remedies.
Can an overflow situation occur even with linked implementation of stacks that
uses dynamic memory allocation techniques? Explain.
Excises programs
Write a code snippet for declaring the node of a doubly linked list.
Write a C function to delete a node from a singly linked list.
Write a C function to insert a new node at the end of a circular linked list.
Write a C function to print the elements of a linked list.
Write a C function to print the elements of a doubly linked list in both forward
and backward directions.
15
BY DR GANESH V BHAT DEPT ECE @ CEC