Data Structures are the programmatic way of storing data so that data can be used efficiently. Almost every enterprise application uses various types of data structures in one or the other way. This tutorial will give you a great understanding on Data Structures needed to understand the complexity of enterprise level applications and need of algorithms, and data structures.
2. 1
Content
WAP to search an element using Linear Search..........................................................3
WAP To Search an Element Using Binary Search .......................................................6
WAP To Sort Elements Using Bubble Sort ...................................................................9
WAP To Sort Elements Using Insertion Sort ..............................................................12
WAP to Sort Elements Using Selection Sort ..............................................................14
WAP to Sort Elements Using Quick Sort ....................................................................17
WAP To Sort Elements Using Merge Sort...................................................................21
Perform Push, Pop & Display Operations on Stack Using Array .............................23
WAP to perform insertion, deletion & display operations on circular queue using
array................................................................................................................................27
WAP to Perform insertion, deletion, searching & display operations on a BST.....30
4. 3
WAP to search an element using Linear Search
#include<stdio.h>
#include<stdlib.h>
//function prototypes
int l_search(int*, int, int); //linear search
int* createArray(int); //takes entries and creates a new array
void display(int*,int); //display the newly created array
int main()
{
int size = 0, x = 0,result = -1; //size of Array, Element to be searched,
result
printf("Enter the size of the matrix: ");
scanf("%d",&size);
int* A = createArray(size);
display(A,size);
printf("nEnter element to be searched: ");
scanf("%d",&x);
result = l_search(A,size,x);
if(result>=0)
{
printf("Element %d found at index: %d",x,result);
}
else
{
printf("Element %d was not found",x);
}
return 0;
}
int* createArray(int size)
{
int *A = (int*)malloc(size*sizeof(int));
for(int i = 0;i<size;i++)
{
printf("Enter the %dst element: ",i);
scanf("%d",&A[i]);
}
return A;
}
void display(int* A,int size)
{ printf("n[");
for(int i = 0;i<size;i++)
{
printf("%d ",A[i]);
}
printf("]n");
}
7. 6
WAP To Search an Element Using Binary Search
#include<stdio.h>
#include<stdlib.h>
//function prototypes
int b_search(int*, int, int,int,int); //binary search
int* createArray(int); //takes entries and creates a new array
void display(int*,int); //display the newly created array
int main()
{
int size = 0, x = 0,result = -1; //size of Array, Element to be searched,
result
printf("Enter the size of the matrix: ");
scanf("%d",&size);
int* A = createArray(size);
display(A,size);
printf("nEnter element to be searched: ");
scanf("%d",&x);
//printf("Enter left and right index for the search:");
int left = 0; //default
int right = size; //default
//scanf("%d %d",&left,&right);
result = b_search(A,size,left,right,x);
if(result>0)
{
printf("Element %d found at index: %d",x,result);
}
else
{
printf("Element %d was not found",x);
}
return 0;
}
int* createArray(int size)
{
int *A = (int*)malloc(size*sizeof(int));
for(int i = 0;i<size;i++)
{
printf("Enter the %dst element: ",i);
scanf("%d",&A[i]);
}
return A;
}
8. 7
void display(int* A,int size)
{ printf("n[");
for(int i = 0;i<size;i++)
{
printf("%d ",A[i]);
}
printf("]n");
}
int b_search(int* A, int size, int left, int right, int x)
{
while(left<=right)
{
int mid = left - (left+right)/2; //avoids overflow
if(A[mid]==x)
{
return mid;
}
else if(x<A[mid])
{
return b_search(A,size,left,mid-1,x);
}
else
return b_search(A,size,mid+1,right,x);
}
return -1;
}
19. 18
WAP to Sort Elements Using Quick Sort
#include<stdio.h>
#include<stdlib.h>
void display(int A[],int size);
void swap(int *a,int* b);
int partition (int arr[], int low, int high);
void quickSort(int arr[], int low, int high);
int* createArray(int size);
int main()
{
int size = 0;
printf("Enter size of Array: ");
scanf("%d",&size);
int* A = createArray(size);
printf("Original Array:n");
display(A,size);
int low = 0; //default
int high = size-1; //default
quickSort(A,low,high);
printf("Sorted Array: ");
display(A,size);
return 0;
}
int partition (int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; 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);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
22. 21
WAP To Sort Elements Using Merge Sort
#include <stdio.h>
#include <stdlib.h>
void merge(int arr[], int l, int m, int r);
void mergeSort(int arr[], int l, int r);
void display(int A[],int size);
int* createArray(int size);
int main()
{
int size = 0;
printf("Enter size of Array: ");
scanf("%d",&size);
int* A = createArray(size);
printf("Original Array:n");
display(A,size);
int l = 0; //default
int r = size-1; //default
mergeSort(A,l,r);
printf("Sorted Array: ");
display(A,size);
return 0;
}
int* createArray(int size)
{
int* A = (int*)malloc(size*sizeof(int));
for(int i = 0;i < size;i++)
{ printf("Enter the %dst Element: ",i);
scanf("%d",&A[i]);
}
return A;
}
void display(int* A,int size)
{ printf("n[");
for(int i = 0;i<size;i++)
{
printf("%d ",A[i]);
}
printf("]n");
}
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
/* create temp arrays */
int L[n1], R[n2];
23. 22
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
/* Merge the temp arrays back into arr[l..r]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
/* Copy the remaining elements of L[], if there
are any */
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
/* Copy the remaining elements of R[], if there
are any */
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r)
{
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
31. 30
WAP to Perform insertion, deletion, searching & display
operations on a BST
#include <stdio.h>
#include <stdlib.h>
struct Node
{
struct Node* lchild;
int data;
struct Node* rchild;
}*root = NULL;
void Inorder(struct Node* p)
{
if (p)
{
Inorder(p->lchild);
printf("%d ", p->data);
Inorder(p->rchild);
}
}
struct Node* Search(int key)
{
struct Node* t = root;
while (t != NULL)
{
if (key == t->data)
return t;
else if (key < t->data)
t = t->lchild;
else
t = t->rchild;
}
return NULL;
}
struct Node* Insert(struct Node* p, int key)
{
struct Node* t = NULL;
if (p == NULL)
{
t = (struct Node*)malloc(sizeof(struct Node));
t->data = key;
t->lchild = t->rchild = NULL;
return t;
}
if (key < p->data)
p->lchild = Insert(p->lchild, key);
else if (key > p->data)
p->rchild = Insert(p->rchild, key);
return p;
}
int Height(struct Node* p)
{
int x, y;
if (p == NULL)return 0;
x = Height(p->lchild);
y = Height(p->rchild);
32. 31
return x > y ? x + 1 : y + 1;
}
struct Node* InPre(struct Node* p)
{
while (p && p->rchild != NULL)
p = p->rchild;
return p;
}
struct Node* InSucc(struct Node* p)
{
while (p && p->lchild != NULL)
p = p->lchild;
return p;
}
struct Node* Delete(struct Node* p, int key)
{
struct Node* q;
if (p == NULL)
return NULL;
if (p->lchild == NULL && p->rchild == NULL)
{
if (p == root)
root = NULL;
free(p);
return NULL;
}
if (key < p->data)
p->lchild = Delete(p->lchild, key);
else if (key > p->data)
p->rchild = Delete(p->rchild, key);
else
{
if (Height(p->lchild) > Height(p->rchild))
{
q = InPre(p->lchild);
p->data = q->data;
p->lchild = Delete(p->lchild, q->data);
}
else
{
q = InSucc(p->rchild);
p->data = q->data;
p->rchild = Delete(p->rchild, q->data);
}
}
return p;
}
void Insert(int key)
{
struct Node* t = root;
struct Node* r = NULL, * p;
if (root == NULL)
{
p = (struct Node*)malloc(sizeof(struct Node));
p->data = key;
p->lchild = p->rchild = NULL;
root = p;
return;
}
33. 32
while (t != NULL)
{
r = t;
if (key < t->data)
t = t->lchild;
else if (key > t->data)
t = t->rchild;
else
return;
}
p = (struct Node*)malloc(sizeof(struct Node));
p->data = key;
p->lchild = p->rchild = NULL; if (key < r->data) r->lchild = p;
else r->rchild = p;
}
int main()
{
int data = 0;
int ch = 0;
while(1)
{
printf("nEnter 1:Insert 2:Delete 3:Search 4:Inorder
TraversalnPress Any other key to Exitn");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("nEnter key to be inserted: ");
scanf("%d",&data);
Insert(data);
break;
case 2:
printf("nEnter key to be deleted: ");
scanf("%d",&data);
(Delete(root,data))? printf("nSuccessfully Deleted
%d",data):printf("n %d not found",data);
break;
case 3:
printf("nEnter key to be searched: ");
scanf("%d",data);
Search(data)?printf("n%d Found",data):printf("%d not
found",data);
break;
case 4:
printf("nInorder Traversal of BST:");
Inorder(root);
break;
default:
exit(0);
}
}
return 0;
}