Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Data Structure Practical Code

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Data Structure Practical Code

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

DS PRACTICALS

1. STACK(PUSH, POP, DISPLAY):


#include <stdio.h>
int stack[100], top, choice, n, x, i;
void push(void)
{
if (top >= n - 1)
{
printf("stack is overflow");-
}
else
{
printf("Enter the value of stack:");
scanf("%d", &x);
top++;
stack[top] = x;
}
}
void pop(void)
{
if (top < -1)
{
printf("stack is underflow");
}
else
{
printf("The pop element :%d ", stack[top]);
top--;
}
}
void display(void)
{
if (top == -1)
{
printf("stack is underflow");
}
else
{
printf("Dispaly the stack is :");
for (i = 0; i < n; i++)
{
printf("%d/n ", stack[i]);
}
}
}

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

Enter the choice:1


Enter the value of stack:20

Enter the choice:1


Enter the value of stack:30

Enter the choice:2


The pop element :30
Enter the choice:2
The pop element :20
Enter the choice:3
Dispaly the stack is :102030

2. QUEUE(INSERTION, DELETION, DISPLAY):-


#include <stdio.h>
int queue[100], rear = 0, front=0,choice, n, x, i;
void Insertion(void)
{
if (rear >= n)
{
printf("stack is overflow");
}
else
{
printf("Enter the value of queue:");
scanf("%d", &x);
rear++;
front=1;
queue[rear] = x;
}
}
void Deletion(void)
{
if (front == 0 || front > rear)
{
printf("stack is underflow");
}
else
{
printf("The pop element :%d ",queue[front]);
front++;
}
}
void Display(void)
{
if (front == 0)
{
printf("stack is underflow");
}
else
{
for ( i=front; i<=rear; i++)
{
printf("Dispaly the queue is :");
printf("%d\n ",queue[i]);
}

}
}

int main()
{

printf("\n Enter the size of stack[max=100]:");


scanf("%d", & n);
printf(" stack opretion using array:");
printf("\n 1.Insertion 2.Deletion 3.Display 4.exit ");
do
{
printf("\n Enter the choice:");
scanf("%d", & choice);
switch (choice)
{
case 1:
{
Insertion();
break;
}
case 2:
{
Deletion();
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.Insertion 2.Deletion 3.Display 4.exit
Enter the choice:1
Enter the value of queue:10

Enter the choice:1


Enter the value of queue:20

Enter the choice:1


Enter the value of queue:30

Enter the choice:1


stack is overflow
Enter the choice:2
The pop element :10
Enter the choice:2
The pop element :20
Enter the choice:3
Dispaly the queue is :30

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>

int a[100], temp, i, j, n, min;


int main()
{
// Prompt for size of the array
printf("Enter the size of the array: ");
scanf("%d", &n);

// Check if the size of the array is valid


if (n > 100 || n <= 0) {
printf("Invalid size! Please enter a size between 1 and 100.\n");
return 1;
}

// Input the array elements


printf("Enter the array elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}

// Selection sort logic


for (i = 0; i < n - 1; i++) {
min = i; // Assume the current index is the minimum
for (j = i + 1; j < n; j++) { // Iterate through unsorted portion
if (a[j] < a[min]) { // Find the index of the smallest element
min = j;
}
}

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

// Print the sorted array


printf("Sorted array:\n");
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}

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;

while (low <= high)


{
int mid = (low + high) / 2; // Calculate the middle index
if (array[mid] == target)
{
printf("Element found at index %d\n", mid);
return 0; // Exit after finding the element
}
if (array[mid] < target)
{
low = mid + 1; // Search in the right half
}
else
{
high = mid - 1; // Search in the left half
}
}
printf("Element not found\n"); // If the loop ends without finding the
element
return 0;
}

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;

// Linear Search Logic


for (int i = 0; i < size; i++) {
if (array[i] == target) {
result = i; // Target found
break;
}
}
// Output the result
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found\n");
}

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()
{

struct Node *first = NULL;


struct Node *second = NULL;
struct Node *third = NULL;
first = (struct Node *)malloc(sizeof(struct Node));
second = (struct Node *)malloc(sizeof(struct Node));
third = (struct Node *)malloc(sizeof(struct Node));
first->data = 10;
first->next = second;

second->data = 20;
second->next = third;
third->data = 30;
third->next = NULL;

insertAtBeginning(&first, 5);
struct Node *temp = first;

while (temp != NULL)


{
printf("Data: %d\n", temp->data);
temp = temp->next;
}
return 0;
}

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()
{

struct Node *head = NULL;


struct Node *second = NULL;
struct Node *third = NULL;
head = (struct Node *)malloc(sizeof(struct Node));
second = (struct Node *)malloc(sizeof(struct Node));
third = (struct Node *)malloc(sizeof(struct Node));
head->data = 10;
head->next = second;

second->data = 20;
second->next = third;

third->data = 30;
third->next = NULL;

insertAtLast(&head, 40);
struct Node *temp = head;

while (temp != NULL)


{
printf("Data: %d\n", temp->data);
temp = temp->next;
}
return 0;
}
OUTPUT:-
Data: 10
Data: 20
Data: 30
Data: 40

8. SLINKLIST DELETION:-
1.delete at beginning:-
#include <stdio.h>
#include <stdlib.h>

struct Node
{
int data;
struct Node *next;
};
int main()
{

struct Node *first = NULL;


struct Node *second = NULL;
struct Node *third = NULL;
first = (struct Node *)malloc(sizeof(struct Node));
second = (struct Node *)malloc(sizeof(struct Node));
third = (struct Node *)malloc(sizeof(struct Node));
first->data = 10;
first->next = second;

second->data = 20;
second->next = third;

third->data = 30;
third->next = NULL;

printf("Linked list before deletion the last node \n");


struct Node *temp = first;
while (temp != NULL)
{
printf("Data: %d\n", temp->data);
temp = temp->next;
}
if(first != NULL){
struct Node* temp=first;
first =first->next;

printf("\nLinked List after deleting the first node:\n");


temp = first;
while (temp != NULL)
{
printf("Data: %d\n", temp->data);
temp = temp->next;
}
return 0;
}

OUTPUT:-

Linked list before deletion the last node


Data: 10
Data: 20
Data: 30
Linked List after deleting the first node:
Data: 20
Data: 30

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()
{

struct Node *first = NULL;


struct Node *second = NULL;
struct Node *third = NULL;
first = (struct Node *)malloc(sizeof(struct Node));
second = (struct Node *)malloc(sizeof(struct Node));
third = (struct Node *)malloc(sizeof(struct Node));
first->data = 10;
first->next = second;

second->data = 20;
second->next = third;

third->data = 30;
third->next = NULL;

printf("Linked list before deletion the last node \n");


struct Node *temp = first;
while (temp != NULL)
{
printf("Data: %d\n", temp->data);
temp = temp->next;
}
deleteLastNode(&first);

printf("\nLinked List after deleting the last node:\n");


temp = first;
while (temp != NULL)
{
printf("Data: %d\n", temp->data);
temp = temp->next;
}
return 0;
}

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

You might also like