Assignment of Data Structures
Assignment of Data Structures
USING C
#include <stdio.h>
#include <stdlib.h>
// Main function
int main() {
struct Node* head = NULL;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Main function
int main() {
struct Node* head = NULL;
return 0;
}
PROGRAM TO CREATE LINKED LIST AND INSERT THE ELEMENT AT FIRST, LAST AND MIDDLE OF
LINKED LIST
Output:
#include <stdio.h>
#include <stdlib.h>
// Main function
int main() {
struct Node* head = NULL;
return 0;
}
PROGRAM TO CREATE A LINKED LIST AND DELETE THE ELEMENT FROM FIRST, MIDDLE AND LAST
LOCATION
Output:
#include <stdio.h>
#include <stdlib.h>
// Main function
int main() {
struct Node* head = NULL;
return 0;
}
WRITE A PROGRAM TO CREATE A LINKED LIST AND COUNT THE NUMBER OF ELEMENTS IN THE
LINKED LIST
Output:
#include <stdio.h>
#include <stdlib.h>
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
return;
}
last->next = newNode;
}
int main() {
struct Node* head = NULL;
int item, position;
if (position == -1)
printf("Item not found in the list.\n");
else
printf("Position of %d in the list: %d\n", item, position);
return 0;
}
WAP TO CREATE A LINKED LIST AND FIND THE POSITION OF ITEM ENTERED BY USER
Output:
Linked List: 10 20 30 40 50
Enter the item to find its position: 30
Position of 30 in the list: 3
PROGRAM TO SORT LINKED LIST IN ASCENDING ORDER
Input:
#include <stdio.h>
#include <stdlib.h>
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
return;
}
last->next = newNode;
}
if (*head == NULL)
return;
int main() {
struct Node* head = NULL;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
return;
}
last->next = newNode;
}
*head = prev;
}
if (*head == NULL)
return;
for (current = *head; current != NULL; current = current->next) {
for (index = current->next; index != NULL; index = index->next) {
if (current->data < index->data) {
temp = current->data;
current->data = index->data;
index->data = temp;
}
}
}
}
int main() {
struct Node* head = NULL;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
newNode->data = data;
newNode->next = *head;
if (*head != NULL) {
while (temp->next != *head)
temp = temp->next;
temp->next = newNode;
} else
newNode->next = newNode; // For the first node
*head = newNode;
}
if (head != NULL) {
do {
printf("%d ", current->data);
current = current->next;
} while (current != head);
}
printf("\n");
}
int main() {
struct Node* head = NULL;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
return;
}
last->next = newNode;
}
return count;
}
int main() {
struct Node* head = NULL;
return 0;
}
Linked List: 10 20 30 40 50
Number of elements in the linked list: 5
PROGRAM TO CONCATENATE TWO LISTS
Input:
#include <stdio.h>
#include <stdlib.h>
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
return;
}
last->next = newNode;
}
current->next = list2;
}
int main() {
struct Node* list1 = NULL;
struct Node* list2 = NULL;
return 0;
}
First List: 10 20 30
Second List: 40 50
Concatenated List: 10 20 30 40 50
PROGRAM TO CREATE A STACK AND APPLY PUSH AND POP OPERATION
Input:
#include <stdio.h>
#include <stdlib.h>
int main() {
struct Stack stack;
initializeStack(&stack);
return 0;
}
Stack: 10 20 30
Popped value: 30
Stack: 10 20
PROGRAM TO IMPLEMENT FACTORIAL OF A NUMBER USING RECURSION
Input:
#include <stdio.h>
// Function prototype
int factorial(int n);
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
Enter a number: 5
Factorial of 5 = 120
WAP FOR FIBONACCI SERIES THROUGH RECURSION
Input:
#include <stdio.h>
// Function prototype
int fibonacci(int n);
int main() {
int terms;
printf("Enter the number of terms: ");
scanf("%d", &terms);
#include <stdio.h>
#include <stdlib.h>
return root;
}
int main() {
struct Node* root = NULL;
int n, value;
return 0;
}
#include <stdio.h>
int main() {
int n, i;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
return 0;
}
#include <stdio.h>
int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Unsorted array: \n");
printArray(arr, n);
quickSort(arr, 0, n - 1);
Unsorted array:
10 7 8 9 1 5
Sorted array:
1 5 7 8 9 10
PROGRAM TO IMPLEMENT BUBBLE SORT
Input:
#include <stdio.h>
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Unsorted array: ");
printArray(arr, n);
bubbleSort(arr, n);
Unsorted array: 64 34 25 12 22 11 90
Sorted array: 11 12 22 25 34 64 90
PROGRAM FOR LINEAR SEARCH
Input:
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50, 60};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 30;
if (result != -1) {
printf("Element %d found at index %d\n", key, result);
} else {
printf("Element %d not found in the array\n", key);
}
return 0;
}
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50, 60};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 30;
if (result != -1) {
printf("Element %d found at index %d\n", key, result);
} else {
printf("Element %d not found in the array\n", key);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
newNode->prev = NULL;
*head = newNode;
return;
}
last->next = newNode;
newNode->prev = last;
}
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 1);
insertAtEnd(&head, 2);
insertAtEnd(&head, 3);
displayList(head);
return 0;
}