Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
27 views24 pages

DS Programs

Uploaded by

saniyakhoja21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views24 pages

DS Programs

Uploaded by

saniyakhoja21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

1.

// sequential search or linear search


#include<stdio.h>
int main()
{
int n,arr[100],search,flag=0;
printf("enter the size of array \n");
scanf("%d",&n);

for (int i = 0; i < n; i++)// to take input from user


scanf("%d",&arr[i]);

printf("enter the number to search \n"); // taking input to search the value in array
scanf("%d",&search);

for (int i = 0; i < n; i++) // loop to search value in the array


if(search == arr[i])
{
flag =1;
break;
}

if(flag == 0)// if the flag is zero then the value not found in the arr
printf("value not founf in the array \n");
else
printf("the value is found ");

return 0;
}

2. //binary search

#include<stdio.h>

int main()

int n,search,arr[100],beg,end,flag=0;

printf("enter the size of array \n");//to take the size of array

scanf("%d",&n);

printf("enter the your values in array \n");

for (int i = 0; i < n; i++) // to take value from user


scanf("%d",&arr[i]);

printf("enter the number to search \n");// number to search in array

scanf("%d",&search);

beg = 0;

end = n-1;

while(end >= beg)

int mid= (beg + end)/2;

if(arr[mid] == search) // number is found break and flag =1 means true

flag =1;

break;

if( arr[mid] < search )

beg = mid +1;

else

end = mid-1;

if(flag == 0)// if the flag is zero then the value not found in the arr

printf("value not founf in the array \n");

else

printf("the value is found ");

return 0;

2 //bubble sort
#include<stdio.h>
int main()
{
int n,arr[100];

printf("enter the size of array \n");


scanf("%d",&n);

printf("enter the value in the array \n");


for(int i=0;i<n;i++)
scanf("%d",&arr[i]);

for (int i = 0; i < n; i++)


{
for(int j=0 ;j < n-1-i; j++)
if(arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1]=temp;
}

printf("pass %d : ",i+1); // to print the pass


for(int k=0;k<n;k++)
printf("%d ",arr[k]);
printf("\n");

}
printf("the sorted array ");
for (int i = 0; i < n; i++)
printf("%d ",arr[i]);

return 0;
}

3 // selection sort
#include<stdio.h>
int main()
{
int count,arr[100];

printf("enter the size of array \n");


scanf("%d",&count);

printf("enter the values in the array ");


for(int i=0;i<count;i++)
scanf("%d",&arr[i]);
for(int i=0; i < count; i++)
{
int min =i;
int temp = arr[i];

for(int j = i+1; j < count; j++)//to find the minimun mber in the array
if(arr[j] < arr[min])
min =j;

arr[i] = arr[min];
arr[min]=temp;

printf("pass %d : ",i+1); // to print the pass


for(int k=0; k < count; k++)
printf("%d ",arr[k]);
printf("\n");
}

printf("the sorted array \n");


for (int i = 0; i < count; i++)
printf("%d ",arr[i]);

return 0;
}

4 //insertion sort
#include<stdio.h>
int main()
{
int count,arr[100];

printf("enter the size of array \n");


scanf("%d",&count);

printf("enter the values of array \n");


for (int i = 0; i < count; i++)
scanf("%d",&arr[i]);

for (int i = 0; i < count; i++)


{
int temp =arr[i];
int j=i-1;
while(j >= 0 && arr[j] > temp)
{
arr[j+1] = arr[j];
j -=1;
}
arr[j+1]=temp;

printf("pass %d : ",i+1); // to print the pass


for(int k=0; k < count; k++)
printf("%d ",arr[k]);
printf("\n");
}
printf("sorted array \n");

for (int i = 0; i < count; i++)


printf("%d ",arr[i]);

return 0;
}

