0% found this document useful (0 votes)
5 views57 pages

Data All Program

The document contains a comprehensive index of programming exercises divided into two parts: Part-A and Part-B. Each part includes various algorithms and data structure implementations such as sorting techniques, stack and queue operations, and recursion examples. The document provides code snippets in C for each program along with their respective output sections.

Uploaded by

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

Data All Program

The document contains a comprehensive index of programming exercises divided into two parts: Part-A and Part-B. Each part includes various algorithms and data structure implementations such as sorting techniques, stack and queue operations, and recursion examples. The document provides code snippets in C for each program along with their respective output sections.

Uploaded by

ayshaazmiya66
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

INDEX

Part-A

[Link] Program Page No

1. Program to sort the given list using selection sort 4-5


technique.

2. Program to sort the given list using insertion sort 6-7


technique

3. Program to solve Tower of Hanoi using Recursion 8-9

4. Program to reverse String using Stack 10-11

5. Program to search an element using recursive binary 12-13


search technique.

6. Program to implement Stack operations using arrays. 14-17

7. Program to implement Queue operations using arrays. 18-21

8. Program to implement dynamic array. Find smallest and 22-23


largest element.

1
Part-B

[Link] Program Page No

1. Program to sort the given list using merge sort technique 25-27

2. Program to implement circular queue using array. 28-32

3. Program to sort the given list using quick sort technique. 33-34

4. Program to implement Stack operations using linked list 35-38

5. Program to implement Queue operations using linked list. 39-42

6. Program to evaluate postfix expression. 43-45

7. Program to perform insert node at the end, delete a given 46-51


node and display contents of single linked list.

8. Menu driven program for the following operations on 52-57


Binary Search Tree(BST) of Integers
(a) Create a BST of N Integers
(b) Traverse the BST in Inorder, Preorder and Post Order.

2
Part-A

3
[Link] to sort the given list using selection sort
technique.
#include<stdio.h>

