Data Structure Practical Code
Data Structure Practical Code
int main()
{
top = -1;
printf("\n Enter the size of stack[max=100]:");
scanf("%d", &n);
printf(" stack opretion using array:");
printf("\n 1.push 2.pop 3.display 4.exit ");
do
{
printf("\n Enter the choice:");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("exit statement");
break;
}
default:
{
printf("please enter the valid value for stack!");
break;
}
}
} while (choice != 4);
{
return 0;
}
}
OUTPUT:-
Enter the size of stack[max=100]:3
stack opretion using array:
1.push 2.pop 3.display 4.exit
Enter the choice:1
Enter the value of stack:10
}
}
int main()
{
default:
{
printf("please enter the valid value for stack!");
break;
}
}
}
while (choice!=4);
{
return 0;
}
}
OUTPUT:-
Enter the size of stack[max=100]:3
stack opretion using array:
1.Insertion 2.Deletion 3.Display 4.exit
Enter the choice:1
Enter the value of queue:10
3. BUBBLE SORTING:-
#include <stdio.h>
void bubbleSort(int arr[], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main()
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
OUTPUT:-
Sorted array:
11 12 22 25 34 64 90
4.SELECTION SORTING:-
#include <stdio.h>
// Swap the found minimum element with the first element of the
unsorted portion
if (min != i) {
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
OUTPUT:-
Enter the size of the array: 3
Enter the array elements:
3
2
1
Sorted array:
123
5. BINARY SEARCH:-
#include <stdio.h>
int main()
{
int array[] = {2, 4, 6, 8, 10, 12, 14, 16};
int size = sizeof(array) / sizeof(array[0]);
int target = 10;
int low = 0, high = size - 1;
OUTPUT:-
Element found at index 4
6. LINEAR SEARCH:-
#include <stdio.h>
int main() {
int array[] = {2, 4, 6, 8, 10, 12, 14, 16};
int size = sizeof(array) / sizeof(array[0]);
int target = 10;
int result = -1;
return 0;
}
OUTPUT:-
Element found at index 4
7.SLINKLIST INSERTION:-
1.insert at beginning:-
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void insertAtBeginning(struct Node **head_ref, int new_data)
{
struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = *head_ref;
*head_ref = new_node;
}
int main()
{
second->data = 20;
second->next = third;
third->data = 30;
third->next = NULL;
insertAtBeginning(&first, 5);
struct Node *temp = first;
OUTPUT:-
Data: 5
Data: 10
Data: 20
Data: 30
2.insert at last:-
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void insertAtLast(struct Node **head_ref, int new_data)
{
struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));
struct Node* last=*head_ref;
while(last->next!=NULL){
last =last->next;
}
last->next=new_node;
new_node->data = new_data;
new_node->next =NULL;
}
int main()
{
second->data = 20;
second->next = third;
third->data = 30;
third->next = NULL;
insertAtLast(&head, 40);
struct Node *temp = head;
8. SLINKLIST DELETION:-
1.delete at beginning:-
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
int main()
{
second->data = 20;
second->next = third;
third->data = 30;
third->next = NULL;
OUTPUT:-
2.delete at last:-
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void deleteLastNode(struct Node **head)
{
struct Node *temp = *head;
while (temp->next->next != NULL)
{
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
}
int main()
{
second->data = 20;
second->next = third;
third->data = 30;
third->next = NULL;
OUTPUT:-
Linked list before deletion the last node
Data: 10
Data: 20
Data: 30
Linked List after deleting the last node:
Data: 10
Data: 20