0% found this document useful (0 votes)
5 views

Data Structures Unit-II Lecture Notes

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Data Structures Unit-II Lecture Notes

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Data Structures

Dr. Pandu Sowkuntla


Asst. Professor,
Dept. of CSE, SRM University AP

Data Structures--> Unit-II: Pandu Sowkuntla 1


LINKED LISTS
UNIT-II
Why Linked Lists?

➢ Operations performed on Arrays 3. Cost of Inserting an Element

1. Create Inserting at the beginning


2. Insert
3. Delete
4. Traverse
5. Search
6. Display
Complexity= O(n)
➢ Problems with Arrays

1. Memory requirements
Inserting at the middle

If I store only 3 elements,


2 memory locations will be wasted

2. Cost of accessing an element


Complexity= O(n)

Time complexity= O(1) Inserting at the end

Complexity= O(1)
-5
-3
3
Data Structures--> Unit-II: Introduction to Linked lists Pandu Sowkuntla
Why Linked Lists?

1. An array is a collection of elements of a 1. A linked list is a collection nodes where a node


similar data type. consists of two parts, i.e., data and address.

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.

5. Memory is allocated at compile-time. 5. Memory is allocated at run time.

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

▪ Each node contains at least


▪ A piece of data (any type)
▪ Pointer to the next node in the list Data/Info Address
field field
▪ Head: pointer to the first node
▪ NULL: The last node points to NULL
Operations performed on Singly Linked Lists

Linked Lists ▪ Create


▪ Traversing
▪ Search
▪ Insertion
▪ At the begin
▪ At the end
Singly Doubly ▪ At the middle
Circularly
Linked Linked Linked
▪ Deletion
Lists Lists Lists ▪ At the begin
▪ At the end
▪ At the middle
▪ Display Pandu Sowkuntla
Data Structures--> Unit-II: Introduction to Linked lists
Operations on Singly Linked List

300 400 500 \0


data next data next data next data next
head
200 20 30 40 50 NULL

200 300 400 500 Last node


First node

1. Creation of Linked List

► Empty Linked list is a single pointer with the value of NULL.


head = NULL;
► A node can be declared as::
struct Node
{
int data;
struct Node *next;
};
struct Node *head;
► Create the first node and make head point to it.
head = (struct Node*)malloc(sizeof(struct Node));
Pandu Sowkuntla
6
Data Structures--> Unit-II: Operations on SLL
Operations on Singly Linked List
300 150 500 \0
data next data next data next data next
head
200 20 10 30 25 NULL

200 First node 300 150 500 Last node


struct Node *create_SLL(int n)
{
1. Creation of Linked List for (i=1; i<=n; i++)
{
► Empty Linked list is a single pointer with
if (i == 1)
the value of NULL.
{ //Allocate the memory space at runtime for first node
head = NULL; head = (struct Node*)malloc(sizeof(struct Node));
► A node can be declared as:: ptr = head;
}
else
struct Node {
{ //Allocate the memory space at runtime for remaining nodes
ptr->next = (struct Node*)malloc(sizeof(struct Node));
int data; ptr = ptr->next;
struct Node *next; }
//Read the data into each node of SLL
}; printf("\n Enter an %dth element",i);
scanf("%d",&ptr->data);
}
► Create the first node and make head point to it.
//Assign null pointer to last node of SLL
head = (struct Node*)malloc(sizeof(struct Node)); ptr->next = NULL;
return(head);
}
Data Structures--> Unit-II: Operations on SLL Pandu Sowkuntla
Operations on Singly Linked List

head = 50
12 100 13 200 11 300 10 \0

50 100 200 300


3. Searching an element in linked list
void main()
{ void search_SLL(struct Node *head)
struct Node *head; {
head=create_SLL(); struct Node *ptr;
display(head); int key, flag=0;
search(head);
2. Traversing a linked list } printf(“Enter an element to search:”);
scanf(“%d”,&key);

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

//Step-3. Make next of new node as head


40 100 10 200 20 300 30 \0
newnode->next = head;
1000 100 200 300
//Step-4. Move the head to point to the new node
head=newnode;
return(head);
} 40 100 10 200 20 300 30 \0
head = 1000 1000 100 200 300
Linked list after insertion

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));

1000 //Step-2. Store the data


printf(“Enter an element to insert:”);
10 scanf(“%d”,&newnode->data);
//Step-3. New node Should be inserted last
1000 // so make next of it as NULL
10 \0 newnode->next = NULL;

1000 //Step-4. If list is empty, then make newnode as head


