SlideShare une entreprise Scribd logo
1  sur  33
Télécharger pour lire hors ligne
Data Structures &
Algorithms
Practical File
Submitted To:
Dr. Amita Dhankhar
Submitted By:
Rishab
25634
CSE-B
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
2
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");
}
4
int l_search(int* A,int size,int x)
{
for(int i = 0;i<size;i++)
{
if(A[i]==x)
{
return i;
}
}
return -1;
}
5
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;
}
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;
}
8
9
WAP To Sort Elements Using Bubble Sort
#include<stdio.h>
#include<stdlib.h>
int* createArray(int size);
void bubbleSort(int A[],int size);
void swap(int *a,int* b);
void display(int A[],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);
bubbleSort(A,size);
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 swap(int *a, int*b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void bubbleSort(int A[], int size)
{
for(int i = 0; i<size-1;i++)
{
for(int j = 0; j<size-1-i;j++)
{
if(A[j]>A[j+1])
{
swap(&A[j],&A[j+1]);
}
}
}
}
10
void display(int* A,int size)
{ printf("n[");
for(int i = 0;i<size;i++)
{
printf("%d ",A[i]);
}
printf("]n");
}
11
12
WAP To Sort Elements Using Insertion Sort
#include<stdio.h>
#include<stdlib.h>
int* createArray(int size);
void insertionSort(int A[],int size);
void display(int A[],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);
insertionSort(A,size);
printf("Sorted Array: ");
display(A,size);
return 0;
}
void insertionSort(int A[], int size)
{
int i, x, j;
for (i = 1; i < size; i++) {
x = A[i];
j = i - 1;
while (j >= 0 && A[j] > x) {
A[j + 1] = A[j];
j = j - 1;
}
A[j + 1] = x;
}
}
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;
}
13
void display(int* A,int size)
{ printf("n[");
for(int i = 0;i<size;i++)
{
printf("%d ",A[i]);
}
printf("]n");
}
14
15
WAP to Sort Elements Using Selection Sort
#include<stdio.h>
#include<stdlib.h>
int* createArray(int size);
void insertionSort(int A[],int size);
void display(int A[],int size);
void swap(int *a,int* b);
int main()
{
int size = 0;
printf("Enter size of Array: ");
scanf("%d",&size);
int* A = createArray(size);
printf("Original Array:n");
display(A,size);
insertionSort(A,size);
printf("Sorted Array: ");
display(A,size);
return 0;
}
void insertionSort(int A[], int size)
{
int i, j, min;
for (i = 0; i < size-1; i++)
{
min = i;
for (j = i+1; j < size; j++)
if (A[j] < A[min])
min = j;
swap(&A[min], &A[i]);
}
}
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;
}
16
void display(int* A,int size)
{ printf("n[");
for(int i = 0;i<size;i++)
{
printf("%d ",A[i]);
}
printf("]n");
}
void swap(int *a, int*b)
{
int temp = *a;
*a = *b;
*b = temp;
}
17
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);
}
19
}
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 swap(int *a, int*b)
{
int temp = *a;
*a = *b;
*b = temp;
}
20
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];
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);
}
}
23
24
Perform Push, Pop & Display Operations on Stack Using Array
#include <stdio.h>
#include <stdlib.h>
struct Stack
{
int size;
int top;
int *S;
};
void create(struct Stack *st)
{
printf("Enter Size");
scanf("%d",&st->size);
st->top=-1;
st->S=(int *)malloc(st->size*sizeof(int));
}
void Display(struct Stack st)
{
int i;
for(i=st.top;i>=0;i--)
printf("%d ",st.S[i]);
printf("n");
}
void push(struct Stack *st,int x)
{
if(st->top==st->size-1)
printf("Stack overflown");
else
{
st->top++;
st->S[st->top]=x;
}
}
int pop(struct Stack *st)
{
int x=-1;
if(st->top==-1)
printf("Stack Underflown");
else
{
x=st->S[st->top--];
}
return x;
}
int peek(struct Stack st,int index)
{
int x=-1;
if(st.top-index+1<0)
printf("Invalid Index n");
x=st.S[st.top-index+1];
return x;
}
25
int isEmpty(struct Stack st)
{
if(st.top==-1)
return 1;
return 0;
}
int isFull(struct Stack st)
{
return st.top==st.size-1;
}
int stackTop(struct Stack st)
{
if(!isEmpty(st))
return st.S[st.top];
return -1;
}
int main()
{
int data = 0;
int i = 0;
struct Stack stack;
stack.top = -1;
printf("Enter Stack Size: ");
scanf("%d",&stack.size);
stack.S = (int*)malloc(stack.size*sizeof(int));
int ch = 0;
while(1)
{
printf("nEnter 1: Push Operation 2: Pop 3: Peek 4: stackTop 5: Display, Press Any
Other Key to Exitn");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("nEnter data: ");
scanf("%d",&data);
push(&stack,data);
break;
case 2:
printf("nValue poped from the stack: %d",pop(&stack));
break;
case 3:
printf("nEnter the position: ");
scanf("%d",&i);
printf("nValue at positon %d is %d",i,peek(stack,i));
break;
case 4:
printf("nValue at top of stack: %d",stackTop(stack));
break;
case 5:
Display(stack);
break;
default:
exit(0)
}
}
return 0;
}
26
27
WAP to perform insertion, deletion & display operations on
circular queue using array
#include <stdio.h>
#include <stdlib.h>
struct Queue
{
int size;
int front;
int rear;
int* Q;
};
void create(struct Queue* q, int size)
{
q->size = size;
q->front = q->rear = 0;
q->Q = (int*)malloc(q->size * sizeof(int));
}
void enqueue(struct Queue* q, int x)
{
if ((q->rear + 1) % q->size == q->front)
printf("Queue is Full");
else
{
q->rear = (q->rear + 1) % q->size;
q->Q[q->rear] = x;
}
}
int dequeue(struct Queue* q)
{
int x = -1;
if (q->front == q->rear)
printf("Queue is Emptyn");
else {
q->front = (q->front + 1) % q->size;
x = q->Q[q->front];
}
return x;
}
void Display(struct Queue q)
{
int i = q.front + 1;
do
{
printf("%d ", q.Q[i]);
i = (i + 1) % q.size;
} while (i != (q.rear + 1) % q.size);
printf("n");
}
int main()
{
struct Queue q;
int size = 0;
int data =0;
printf("Enter the size of the Queue: ");
28
scanf("%d",&size);
create(&q,size);
int ch = 0;
while(1)
{
printf("nEnter 1: Enqueue 2: Dequeue 3: Display, Press Any Other Key to
Exitn");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("nEnter data: ");
scanf("%d",&data);
enqueue(&q,data);
break;
case 2:
printf("nValue dequeued: %d",dequeue(&q));
break;
case 3:
Display(q);
break;
default:
exit(0);
}
}
return 0;
}
29
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);
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;
}
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;
}

