DS Practical With Index
DS Practical With Index
DS Practical With Index
#include<stdio.h>
int main()
int A,B,sum=0;
scanf("%d%d",&A,&B);
//using'+' operator
sum=A+B;
return 0;
10
20
Program 2
#include<stdio.h>
if(n==0)
return 1;
return n*factorial(n-1);
}
//driver code
int main()
int num=5;
printf("factorail of %d is %d",num,factorial(num));
return 0;
Program 3
#include<stdio.h>
int main()
int A,B,C;
scanf("%d%d%d",&A,&B,&C);
if(C>=A&&C>=B);
return 0;
56
45
#include<stdio.h>
int i;
printf("Array:");
for(i=0;i<n;i++){
printf ("%d",arr[i]);
printf("\n");
int main()
{ int arr[]={2,-1,5,6,0,-3};
int n = sizeof(arr)/sizeof(arr[0]);
printArray(arr, n);
return 0;
Output: Array:2-1560-3
Program 5
#include<stdio.h>
int main()
{ int arr[100]={0};
int i,x,pos,n=10;
for(i=0;i<10;i++)
arr[i]=i+1;
for(i=0;i<n;i++)
printf("%d",arr[i]);
printf("\n");
//element to be inserted
x=50;
pos=5;
n++;
for(i=n-1;i>=pos;i--)
arr[i]=arr[i-1];
for(i=0;i,n;i++)
printf("%d",arr[i]);
printf("\n");
return 0;
Output: 12345678910
123455678910
PROGRAM 6
#include <stdio.h>
#include <conio.h>
int i;
arr[i] = arr[i+1];//
int i;
int main ()
int arr[100];
printf (" Enter the position of the element you want to delete from an array: ");
// check whether thr defined pos is less than equal to size of array (num) and larger than
} return 0; }
arr[0] = 23
23arr[1] = 34
arr[2] = 50
arr[3] = 67
arr[4] = 34
arr[5] = 56
arr[6] = 56
arr[7] = 56
arr[8] = 23
arr[9] = 67
Enter the position of the element you want to delete from an array: 89
Program 7
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
//clrscr();
top=-1;
printf("\n\t ");
do
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
default:
}
}
while(choice!=4);
return 0;
void push()
if(top>=n-1)
else
scanf("%d",&x);
top++;
stack[top]=x;
void pop()
if(top<=-1)
else
top--;
void display()
if(top>=0)
{
printf("\n%d",stack[i]);
else
1.PUSH
2.POP
3.DISPLAY
4.EXIT
68
Program 8
#include <stdio.h>
#include<stdlib.h>
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
intfront = - 1;
int main()
int choice;
while (1)
printf("4.Quit\n");
scanf("%d", &choice);
switch(choice)
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
void insert()
int item;
if(rear == MAX - 1)
else
if(front== - 1)
front = 0;
scanf("%d", &item);
rear = rear + 1;
queue_array[rear] = item;
void delete()
return;
else
front = front + 1;
void display()
int i;
if(front == - 1)
else
printf("Queue is : \n");
printf("\n");
OutPut :
4.Quit
4.Quit
4.Quit
Queue Underflow
Program 9
i) Implementation of Array
i)
#include <stdio.h>
int main()
int arr[10];
int i;
scanf("%d", &arr[i]);
printf("\n");
Output:
5
6
Program 9
#include<stdlib.h>
#include<stdio.h>
struct Node{
int data;
};
if(*head == NULL){
return;
*head = (*head)->next;
free(temp);
newNode->data = data;
newNode->next = *head;
*head = newNode;
printf("Inserted %d\n",newNode->data);
while(node!=NULL){
printf("%d ",node->data);
node = node->next;
printf("\n");
int main()
insertStart(&head,100);
insertStart(&head,80);
insertStart(&head,60);
insertStart(&head,40);
insertStart(&head,20);
// No Need for '&' as not changing head in display operation
display(head);
deleteStart(&head);
deleteStart(&head);
display(head);
return 0;
OutPut :
Inserted 100
Inserted 80
Inserted 60
Inserted 40
Inserted 20
Deleted: 20
Deleted: 40
Program 10
integer.
#include<stdio.h>
int find_factorial(int);
int main() {
scanf("%d",&num);
fact =find_factorial(num);
//Displaying factorial of input number
return 0;
int find_factorial(int n)
if(n==0)
return(1);
return(n*find_factorial(n-1));
Output :
Program 11
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
//clrscr();
top=-1;
scanf("%d",&n);
printf("\n\t ");
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:
break;
default:
while(choice!=4);
return 0;
}
void push()
if(top>=n-1)
else
scanf("%d",&x);
top++;
stack[top]=x;
void pop()
if(top<=-1)
else
top--;
void display()
if(top>=0)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
else
Output:
1.PUSH
2.POP
3.DISPLAY
4.EXIT
30
52
EXIT POINT
Program No 11
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
}*top;
/*
*/
void initialize() {
top = NULL;
/*
*/
int isEmpty() {
if (top == NULL)
return 1;
else
return 0;
/*
*/
int peek() {
return top->data;
/* Countstack elements */
/* Input Validation */
if (head == NULL) {
int length = 0;
while(head != NULL){
head = head->next;
length++;
return length;
/*
*/
temp->data = num;
if (top == NULL) {
top = temp;
top->next = NULL;
} else {
temp->next = top;
top = temp;
/*
*/
void pop() {
if (isEmpty(top)) {
printf("\nStack is Empty\n");
return;
} else {
temp = top;
top = top->next;
free(temp);
/*
*/
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("-->");
printf("\n");
void main() {
/* Initialize Stack */
initialize();
push(1);
push(2);
push(3);
push(4);
/* Printing Stack */
printStack(top);
pop();
pop();
pop();
pop();
pop();
printStack(top);
return;
Output :
Stack Size : 4
Top Element : 4
4-->3-->2-->1
Removed Element : 4
Removed Element : 3
Removed Element : 2
Removed Element : 1
Stack is Empty
Program No 12
#include <stdio.h>
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
intfront = - 1;
main()
int choice;
while (1)
printf("4.Quit\n");
scanf("%d", &choice);
switch (choice)
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
} /* End of switch */
void insert()
int add_item;
if (rear == MAX - 1)
else
if (front == - 1)
front = 0;
rear = rear + 1;
queue_array[rear] = add_item;
void delete()
return ;
else
front = front + 1;
void display()
int i;
if (front == - 1)
else
printf("Queue is : \n");
printf("\n");
OutPut:
4.Quit
: 10
4.Quit
: 50
4.Quit
Queue is
10 50
4.Quit
4.Quit
: 50
4.Quit
Program No 12
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
printf("\nMain Menu\n");
queue\n4.Exit\n");
scanf("%d",& choice);
switch(choice)
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
void insert()
int item;
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
else
scanf("%d",&item);
if(front == NULL)
front = ptr;
rear = ptr;
else
rear = ptr;
rear->next = NULL;
void delete ()
if(front == NULL)
printf("\nUNDERFLOW\n");
return;
else
ptr = front;
front = front -> next;
free(ptr);
void display()
ptr = front;
if(front == NULL)
printf("\nEmpty queue\n");
else
while(ptr != NULL)
OutPut :
Main Menu
1.insert an element
2.Delete an element
4.Exit
Enter Element
10
Main Menu
1.insert an element
2.Delete an element
4.Exit
Enter Element
20
Main Menu
1.insert an element
2.Delete an element
4.Exit
10
20
Main Menu
1.insert an element
2.Delete an element
4.Exit
Program No 13
#include<stdio.h>
#include<stdlib.h>
void insert(int);
struct node
int data;
void main ()
int choice,item;
do
scanf("%d",&item);
insert(item);
scanf("%d",&choice);
}while(choice == 0);
if(ptr == NULL)
printf("can't insert");
else
if(root == NULL)
root = ptr;
else
{
parentptr = NULL;
nodeptr = root;
while(nodeptr != NULL)
parentptr = nodeptr;
else
else
printf("Node Inserted");
OutPut :
10
Node Inserted
Node Inserted
Program No 14
#include<stdio.h>
#include<malloc.h>
struct node{
int data;
};
if(root!=NULL){
preOrder(root->left);
preOrder(root->right);
postOrder(root->left);
postOrder(root->right);
if(root!=NULL){
inOrder(root->left);
inOrder(root->right);
if(root!=NULL){
if(!isBST(root->left)){
return 0;
return 0;
prev = root;
return isBST(root->right);
else{
return 1;
while(root!=NULL){
if(key == root->data){
return root;
}
else if(key<root->data){
root = root->left;
else{
root = root->right;
return NULL;
while(root!=NULL){
prev = root;
if(key==root->data){
return;
else if(key<root->data){
root = root->left;
else{
root = root->right;
if(key<prev->data){
prev->left = new;
else{
prev->right = new;
}
struct node *inOrderPredecessor(struct node* root){
root = root->left;
while (root->right!=NULL)
root = root->right;
return root;
if (root == NULL){
return NULL;
if (root->left==NULL&&root->right==NULL){
free(root);
return NULL;
else{
iPre = inOrderPredecessor(root);
root->data = iPre->data;
return root;
}
int main(){
// 5
// / \
// 3 6
// / \
// 1 4
p->left = p1;
p->right = p2;
p1->left = p3;
p1->right = p4;
inOrder(p);
printf("\n");
deleteNode(p, 3);
inOrder(p);
return 0;
Output :
13456
1456
Program No 15
#include<stdio.h>
#include<malloc.h>
struct node{
int data;
};
if(root!=NULL){
preOrder(root->left);
preOrder(root->right);
if(root!=NULL){
postOrder(root->left);
postOrder(root->right);
if(root!=NULL){
inOrder(root->left);
inOrder(root->right);
if(root!=NULL){
if(!isBST(root->left)){
return 0;
return 0;
prev = root;
return isBST(root->right);
else{
return 1;
if(root==NULL){
return NULL;
if(key==root->data){
return root;
else if(key<root->data){
else{
return search(root->right, key);
int main(){
// 5
// / \
// 3 6
// / \
// 1 4
p->left = p1;
p->right = p2;
p1->left = p3;
p1->right = p4;
if(n!=NULL){
else{
return 0;
}
OutPut:
Program No 16
#include<stdio.h>
int main()
int a[20],i,x,n;
scanf("%d",&n);
for(i=0;i<n;++i)
scanf("%d",&a[i]);
scanf("%d",&x);
for(i=0;i<n;++i)
if(a[i]==x)
break;
if(i<n)
else
return 0;
OutPut :
20
30
40
50
Program No : 17
#include<stdio.h>
int main(){
scanf("%d",&n);
scanf("%d",&array[c]);
scanf("%d",&search);
first = 0;
last = n - 1;
middle = (first+last)/2;
first = middle + 1;
break;
else
last = middle - 1;
middle = (first + last)/2;
return 0;
OutPut :
Enter 5 integers
10
20
30
40
50
20
20 found at location 2.
Program No : 18
#include <stdio.h>
int main(){
printf("Please Enter the Number of Elements you want in the array: ");
scanf("%d", &num);
scanf("%d", &arr[x]);
temp = arr[y];
Page 58 of 62
arr[y + 1] = temp;
return 0;
OutPut :
10
30
40
50
60
70
80
90
100
Program No : 19
#include <stdio.h>
int main() {
int arr[10]={6,12,0,18,11,99,55,45,34,2};
int n=10;
position = i;
position = j;
if (position != i) {
swap = arr[i];
arr[i] = arr[position];
arr[position] = swap;
printf("%d\t", arr[i]);
return 0;
}
OutPut :
0 2 6 11 12 18 34 45 55 99
Program No : 20
// Insertion sort in C
#include <stdio.h>
printf("\n");
int j = step - 1;
// Compare key with each element on the left of it until an elementsmaller than
// it is found.
array[j + 1] = array[j];
--j;
array[j + 1] = key;
// Driver code
int main() {
insertionSort(data, size);
printArray(data,size);
OutPut :
13459