int main(){

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

printf("enter number of elements:");

scanf("%d",&n);

printf("enter elements:\n");

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

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

for(i=0; i<n-1; i++){

min=i;

for(j=i+1; j<n; j++){

if(a[j]<a[min]){

min=j;

temp=a[i];

a[i]=a[min];

a[min]=temp;

printf("sorted list:\n");

4
for (i=0; i<n; i++){

printf("%d\t" , a[i]);

return 0;

Out put:

5
[Link] to sort the given list using insertion sort
technique

#include <stdio.h>

int main() {

int a[10], n, i, j, key;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter elements:\n");

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

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

for (i = 1; i < n; i++) {

key = a[i];

j = i - 1;

while (j >= 0 && a[j] > key) {

a[j + 1] = a[j];

j--;

a[j + 1] = key;

6
printf("Sorted list:\n");

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

printf("%d\t", a[i]);

return 0;

Out put:

7
[Link] to solve Tower of Hanoi using Recursion

#include<stdio.h>

void towerOfHanoi(int n,char source,char auxillary,char destination)

if (n==1)

printf("move disk 1 from %c to %c\n",source,destination);

return;

towerOfHanoi(n-1,source, destination,auxillary);

printf("move disk 1 from %c to %c\n",source,destination);

towerOfHanoi(n-1,source,destination,auxillary);

int main()

int n;

printf("enter number of disk");

scanf("%d",&n);

towerOfHanoi(n,'A','B','C');

return 0;

8
Out put:1

9
4.C Program To Reverse A String Using A Stack

#include<stdio.h>

#include<string.h>

#define MAX 100

char stack[MAX];

int top=1;

/*push character onto stack*/

void push(char ch)

stack[++top]=ch;

char pop()

return stack[top--];

int main()

char str[MAX];

int i;

printf("enter a string:");

gets(str);

for (i=0; str[i]!='\0'; i++)

10
{

push(str[i]);

for(i=0; str[i]!='\0'; i++)

str[i]=pop();

printf("riversed string:%s\n",str);

return 0;

Out put :1

11
[Link] to search an element using recursive binary
search technique.
#include<stdio.h>

int binarySearch(int a[], int low, int high,int key)

if(low>high)

return-1;

int mid=(low+high)/2;

if (a[mid]==key)

return mid;

if(key<a[mid])

return binarySearch(a,low,mid-1,key);

return binarySearch(a,mid+1,high,key);

int main()

int a[5]={10,20,30,40,50};

int key,pos;

printf("enter element to search:");

scanf("%d",&key);

pos=binarySearch(a,0,4,key);

if (pos!=-1)

printf("element found at position %d",pos+1);

else

12
printf("element not found");

return 0;

Out put:

13
[Link] to implement Stack operations using arrays

#include<stdio.h>

#define MAX 5

int stack[MAX];

int top=-1;

void push(int value){

if(top==MAX-1){

printf("stack overflow! cannot push %d\n",value);

}else{

top++;

stack[top]=value;

printf("%d pushed into stack\n",value);

void pop(){

if(top==-1){

printf("stack underflow! stack is empty\n");

}else {

printf("%d popped from stack\n",stack[top]);

top--;

14
void peek(){

if (top==-1){

printf("stack is empty\n");

}else{

printf(" top element is %d\n",stack[top]);

void display(){

if (top==-1){

printf("stack is empty\n");

}else{

printf("stack element are:\n");

for(int i=top; i>=0; i--){

printf("%d\n",stack[i]);

int main(){

int choice,value;

do{

printf("\n--stack menu--\n");

printf("[Link]\n");

printf("[Link]\n");

15
printf("[Link]\n");

printf("[Link]\n");

printf("[Link]\n");

printf("enter your choice:");

scanf("%d",&choice);

switch(choice){

case 1:

printf("enter value to push:");

scanf("%d",&value);

push(value);

break;

case 2:

pop();

break;

case 3:

peek();

break;

case 4:

display();

break;

case 5:

printf("exiting program\n");

16
break;

default:

printf("invalid choice\n");

}while(choice !=5);

return 0;

Out put:

17
7. Program to implement Queue operations using arrays.

#include<stdio.h>

#define MAX 5

int queue[MAX];

int front=-1;

int rear=-1;

void insertion(int value){

if(rear==MAX-1){

printf("queue Overflow! cannot insert %d\n",value);

}else{

if(front==-1)

front=0;

rear++;

queue[rear]=value;

printf("%d inserted into queue\n",value);

void deletion(){

18
if(front==-1 || front>rear){

printf("queue underflow! queue is empty\n");

}else{

printf("%d deleted from queue\n",queue[front]);

front++;

void display(){

if(front==-1|| front>rear){

printf("queue is empty\n");

}else{

printf("queue elements are:");

for (int i=front;i<=rear;i++){

printf("%d\t",queue[i]);

printf("\n");

int main(){

int choice,value;

do{

printf("\n--queue menu--\n");

19
printf("[Link]\n");

printf("[Link]\n");

printf("[Link]\n");

printf("[Link]\n");

printf("enter your choice:");

scanf("%d",&choice);

switch(choice)

case 1:

printf("enter value to insert:");

scanf("%d",&value);

insertion(value);

break;

case 2:

deletion();

break;

case 3:

display();

break;

case 4:

printf("exiting program\n");

break;

default:

20
printf("invalid choice\n");

while(choice!=4);

return 0;

Out Put:

21
8. Program to implement dynamic array. Find smallest and
largest element.

#include<stdio.h>

#include<stdlib.h>

int main(){

int n,i;

int*arr;

int small,large;

printf("enter number of elements :");

scanf("%d",&n);

arr=(int*)malloc(n*sizeof(int));

if(arr==NULL){

printf("memory allocation failes!\n");

return 0;

printf("enter %d element:\n",n);

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

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

small=arr[0];

large=arr[0];

for(i=1;i<n;i++){

if(arr[i]<small){
22
small=arr[i];

if (arr[i]>large){

large=arr[i];

printf("smallest element=%d\n",small);

printf("largest element=%d\n",large);

free(arr);

return 0;

Out put:

23
Part-B

24
1. Program to sort the given list using merge sort technique.

#include<stdio.h>

void merge(int arr[],int left,int mid,int right){

int i,j,k;

int n1=mid-left+1;

int n2=right-mid;

int L[n1],R[n2];

for(i=0;i<n1;i++)

L[i]=arr[left+i];

for(j=0;j<n2;j++)

R[j]=arr[mid+1+j];

i=0;

j=0;

k=left;

while(i<n1&&j<n2){

if(L[i]<=R[j]){

arr[k]=L[i];

i++;

}else{

arr[k]=R[j];

j++;

25
k++;

while(i<n1){

arr[k]=L[i];

i++;

k++;

while(j<n2){

arr[k]=R[j];

j++;

k++;

void mergesort(int arr[],int left,int right){

if(left<right){

int mid=(left+right)/2;

mergesort(arr,left,mid);

mergesort(arr,mid+1,right);

merge(arr,left,mid,right);

int main(){

int arr[100],n,i;

26
printf("enter number of elements:");

scanf("%d",&n);

printf("enter elements:\n");

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

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

mergesort(arr,0,n-1);

printf("sorted array:\n \t");

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

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

return 0;

Out put:

27
2. Program to implement circular queue using array.

#include<stdio.h>

#define MAX 5

int queue[MAX];

int front=-1;

int rear=-1;

void insert(int value){

if((front==0&&rear==MAX-1)||(front==rear+1)){

printf("Queue Overfflow! Cannot insert %d\n",value);

return;

if(front==-1){

front=rear=0;

else if(rear==MAX-1&&front!=0){

rear=0;

else{

rear++;

queue[rear]=value;

printf("%d inserted into the queue\n",value);

28
}

void deleteQueue(){

if(front==-1){

printf("Queue Underflow!Queue is empty\n");

return;

printf("%d deleted from the queue\n",queue[front]);

if(front==rear){

front=rear=-1;

else if(front==MAX-1){

front=0;

else{

front++;

void display(){

if(front==-1){

printf("Queue is empty\n");

return;

printf("Element in Circular Queue:");

29
if (rear>=front){

for(int i=front;i<=rear;i++)

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

else{

for(int i=front;i<MAX;i++)

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

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

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

printf("\n");

int main(){

int choice,value;

while(1){

printf("\n---Circular Queue Menu---\n");

printf("[Link]\n");

printf("[Link]\n");

printf("[Link]\n");

printf("[Link]\n");

printf("enter your choice:");

scanf("%d",&choice);

switch(choice){

30
case 1:

printf("enter value to insert:");

scanf("%d",&value);

insert(value);

break;

case 2:

deleteQueue();

break;

case 3:

display();

break;

case 4:

return 0;

default:

printf("Invalid choice!try again.\n");

Out put:

31
32
3. Program to sort the given list using quick sort technique.

#include<stdio.h>

void swap(int*a,int*b){

int t=*a;

*a=*b;

*b=t;

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 quickSort(int arr[],int low,int high){

if(low<high){

int pi=partition(arr,low,high);

33
quickSort(arr,low,pi-1);

quickSort(arr,pi+1,high);

int main(){

int n,i;

printf("enter number of eliments:");

scanf(" %d",&n);

int arr[n];

printf("enter eliments:");

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

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

quickSort(arr,0,n-1);

printf("sorted array:\n");

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

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

return 0;

} Out put:

34
4. Program to implement Stack operations using linked list.

#include<stdio.h>

#include<stdlib.h>

struct Node{

int data;

struct Node*next;

};

struct Node*top=NULL;

void push(int value){

struct Node*newNode=(struct Node*)malloc(sizeof(struct Node));

if(newNode==NULL){

printf("stack Overflow\n");

return;

newNode->data=value;

newNode->next=top;

top=newNode;

printf("Inserted %d\n",value);

void pop(){

if (top==NULL){

35
printf("stack Underflow\n");

return;

struct Node*temp=top;

printf("Deleted %d\n",temp->data);

top=top->next;

free(temp);

void display(){

struct Node*temp=top;

if(temp==NULL){

printf("Stack is empty\n");

return;

printf("Stack elements:\n");

while (temp!=NULL){

printf("%d\n",temp->data);

temp=temp->next;

int main(){

36
int choice,value;

while(1){

printf("\[Link]\[Link]\[Link]\[Link]\n");

printf("enter your choice:");

scanf("%d",&choice);

switch(choice){

case 1:

printf("enter value:");

scanf("%d",&value);

push(value);

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

exit(0);

default:

37
printf("invalid choice\n");

return 0;

Out put:

38
[Link] to implement Queue operations using linked list.

#include<stdio.h>

#include<stdlib.h>

struct node{

int data;

struct node*next;

};

struct node*front=NULL,*rear=NULL;

void enqueue(int x){

struct node*temp;

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

temp->data=x;

temp->next=NULL;

if(rear==NULL){

front=rear=temp;

}else{

rear->next=temp;

rear=temp;

printf("Inserted %d\n",x);

void dequeue(){

39
struct node*temp;

if(front==NULL){

printf("Queue Underflow\n");

return;

temp=front;

printf("deleted %d\n",front->data);

front=front->next;

if(front==NULL)

rear=NULL;

free(temp);

void display(){

struct node*temp=front;

if (temp==NULL){

printf("queue is empty\n");

return;

printf("queue:");

while(temp!=NULL){

printf("%d",temp->data);

temp=temp->next;

40
printf("\n");

int main(){

int ch,x;

while(1){

printf("\[Link]\[Link]\[Link]\[Link]\n");

printf("Enter choice:");

scanf("%d",&ch);

switch(ch){

case 1:

printf("Enter value:");

scanf("%d",&x);

enqueue(x);

break;

case 2:

dequeue();

break;

case 3:

display();

break;

case 4:

exit(0);

default:

41
printf("invalid choice:\n");

Out put:

42
6. Program to evaluate postfix expression.

#include<stdio.h>

#include<ctype.h>

#include<math.h>

#define MAX 100

int stack[MAX];

int top=-1;

void push(int value){

stack[++top]=value;

int pop(){

return stack[top--];

int main(){

char postfix[MAX];

int i;

printf("Enter postfix expression:");

scanf("%s",postfix);

for(i=0;postfix[i]!='\0';i++){

if(isdigit(postfix[i])){

43
push(postfix[i]-'0');//Convert char int

}else{

int val1=pop();

int val2=pop();

switch(postfix[i]){

case'+':push(val2+val1);break;

case'-':push(val2-val1);break;

case'*':push(val2*val1);break;

case'/':push(val2/val1);break;

case'^':push(pow(val2,val1));break;

printf("Result=%d\n",pop());

return 0;

44
Out put:

45
[Link] to perform insert node at the end,delete a given
node node and display contents of single linked list.

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node*next;

};

struct node*head=NULL;

void insertEnd(int value)

struct node*newnode,*temp;

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

newnode->data=value;

newnode->next=NULL;

if(head==NULL)

head=newnode;

else

temp=head;
46
while(temp->next!=NULL)

temp=temp->next;

temp->next=newnode;

printf("%d inserted\n",value);

void deleteNode(int value)

struct node*temp,*prev;

temp=head;

prev=NULL;

if(head==NULL)

printf("list is empty\n");

return;

if(temp->data==value)

head=temp->next;

free(temp);

printf("%d deleted\n",value);

47
return;

while(temp!=NULL&&temp->data!=value)

prev=temp;

temp=temp->next;

if(temp==NULL)

printf("Node not found\n");

return;

prev->next=temp->next;

free(temp);

printf("%d deleted\n",value);

void display()

struct node*temp;

if(head==NULL)

printf("list is empty\n");

return;

48
}

temp=head;

printf("linked list:");

while(temp!=NULL)

printf("%d ",temp->data);

temp=temp->next;

printf("\n");

int main()

int choice,value;

while(1)

printf("\[Link] at end\n");

printf("[Link] Node\n");

printf("[Link]\n");

printf("[Link]\n");

printf("Enter choice:");

scanf("%d",&choice);

switch(choice)

49
case 1:

printf("Enter value:");

scanf("%d",&value);

insertEnd(value);

break;

case 2:

printf("Enter value to delete:");

scanf("%d",&value);

deleteNode(value);

break;

case 3:

display();

break;

case 4:

exit(0);

default:

printf("invalid choice\n");

return 0;

50
Out put:

51
8. Menu driven program for the following operations on
Binary Search Tree(BST) of Integers
a) Create a BST of N Integers
b) Traverse the BST in Inorder, Preorder and Post
Order.

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node*left,*right;

};

struct node*createNode(int value)

struct node*newNode;

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

newNode->data=value;

newNode->left=NULL;

newNode->right=NULL;

return newNode;

struct node*insert(struct node*root,int value)

52
if(root==NULL)

return createNode(value);

if(value<root->data)

root->left=insert(root->left,value);

else

root->right=insert(root->right,value);

return root;

void inorder(struct node*root)

if(root!=NULL)

inorder(root->left);

printf("%d ",root->data);

inorder(root->right);

void preorder(struct node*root)

if(root!=NULL)

printf("%d ",root->data);

preorder(root->left);

53
preorder(root->right);

void postorder(struct node*root)

if(root!=NULL)

postorder(root->left);

postorder(root->right);

printf("%d ",root->data);

int main()

struct node*root=NULL;

int n,i,value,choice;

while(1)

printf("\n----BST MENU----\n");

printf("[Link] BST\n");

printf("[Link] Traversal\n");

printf("[Link] Traversal\n");

54
printf("[Link] Traversal\n");

printf("[Link]\n");

printf("Enter your choice:");

scanf("%d",&choice);

switch(choice)

case 1:

printf("Enter number of nodes:");

scanf("%d",&n);

printf("Enter BST elements:\n");

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

scanf("%d",&value);

root=insert(root,value);

printf("BST Created Successfully\n");

break;

case 2:

printf("Inorder Traversal:");

inorder(root);

printf("\n");

break;

55
case 3:

printf("preorder Traversal:");

preorder(root);

printf("\n");

break;

case 4:

printf("postorder Traversal:");

postorder(root);

printf("\n");

break;

case 5:

exit(0);

default:

printf("Invalid Choice\n");

return 0;

56
Out put:

57

You might also like