Contenu connexe

Similaire à DSA.pdf

Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File Rahul Chugh
 
Data Structure in C Programming Language
Data Structure in C Programming LanguageData Structure in C Programming Language
Data Structure in C Programming LanguageArkadeep Dey
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020vrgokila
 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab filesNitesh Dubey
 
Intoduction to dynamic memory allocation
Intoduction to dynamic memory allocationIntoduction to dynamic memory allocation
Intoduction to dynamic memory allocationUtsav276
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Er Ritu Aggarwal
 
Input output functions
Input output functionsInput output functions
Input output functionshyderali123
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2YOGESH SINGH
 
(Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++ (Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++ Eli Diaz
 
(Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++ (Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++ Eli Diaz
 
Sorting programs
Sorting programsSorting programs
Sorting programsVarun Garg
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdfanujmkt
 

Similaire à DSA.pdf (20)

Ds
DsDs
Ds
 
C programs
C programsC programs
C programs
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
array.ppt
array.pptarray.ppt
array.ppt
 
Data Structure in C Programming Language
Data Structure in C Programming LanguageData Structure in C Programming Language
Data Structure in C Programming Language
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
 
Intoduction to dynamic memory allocation
Intoduction to dynamic memory allocationIntoduction to dynamic memory allocation
Intoduction to dynamic memory allocation
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
SaraPIC
SaraPICSaraPIC
SaraPIC
 
lets play with "c"..!!! :):)
lets play with "c"..!!! :):)lets play with "c"..!!! :):)
lets play with "c"..!!! :):)
 