head
if (head == NULL){
1000 10 \0 head = newnode;
head = 50 1000 return(head);
}
//Step-5. Else traverse till the last node
struct Node *ptr=head;
12 100 13 200 11 1000 while (ptr->next != NULL){
ptr = ptr->next;
50 100 200 }
//Step-6. Change the next of last node
10 \0
ptr->next = newnode;
1000 return(head);
} Pandu Sowkuntla
10
Data Structures--> Unit-II: Operations on SLL
Inserting node at the middle of the List
head = 50
struct Node *insertMid_SLL(struct Node *head)
12 100 13 200 11 300 14 \0
{
int newdata, loc=2,count=0;
50 100 200 300
//Step-1. Create a newnode At this location
newnode
struct Node *newnode, *ptr; (after loc=2)
newnode = (struct Node*)malloc(sizeof(struct Node));
//Step-2. Store the data
printf(“Enter an element to insert:”); 1000
scanf(“%d”,&newnode->data); 10
ptr=head;
//Step-3. Traverse list, until required location 1000
while(ptr!= NULL && count!=loc) head = 50
{
ptr = ptr->next; 12 100 13 200 11 300
count=count+1;
} 50 100 200
//Step-4. Move the next of prev_node as newnode
newnode->next=ptr->next;
head=50 12 100 13 200 11 1000
ptr->next=newnode; 14 \0
return(head);
} 50 100 200 300
10 300
Data Structures--> Unit-II: Operations on SLL 1000 Pandu Sowk
Delete node at beginning of Singly Linked List

head=100
10 20 30 40 \0

struct Node *deleteBeg_SLL(struct Node *head) 100 200 300 400


{
struct Node *ptr;
//Step-1: Return head if list is empty
if(head==NULL) head =200
return(head);
//Step-2: Break the pointer connection
and reconnect
ptr=head; 10 20 30 40 \0
head=head->next;
//Step-3: Delete the node 100 200 300 400
free(ptr);
return(head);
head=200
} 20 30 40 \0

200 300 400


12
Data Structures--> Unit-II: Operations on SLL Pandu Sowkuntla
Delete node at the end of Singly Linked List
head
struct Node *deleteEnd_SLL(struct Node *head) 20 30 40
10 \0
{
//Step-1: Return head if list is empty 100 200 300 400
if(head==NULL)
return(head);
//Step-2: Traverse the list until end
struct Node *curr, *prev;
for(curr=head; curr->next!=NULL; curr=curr->next)
{ head
prev=curr;
} 10 20 30 40 \0
//Step-3: Break the pointer connection and set 100 200 300 400
//previous node pointer to null
prev->next=NULL; head
10 20 30 40 \0
//Step-2: Delete the node
free(curr); 100 200 300 400
\0
return(head); head
} 10 20 30 \0
100 200 300 13
Data Structures--> Unit-II: Operations on SLL Pandu Sowkuntla
Delete node at the middle of Singly Linked List
struct Node *deleteMid_SLL(struct Node *head)
{
int key;
printf(“Enter an element to delete:”);
scanf(“%d”,&key); head
struct Node *curr = head, *prev; 10 20 30 40 \0
if(curr == NULL)
return(head); 100 200 300 400
if(curr!= NULL && curr->data == key)
head head
{
head= curr->next; 10 \0
free(curr);
return(head);
}
while (curr != NULL && curr->data != key)
{ head
prev = curr; 10 30 40 \0
20
curr = curr->next;
} 100 200 300 400
prev->next = curr->next; head
free(curr); 10 30 40 \0
}
Pandu Sowkuntla
14
Data Structures--> Unit-II: Operations on SLL 400
100 300
Doubly Linked List (DLL)

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.

Lpointer data Rpointer


• Simplifies insertion and deletion because you no longer
have to refer to the previous node. 10
• A node in a singly linked list cannot be removed unless we
have the pointer to its predecessor. node
• In a doubly linked list, we can delete a node even if we
don’t have the previous node’s address (since each node
has a left pointer pointing to the previous node and can
move backward).

Disadvantage:
• Increase in space requirements.

Pandu Sowkuntla
15
Data Structures--> Unit-II: Doubly Linked List
Operations on Doubly Linked List (DLL)

• Empty Linked list is a single pointer having the


value of NULL.
head = NULL;
Operations performed on DLL

• A node in DLL can be declared as: ▪ Create


▪ Traversing
struct Node Lnext data Rnext ▪ Search
{ ▪ Insertion
▪ At the begin
struct Node *Lnext;
▪ At the end
int data; Node
▪ At the middle
struct Node *Rnext; ▪ Deletion
}; ▪ At the begin
▪ At the end
• A node in DLL can be created as: ▪ At the middle
▪ Display
head = (struct Node *)malloc(sizeof(struct Node));

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

printf(“Enter an element to insert at beginning:”);


scanf(“%d”,&newnode->data);

//Step-3. Make next of new node as head


newnode->Rnext = head;
newnode->Lnext = NULL;
head->Lnext = newnode;

//Step-4. Move the head to point to the new node


head=newnode;
return(head);
} 19
Data Structures--> Unit-II: Operations on DLL Pandu Sowkuntla
Inserting node at the end of DLL head

struct Node *insertEnd_DLL(struct Node *head) \0 10 200 20 300 30 \0