5 //merge sort
#include<stdio.h>
void merge(int arr[],int beg,int mid,int end)
{
int i,j,k;
int n1= mid-beg+1;
int n2=end-mid;

int l[n1],r[n2];

for(i=0;i<n1;i++)
l[i]=arr[beg+i];
for(j=0;j<n2;j++)
r[j]=arr[mid+1+j];

i=j=0;
k=beg;
while(i < n1 && j < n2)
{
if(l[i] <= r[j])
{
arr[k] = l[i];
i++;
}
else
{
arr[k] =r[j];
j++;
}
k++;
}

while (i < n1)


{
arr[k]=l[i];
i++;
k++;
}
while(j < n2)
{
arr[k]=r[j];
j++;
k++;
}
}
void merge_sort(int arr[],int beg,int end)
{
if(beg < end)
{
int mid=beg+(end-beg)/2;
merge_sort(arr,beg,mid);
merge_sort(arr,mid+1,end);

merge(arr,beg,mid,end);

}
}
int main()
{
int count,arr[100];

printf("enter the size of array \n");


scanf("%d",&count);

printf("enter the value int the array \n");


for (int i = 0; i < count; i++)
scanf("%d",&arr[i]);

merge_sort(arr,0,count-1);

printf("the sorted array \n");


for (int i = 0; i < count; i++)
printf("%d ",arr[i]);

return 0;
}

6 //quick sort
#include<stdio.h>
void swap(int* a,int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[],int low,int high)
{
int pivot=arr[high];
int i=low-1;

for(int j=low; j< high; j++)


if(arr[j] < pivot)
{
i++;
swap(&arr[i],&arr[j]);
}
swap(&arr[i+1],&arr[high]);

return (i+1);
}
void quick_sort(int arr[],int low,int high)
{
if(low < high)
{
int pi=partition(arr,low,high);

quick_sort(arr,low,pi-1);
quick_sort(arr,pi+1,high);
}
}
int main()
{

int count, arr[100];

// Input the size of the array


printf("Enter the size of array: ");
scanf("%d", &count);

// Input the elements of the array


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

// Call quickSort function


quick_sort(arr, 0, count - 1);

// Print the sorted array


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

return 0;

7 // stack using array


#include<stdio.h>
#include<stdlib.h>
#define MAX 100
struct Stack
{
int arr[MAX];
int top;
};
int isFull(struct Stack* stack) // to check the stack is full or not
{
if(stack->top == MAX - 1)
return 1;
return 0;
}
int isEmpty(struct Stack* stack) // to check the stack is empty or not
{
if(stack->top == -1)
return 1;
return 0;
}
struct Stack* create()
{
struct Stack* stack=(struct Stack*)malloc(sizeof(struct Stack));
stack->top=-1;
return stack;
}
void push(struct Stack* stack) // to insert the elements in array
{
if(isFull(stack))
printf("\n\nthe stack is full \n");
else
{

stack->top +=1;
printf("\n\nenter the value to enter in the stack \n");
scanf("%d",&stack->arr[stack->top]);
printf("\n\nthe value inserted \n");
}
}
void pop(struct Stack* stack)
{
if(isEmpty(stack))
printf("\n\nthe stack is empty \n");
else
{
stack->top -=1;
printf("\n\nthe value deleted successfully\n");
}
}
int peep(struct Stack* stack)
{
if(isEmpty(stack))
printf("the stack is empty \n");
else
printf("the top elements in stack %d\n",stack->arr[stack->top]);
return stack->arr[stack->top];
}
int main()
{
int choice;
struct Stack* stack;
while (1)
{
printf(" 1->create Stack \n 2->push \n 3->pop\n 4->peep\n 5->exit\n");
scanf("%d",&choice);
switch (choice)
{
case 1:
stack=create();
printf("\n\nstack created \n");
break;
case 2:
push(stack);
break;
case 3:
pop(stack);
break;
case 4:
peep(stack);
break;
case 5:
exit(0);
break;
default:
printf("enter the invalid choice \n");
break;
}
}
return 0;
}

8 //stack using link list


#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int value;
struct Node* next;
};
struct Stack
{
struct Node* Head;
};
struct Node* create(int newData)//function to creater a node
{
struct Node* node=(struct Node*)malloc(sizeof(struct Node));
node->value=newData;
node->next=NULL;
return node;
}
void initializeStack(struct Stack* stack)
{
stack->Head = NULL;
}
int isEmpty(struct Stack* stack) // to check then head is empty or not
{
return stack->Head == NULL;
}
void push(struct Stack* stack,int value)//to inset the elements in the node
{
struct Node* newNode=create(value);
if(!newNode)
{
printf("\n\n stack overflow");
return ;
}
newNode->next=stack->Head;
stack->Head=newNode;
}
void pop(struct Stack* stack)// to delete the last insert node
{
if(isEmpty(stack))
{
printf("\n\n the stack is underflow \n\n");
return;
}
struct Node* temp=stack->Head;
stack->Head=temp->next;
free(temp);
}
int peek(struct Stack* stack)// to return the top element
{
int value= stack->Head->value;
if(isEmpty(stack))
{
printf("the stack is empty ");
}

else
return value;
}
int main()
{
// Creating a stack
struct Stack stack;
initializeStack(&stack);

// Push elements onto the stack


push(&stack, 11);
push(&stack, 22);
push(&stack, 33);
push(&stack, 44);

// Print top element of the stack


printf("Top element is %d\n",peek(&stack));

pop(&stack);
pop(&stack);
// Print top element of the stack
printf("Top element is %d\n", peek(&stack));

return 0;
}

