Data Structures Unit-II Lecture Notes
Data Structures Unit-II Lecture Notes
1. Memory requirements
Inserting at the middle
Complexity= O(1)
-5
-3
3
Data Structures--> Unit-II: Introduction to Linked lists Pandu Sowkuntla
Why Linked Lists?
2. Array elements store in a contiguous memory 2. Linked list elements can be stored anywhere in the
location. memory or randomly stored.
3. Array elements are independent of each 3. Linked list elements are dependent on each other.
other.
4. Array works with a static memory. 4. The Linked list works with dynamic memory.
6. Accessing any element in an array is faster 6. Accessing an element in a linked list is slower.
(O(1))
7. Cost is less for performing insertion, deletion
7. Cost is more for performing insertion, operations ( )
deletion, operations (O(n)).
8. Memory utilization is efficient (memory can be
8. Memory utilization is inefficient. (For allocated or deallocated at the run time according
example, if the size of the array is 6, and to our requirement).
array consists of 3 elements only then the
rest of the space will be unused).
Data Structures-I--> Unit-II: Introduction to Linked lists Pandu Sowkuntla
4
What is Linked List?
In linked list, each node consists of its own
data and the address of the next node and Singly Linked Lists
forms a chain.
Head
▪ A linked list is a series of connected
nodes 10 20 30 \0
head = 50
12 100 13 200 11 300 10 \0
void display_SLL(struct Node *head) for(ptr = head; ptr!= NULL; ptr = ptr->next)
{ {
struct Node *ptr; if ((ptr->data)==key)
int count=0; {
for(ptr=head; ptr!= NULL; ptr = ptr->next) flag=1;
{ break;
printf ("\n Node data= %d", ptr->data); }
count++; }
}
printf ("\n"); if(flag==1)
} printf(“found”);
else
printf(“not found”);
} 8
Data Structures--> Unit-II: Operations on SLL Pandu Sowkuntla
First node
Inserting node at the beginning of the List (Beginning of the list)
head = 100
struct Node *insertBeg_SLL(struct Node *head) 10 200 20 300 30 \0
{
//Step-1. create a new node 100 200 300
newnode Linked list before insertion
struct Node* newnode;
newnode = (struct Node*) malloc(sizeof(struct Node));
1000
//Step-2. Store the data
40
printf(“Enter an element to insert at beginning:”); head = 100
scanf(“%d”,&newnode->data); 1000
Pandu Sowkuntla
9
Data Structures --> Unit-II: Operations on SLL
Inserting node at the end of the List
Last node (end of list)
head = 50
10 100 13 200 11 \0 struct Node *insertEnd_SLL(struct Node *head)
{
50 100 200 //Step-1. Create new node
newnode struct Node* newnode;
newnode = (struct Node*) malloc(sizeof(struct Node));
head=100
10 20 30 40 \0
head
\0 10 200 20 300 30 400 40 \0
100 200 300
100 200 300 400
Advantages:
• Doubly linked list can be traversed forward and backward.
Disadvantage:
• Increase in space requirements.
Pandu Sowkuntla
15
Data Structures--> Unit-II: Doubly Linked List
Operations on Doubly Linked List (DLL)
Pandu Sowkuntla
16
Data Structures--> Unit-II: Operations on DLL
Creation of the Doubly Linked List
head
\0 10 200 20 300 30 400 40 \0
100 200 300
100 200 300 400
struct Node *create_DLL()
{
struct Node int i, n;
{ struct Node *curr, *temp, *head;
struct Node *Lnext; printf("\n How many elements you want to enter?");
scanf("%d", &n);
int data; for (i=1; i<=n; i++)
struct Node *Rnext; {
}; if (i == 1){
head = (struct Node*)malloc(sizeof(struct Node));
head->Lnext=NULL;
void main() curr = head;
{ }
else{
struct Node *head; temp = (struct Node*)malloc(sizeof(struct Node));
head=create_DLL(); curr->Rnext=temp;
display(head); temp->Lnext=curr;
} curr=temp;
}
printf(“Enter the %d th element: “, i)
scanf("%d",&curr->data);
}
curr->Rnext = NULL;
return(head); Pandu Sowkuntla
17
Data Structures--> Unit-II: Operations on DLL
Search and Traverse operations on DLL
head
\0 10 200 20 300 30 400 40 \0
100 200 300
100 200 300 400
3. Searching an element in DLL
void main() void search_DLL(struct Node *head)
{ {
struct Node *head; struct Node *ptr;
head=create_DLL(); int key, count=0;
display_DLL(head); printf(“Enter an element to search:”);
search_DLL(head); scanf(“%d”,&key);
} for(ptr = head; ptr!= NULL; ptr = ptr->next)
2. Traversing a DLL {
if ((ptr->data)==key)
void display_DLL(struct Node *head)
{
{
count=1;
struct Node *ptr;
break;
int count=0;
}
for(ptr=head; ptr!= NULL; ptr = ptr->next)
}
{
if(count==1)
printf ("\n Node data= %d", ptr->data);
printf(“found”);
}
else
printf ("\n");
printf(“not found”); 18
} Data Structures--> Unit-II: Pandu Sowkuntla
}
First node
Inserting node at the beginning of DLL (Beginning of the list)
head
\0 10 200 20 300 30 \0
100 200
struct Node *insertBeg_DLL(struct Node *head) 100 300
{ 200
//Step-1. create a new node Lnext data Rnext
struct Node* newnode;
newnode
newnode = (struct Node*) malloc(sizeof(struct Node));
1000
//Step-2. Store the data
return(head);
}
Pandu Sowkuntla
22
Data Structures--> Unit-II: Operations on DLL
Delete node at the end of DLL
head
Pandu Sowkuntla
23
Data Structures--> Unit-II: Operations on DLL
Delete node at the middle of DLL head current
• It supports traversing from the end of the list to the beginning by making the last node
point back to the head of the list.
struct Node
{
int data;
struct Node *next;
};
Pandu Sowkuntla
25
Data Structures--> Unit-II: Introduction to CLL
Operations on Circularly Linked Lists (CLL)
head 10 20 40 55 70
head 10 20 40 55 70
newnode->next = head;
ptr=head;
while(ptr->next !=head)
{
ptr=ptr->next;
}
ptr->next=newnode
head=newnode;
return(head);
Pandu Sowkuntla
28
Data Structures--> Unit-II: Operations on CLL
Inserting node at Middle of CLL
head 10 20 40 55 70
ptr=head;
while(ptr!= head && count!=loc)
{
ptr = ptr->next;
count=count+1;
}
newnode->next=ptr->next;
ptr->next=newnode;
return(head);
Pandu Sowkuntla
29
Data Structures--> Unit-II: Operations on CLL
Delete node at beginning of CLL
head 10 20 40 55 70
Pandu Sowkuntla
30
Data Structures--> Unit-II: Operations on CLL
Circular Doubly Linked Lists (CDLL)
head 10 20 40 55 70
head
400 10 200 20 300 30 400 40 100
100 200 300
100 200 300 400
Pandu Sowkuntla
31
Data Structures--> Unit-II: Operations on CDLL
Circular Doubly Linked Lists (CDLL)
head
400 10 200 20 300 30 400 40 100
100 200 300
100 200 300 400
struct Node *create_CDLL()
{
int i, n;
struct Node *curr, *temp, *head;
printf("\n How many elements you want to enter?");
scanf("%d", &n);
for (i=1; i<=n; i++)
{
if (i == 1){
head = (struct Node*)malloc(sizeof(struct Node));
head->Lnext=NULL;
curr = head;
}
else{
temp = (struct Node*)malloc(sizeof(struct Node));
curr->Rnext=temp;
temp->Lnext=curr;
curr=temp;
}
printf(“Enter the %d th element: “, i)
scanf("%d",&curr->data);
}
curr->Rnext = head;
head->Lnext = curr;
return(head); } Pandu Sowkuntla
32
Data Structures--> Unit-II: Operations on CDLL
Pandu Sowkuntla
33
Data Structures--> Unit-II: