Data Structures: Aad Cse Srm-Ap 1
Data Structures: Aad Cse Srm-Ap 1
Data Structure
Linear Non-Linear
- - Trees
Array
- - Graphs
Stack
- Queue 2
AAD CSE SRM-AP
- Linked Lists
Linear Data Structures
PUSH POP
top top
top top
elements
You want to insert and delete quickly.
AAD CSE SRM-AP 19
Singly Linked List
A singly linked list is a concrete data structure
consisting of a sequence of nodes
Each node stores next
element
(usually empty)
Empty: Check if list is empty
struct node
2000
{
int data; new
struct node *link;
}*new, *ptr, *header, *ptr1;
data link
10 NULL
Creating a node
1200
2500 header
new
5 1300 5 1330 4 1400 8 NULL
10 NULL 1200 1300 1330 1400
2500
header Inserting a node at the end
1500
1400
1500 1800 1200
ptr 50 NULL
1400
1800
1500
1200
2000
Algorithm:
new
1. new=malloc(sizeof(struct node));
2. ptr = header; 2000
3. while(ptr -> link!= NULL)
4. ptr = ptr -> link;
5. ptr -> link = new;
Inserting a node at the given position
header ptr
1500 1800
1500 Insert position : 3
10 1800 20 1200
2000 30 1400 40 NULL
1400
1500 1800 1200
50 NULL
1200
new Algorithm:
1. new=malloc(sizeof(struct node));
2000
2000 2. ptr = header;
3. for(i=1;i < pos-1;i++)
4. ptr = ptr -> link;
5. new -> link = ptr -> link;
6. ptr -> link = new;
Deleting a node at the beginning
header ptr
1500
1800 1500
Algorithm:
1. if (header = = NULL)
2. print “List is Empty”;
3. else
4. {
5. ptr = header;
6. header = header -> link;
7. free(ptr);
8. }
header
Deleting a node at the end
ptr1 ptr1 ptr1
1500
1400
1500 1800 1200
ptr
1800
1200
1400
1500
Algorithm:
1. ptr = header;
2. while(ptr -> link != NULL)
3. ptr1=ptr;
4. ptr = ptr -> link;
5. ptr1 -> link = NULL;
6. free(ptr);
Deleting a node at the given position
ptr1
header ptr
1200
1500 1800
1500 Insert position : 3
10 1800 20 1400
1200 30 1400 40 NULL
1400
1500 1800 1200
Algorithm:
1. ptr = header ;
2. for(i=1;i<pos-1;i++)
3. ptr = ptr -> link;
4. ptr1 = ptr -> link;
5. ptr -> link = ptr1-> link;
6. free(ptr1);
Traversing an elements of a list
header ptr
1500
1500
Algorithm:
1. if(header = = NULL)
2. print “List is empty”;
3. else
4. for (ptr = header ; ptr != NULL ; ptr = ptr -> link)
5. print “ptr->data”;
#include<iostream>
#include<stdlib.h> Creating the linked List
struct Node {
int data; struct Node *create_list()
{
struct Node *next; int k, n;
}; struct Node *p, *Head;
printf("\n How many elements to enter?");
struct Node *create_list(); scanf("%d", &n);
void display(struct Node *); for (k=0; k<n; k++)
{
void main() if (k == 0) {
{ Head = (struct Node*)malloc(sizeof(struct Node));
struct Node *head; p = Head;
}
head=create_list(); else {
display(head); p->next = (struct Node*)malloc(sizeof(struct Node));
p = p->next;
} }
printf("\n Enter an %dth element",k);
scanf("%d",&p->data);
}
void display (struct Node *head)
{ p->next = NULL;
return(Head);
struct Node *p; }
for(p = head; p!= NULL; p = p->next)
{
printf ("\nNode data %d", p->data);
}
printf ("\n");
} 34
COMPLEXITY OF VARIOUS OPERATIONS
IN ARRAYS AND LINKED LIST
Array List
Fixed-Size Singly-Linked
2/24/22 06:13 AM
Vectors 38
Queue implementation using
Linked List
• Basic idea:
• Create a linked list to which items would be added to one
end and deleted from the other end.
• Two pointers will be maintained:
• One pointing to the beginning of the list (point from
where elements will be deleted).
• Another pointing to the end of the list (point where new Rear
elements will be inserted).
front rear
DEQUEUE
front rear
2/24/22 06:13 AM
Vectors 41