0% found this document useful (0 votes)
3 views17 pages

Singly Linked List

The document discusses singly linked lists as a type of Abstract Data Type (ADT), detailing their structure and operations. It includes functions for creating nodes, printing the list, inserting and deleting nodes, searching for data, and counting elements. The main function demonstrates the usage of these operations with example calls to manipulate a linked list.

Uploaded by

mc24bt005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views17 pages

Singly Linked List

The document discusses singly linked lists as a type of Abstract Data Type (ADT), detailing their structure and operations. It includes functions for creating nodes, printing the list, inserting and deleting nodes, searching for data, and counting elements. The main function demonstrates the usage of these operations with example calls to manipulate a linked list.

Uploaded by

mc24bt005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

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

return newNode; //Is this required?


}
Create a new node
struct Node* createNode(______________) {
struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node));
if (_____________________) {
printf("Memory allocation error\n");
exit(1);
}

return newNode; //Is this required?


}
Print a singly linked list
void printList(____________________) {
struct Node* temp = head;
while (___________________) {

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

You might also like