9 //simple queue
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
struct Queue
{
int arr[MAX];
int rear;
int front;
};
int isFull(struct Queue* queue)
{
if(queue->rear == MAX-1)
return 1;
return 0;
}
int isEmpty(struct Queue* queue)
{
if(queue->front == -1)
return 1;
return 0;
}
struct Queue* create()
{
struct Queue* queue=(struct Queue*)malloc(sizeof(struct Queue));
queue->front=queue->rear=-1;
return queue;
}
void enQueue(struct Queue* queue)
{
int value;
char choice;
while(1)
{
printf("enter the value\n\n");
scanf("%d",&value);

if(isFull(queue))
{
printf("the queue is full \n");
return ;
}
if(queue->front == -1)
queue->front = 0;

queue->arr[queue->rear +=1]=value;
printf("enter the N to stop and Y to continue\n");
scanf(" %c",&choice);
if(choice == 'n'|| choice == 'N')
break;
}
}
void deQueue(struct Queue* queue)
{
if(isEmpty(queue))
printf("the queue is underflow to delete any element \n");
else
{
for(int i=0;i<queue->rear;i++)
queue->arr[i]=queue->arr[i+1];
queue->rear -=1;
}

}
void display(struct Queue* queue)
{
if(isEmpty(queue))
printf("the queue is empty \n");
else
for(int i=0;i<=queue->rear;i++)
printf("%d\t",queue->arr[i]);
printf("\n");
}
int main()
{
struct Queue* queue;
int choice;
while (1)
{
printf("1->create queue\n2->enqueue\n3->dequeue\n4->display\n5->exit\n");
scanf("%d",&choice);
switch (choice)
{
case 1:
queue=create();
break;
case 2:
enQueue(queue);
break;
case 3:
deQueue(queue);
break;
case 4:
display(queue);
break;
case 5:
exit(0);
break;
default:
printf("invalid choice \n");
break;
}
}
}