C questions
C questionsC questions
C questions
 
Input output functions
Input output functionsInput output functions
Input output functions
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
(Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++ (Meta 5) ejemplo vectores dev c++
(Meta 5) ejemplo vectores dev c++
 
(Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++ (Meta 5) ejemplo vectores 2 dev c++
(Meta 5) ejemplo vectores 2 dev c++
 
Examples sandhiya class'
Examples sandhiya class'Examples sandhiya class'
Examples sandhiya class'
 
Sorting programs
Sorting programsSorting programs
Sorting programs
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
 

Plus de Rishab Saini

Plus de Rishab Saini (9)

PythonPF.pdf
PythonPF.pdfPythonPF.pdf
PythonPF.pdf
 
DBMS.pdf
DBMS.pdfDBMS.pdf
DBMS.pdf
 
Resonance.pptx
Resonance.pptxResonance.pptx
Resonance.pptx
 
SQL 3.pptx
SQL 3.pptxSQL 3.pptx
SQL 3.pptx
 
PowerFactorImprovement.pptx
PowerFactorImprovement.pptxPowerFactorImprovement.pptx
PowerFactorImprovement.pptx
 
Indian Premier League
Indian Premier LeagueIndian Premier League
Indian Premier League
 
Poverty
PovertyPoverty
Poverty
 
Poetry
PoetryPoetry
Poetry
 
Impact of Social media
Impact of Social mediaImpact of Social media
Impact of Social media
 

Dernier

Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwaitjaanualu31
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Servicemeghakumariji156
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityMorshed Ahmed Rahath
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxmaisarahman1
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdfKamal Acharya
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxSCMS School of Architecture
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...drmkjayanthikannan
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...Amil baba
 
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEGEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEselvakumar948
 

Dernier (20)

Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEGEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
 

DSA.pdf

  • 1. Data Structures & Algorithms Practical File Submitted To: Dr. Amita Dhankhar Submitted By: Rishab 25634 CSE-B
  • 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
  • 3. 2
  • 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"); }
  • 5. 4 int l_search(int* A,int size,int x) { for(int i = 0;i<size;i++) { if(A[i]==x) { return i; } } return -1; }
  • 6. 5
  • 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; }
  • 9. 8
  • 10. 9 WAP To Sort Elements Using Bubble Sort #include<stdio.h> #include<stdlib.h> int* createArray(int size); void bubbleSort(int A[],int size); void swap(int *a,int* b); void display(int A[],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); bubbleSort(A,size); 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 swap(int *a, int*b) { int temp = *a; *a = *b; *b = temp; } void bubbleSort(int A[], int size) { for(int i = 0; i<size-1;i++) { for(int j = 0; j<size-1-i;j++) { if(A[j]>A[j+1]) { swap(&A[j],&A[j+1]); } } } }
  • 11. 10 void display(int* A,int size) { printf("n["); for(int i = 0;i<size;i++) { printf("%d ",A[i]); } printf("]n"); }
  • 12. 11
  • 13. 12 WAP To Sort Elements Using Insertion Sort #include<stdio.h> #include<stdlib.h> int* createArray(int size); void insertionSort(int A[],int size); void display(int A[],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); insertionSort(A,size); printf("Sorted Array: "); display(A,size); return 0; } void insertionSort(int A[], int size) { int i, x, j; for (i = 1; i < size; i++) { x = A[i]; j = i - 1; while (j >= 0 && A[j] > x) { A[j + 1] = A[j]; j = j - 1; } A[j + 1] = x; } } 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; }
  • 14. 13 void display(int* A,int size) { printf("n["); for(int i = 0;i<size;i++) { printf("%d ",A[i]); } printf("]n"); }
  • 15. 14
  • 16. 15 WAP to Sort Elements Using Selection Sort #include<stdio.h> #include<stdlib.h> int* createArray(int size); void insertionSort(int A[],int size); void display(int A[],int size); void swap(int *a,int* b); int main() { int size = 0; printf("Enter size of Array: "); scanf("%d",&size); int* A = createArray(size); printf("Original Array:n"); display(A,size); insertionSort(A,size); printf("Sorted Array: "); display(A,size); return 0; } void insertionSort(int A[], int size) { int i, j, min; for (i = 0; i < size-1; i++) { min = i; for (j = i+1; j < size; j++) if (A[j] < A[min]) min = j; swap(&A[min], &A[i]); } } 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; }
  • 17. 16 void display(int* A,int size) { printf("n["); for(int i = 0;i<size;i++) { printf("%d ",A[i]); } printf("]n"); } void swap(int *a, int*b) { int temp = *a; *a = *b; *b = temp; }
  • 18. 17
  • 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); }
  • 20. 19 } 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 swap(int *a, int*b) { int temp = *a; *a = *b; *b = temp; }
  • 21. 20
  • 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); } }
  • 24. 23
  • 25. 24 Perform Push, Pop & Display Operations on Stack Using Array #include <stdio.h> #include <stdlib.h> struct Stack { int size; int top; int *S; }; void create(struct Stack *st) { printf("Enter Size"); scanf("%d",&st->size); st->top=-1; st->S=(int *)malloc(st->size*sizeof(int)); } void Display(struct Stack st) { int i; for(i=st.top;i>=0;i--) printf("%d ",st.S[i]); printf("n"); } void push(struct Stack *st,int x) { if(st->top==st->size-1) printf("Stack overflown"); else { st->top++; st->S[st->top]=x; } } int pop(struct Stack *st) { int x=-1; if(st->top==-1) printf("Stack Underflown"); else { x=st->S[st->top--]; } return x; } int peek(struct Stack st,int index) { int x=-1; if(st.top-index+1<0) printf("Invalid Index n"); x=st.S[st.top-index+1]; return x; }
  • 26. 25 int isEmpty(struct Stack st) { if(st.top==-1) return 1; return 0; } int isFull(struct Stack st) { return st.top==st.size-1; } int stackTop(struct Stack st) { if(!isEmpty(st)) return st.S[st.top]; return -1; } int main() { int data = 0; int i = 0; struct Stack stack; stack.top = -1; printf("Enter Stack Size: "); scanf("%d",&stack.size); stack.S = (int*)malloc(stack.size*sizeof(int)); int ch = 0; while(1) { printf("nEnter 1: Push Operation 2: Pop 3: Peek 4: stackTop 5: Display, Press Any Other Key to Exitn"); scanf("%d",&ch); switch(ch) { case 1: printf("nEnter data: "); scanf("%d",&data); push(&stack,data); break; case 2: printf("nValue poped from the stack: %d",pop(&stack)); break; case 3: printf("nEnter the position: "); scanf("%d",&i); printf("nValue at positon %d is %d",i,peek(stack,i)); break; case 4: printf("nValue at top of stack: %d",stackTop(stack)); break; case 5: Display(stack); break; default: exit(0) } } return 0; }
  • 27. 26
  • 28. 27 WAP to perform insertion, deletion & display operations on circular queue using array #include <stdio.h> #include <stdlib.h> struct Queue { int size; int front; int rear; int* Q; }; void create(struct Queue* q, int size) { q->size = size; q->front = q->rear = 0; q->Q = (int*)malloc(q->size * sizeof(int)); } void enqueue(struct Queue* q, int x) { if ((q->rear + 1) % q->size == q->front) printf("Queue is Full"); else { q->rear = (q->rear + 1) % q->size; q->Q[q->rear] = x; } } int dequeue(struct Queue* q) { int x = -1; if (q->front == q->rear) printf("Queue is Emptyn"); else { q->front = (q->front + 1) % q->size; x = q->Q[q->front]; } return x; } void Display(struct Queue q) { int i = q.front + 1; do { printf("%d ", q.Q[i]); i = (i + 1) % q.size; } while (i != (q.rear + 1) % q.size); printf("n"); } int main() { struct Queue q; int size = 0; int data =0; printf("Enter the size of the Queue: ");
  • 29. 28 scanf("%d",&size); create(&q,size); int ch = 0; while(1) { printf("nEnter 1: Enqueue 2: Dequeue 3: Display, Press Any Other Key to Exitn"); scanf("%d",&ch); switch(ch) { case 1: printf("nEnter data: "); scanf("%d",&data); enqueue(&q,data); break; case 2: printf("nValue dequeued: %d",dequeue(&q)); break; case 3: Display(q); break; default: exit(0); } } return 0; }
  • 30. 29
  • 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; }