Singly Linked List
Singly Linked List
Array of structures
Abstract Data Types
The data structures along with their operations are called
Abstract Data Types (ADTs).
An ADT consists of two parts:
1.Declaration of data
2.Declaration of operations
Linked list
Is linked list an ADT?
Singly linked list
struct Node {
int data;
struct Node* next;
};
Create a new node
struct Node* createNode(______________) {
struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));
if (_____________________) {
printf("Memory allocation error\n");
exit(1);
}
}
printf("NULL\n");
}
Insertion at end in singly linked list
void insertAtEnd(______________________, int
data) {
struct Node* newNode = createNode(data);
if (______________) { //If list is empty
}
struct Node* temp = *head;
while (________________) { //Go till the last
node
}
//Attach new node at the end
Insertion in the beginning in singly
linked list
Deletion in singly linked list (based on
position)
void deleteNode(struct Node** head, int position)
if (temp
{ == NULL) {
struct Node* temp = *head; printf("Key not found\n");
struct Node* prev = NULL; return;
if (position == 1) { //If it is first position }
//Free the memory and
adjust the pointer
}
int count = 1;
while (temp != NULL && count<position) {
}
Deletion in singly linked list (based on data):
If data is found, return the first position.
If data is not found, return -1.
Search in singly linked list:
If data is found, return the position.
If data is not found, return -1.
Count the number of elements in a
linked list
Free the entire linked list
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 10);
Write the output insertAtEnd(&head, 20);
insertAtBeginning(&head, 5);
printList(head);
deleteNode(&head, 20);
printList(head);
Int position = searchNode(head, 10);
if (position != -1) {
printf("Node found at position: %d\n",
position);
}
else {
printf("Node not found\n");
}
freeList(head);
return 0;
}