Ds Programs
Ds Programs
/*
DS LAB TASK 1A
1a. Write a C program to implement STACK operations using array
Write a C program to implement the folowing operations on STACKS using arrays.
[Link] [Link] [Link] [Link] [Link]/STOP
TEST CASES:
case=t6
input=
1 10
1 20
2
3
4
5
output=
"STACK OPERATIONS USING ARARYS
10 is Pushed into stack[0]
20 is Pushed into stack[1]
20 is Popped from stack[1]
The elements of Stack are :10
Top Most element of the Stack is 10"
case = t1
input=
2
3
4
5
output=
"STACK OPERATIONS USING ARARYS
Cannot POP - STACK UNDERFLOW
Nothing to display - Stack Empty
No PEAK - Stack Empty"
NOTE:
CHECK OUTPUTS FOR ALL THE GIVEN OPERATIONS
NOTE : JUST REMEMEBER EVERY TEST CASE MUST END WITH EXIT OPERATION SO
THAT IT WILL STOP FINITELY.
*/
//Start writing program from here
#include <stdio.h>
#include <stdlib.h>
#define Max 4
int stack[Max],choice,n,top,x,i;
void push(void);
void pop(void);
void peak(void);
void display(void);
int main(){
top=-1;
printf("STACK OPERATIONS USING ARRAYS");
do{
scanf("%d",&choice);
switch(choice){
case 1:{
push();
break;
}
case 2:{
pop();
break;
}
case 3:{
display();
break;
}
case 4:{
peak();
break;
}
case 5:
exit(0);
default :
{
printf("\nInvalid Choice");
}
}
}
while(choice!=5);
return 0;
}
void push(){
scanf("%d",&x);
if (top>=Max-1){
printf("\nCannot PUSH - STACK OVERFLOW");
}else{
top++;
stack[top]=x;
printf("\n%d is Pushed into stack[%d]",x,top);
}
}
void pop(){
if (top<=-1){
printf("\nCannot POP - STACK UNDERFLOW");
}else{
}else{
printf("\nNothing to display - Stack Empty");
}
}
void peak(){
if (top<0){
printf("\nNo PEAK - Stack Empty");
}else{
printf("\nTop Most element of the Stack is %d",stack[top]);
}
}
TASK 1B
/*
DS LAB TASK 1B
1b. Write a C program to implement QUEUE operations using array
Write a C program to implement the folowing operations on QUEUE using arrays.
NOTE : [Link] [Link] [Link] [Link]/STOP
NOTE: ASSUME MAX SIZE OF QUEUE AS 4
Also check for "Invalid Option"
INPUT FORMAT:OPERATIONS AS INTEGERS GIVEN IN NOTE AND TERMINATED
ALWAYS BY EXIT OPERATION.
OUTPUT FORMAT: AS PER THE TEST CASE
TEST CASES:
case = t2
input =
3
2
4
output =
"QUEUE OPERATIONS USING ARRAY
Nothing to Display - QUEUE EMPTY
Cannot Dequeue - QUEUE UNDERFLOW"
case = t3
input =
1 10
1 20
3
2
2
2
3
4
output =
"QUEUE OPERATIONS USING ARRAY
20 is added into Q[1]
The elements of QUEUE are : 10 20
10 is dequeued from Q[0]
20 is dequeued from Q[1]
Cannot Dequeue - QUEUE UNDERFLOW
Nothing to Display - QUEUE EMPTY"
}
}
void dequeue(){
if (front==-1 || front>rear){
printf("Cannot Dequeue - QUEUE UNDERFLOW\n");
}else{
printf("%d is dequeued from Q[%d]\n",Q[front],front);
front++;
if (front>rear){
front=rear=-1;
}
}
}
void display(){
int i;
if (front==-1){
printf("Nothing to Display - QUEUE EMPTY\n");
}else{
printf("The elements of QUEUE are : ");
for (i=front;i<=rear;i++){
printf("%d ",Q[i]);
}
printf("\n");
}
}
int main(){
int choice,value;
printf("QUEUE OPERATIONS USING ARRAY\n");
while(1){
scanf(" %d",&choice);
switch(choice){
case 1:
scanf("%d",&value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid Choice\n");
}
}
return 0;
}
TASK 1C
/*
DS LAB TASK 1C
1c. Write a C programto implement Circular Queue operations using arrays
Write a C program to implement the folowing operations on CIRCULAR QUEUE
NOTE : [Link] [Link] [Link] [Link]/STOP
TESTCASES:
case = t1
input = 1
10
1
20
1
30
1
40
1
50
4
output = " QUEUE IS FULL "
case = t4
input = 1
10
20
4
output =
" INVALID CHOICE "
TESTCASES:
case = t1
input = 1
10
1
20
1
30
1
40
1
50
4
output = " QUEUE IS FULL "
case = t4
input = 1
10
20
4
output =
" INVALID CHOICE "
}
}
printf("\n");
}
TASK 2A
case=t9
input=a+b*c-(d+e)
output=
"Given Infix expression is : a+b*c-(d+e)
Postfix expression is : abc*+de+-"
*/
//Start writing program from heee
#include<stdio.h>
#include<ctype.h>
#include<string.h>
char stack[MAX];
int top=-1;
void push(char x)
{
stack[++top]=x;
}
char pop()
{
if(top==-1)
return '\0';
else
return stack[top--];
}
int priority(char x)
{
if(x=='+'||x=='-')
return 1;
if(x=='*'||x=='/')
return 2;
return 0;
}
int main()
{
char infix[MAX],postfix[MAX];
int i,j=0;
int open=0,close=0;
scanf("%s",infix);
for(i=0;i<strlen(infix);i++)
{
char ch=infix[i];
if(isalnum(ch))
{
postfix[j++]=ch;
}
else if(ch=='(')
{
push(ch);
open++;
}
else if(ch==')')
{
close++;
if(top==-1)
{
printf("Stack underflow - Open Brace Missing");
return 0;
}
pop();
}
else if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
{
while(top!=-1 && stack[top]!='(' && priority(stack[top])>=priority(ch))
{
postfix[j++]=pop();
}
push(ch);
}
else
{
printf("Incorrect Infix Expression - Invalid Symbols");
return 0;
}
}
if(open>close)
{
printf("Invalid Infix Expression - Missing close braces");
return 0;
}
while(top!=-1)
{
if(stack[top]=='(')
{
printf("Invalid Infix Expression - Missing close braces");
return 0;
}
postfix[j++]=pop();
}
postfix[j]='\0';
return 0;
}
TASK 2B
/*DS LAB
TASK 2B Write a C program to evalulate a postfix expression.
INPUT FORMAT:
Enter a mathematical expression in postfix notation.
Ensure that operands(numbers)and operators(+, -, *, /) are not separated by spaces.
OUTPUT FORMAT:
Result after evaluation of the Postfix Expression.
or
Error codes for invalid expressions as per test cases.
Test Cases:
Case=t8
Input=ab*c-d+
25
7
14
6
Output=
"Result of Postfix expression ab*c-d+ = 167"
Case=t2
Input=ab*c-d+
25
7
14
6
Output=
"Result of Postfix expression ab*c-d+ = 167"
case=t4
input=
ab$
2
0
output=
"Invalid symbols in the Postfix Expression"
*/
//Start writing program from here
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int stack[MAX];
int top=-1;
void push(int x)
{
stack[++top]=x;
}
int pop()
{
if(top==-1)
return -9999;
return stack[top--];
}
int main()
{
char postfix[MAX];
int value[26];
int i,a,b,res;
for(i=0;i<26;i++)
value[i]=-1;
scanf("%s",postfix);
for(i=0;i<strlen(postfix);i++)
{
if(isalpha(postfix[i]))
{
int idx = tolower(postfix[i])-'a';
if(value[idx]==-1)
scanf("%d",&value[idx]);
}
}
for(i=0;i<strlen(postfix);i++)
{
char ch=postfix[i];
if(isdigit(ch))
{
push(ch-'0');
}
else if(isalpha(ch))
{
int idx = tolower(ch)-'a';
push(value[idx]);
}
else if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
{
b=pop();
a=pop();
if(a==-9999 || b==-9999)
{
printf("Stack underflow - Missing Operands");
return 0;
}
switch(ch)
{
case '+': res=a+b; break;
case '-': res=a-b; break;
case '*': res=a*b; break;
case '/': res=a/b; break;
}
push(res);
}
else
{
printf("Invalid symbols in the Postfix Expression");
return 0;
}
}
if(top!=0)
{
printf("Invalid or Incomplete Postfix expression");
return 0;
}
return 0;
}
TASK 3
//sll implementation
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*start=NULL;
void SLLdisplay();
void SLLsearch(int x);
void SLLdeletePos(int pos);
void SLLinsertPos(int pos ,int val);
int SLLCountNodes();
void SLLreverse(struct node *temp);
int main()
{
int ch,val,pos,x;
while(1)
{
scanf("%d",&ch);
switch(ch)
{
case 1:
scanf("%d %d",&pos,&val);
SLLinsertPos(pos,val);
break;
case 2:
scanf("%d",&pos);
SLLdeletePos(pos);
break;
case 3:
SLLdisplay();
break;
case 4:
scanf("%d",&x);
SLLsearch(x);
break;
case 5:
printf("\nNo of nodes in SLL = %d",SLLCountNodes());
break;
case 6:
if(start==NULL)
printf("\nEmpty SLL - Cannot Reverse Display");
else
{
printf("\nSLL ELEMENTS IN REVERSE ORDER\n");
SLLreverse(start);
}
break;
case 7:
exit(0);
default:
printf("\nInvalid Choice");
}
}
}
if(pos==1)
{
newnode->next=start;
start=newnode;
return;
}
temp=start;
if(temp==NULL)
{
printf("\nPosition does not exist - Cannot insert into SLL");
free(newnode);
return;
}
newnode->next=temp->next;
temp->next=newnode;
}
if(start==NULL)
{
printf("\nPosition does not exist - Cannot delete from SLL");
return;
}
if(pos==1)
{
temp=start;
start=start->next;
printf("\nDeleted element from SLL is %d",temp->data);
free(temp);
return;
}
temp=start;
if(temp==NULL)
{
printf("\nPosition does not exist - Cannot delete from SLL");
return;
}
prev->next=temp->next;
printf("\nDeleted element from SLL is %d",temp->data);
free(temp);
}
void SLLdisplay()
{
struct node *temp=start;
if(start==NULL)
{
printf("\nEmpty SLL - Cannot display");
return;
}
while(temp!=NULL)
{
printf("%d ->",temp->data);
temp=temp->next;
}
}
void SLLsearch(int x)
{
struct node *temp=start;
if(start==NULL)
{
printf("\nSLL Empty - Cannot Search");
return;
}
while(temp!=NULL)
{
if(temp->data==x)
{
printf("\n%d is found in SLL",x);
return;
}
temp=temp->next;
}
int SLLCountNodes()
{
struct node *temp=start;
int count=0;
while(temp!=NULL)
{
count++;
temp=temp->next;
}
return count;
}
SLLreverse(temp->next);
printf("%d ",temp->data);
}
TASK 4
/* TASK 7:
[Link] a C program to implement the folowing operations on CIRCULAR LINKED LIST
OPERATIONS:
[Link] [Link] [Link] [Link] [Link] [Link] Display [Link]
Also check for "Invalid Choice"
INPUT FORMAT :
FOR INSERT - READ VALUE AND POSITION
FOR DELETE - READ POSITION
FOR SEARCH - READ KEY
OUTPUT FORMAT:
DISPLAY AS PER TEST CASES.
TEST CASE :
CASE=T10
INPUT=
1
100 1
1
200 1
1
300 1
5
6
3
4
2500
4
200
2
2
2
10
1
1000 10
7
OUTPUT=
CLL OPERATIONS
100 is inserted into CLL at position 1
200 is inserted into CLL at position 1
300 is inserted into CLL at position 1
No of nodes in CLL = 3
CLL Reverse Display :100 ->200 -> 300 ->
CLL elements are :300 ->200 ->100 ->
2500 is not found in CLL
200 is found in CLL
Deleted Node form CLL is 200 from position 2
Cannot Delete from CLL - Invalid Position or CLL Empty
Cannot Insert in CLL - Invalid Position
struct node
{
int data;
struct node *next;
};
int countNodes()
{
int count = 0;
if(head == NULL)
return 0;
do
{
count++;
temp = temp->next;
} while(temp != head);
return count;
}
if(head == NULL)
{
newnode->next = newnode;
head = newnode;
printf("\n%d is inserted into CLL at position %d", value, pos);
return;
}
if(pos == 1)
{
struct node *last = head;
while(last->next != head)
last = last->next;
newnode->next = head;
last->next = newnode;
head = newnode;
newnode->next = temp->next;
temp->next = newnode;
if(pos == 1)
{
deletedValue = head->data;
if(head->next == head)
{
free(head);
head = NULL;
}
else
{
struct node *last = head;
while(last->next != head)
last = last->next;
head = head->next;
last->next = head;
free(temp);
}
deletedValue = temp->data;
prev->next = temp->next;
free(temp);
do
{
printf("%d ->", temp->data);
temp = temp->next;
} while(temp != head);
}
do
{
if(temp->data == key)
{
printf("\n%d is found in CLL", key);
return;
}
temp = temp->next;
} while(temp != head);
void reverseDisplay()
{
int count = countNodes();
if(count == 0)
{
printf("\nEmpty CLL- Nothing TO REV Display");
return;
}
int i, j;
struct node *temp;
int main()
{
int choice, value, pos, key;
printf("CLL OPERATIONS");
while(1)
{
scanf("%d", &choice);
switch(choice)
{
case 1:
scanf("%d%d", &value, &pos);
insertNode(value, pos);
break;
case 2:
scanf("%d", &pos);
deleteNode(pos);
break;
case 3:
display();
break;
case 4:
scanf("%d", &key);
searchNode(key);
break;
case 5:
printf("\nNo of nodes in CLL = %d", countNodes());
break;
case 6:
reverseDisplay();
break;
case 7:
return 0;
default:
printf("\nInvalid Choice");
}
}
return 0;
}
TASK 5
/*
DS LAB TASK 5
5. Write a C program to implement Double Linked List operations – create, insert, delete and
display.
case = t8
input =
1
10
2
1
34
3
4
200
5
6
output =
IMPLEMENTING OPERATIONS ON DOUBLE LINKED LIST.
Position does not exist - Cannot insert into DLL
Position does not exist - Cannot delete from DLL
Invalid Choice
Empty DLL - Cannot Display.
DLL Empty - Cannot Search
Empty DLL - Cannot Perform Reverse Display.
CASE=T9
INPUT=
1
1
100
1
2
200
1
3
300
4
300
3
4
250
6
OUTPUT=
IMPLEMENTING OPERATIONS ON DOUBLE LINKED LIST.
300 is found in DLL
THE DLL ELEMENTS :
100 ->200 ->300 ->
250 is not found in DLL
CHECK OUTPUT FOR ALL THE GIVEN OPERATIONS INCLUDING INVALID CASES
NOTE : JUST REMEMEBER EVERY TEST CASE MUST END WITH EXIT OPERATION SO
THAT IT WILL STOP FINITELY.
PREFER A "\n" BEFORE EVERY PRINTF STATEMENT.
*/
//Start writing program from here
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};
int countNodes()
{
int count = 0;
struct node *temp = head;
while(temp != NULL)
{
count++;
temp = temp->next;
}
return count;
}
if(pos == 1)
{
newnode->next = head;
if(head != NULL)
head->prev = newnode;
head = newnode;
return;
}
struct node *temp = head;
int i;
newnode->next = temp->next;
newnode->prev = temp;
if(temp->next != NULL)
temp->next->prev = newnode;
temp->next = newnode;
}
if(pos == 1)
{
deletedValue = head->data;
head = head->next;
if(head != NULL)
head->prev = NULL;
free(temp);
deletedValue = temp->data;
temp->prev->next = temp->next;
if(temp->next != NULL)
temp->next->prev = temp->prev;
free(temp);
void display()
{
if(head == NULL)
{
printf("\nEmpty DLL - Cannot Display.");
return;
}
while(temp != NULL)
{
printf("%d ->", temp->data);
temp = temp->next;
}
}
while(temp != NULL)
{
if(temp->data == key)
{
printf("\n%d is found in DLL", key);
return;
}
temp = temp->next;
}
while(temp->next != NULL)
temp = temp->next;
while(temp != NULL)
{
printf("%d ->", temp->data);
temp = temp->prev;
}
}
int main()
{
int choice, pos, value;
while(1)
{
scanf("%d", &choice);
switch(choice)
{
case 1:
scanf("%d", &pos);
case 3:
display();
break;
case 4:
scanf("%d", &value);
searchNode(value);
break;
case 5:
reverseDisplay();
break;
case 6:
return 0;
default:
printf("\nInvalid Choice");
}
}
return 0;
}
TASK 6
/*
TASK - 6a - Develop a C code for preorder, inorder and postorder traversals of
a Binary Search Tree using recursion.
i. insert ii. preorder iii. inorder iv. postorder v. exit.
case = t2
input= 1
45
1
23
1
67
1
12
2
3
4
5
output=
"
PREORDER TRAVERSAL OF BINARY SEARCH TREE
45 23 12 67
INORDER TRAVERSAL OF BINARY SEARCH TREE
12 23 45 67
POSTORDER TRAVERSAL OF BINARY SEARCH TREE
12 23 67 45 "
case = t5
input= 1
0
1
-3
1
5
1
-10
1
2
1
8
2
3
4
5
output=
"
PREORDER TRAVERSAL OF BINARY SEARCH TREE
0 -3 -10 5 2 8
INORDER TRAVERSAL OF BINARY SEARCH TREE
-10 -3 0 2 5 8
POSTORDER TRAVERSAL OF BINARY SEARCH TREE
-10 -3 2 8 5 0 "
NOTE:
CHECK OUTPUTS FOR ALL THE GIVEN OPERATIONS
NOTE : JUST REMEMBER EVERY TEST CASE MUST END WITH EXIT OPERATION SO
THAT IT WILL STOP FINITELY.
*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
newnode->data = value;
newnode->left = NULL;
newnode->right = NULL;
return newnode;
}
return root;
}
int main()
{
int choice, value;
while(1)
{
scanf("%d", &choice);
switch(choice)
{
case 1:
scanf("%d", &value);
root = insert(root, value);
break;
case 2:
if(root == NULL)
{
printf("\nBST Empty\n");
}
else
{
printf("\nPREORDER TRAVERSAL OF BINARY SEARCH TREE\n");
preorder(root);
printf("\n");
}
break;
case 3:
if(root == NULL)
{
printf("BST Empty\n");
}
else
{
printf("INORDER TRAVERSAL OF BINARY SEARCH TREE\n");
inorder(root);
printf("\n");
}
break;
case 4:
if(root == NULL)
{
printf("BST Empty\n");
}
else
{
printf("POSTORDER TRAVERSAL OF BINARY SEARCH TREE\n");
postorder(root);
printf("\n");
}
break;
case 5:
exit(0);
default:
printf("Invalid Choice\n");
}
}
return 0;
}
TASK 6B
/*
TASK 6b. Design a C program for level order traversal of a Binary Search Tree.
case = t1
input =
2
3
output =
Level Order Traversal of BST :
Binary Search Tree is Empty
case = t2
input =
1
10
1
20
1
30
1
5
2
3
output =
Level Order Traversal of BST :
10 5 20 30
*/
//start writing program from here
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
newnode->data = value;
newnode->left = NULL;
newnode->right = NULL;
return newnode;
}
return root;
}
rear++;
queue[rear] = temp;
}
return temp;
}
int isempty()
{
if(front > rear || front == -1)
{
return 1;
}
return 0;
}
if(root == NULL)
{
printf("Binary Search Tree is Empty\n");
return;
}
enqueue(root);
while(!isempty())
{
temp = dequeue();
if(temp->left != NULL)
{
enqueue(temp->left);
}
if(temp->right != NULL)
{
enqueue(temp->right);
}
}
printf("\n");
}
int main()
{
int choice, value;
while(1)
{
scanf("%d", &choice);
switch(choice)
{
case 1:
scanf("%d", &value);
root = insert(root, value);
break;
case 2:
printf("Level Order Traversal of BST :");
levelorder(root);
break;
case 3:
exit(0);
default:
printf("Invalid Choice\n");
}
}
return 0;
}
TASK 7A
/*
DS LAB TASK 7A
7A. Implement the following operations on Binary Search Tree i. Create ii. Insert iii. Search
NOTE OPTIONS: (i)CREATE-INSERT (ii) SEARCH (iii)EXIT
Also check for "Invalid Option"
INPUT FORMAT: AS PER TEST CASE
OUTPUT FORMAT: AS PER THE TEST CASE
SAMPLE TEST CASES :
case = t5
input=
1
100
1
200
2
300
2
100
3
output=
"Binary Search Tree Operations
100 is inserted into BST.
200 is inserted into BST.
300 is not found in BST.
100 is found in BST."
*/
//Start writing program from here
// BST CREATE INSERT SEARCH
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
// Create Node
struct node* createNode(int value)
{
struct node *newnode;
newnode->data = value;
newnode->left = NULL;
newnode->right = NULL;
return newnode;
}
// Insert into BST
struct node* insert(struct node *root, int value)
{
if(root == NULL)
{
return createNode(value);
}
return root;
}
// Search Function
int search(struct node *root, int key)
{
if(root == NULL)
{
return 0;
}
if(root->data == key)
{
return 1;
}
int main()
{
int choice, value;
switch(choice)
{
case 1:
scanf("%d", &value);
break;
case 2:
scanf("%d", &value);
if(root == NULL)
{
printf("EMPTY BST - CANNOT SEARCH.\n");
}
else
{
if(search(root, value))
{
printf("%d is found in BST.\n", value);
}
else
{
printf("%d is not found in BST.\n", value);
}
}
break;
case 3:
exit(0);
default:
printf("Invalid Choice.\n");
}
}
return 0;
}
TASK 7B
/*
DS LAB TASK 7B
7b. Implement the following operations on Binary Search Tree
i. Delete ii. Display (INORDER)
NOTE THE OPTIONS: [Link] [Link] [Link]-INORDER [Link]
Also check for "Invalid Option"
NOTE : USE THE PREXISTING INSERT FUNCTION GIVEN IN THE CODE AS CASE 1 IN
LOGIC IMPLEMENTATION.
INPUT FORMAT: AS PER TEST CASE
OUTPUT FORMAT: AS PER THE TEST CASE
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node*root = NULL;
else {
struct Node* successor = findMin(root->right);
root->data = successor->data;
int dummyFound = 0;
root->right = deleteNode(root->right, successor->data, &dummyFound);
}
}
return root;
}
int main() {
struct Node* root = NULL;
int choice, data, found;
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
scanf("%d", &data);
if (root == NULL) {
printf("\nEmpty BST - Cannot delete");
} else {
found = 0;
root = deleteNode(root, data, &found);
if (found)
printf("\n%d is found and deleted from BST", data);
else
printf("\n%d is not found in BST - So cannot delete\n", data);
}
break;
case 3:
if (root == NULL) {
printf("\nEmpty BST - Nothing to Display");
} else {
printf("\nThe elements of BST - INORDER :");
inorder(root);
//printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Invalid Choice.\n");
}
}
return 0;
}
TASK 8A
/*
TASK 8A. Implement the following operations on BINARY SEARCH TREE using a C
program.
TASK 8-BST OPERATIONS - Countnodes Height Minimum node Maximum node
(i) CREATE/INSERT (ii) COUNT NODES (iii) HEIGHT (iV) MINIMUM (v) MAXIMUM (vi) Exit
NOTE:
CHECK OUTPUTS FOR ALL THE GIVEN OPERATIONS
NOTE : JUST REMEMBER EVERY TEST CASE MUST END WITH EXIT OPERATION SO
THAT IT WILL STOP FINITELY.
*/
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node*left,*right;
};
struct node*root = NULL;
struct node*create(int data){
struct node*newnode = (struct node*)malloc(sizeof(struct node));
newnode -> data = data;
newnode -> left = newnode -> right = NULL;
return newnode;
}
struct node*insert(struct node*root,int data){
if(root == NULL)
return create(data);
if(data < root -> data)
root -> left = insert(root -> left,data);
if(data > root -> data)
root -> right = insert(root -> right,data);
return root;
}
int countnodes(struct node*root){
if(root == NULL)
return 0;
return 1 + countnodes(root -> left) + countnodes(root -> right);
}
int height(struct node*root){
int lh,rh;
if(root == NULL)
return -1;
lh = height(root -> left);
rh = height(root -> right);
if(lh > rh)
return lh + 1;
else
return rh + 1;
}
int minimum(struct node*root){
while(root -> left != NULL)
root = root -> left;
return root -> data;
}
int maximum(struct node*root){
while(root -> right != NULL)
root = root -> right;
return root -> data;
}
int main(){
int ch,val;
printf("\nBinary Search Tree Operations\n");
while(1){
scanf("%d",&ch);
switch(ch){
case 1:
scanf("%d",&val);
root = insert(root,val);
break;
case 2:
printf("NODES IN BST = %d\n",countnodes(root));
break;
case 3:
if(root == NULL)
printf("BST EMPTY - NO HEIGHT\n");
else
printf("HEIGHT OF BST = %d\n",height(root));
break;
case 4:
if(root == NULL)
printf("BST EMPTY - NO MINIMUM\n");
else
printf("MINIMUM VALUE IN BST = %d\n",minimum(root));
break;
case 5:
if(root == NULL)
printf("BST EMPTY - NO MAXIMUM\n");
else
printf("MAXIMUM VALUE IN BST = %d\n",maximum(root));
break;
case 6:
exit(0);
default:
printf("INVALID CHOICE");
}
}
return 0;
}
TASK 9A
/*
LAB TASK 9A
QUESTION: 9a-Develop a C program for Quick sort.
INPUT FORMAT:
FIRST LINE CONTAINS "n" THE SIZE OF THE LIST.
SECOND LINE CONTAINS "n" integers seperated by a space.
OUTPUT FORMAT:
DISPLAY UNSORTED LIST,SORTING METHOD NAME AND SORTED LIST AS PER THE
TEST CASE
THE OUTPUT VALUES ARE SEPERATED BY A TRAILING SPACE.
Test Cases:
case = t1
input = 6
-1 6 -45 -8 0 4
output =
"Unsorted List
-1 6 -45 -8 0 4
QUICK SORT
Sorted List
-45 -8 -1 0 4 6"
*/
//Start writing program from here
#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);
quicksort(arr,low,pi-1);
quicksort(arr,pi+1,high);
}
}
int main(){
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
printf("Unsorted List\n");
for(int i=0;i<n;i++){
printf("%d ",arr[i]);
}
quicksort(arr,0,n-1);
printf("\nQUICK SORT\nSorted List\n");
for(int i=0;i<n;i++){
printf("%d ",arr[i]);
}
}
TASK 9B
/*
LAB TASK 9B
QUESTION: 9B-Demonstrate Merge sort using a C program.
INPUT FORMAT:
FIRST LINE CONTAINS "n" THE SIZE OF THE LIST.
SECOND LINE CONTAINS "n" integers seperated by a space.
OUTPUT FORMAT:
DISPLAY UNSORTED LIST,SORTING METHOD NAME AND SORTED LIST AS PER THE
TEST CASE
THE OUTPUT VALUES ARE SEPERATED BY A TRAILING SPACE.
Test Cases:
case = t1
input = 6
-1 6 -45 -8 0 4
output =
"Unsorted List
-1 6 -45 -8 0 4
MERGE SORT
Sorted List
-45 -8 -1 0 4 6"
*/
//Start writing program from here
#include<stdio.h>
void mergesort(int arr[],int left,int mid,int right){
int size1=mid-left+1;
int size2=right-mid;
int l[size1],r[size2];
int i,j,k;
for(i=0;i<size1;i++){
l[i]=arr[left+i];
}for(j=0;j<size2;j++){
r[j]=arr[mid+1+j];
}
i=0;
j=0;
k=left;
TASK 9C
/*
LAB TASK 9C
QUESTION: 9C- Design a C program for Radix Sort.
INPUT FORMAT:
FIRST LINE CONTAINS "n" THE SIZE OF THE LIST
SECOND LINE CONTAINS "n" integers seperated by a space
OUTPUT FORMAT:
DISPLAY UNSORTED LIST,SORTING METHOD NAME AND SORTED LIST AS PER THE
TEST CASE.
THE VALUES ARE DISPLAYED BY A TRAILING SPACE.
Test Cases:
case = t1
input = 6
502 674 175 542 874 245
output =
"UNSORTED LIST
502 674 175 542 874 245
RADIX SORT
SORTED LIST
175 245 502 542 674 874 "
*/
//Start writing program from here
#include<stdio.h>
int getmax(int arr[],int n){
int max=arr[0];
for(int i=0;i<n;i++){
if(arr[i]>max){
max=arr[i];
}
}return max;
}
void countingsort(int arr[],int n,int place){
int output[n],count[10]={0};
for(int i=0;i<n;i++){
count[(arr[i]/place)%10]++;
}
for(int i=1;i<10;i++){
count[i]+=count[i-1];
}
for(int i=n-1;i>=0;i--){
output[count[(arr[i]/place)%10]-1]=arr[i];
count[(arr[i]/place)%10]--;
}
for(int i=0;i<n;i++){
arr[i]=output[i];
}
}
void radixsort(int arr[],int n){
int max=getmax(arr,n);
for(int place=1;max/place>0;place*=10){
countingsort(arr,n,place);
}
}
int main(){
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
printf("UNSORTED LIST\n");
for(int i=0;i<n;i++){
printf("%d ",arr[i]);
}
radixsort(arr,n);
printf("\nRADIX SORT\nSORTED LIST\n");
for(int i=0;i<n;i++){
printf("%d ",arr[i]);
}
}
TASK 10A
INPUT FORMAT:
FIRST LINE CONTAINS "n" THE SIZE OF THE LIST.
SECOND LINE CONTAINS "n" integers seperated by a space.
OUTPUT FORMAT:
DISPLAY UNSORTED LIST,SORTING METHOD NAME AND SORTED LIST AS PER THE
TEST CASE
THE OUTPUT VALUES ARE SEPERATED BY A TRAILING SPACE.
Test Cases:
case = t6
input =
7
-98 -567 -345 1000 -1000 54 202
output=
"
TREE SORT - SORTED LIST
-1000 -567 -345 -98 54 202 1000 "
*/
//Start writing program from here
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *left, *right;
};
return root;
}
int main()
{
int n, i, value;
struct Node* root = NULL;
scanf("%d", &n);
return 0;
}
TASK 10B
OUTPUT FORMAT:
DISPLAY UNSORTED LIST,SORTING METHOD NAME AND SORTED LIST AS PER THE
TEST CASE
THE OUTPUT VALUES ARE SEPERATED BY A TRAILING SPACE.
Test Cases:
case = t6
input=10
0987654321
output =
"
UNSORTED LIST
0987654321
HEAP SORT
SORTED LIST
0123456789"
*/
//Start writing program from here
#include<stdio.h>
void heapify(int arr[],int n,int i){
int largest=i;
int left=2*i+1;
int right=2*i+2;
if(left<n && arr[left]>arr[largest]){
largest=left;
}if(right<n && arr[right]>arr[largest]){
largest=right;
}if(largest != i){
int temp=arr[i];
arr[i]=arr[largest];
arr[largest]=temp;
heapify(arr,n,largest);
}
}
void heapsort(int arr[],int n){
for(int i=n/2-1;i>=0;i--){
heapify(arr,n,i);
}for(int i=n-1;i>0;i--){
int temp=arr[0];
arr[0]=arr[i];
arr[i]=temp;
heapify(arr,i,0);
}
}
int main(){
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}printf("\nUNSORTED LIST\n");
for(int i=0;i<n;i++){
printf("%d ",arr[i]);
}heapsort(arr,n);
printf("\nHEAP SORT\nSORTED LIST\n");
for(int i=0;i<n;i++){
printf("%d ",arr[i]);
}
}
TASK 11A
Steps:
1. Read the number of vertices in the graph.
2. Read the adjacency matrix representing the graph.
3. Read the starting node for DFS traversal.
4. Use a recursive function to traverse and print the nodes in DFS order.
Output=
The DFS Traversal of Graph is :
1-2-4-3-5-
*/
//Start writing program from here
#include <stdio.h>
void DFS(int v)
{
int i;
visited[v] = 1;
printf("%d - ", v + 1);
int main()
{
int i, j, start;
scanf("%d", &n);
DFS(start - 1);
return 0;
}
TASK 11B
/*
LAB TASK 11B
11 b. Implement a C program for BFS traversal on graph.
Note : Also visit the finally unvisited nodes.
Do test for invalid start vertex.
INPUT FORMAT: FIRST LINE NO OF VERTICES 'n',
IN NEXT 'n' LINES THE ADJACENCY MATRIX OF THE GRAPH.
LAST LINE IS THE START VERTEX OF THE BFS TRAVERSAL.
OUTPUT FORMAT: DISPLAY THE BFS TARVERSAL AS PER THE GIVEN TEST CASE
FORMAT.
ALSO HANDLE NOT POSSIBLE SITUATIONS ACCORDINGLY.
int graph[MAX][MAX];
int visited[MAX];
int queue[MAX];
int n;
int front=0,rear=-1;
void bfs(int start)
{
queue[++rear] = start;
visited[start] = 1;
int main()
{
int start;
scanf("%d", &n);
scanf("%d", &start);
bfs(start - 1);
for (int i = 0; i < n; i++)
{
if (visited[i] == 0)
{
bfs(i);
}
}
return 0;
}
TASK 12A
/*
LAB TASK 12A
QUESTION:
Task 12A. Implement a C program for the following operations on Hashing:
i. insert ii. delete iii. search iv. Display
v Exit
Also Check for Invalid Case.
Note: Initilaize the Hash Table with -1.
Also Use MAX size as 5.
Test Cases:
case=t4
input=
1
16
1
67
1
56
1
20
1
78
4
3
20
3
55
2
16
3
67
5
output=
Operations on Hash Table
16 is inserted at H[1]
67 is inserted at H[2]
Collision at H[1] in Hash table - Cannot Insert 56
20 is inserted at H[0]
78 is inserted at H[3]
Hash Table
H[0] = 20
H[1] = 16
H[2] = 67
H[3] = 78
H[4] = -1
20 is found in Hash Table
55 is not found in Hash Table
16 is found in the Hash table at H[1] and deleted from it.
67 is found in Hash Tab
*/
//Start writing program from here
#include <stdio.h>
#define MAX 5
int hashTable[MAX];
void initialize()
{
int i;
for (i = 0; i < MAX; i++)
hashTable[i] = -1;
}
if (hashTable[index] == -1)
{
hashTable[index] = key;
printf("%d is inserted at H[%d]\n", key, index);
}
else
{
printf("Collision at H[%d] in Hash table - Cannot Insert %d\n", index, key);
}
}
void deleteKey(int key)
{
int index = key % MAX;
if (hashTable[index] == key)
{
hashTable[index] = -1;
printf("%d is found in the Hash table at H[%d] and deleted from it.\n", key, index);
}
else
{
printf("%d is not found in the Hash table - Cannot Delete\n", key);
}
}
if (hashTable[index] == key)
{
printf("%d is found in Hash Table\n", key);
}
else
{
printf("%d is not found in Hash Table\n", key);
}
}
void display()
{
int i;
printf("Hash Table\n");
for (i = 0; i < MAX; i++)
{
printf("H[%d] = %d\n", i, hashTable[i]);
}
}
int main()
{
int choice, key;
initialize();
switch (choice)
{
case 1:
scanf("%d", &key);
insert(key);
break;
case 2:
scanf("%d", &key);
deleteKey(key);
break;
case 3:
scanf("%d", &key);
search(key);
break;
case 4:
display();
break;
case 5:
return 0;
default:
printf("Invalid Choice\n");
}
}
return 0;
}