10 Doubly ll
#include<stdio.h>
#include<stdlib.h>
struct node // node date and next node no address and previous node no address
{
int data;//
struct node *next;
struct node *prev;
};
struct node *start=NULL;
void create_dll()// to create a double link list
{
struct node *ptr,*temp;
int item;
while(1)//while loop untill user enter -1
{
printf("enter the value and -1 to end \n");
scanf("%d",&item);
if(item== -1)//to break loop when user enter the -1
break;

ptr=(struct node*)malloc(sizeof(struct node));


if (ptr == NULL)//to check memory is aval or not
{
printf("\noverflow");
}
else
{
ptr->data=item;
ptr->next=NULL;
ptr->prev=NULL;
if(start == NULL)
start=ptr;
else
{
temp=start;
while(temp->next != NULL)
temp=temp->next;

temp->next=ptr;
ptr->prev=temp;
}
}
}
}
void display()// to display a list
{
struct node *ptr;
ptr=start;
while(ptr != NULL)
{
printf("%p %d %p \t\t",ptr->prev,ptr->data,ptr->next);
ptr=ptr->next;
}

}
void insertion_beginning()
{
struct node *ptr=(struct node *)malloc(sizeof(struct node));
int item;
if(start == NULL)
printf("underflow");
else
{
printf("enter the value");
scanf("%d",&item);

ptr->data=item;
ptr->next=start;
start->prev=ptr;
ptr->prev=NULL;
start=ptr;
}

}
void insertion_at_end()//to add a node at end of list
{
struct node *ptr=(struct node *)malloc(sizeof(struct node));
struct node *temp;
int item;
if(start == NULL)
printf("\n underflow");
else
{
printf("enter the number");
scanf("%d",&item);
ptr->data=item;
ptr->next=NULL;
ptr->prev=NULL;
temp=start;
while(temp->next != NULL)
temp=temp->next;

temp->next=ptr;
ptr->prev=temp;
}
printf("node has been inserted \n");
}
void Insert_at_specified_location_Before_or_After()
{
int specified,value,flag=0;
char choice;
if(start == NULL)
{
printf("\nthe linklist is underflow \n");
return ;
}
else
{
struct node* ptr=(struct node *)malloc(sizeof(struct node));
struct node* temp=(struct node *)malloc(sizeof(struct node));
ptr=start;

printf("enter at which node you want to insert\n");


scanf("%d",&specified);
printf("for before a given node enter B and for after a give node enter A\n");
scanf(" %c",&choice);

while(ptr->next != NULL)
{
if(ptr->data == specified)
{
flag =1;
break;
}
ptr=ptr->next;
}

if(flag == 0)
printf("node not found \n\n");
else
{
if(choice == 'a'|| choice == 'A')
{

if(ptr->next == NULL)
insertion_at_end();
else
{
printf("enter the value for the node \n");
scanf("%d",&value);
temp->data=value;
temp->prev=ptr;
temp->next=ptr->next;
}
}
else if(choice == 'b'|| choice == 'B')
{

if(ptr->prev == NULL)
insertion_beginning();
else
{
printf("enter the value for the node \n");
scanf("%d",&value);
temp->data=value;
temp->prev=ptr->prev;
temp->next=ptr;
ptr->prev=temp;
}
}
else
printf("invalid choice or node not fount \n\n");
}
}
printf("hello");
display();
}
void delete_beg()//to delete the node from beginning
{
struct node* ptr=(struct node *)malloc(sizeof(struct node));
ptr=start;
if(start == NULL)
printf("underflow");
else
{
start=start->next;
start->prev=NULL;
free(ptr);
}
}
void delete_end()// to delete the node from end
{
struct node* ptr=(struct node *)malloc(sizeof(struct node));
struct node*temp;
ptr=start;
if(start == NULL)// to check node is empty or not
printf("underflow");
else
{
while(ptr->next != NULL)
ptr=ptr->next;

temp=ptr;
ptr=ptr->prev;
ptr->next=NULL;
free(temp);
}
}
void delete_a_specified()// to delete any specific data node
{
int value,flag=0;
if(start == NULL)
printf("the stack is underflow \n");
else
{
struct node* ptr=(struct node *)malloc(sizeof(struct node));
printf("enter the which node you want to delete \n");
scanf("%d",&value);

ptr=start;
while(ptr->next != NULL)
{
if(ptr->data == value)
{
flag =1;
break;
}
ptr=ptr->next;
}

if(ptr->prev == NULL)
delete_beg();
else if(ptr->next == NULL)
delete_end();
else
{
ptr->prev->next=ptr->next;
ptr->next->prev=ptr->prev;
}
if(flag == 0)
printf("invalid value node not found\n\n ");
else
printf("success\n");
}
}
int main()
{
int choice=0;

while(choice!= 9)
{
printf("\n=====main menu=====\n");
printf("\nchoose one option from the following list...\n");

printf("\n===================\n");
printf("\n1.create DLL.\n2.Display\n");
printf("3.insert at begging\n4.insert at end\n5. delete at begging\n6.delete
st end\n7. delete_a_specified\n8. Insert_at_specified_location_Before_or_After");
printf("9 EXIT");

printf("\nenter your choice?\n");


scanf("%d",&choice);

switch(choice)
{
case 1:
create_dll();
break;
case 2:
display();
break;
case 3:
insertion_beginning();
break;
case 4:
insertion_at_end();
break;
case 5:
delete_beg();
break;
case 6:
delete_end();
break;
case 7:
delete_a_specified();
break;
case 8:
Insert_at_specified_location_Before_or_After();
break;
case 9:
exit(0);
break;
default:
printf("please enter valid choice...");
}
}
return 0;
}

11 #include <stdio.h>

# define max 6
int queue[max]; // array declaration
int front=-1;
int rear=-1;
// function to insert an element in a circular queue
void enqueue(int element)
{
if(front==-1 && rear==-1) // condition to check queue is empty
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front) // condition to check queue is full
{
printf("Queue is overflow..");
}
else
{
rear=(rear+1)%max; // rear is incremented
queue[rear]=element; // assigning a value to the queue at the rear position.
}
}

