0% found this document useful (0 votes)
15 views15 pages

Data Structure

Bscit data structure practical

Uploaded by

Vivek Naik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
15 views15 pages

Data Structure

Bscit data structure practical

Uploaded by

Vivek Naik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 15

Write a program to implement Bubble sort and selection sort

Souece code:
Bubble sort:
#include<stdio.h>
void swap(int *arr,int i,int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
void bubble_sort(int *arr,int num){
for(int i=0;i<num;i++){
for(int j=0;j<num-i-1;j++){
if(arr[j]>arr[j+1]){
swap(arr,j,j+1);
}
}
}
}
void display(int* arr,int num){
for(int i = 0;i < num;i++){
printf("%d ",arr[i]);
}
}
int main(){

int num;
printf("Enter size of Array : ");
scanf("%d",&num);
int arr[num];
for(int i=0;i<num;i++){
printf("Enter element : ");
scanf("%d",&arr[i]);
}

display(arr,num);
bubble_sort(arr,num);
printf("\n");
display(arr,num);
return 0;
}
Sselection sort:
Source code:
#include<stdio.h>
void bubble_sort(int *arr,int num){
for (int i = 0; i < num; i++) {
int min_idx = i;
for (int j = i + 1; j < num; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}

}
void display(int* arr,int num){
for(int i = 0;i < num;i++){
printf("%d ",arr[i]);
}
}
int main(){

int num;
printf("Enter size of Array : ");
scanf("%d",&num);
int arr[num];
for(int i=0;i<num;i++){
printf("Enter element : ");
scanf("%d",&arr[i]);
}

display(arr,num);
bubble_sort(arr,num);
printf("\n");
display(arr,num);
return 0;
}
insertion sort:
Source code:
#include<stdio.h>
void display(int* arr,int num){
for(int i = 0;i < num;i++){
printf("%d ",arr[i]);
}
}
void insertion_sort(int* arr,int num){
for (int i=1;i<num;i++)
{
int j = i;
while ( j>0 && arr[j - 1] > arr[j])
{
int temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
j--;
}
}
}
int main(){

int num;
printf("Enter size of Array : ");
scanf("%d",&num);
int arr[num];
for(int i=0;i<num;i++){
printf("Enter element : ");
scanf("%d",&arr[i]);
}

display(arr,num);
printf("\n");
insertion_sort(arr,num);
display(arr,num);

return 0;
}
write a program to implement the concept of queue with insert,delete,display
and exit operation
Source code:
#include <stdio.h>
#define MAX_SIZE 5

int queue[MAX_SIZE];
int front = -1, rear = -1;

void enqueue(int item) {


if (rear == MAX_SIZE - 1) {
printf("Overflow! Cannot insert %d\n", item);
} else {
if (front == -1) front = 0;
queue[++rear] = item;
printf("%d inserted into the queue.\n", item);
}
}
void dequeue() {
if (front == -1 || front > rear) {
printf("Underflow! Queue is empty.\n");
} else {
printf("%d removed from the queue.\n", queue[front++]);
if (front > rear) front = rear = -1; // Reset the queue if empty
}
}
void display() {
if (front == -1) {
printf("The queue is empty.\n");
} else {
printf("Queue elements:\n");
for (int i = front; i <= rear; i++) {
printf("%d\n", queue[i]);
}
}
}
int main() {
int choice, item;

while (1) {
printf("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the item to enqueue: ");
scanf("%d", &item);
enqueue(item);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
}

..
Write a program to implement the concept of stack with push,pop,display and
exit operation
Source code:
#include <stdio.h>
#define MAX_SIZE 5

int stack[MAX_SIZE];
int top = -1;

void push(int item) {


if (top == MAX_SIZE - 1) {
printf("Overflow and Exit\n");
} else {
stack[++top] = item;
printf("%d item inserted\n", item);
}
}

void pop() {
if (top == -1) {
printf("Underflow & Exit\n");
} else {
int item = stack[top--];
printf("%d item deleted\n", item);
}
}

void display() {
if (top == -1) {
printf("The stack is empty\n");
} else {
printf("Stack elements:\n");
for (int i = 0; i <= top; i++) {
printf("%d\n", stack[i]);
}
}
}

int main() {
int choice, item;
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
while (1) {
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the item: ");
scanf("%d", &item);
push(item);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid choice\n");
}
}
return 0;
}
Write a program to create a tree and display the elements
Source code:
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *left;
struct Node *right;
};
struct Node* create_node(int val)
{
struct Node* node =(struct Node *)malloc(sizeof(struct Node));

node->data= val;
node->left = NULL;
node->right= NULL;
}
void display_tree(struct Node *node)
{
if(node == NULL)
return;
printf("%d ",node->data);
display_tree(node->left);
display_tree(node->right);
}
int main()
{
printf("Tree elemnts are:: \n");
struct Node* root =create_node(7);
root->left =create_node(2);
root->right =create_node(6);
root->left->left =create_node(5);
root->left->right=create_node(8);
root->right->left =create_node(9);
root->right->right =create_node(4);

display_tree(root);

}
Write a program for inorder , Postorder and preorder traversal of tree
Source code:
#include<stdio.h>
#include<stdlib.h>

struct Node{
int data;
struct Node* left;
struct Node* right;
};
struct Node* create_node(int val){
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = val;
node->left = NULL;
node->right = NULL;

return node;
}
void inorderTraversal(struct Node* root){
if(root == NULL )return;
inorderTraversal(root->left);
printf("%d->",root->data);
inorderTraversal(root->right);
}
void preorderTraversal(struct Node* root){
if(root == NULL )return;
printf("%d->",root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
void postorderTraversal(struct Node* root){
if(root == NULL )return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d->",root->data);
}
struct Node* insertLeft(struct Node* root,int value){
root->left = create_node(value);
return root->left;
}
struct Node* insertRight(struct Node* root,int value){
root->right = create_node(value);
return root->right;
}
int main(){
struct Node* root = create_node(1);
insertLeft(root,12);
insertRight(root,9);

insertLeft(root->left,5);
insertRight(root->left,6);

printf("Inoder traversal : ");


inorderTraversal(root);

printf("\nPreoder traversal : ");


preorderTraversal(root);

printf("\nPostorder traversal : ");


postorderTraversal(root);

return 0;
}
Write a program to store the elements in 1-D array and perform the operations
like searching,sorting and reversing the elements.
Source Code:
#include<iostream>
int main()
{
int myArray[5]={12,34,32,41,10};
int Search=1;
for (int i=0;i<5;++i)
{
std::cout<<"Element in index"<<": "<<myArray[i]<<std::endl;
if(myArray[i]==Search)
{
std::cout<<"Element found in the given array"<<i<<std::endl;
}
}
for(int i=0;i<5/2;++i)
{
std::swap(myArray[i],myArray[5-i-1]);
}
std::cout<<"Reversed array:";
for(int i=0;i<5;++i)
{
std::cout<<myArray[i]<<" ";
}
for(int i=0;i<5-1;++i)
{
for(int j=i+1;j<5;++j)
{
if(myArray[i]>myArray[j])
{
std::swap(myArray[i],myArray[j]);
}
}
}
std::cout<<"\nSorting array:";
for(int i=0;i<5;++i)
{
std::cout<<myArray[i]<<" ";
}
return 0;
}
Write a program to create a single linked list and display the node
elements in revers order.
Source Code:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
void createlist(int n,node* start)
{
struct node *temp,*new_node;
int num,i;
if(start==NULL)
{
printf("Memory can not be allocated");

}
else
{
printf("Enter data of first node: ");
scanf("%d",&num);
start->data=num;
start->next=NULL;
temp=start;
for(i=2;i<=n;i++)
{
struct node *new_node=(struct node *)malloc(sizeof(struct
node));
if (new_node==NULL)
{
printf("Memory can not be allocated");
break;
}
else
{
printf("Enter data of next node: ");
scanf("%d",&num);
new_node->data=num;
new_node->next=NULL;
temp->next=new_node;
temp=temp->next;
}
}
}
}
void displaylist(node *start)
{
node *temp=start;
while (temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
}
int main()
{
struct node *start=(struct node*)malloc(sizeof(struct node));
createlist(3,start);
displaylist(start);

}
Write a program to search the element in the linked list and display the same.
Source code:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
void createlist(int n,node* start)
{
struct node *temp,*new_node;
int num,i;
if(start==NULL)
{
printf("Memory can not be allocated");

}
else
{
printf("Enter data of first node: ");
scanf("%d",&num);
start->data=num;
start->next=NULL;
temp=start;
for(i=2;i<=n;i++)
{
struct node *new_node=(struct node *)malloc(sizeof(struct node));
if (new_node==NULL)
{
printf("Memory can not be allocated");
break;
}
else
{
printf("Enter data of next node: ");
scanf("%d",&num);
new_node->data=num;
new_node->next=NULL;
temp->next=new_node;
temp=temp->next;
}
}
}
}
void displaylist(node *start)
{
node *temp=start;
while (temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
}
int searchNode(node *start, int value)
{
node *temp=start;
while (temp!=NULL)
{
if (temp->data == value)
return 1;
temp=temp->next;
}
return 0;
}
int main()
{
struct node *start=(struct node*)malloc(sizeof(struct node));
createlist(3,start);
displaylist(start);

int value = 47;


if(searchNode(start, value))
{
printf("\nElement %d found in the linked list\n",value);
}
else
{
printf("\nElement %d not found in the linked list\n",value);
}
}

You might also like