{ 100 200
//Step-1. Create new node 100 200 300
struct Node *newnode, *ptr;
newnode = (struct Node*) malloc(sizeof(struct Node)); Lnext data Rnext

//Step-2. Store the data


1000
printf(“Enter an element to insert:”);
scanf(“%d”,&newnode->data);
//Step-3. New node Should be inserted last
// so make next of it as NULL
newnode->Rnext = NULL; //Step-5. Else traverse till the last node
ptr=head;
//Step-4. If list is empty, while (ptr->Rnext != NULL){
//then make newnode as head ptr = ptr->Rnext;
if (head == NULL){ }
newnode->Lnext = NULL; //Step-6. Change the next of last node
head = newnode; ptr->Rnext = newnode;
return(head); newnode->Lnext = ptr;
} return(head);
} 20
Data Structures--> Unit-II: Operations on DLL Pandu Sowkuntla
Inserting node at the middle of DLL
head
struct Node *insertMid_DLL(struct Node *head)
{ \0 10 200 20 300 30 \0
100 200
int newdata, loc=2,count=0; 100 200 300
struct Node *newnode, *ptr;
newnode = (struct Node*)malloc(sizeof(struct Node)); Lnext data Rnext

printf(“Enter an element to insert:”);


1000
scanf(“%d”,&newnode->data);

//Step-3. If List is empty, then make newnode as head


//head=newnode;
//newnode->next=NULL
ptr=head;
while(ptr!= NULL && count!=loc)
{
ptr = ptr->next;
count=count+1;
}
newnode->next=ptr->next;
ptr->next=newnode;
return(head);
} 21
Data Structures--> Unit-II: Operations on DLL Pandu Sowkuntla
Delete node at beginning of DLL
head
\0 10 200 20 300 30 \0
100 200
struct Node *deleteBeg_DLL(struct Node *head) 100 300
200
{
struct Node *ptr;
//Step-1: Return head if list is empty
if(head==NULL)
return(head);

//Step-2: Break the pointer connection and


//reconnect
ptr=head;
head=head->next;

//Step-3: Delete the node


free(ptr);

return(head);
}

Pandu Sowkuntla
22
Data Structures--> Unit-II: Operations on DLL
Delete node at the end of DLL
head

\0 10 200 100 20 300 30 \0


struct Node *deleteEnd_DLL(struct Node *head) 200
{ 100 200 300
//Step-1: Return head if list is empty
if(head==NULL)
return(head);
//Step-2: Traverse the list until end
struct Node *curr, *prev;
for(curr=head; curr->next!=NULL;curr=curr->next)
{
prev=curr;
}
//Step-3: Break the pointer connection and set
//previous node pointer to null
prev->next=NULL;

//Step-2: Delete the node


free(curr);
return(head);
}

Pandu Sowkuntla
23
Data Structures--> Unit-II: Operations on DLL
Delete node at the middle of DLL head current

struct Node *deleteMid_DLL(struct Node *head)


{
printf(“Enter an element to delete:”);
10 20 30
scanf(“%d”,&key);
if(curr == NULL)
return(head);
curr=head;
if(curr!= NULL && curr->data == key){
head= curr->Rnext;
free(curr);
return(head);
}
while (curr != NULL && curr->data != key){
prev = curr;
curr = curr->Rnext;
}
prev->Rnext = curr->Rnext;
temp=curr;
curr=curr->Rnext;
curr->Lnext=Prev
free(temp);
}
Pandu Sowkuntla
24
Data Structures--> Unit-II: Operations on DLL
Circularly Linked Lists (CLL)

• 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.

10 200 20 300 40 400 55 500 70 100

100 200 300 400 500


head

struct Node
{
int data;
struct Node *next;
};

Pandu Sowkuntla
25
Data Structures--> Unit-II: Introduction to CLL
Operations on Circularly Linked Lists (CLL)

struct Node *create_CLL()


{ 10 20 40 55 70 100
int i, n;
struct Node *ptr, *head; 100 200 300 400 500
head
printf("\n How many elements to enter?");
scanf("%d", &n);
for (i=1; i<=n; i++)
{
if (i == 0)
{
head = (struct Node*)malloc(sizeof(struct Node));
ptr = head;
}
else
{
ptr->next = (struct Node*)malloc(sizeof(struct Node));
ptr = ptr->next;
}
printf(“Enter the value of %d node: “, i)
scanf("%d",&ptr->data);
}
ptr->next = head;
return(head);
} Pandu Sowkuntla
26
Data Structures--> Unit-II: Operations on CLL
Operations on Circular Linked Lists (CLL)

head 10 20 40 55 70

void search_CLL(struct Node *head)


{
struct Node *ptr;
int key, count=0;
printf(“Enter an element to search:”);
scanf(“%d”,&key);
if(head->data==key){
printf(“Found”); return; }
for(ptr = head->next; ptr!= head; ptr = ptr->next)
{
void display_CLL(struct Node *head)
if ((ptr->data)==key){
{
count=1;
struct Node *ptr;
break;
printf("\nNode data %d", head->data);
}
for(ptr=head->next; ptr!= head; ptr = ptr->next)
}
{
if(count==1)
printf("\nNode data=%d", ptr->data);
printf(“found”);
}
else
printf ("\n");
printf(“not found”);
} 27
} Data Structures--> Unit-II: Pandu Sowkuntla
Inserting node at beginning of CLL

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:

You might also like