Data Structures Record 2
Data Structures Record 2
NO : 7
AIM:
To write a C program to implement singly and doubly linked lists.
ALGORITHM:
Singly Linked List Algorithm:
Step 1: Start.
Step 2: Create a new node by allocating memory and initializing its data field with the
given data.
Step 3: Insert the new node at the beginning of the list by updating the head pointer.
Step 4: Insert the new node at the end of the list by traversing the list and updating the
next pointer of the last node.
Step 5: Delete a node from the list by updating the next pointer of the previous node to
skip over the node to be deleted.
Step 6: Display the list by traversing the list and printing the data of each node.
Step 7: End.
Doubly Linked List Algorithm:
Step 1: Start.
Step 2: Create a new node by allocating memory and initializing its data field with the
given data.
Step 3: Insert the new node at the beginning of the list by updating the head pointer and
setting the previous pointer of the new node to NULL.
Step 4: Insert the new node at the end of the list by traversing the list and updating the
next pointer of the last node, and setting the previous pointer of the new node to the last
node.
Step 5: Delete a node from the list by updating the next pointer of the previous node to
skip over the node to be deleted, and updating the previous pointer of the next node to
point to the previous node.
Step 6: Display the list by traversing the list and printing the data of each node, which
can be done in both forward and backward directions using the next and previous
pointers.
Step 7: End.
PROGRAM 1:
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
int choice, data, pos;
while (1) {
printf("Singly Linked List");
printf("\n1. Insert\n2. Delete\n3. Print\n4. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data: ");
scanf("%d", &data);
printf("Enter position (0 for beginning, -1 for end , or specific index position): ");
scanf("%d", &pos);
insert(&head, data, pos);
break;
case 2:
printf("Enter position to delete (0 for beginning, -1 for end, or specific index position):
");
OUTPUT:
scanf("%d", &pos);
deleteNode(&head, pos);
break;
case 3:
printList(head);
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
PROGRAM 2 :
#include <stdio.h>
#include <stdlib.h>
// Delete node at a specific position (0 for beginning, -1 for end, any other value for middle)
void deleteNode(struct Node** head, int position) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
if (position == 0) { // Delete from beginning
*head = temp->next;
if (*head) (*head)->prev = NULL;
free(temp);
return;
}
int i = 1;
if (position == -1) { // Delete from the end
while (temp->next) temp = temp->next;
} else { // Delete from a specific middle position
while (temp->next && i < position) temp = temp->next, i++;
}
if (temp->prev) temp->prev->next = temp->next;
if (temp->next) temp->next->prev = temp->prev;
free(temp);
}
int main() {
struct Node* head = NULL;
int choice, data, pos;
while (1) {
printf("\n Doubly Linked List\n1. Insert\n2. Delete\n3. Print Forward\n4. Print Backward\n5.
Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data: ");
scanf("%d", &data);
printf("Enter position (0 for beginning, -1 for end, or specific position): ");
scanf("%d", &pos);
insert(&head, data, pos);
break;
case 2:
printf("Enter position to delete (0 for beginning, -1 for end, or specific position): ");
scanf("%d", &pos);
deleteNode(&head, pos);
break;
case 3:
printListForward(head);
break;
case 4:
printListBackward(head);
break;
case 5:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
RESULT :
Thus the C program to implement singly and doubly linked lists was successfully
executed and verified.
EXP.NO : 8
IMPLEMENTATION PRIORITY QUEUE
DATE :
AIM :
To write a C program to implement Priority Queue.
ALGORITHM :
Step 1 ; Start.
Step 2: Create an empty priority queue.
Step 3: Enqueue: Create a new node with the given element and priority, and insert it
into the queue in the correct position based on its priority.
Step 4: Dequeue: Remove the node with the highest priority from the queue, update the
head of the queue to point to the next node, and return the removed node's element.
Step 5: Peek: Return the element of the node with the highest priority without removing it
from the queue.
Step 6: Display: Traverse the queue and print the elements in the order of their priority,
along with their priority.
Step 7: End.
OUTPUT :
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
int main() {
PriorityQueue* queue = createPriorityQueue();
int choice, data, priority;
while (1) {
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Peek\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data: ");
scanf("%d", &data);
printf("Enter priority: ");
scanf("%d", &priority);
enqueue(queue, data, priority);
break;
case 2:
printf("Dequeued element: %d\n", dequeue(queue));
break;
case 3:
printf("Front element: %d\n", peek(queue));
break;
case 4:
display(queue);
break;
case 5:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
RESULT :
Thus the C program to implement Priority Queue was successfully executed and
verified.
EXP.NO : 9
IMPLEMENTATION OF BINARY TREE
DATE :
AIM :
To write a C program to implement Binary Tree.
ALGORITHM :
Step 1 : Start.
Step 2: Start by checking if the tree is empty for relevant operations (e.g., traversal, find
min/max). If yes, output that the tree is empty.
Step 3: To insert a node, traverse from the root:
o If NULL, create a new node.
o Move left if data is less; move right if data is greater. Repeat until insertion point
is found.
Step 4: To delete a node, find the node:
o If data matches:
int main() {
Node* root = NULL;
int choice, data;
while (1) {
printf(“Binary Tree\n”);
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Inorder Traversal\n");
printf("4. Preorder Traversal\n");
printf("5. Postorder Traversal\n");
printf("6. Find Min\n");
printf("7. Find Max\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data: ");
scanf("%d", &data);
root = insertNode(root, data);
break;
case 2:
printf("Enter data: ");
scanf("%d", &data);
root = deleteNode(root, data);
break;
case 3:
inorderTraversal(root);
printf("\n");
break;
case 4:
preorderTraversal(root);
printf("\n");
break;
case 5:
postorderTraversal(root);
printf("\n");
break;
case 6:
printf("Minimum value: %d\n", findMin(root));
break;
case 7:
printf("Maximum value: %d\n", findMax(root));
break;
case 8:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
RESULT :
Thus the C program for Binary Tree was successfully executed and verified.
EXP.NO : 10
AIM :
To write a C program to implement Binary Search Tree.
ALGORITHM :
Step 1: Start .
Step 2: Take user input for the menu choice.
Step 3: Based on the user's choice, perform the corresponding operation:
Insert a node:
o Check if the tree is empty (root == NULL). If true, create a new node as the root.
Move left if the data is less than the current node’s data.
Move right if the data is greater than the current node’s data.
Insert the new node at the found position.
Delete a node:
o Find the node to be deleted by comparing data:
Step 4: Print the result of the operation (e.g., node data for traversal, min/max value).
Step 5: Repeat the process until the user selects the option to exit.
Step 6: If the user inputs an invalid choice, display an error message and prompt for input
again.
Step 7: End.
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
int main() {
Node* root = NULL;
int choice, data;
while (1) {
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Inorder Traversal\n");
printf("4. Preorder Traversal\n");
printf("5. Postorder Traversal\n");
printf("6. Find Min\n");
printf("7. Find Max\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data: ");
scanf("%d", &data);
root = insertNode(root, data);
break;
case 2:
printf("Enter data: ");
scanf("%d", &data);
root = deleteNode(root, data);
break;
case 3:
inorderTraversal(root);
printf("\n");
break;
case 4:
preorderTraversal(root);
printf("\n");
break;
case 5:
postorderTraversal(root);
printf("\n");
break;
case 6:
printf("Min value: %d\n", findMin(root));
break;
case 7:
printf("Max value: %d\n", findMax(root));
break;
case 8:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
RESULT :
Thus the C program for Binary Search Tree was successfully executed and verified.