// function to delete the element from the queue


int dequeue()
{
if((front==-1) && (rear==-1)) // condition to check queue is empty
{
printf("\nQueue is underflow..");
}
else if(front==rear)
{
printf("\nThe dequeued element is %d", queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe dequeued element is %d", queue[front]);
front=(front+1)%max;
}
}
// function to display the elements of a queue
void display()
{
int i=front;
if(front==-1 && rear==-1)
{
printf("\n Queue is empty..");
}
else
{
printf("\nElements in a Queue are :");
while(i<=rear)
{
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
}
int main()
{
int choice=1,x; // variables declaration

while(choice<4 && choice!=0) // while loop


{
printf("\n Press 1: Insert an element");
printf("\nPress 2: Delete an element");
printf("\nPress 3: Display the element");
printf("\nEnter your choice");
scanf("%d", &choice);

switch(choice)
{

case 1:

printf("Enter the element which is to be inserted");


scanf("%d", &x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();

}}
return 0;
}

12 // stack using array


#include<stdio.h>
#include<stdlib.h>
#define MAX 100
struct Stack
{
char arr[MAX];
int top;
};
int isFull(struct Stack* stack) // to check the stack is full or not
{
if(stack->top == MAX - 1)
return 1;
return 0;
}
int isEmpty(struct Stack* stack) // to check the stack is empty or not
{
if(stack->top == -1)
return 1;
return 0;
}
struct Stack* create()
{
struct Stack* stack=(struct Stack*)malloc(sizeof(struct Stack));
stack->top=-1;
return stack;
}
void push(struct Stack* stack,char value) // to insert the elements in array
{

if(isFull(stack))
printf("\n\nthe stack is full \n");
else
{

stack->top +=1;
stack->arr[stack->top]=value;

}
}
void pop(struct Stack* stack)
{
if(isEmpty(stack))
printf("\n\nthe stack is empty \n");
else
{
stack->top -=1;
}
}
char peep(struct Stack* stack)
{
if(isEmpty(stack))
printf("the stack is empty \n");
else
return stack->arr[stack->top];
}
int main()
{
struct Stack* stack=create();
char name[100];
int i=0;

printf("enter the name ");


scanf("%s",&name);
printf("%s",name);
while(name[i] != '\0')
{
push(stack,name[i]);
i++;
}
i=0;
char reverse[100];
while (stack->top != -1)
{
reverse[i]=peep(stack);
pop(stack);
i++;
}
reverse[i]='\0';
printf("\n\n%s",name);
printf("\n\n%s",reverse);
return 0;
}

You might also like