Linked List
Linked List
in c
A linked list is a dynamic data structure used to store and manipulate collections
of elements. Learn about their advantages, disadvantages, and how they're useful
in programming.
NAME:-Basir Khan
Regd:-220301120209
Sec:-D
Contents:-
A B C
Types of Linked List
Singly Linked List
A linked list where each node contains a reference to the next node in the list.
A linked list where each node contains references to both the next and previous nodes in the list.
A linked list where the last node in the list points back to the first node, forming a loop.
Types of Linked List Circular
Linked
List
head
A B C
Doubly
Linked
List
head tail
A B C
Advantages and Disadvantages of
Linked List
A music playlist, where each song is connected Task management applications that keep track
to the next song. of to-do lists or project tasks.
Operations on Linked List
1 Insertion
Add a new node to the linked list at a specified position or at the beginning/end of
the list.
2 Deletion
Remove a node from the linked list at a specified position or based on a given value.
3 Traversal
Visit each node in the linked list in a sequential manner and perform operations on
the data.
Algoritm’s and examples
struct Node * insertAtFirst(struct Node *head,int data){ struct Node *insertAtIndex(struct Node *head,int
struct Node *ptr = (struct Node *)malloc(sizeof(struct data,int index){
Node)); struct Node *ptr = (struct Node *)malloc(sizeof(struct
ptr->data=data; Node));
ptr->next=head; struct Node *p = head;
return ptr; int i =0;
} while (i!=index-1)
{
p=p->next;
Insert after a node in Linked List i++;
}
struct Node *insertAfterNode(struct Node *head,struct Node ptr->data=data;
*prevNode,int data){ ptr->next=p->next;
struct Node *ptr =(struct Node *)malloc(sizeof(struct p->next=ptr;
Node)); return head;
ptr->data=data;
ptr->next=prevNode->next; }
prevNode->next=ptr;
return head;
}
Algoritm’s and examples
struct Node *insertAfterNode(struct Node *head,struct struct Node *insertAtEnd(struct Node *head,int data){
Node *prevNode,int data){ struct Node *ptr =(struct Node *)malloc(sizeof(struct
struct Node *ptr =(struct Node *)malloc(sizeof(struct Node));
Node)); ptr->data=data;
ptr->data=data; struct Node *p = head;
ptr->next=prevNode->next; while(p->next!=NULL){
prevNode->next=ptr; p=p->next;
return head; }
} p->next=ptr;
ptr->next=NULL;
return head;
}
Algoritm’s and examples
Delete at first in Linked List Delete end node in Linked List
struct Node *deleteFirst(struct Node *head){ struct Node *deleteEnd(struct Node *head){
struct Node *p = head; struct Node *p= head;
head =head->next; struct Node *q =head->next;
free(p); while(q->next!=NULL){
return head; p=p->next; Delete Given Key in Linked List
} q=q->next;
} struct Node *deleteAtGivenKey(struct Node *head,int
Delete at index in Linked List p->next=NULL; value){
free(q); struct Node *p =head;
return head; struct Node *q =head->next;
struct Node *deleteAtIndex(struct Node *head,int index){ while(q->data!=value&&q->next!=NULL){
struct Node *p = head; }
p=p->next;
struct Node *q = head->next; q=q->next;
for(int i =0;i<index-1;i++){ }
p=p->next; if(q->data==value){
q=q->next; p->next=q->next;
} free(q);
p->next=q->next; return head;
free(q); }
return head; }
}
Conclusion and Summary
Linked lists provide a flexible data structure for efficient manipulation, making them
indispensable in various applications.