0% found this document useful (0 votes)
28 views38 pages

Mca 1ST Sem Programs & Outputs 2022

The document discusses circular queue operations using an array. It includes functions to insert, delete and display elements in a circular queue. Sample outputs are provided showing insertion, deletion and display of elements in the queue.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views38 pages

Mca 1ST Sem Programs & Outputs 2022

The document discusses circular queue operations using an array. It includes functions to insert, delete and display elements in a circular queue. Sample outputs are provided showing insertion, deletion and display of elements in the queue.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

1.

ARRAY OPERATIONS

1. PROGRAM:-

#include<stdio.h>
#include<conio.h>
/* Global variables declaration */
int a[4], n, elem, i, pos;
/*Function Prototype and Definitions*/
void create()
{
printf("\nEnter the size of the array elements: ");
scanf("%d", &n);
printf("\nEnter the elements for the array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
}
void display()
{
int i;
printf("\nThe array elements are:\n");
for(i=0; i<n; i++)
{
printf("%d\t", a[i]);
}
}
void insert()
{
printf("\nEnter the position for the new element: ");
scanf("%d", &pos);
printf("\nEnter the element to be inserted: ");
scanf("%d", &elem);
for(i=n-1; i>=pos-1; i--)
{
a[i+1] = a[i];
}
a[pos-1] = elem;
n = n+1;
}
void del()
{

1
printf("\nEnter the position of the element to be deleted: ");
scanf("%d", &pos);
elem = a[pos-1];
for(i=pos-1; i<n-1; i++)
{
a[i] = a[i+1];
}
n = n-1;
printf("\nThe deleted element is = %d", elem);
}
void main()
{
int ch;
clrscr();
do{
printf("\n\n--------Menu----------- \n");
printf("1.Create\n 2.Display\n 3.Insert\n 4.Delete\n 5.Exit\n");
printf(" ");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert();
break;
case 4: del();
break;
case 5: exit(0);
break;
default: printf("\nInvalid choice:\n");
break;
}
}while(ch!=5);
getch();
}

2
SAMPLE OUTPUT:-
------------- Menu--------------
1.Create 2.Display 3.Insert 4.Delete 5.Exit
Enter your choice: 1
Enter the size of the array elements: 5 Enter the elements for the array:
10 20 30 40 50
------------- Menu--------------
1. Create 2.Display 3.Insert 4.Delete 5.Exit
Enter your choice: 2
The array elements are:
10 20 30 40 50
------------- Menu--------------
1. Create 2.Display 3.Insert 4.Delete 5.Exit
Enter your choice: 3
Enter the position for the new element: 2
Enter the element to be inserted: 90
------------- Menu---------------
1. Create 2.Display 3.Insert 4.Delete 5.Exit
Enter your choice: 2
The array elements are:
10 20 90 30 40 50
------------- Menu----------------
1. Create 2. Display 3.Insert 4.Delete 5.Exit
Enter your choice: 4
Enter the position of the element to be deleted: 5
The deleted element is = 50
------------- Menu----------------
1. Create 2.Display 3.Insert 4.Delete 5.Exit
Enter your choice: 2
The array elements are:
10 20 90 30 40
------------- Menu----------------
1. Create 2.Display 3.Insert 4.Delete 5.Exit
Enter your choice: 5

3
2. STACK OPERATIONS USING ARRAY

2. PROGRAM:-
#include<stdio.h>
#include<conio.h>
#define MAX 4
int stack[MAX], item;
int ch, top = -1, count = 0, status = 0;
/*PUSH FUNCTION*/
void push(int stack[], int item)
{
if (top == (MAX-1))
printf("\n\nStack is Overflow");
else
{
stack[++top] = item;
status++;
}
}
/*POP FUNCTION*/
int pop(int stack[])
{
int ret;
if(top == -1)
printf("\n\nStack is Underflow");
else
{
ret = stack[top--];
status--;
printf("\nPopped element is %d", ret);
}
return ret;
}
/* FUNCTION TO CHECK STACK IS PALINDROME OR NOT */
void palindrome(int stack[])
{
int i, temp;
temp = status;

4
for(i=0; i<temp; i++)
{
if(stack[i] == pop(stack))
count++;
}
if(temp==count)
printf("\nStack contents are Palindrome");
else
printf("\nStack contents are not palindrome");
}
/*FUNCTION TO DISPLAY STACK*/
void display(int stack[])
{
int i;
printf("\nThe stack contents are:");
if(top == -1)
printf("\nStack is Empty");
else
{
for(i=top; i>=0; i--)
printf("\n ------\n| %d |", stack[i]);
printf("\n");
}
}
/*MAIN PROGRAM*/
void main()
{
clrscr();
do{
printf("\n\n----MAIN MENU \n");
printf("\n1. PUSH (Insert) in the Stack");
printf("\n2. POP (Delete) from the Stack");
printf("\n3. PALINDROME check using Stack");
printf("\n4. Exit (End the Execution)");
printf("\nEnter Your Choice: ");
scanf("%d", &ch);
switch(ch){

5
case 1: printf("\nEnter a element to be pushed: ");
scanf("%d", &item);
push(stack, item);
display(stack);
break;
case 2: item=pop(stack);
display(stack);
break;
case 3:palindrome(stack);
break;
case 4:exit(0);
break;
default:
printf("\nEND OF EXECUTION");
}//end switch
}
while (ch != 4);
getch();
}

6
SAMPLE OUTPUT:-
----MAIN MENU----
1.PUSH (Insert) in the Stack
2.POP (Delete) from the Stack
3.PALINDROME check using Stack
4.Exit (End the Execution) Enter Your Choice: 1
Enter an element to be pushed: 1
The stack contents are:
|1|
----MAIN MENU----
1.PUSH (Insert) in the Stack
2.POP (Delete) from the Stack
3.PALINDROME check using Stack
4.Exit (End the Execution) Enter Your Choice: 1
Enter an element to be pushed: 2
The stack contents are:
|2|
|1|

(-------- AFTER THE 4 TIMES PUSH OPERATION )


----MAIN MENU----
1.PUSH (Insert) in the Stack
2.POP (Delete) from the Stack
3.PALINDROME check using Stack
4.Exit (End the Execution) Enter Your Choice: 1
Enter an element to be pushed: 9
Stack is Overflow
The stack contents are:
|1|
|2|
|2|
|1|
----MAIN MENU----
1.PUSH (Insert) in the Stack
2.POP (Delete) from the Stack
3.PALINDROME check using Stack
4.Exit (End the Execution)

7
Enter Your Choice: 2
Popped element is 1
The stack contents are:
|2|
|2|
|1|
(-------- AFTER THE 4 TIMES POP OPERATION )
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution) Enter Your Choice: 2
Stack is Underflow
The stack contents are:
Stack is Empty
(-------- CHECKING FOR PALINDROME OR NOT USING STACK )
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution) Enter Your Choice: 1
Enter an element to be pushed: 1
The stack contents are:
|1|
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution) Enter Your Choice: 1
Enter an element to be pushed: 2
The stack contents are:
|2|
|1|
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack

8
4. Exit (End the Execution) Enter Your Choice: 1
Enter a element to be pushed: 1
The stack contents are:
|1|
|2|
|1|
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution) Enter Your Choice: 3
Stack contents are is Palindrome
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution) Enter Your Choice: 1
Enter an element to be pushed: 1
The stack contents are:
|1|
(AFTER 3 TIMES PUSH)
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution) Enter Your Choice: 1
Enter an element to be pushed: 3
The stack contents are:
|3|
|2|
|1|
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution) Enter Your Choice: 3
Stack contents are not Palindrome

9
3. INFIX TO POSTFIX CONVERSION

3. PROGRAM:-
#include<stdio.h>
#include<string.h>
#include<conio.h>
int F(char symbol)
{
switch(symbol)
{
case '+' :
case '-': return 2;
case '*':
case '/': return 4;
case '^':
case '$': return 5;
case '(': return 0;
case '#': return -1;
default: return 8;
}
}
int G(char symbol)
{
switch(symbol)
{
case '+':
case '-': return 1;
case '*':
case '/': return 3;
case '^':
case '$': return 6;
case '(': return 9;
case ')': return 0;
default: return 7;
}
}
void infix_postfix(char infix[], char postfix[])
{

10
int top, j, i;
char s[30], symbol;top = -1;
s[++top] = '#';
j = 0;
for(i=0; i < strlen(infix); i++)
{
symbol = infix[i];
while(F(s[top]) > G(symbol))
{
postfix[j] = s[top--];
j++;
}
if(F(s[top]) != G(symbol))
s[++top] = symbol;
else
top--;
}
while(s[top] != '#')
{
postfix[j++] = s[top--];
}
postfix[j] = '\0';
}
void main()
{
char infix[20], postfix[20];
clrscr();
printf("\nEnter a valid infix expression\n");
flushall();
gets(infix);
infix_postfix(infix,postfix);
printf("\nThe infix expression is:\n");
printf ("%s",infix);
printf("\nThe postfix expression is:\n");
printf ("%s",postfix);
getch();
}

11
SAMPLE OUTPUT:-
Enter a valid infix expression (a+(b-c)*d)
The infix expression is: (a+(b-c)*d)
The postfix expression is: abc-d*+

12
4. CIRCULAR QUEUE

4. PROGRAM:-
#include<stdio.h>
#include<conio.h>
#define MAX 4
int ch, front = 0, rear = -1, count=0;
char q[MAX], item;
void insert()
{
if(count == MAX)
printf("\nQueue is Full");
else
{
rear = (rear + 1) % MAX;
q[rear]=item;
count++;
}
}
void del()
{
if(count == 0)
printf("\nQueue is Empty");
else
{
if(front > rear && rear==MAX-1)
{
front=0;
rear=-1;
count=0;
}
else
{
item=q[front];
printf("\nDeleted item is: %c",item);
front = (front + 1) % MAX;
count--;

13
}
}
}
void display()
{
int i;
if(count == 0)
printf("\nQueue is Empty");
else
{
printf("\nContents of Queue is:\n");
i=front;
while(i!=rear)
{
printf("%c\t", q[i]);
i=(i+1)%MAX;
}
printf("%c\t",q[rear]);
}
}
void main()
{
clrscr();
do
{
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit");
printf("\nEnter the choice: ");
scanf("%d", &ch);
flushall();
switch(ch)
{
case 1: printf("\nEnter the character / item to be inserted: ");
scanf("%c", &item);
insert();
break;
case 2: del();
break;

14
case 3: display();
break;
case 4: exit(0);
break;
}
}
while(ch!=4);
getch();
}

15
SAMPLE OUTPUT:
1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 1
Enter the character / item to be inserted: A

1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 1

Enter the character / item to be inserted: B

1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 1


Enter the character / item to be inserted: C

1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 1


Enter the character / item to be inserted: D

1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 3

Contents of Queue is:


A B C D
1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 1
Enter the character / item to be inserted: F

Queue is Full

1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 2


Deleted item is: A

1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 2


Deleted item is: B

1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 3


Contents of Queue is:
C D

1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 1


Enter the character / item to be inserted: K

1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 3


Contents of Queue is:
C D K

1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 4

16
5. SINGLE LINKED LIST

5. PROGRAM:-
#include<stdio.h>
#include<conio.h>
int MAX=4, count;
struct student
{
char usn[10]; char name[30]; char branch[5]; int sem;
char phno[10];
struct student *next; //Self referential pointer.
};
typedef struct student NODE;
int countnodes(NODE *head)
{
NODE *p;
count=0; p=head;
while(p!=NULL)
{
p=p->next; count++;
}
return count;
}

NODE* getnode(NODE *head)


{
NODE *newnode;
newnode=(NODE*)malloc(sizeof(NODE)); //Create first NODE
printf("\nEnter USN, Name, Branch, Sem, Ph.No\n");
flushall(); gets(newnode->usn); flushall(); gets(newnode->name); flushall();
gets(newnode->branch); scanf("%d",&(newnode->sem)); flushall();
gets(newnode->phno);
newnode->next=NULL; //Set next to NULL...
head=newnode;
return head;
}

NODE* display(NODE *head)


{
NODE *p;
if(head == NULL)
printf("\nNo student data\n");
else

17
{
p=head;
printf("\n----STUDENT DATA \n");
printf("\nUSN\tNAME\t\tBRANCH\tSEM\tPh.NO.");
while(p!=NULL)
{
printf("\n%s\t%s\t\t%s\t%d\t%s", p->usn, p->name, p->branch, p->sem, p-
>phno);
p = p->next; //Go to next node...
}
printf("\nThe no. of nodes in list is: %d",countnodes(head));
}
return head;
}

NODE* create(NODE *head)


{
NODE *newnode;
if(head==NULL)
{
newnode=getnode(head); head=newnode;
}
else
{
newnode=getnode(head);
newnode->next=head;
head=newnode;
}
return head;
}

NODE* insert_front(NODE *head)


{
if(countnodes(head)==MAX)
printf("\nList is Full / Overflow!!");
else
head=create(head); //create()insert nodes at front.
return head;
}

NODE* insert_rear(NODE *head)


{
NODE *p, *newnode;

18
p=head;
if(countnodes(head) == MAX)
printf("\nList is Full(QUEUE)!!");
else
{
if(head == NULL)
{
newnode=getnode(head);
head=newnode; //set first node to be head
}
else
{
newnode=getnode(head);
while(p->next!=NULL)
{
p=p->next;
}
p->next=newnode;
}
}
return head;
}

NODE* insert(NODE *head)


{
int ch;
do
{
printf("\n1.Insert at Front(First)\t2.Insert at End(Rear/Last)\t3.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: head=insert_front(head);
break;
case 2: head=insert_rear(head);
break;
case 3: break;
}
head=display(head);
}
while(ch!=3);
return head;

19
}

NODE* delete_front(NODE *head)


{
NODE *p;
if(head==NULL)
printf("\nList is Empty/Underflow (STACK/QUEUE)");
else
{
p=head;
head=head->next;
free(p); //Set 2nd NODE as head free(p);
printf("\nFront(first)node is deleted");
}
return head;
}

NODE* delete_rear(NODE *head)


{
NODE *p, *q; p=head;
while(p->next!=NULL)
{
p=p->next; //Go upto -1 NODE which you want to delete
}
q=p->next;
free(q);//Delete last NODE... p->next=NULL;
printf("\nLast(end) entry is deleted"); return head;
}

NODE* del(NODE *head)


{
int ch;
do
{
printf("\n1.Delete from Front(First)\t2. Delete from End(Rear/Last))\t3.Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: head=delete_front(head); break;
case 2: head=delete_rear(head); break;
case 3: break;
}

20
head=display(head);
}
while(ch!=3);
return head;
}

NODE* stack(NODE *head)


{
int ch; do
{
printf("\nSSL used as Stack...");
printf("\n 1.Insert at Front(PUSH) \t 2.Delete from Front(POP))\t3.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: head=insert_front(head); break;
case 2: head=delete_front(head); break;
case 3: break;
}
head=display(head);
}while(ch!=3);
return head;
}

NODE* queue(NODE *head)


{
int ch; do
{
printf("\nSSL used as Queue...");
printf("\n 1.Insert at Rear(INSERT) \t 2.Delete from Front(DELETE))\t3.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: head=insert_rear(head); break;
case 2: head=delete_front(head); break;
case 3: break;
}
head=display(head);
}while(ch!=3);
return head;
}

21
void main()
{
int ch, i, n; NODE *head;
head=NULL;
clrscr();
printf("\n*----------Studednt Database-------------*");
do
{
printf("\n 1.Create\t 2.Display\t 3.Insert\t 4.Delete\t 5.Stack\t 6.Queue\t 7.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nHow many student data you want to create: ");
scanf("%d", &n);
for(i=0;i<n;i++)
head=create(head);//Call to Create NODE...
break;
case 2: head=display(head);
break;
case 3: head=insert(head); //Call to Insert...
break;
case 4: head=del(head); //Call to delete
break;
case 5: head=stack(head);
break;
case 6: head=queue(head);
break;
case 7: exit(); //Exit...
}
}
while(ch!=7);
getch();
}

22
SAMPLE OUTPUT:-
1. Create 2. Display 3. Insert 4. Delete 5. Stack 6.Queue 7.
Exit Enter your choice: 1
How many student data you want to create: 2 Enter USN, Name, Branch, Sem,
Ph.No
1kt12cs001 kumar mca 1 9900099000
Enter USN, Name, Branch, Sem, Ph.No
1kt12is002 ravi msc 1 9900099111
1. Create 2. Display 3. Insert 4. Delete 5. Stack 6.Queue 7.
Exit Enter your choice: 2
----STUDENT DATA----
USN NAME BRANCH SEM Ph.NO.
1kt12is002 ravi msc 1 9900099111
1kt12cs001 kumar mca 1 9900099000

The no. of nodes in list is: 2

1. Create 2. Display 3. Insert 4. Delete 5. Stack 6.Queue 7.


Exit Enter your choice: 3

1.Insert at Front(First) 2.Insert at End(Rear/Last) 3.Exit Enter your choice:


1
Enter USN, Name, Branch, Sem, Ph.No
1kt12cs003 suresh mca 1 99000992222
----STUDENT DATA----
USN NAME BRANCH SEM Ph.NO.
1kt12cs003 suresh mca 3 9900099222
1kt12is002 ravi msc 3 9900099111
1kt12cs001 kumar mca 3 9900099000

The no. of nodes in list is:


3

23
6. BINARY SEARCH TREE

6. PROGRAM:-
#include <stdio.h>
#include <stdlib.h>
struct BST
{
int data;
struct BST *left;
struct BST *right;
};
typedef struct BST NODE;
NODE *node;
NODE* createtree(NODE *node, int data)
{
if (node == NULL)
{
NODE *temp;
temp= (NODE*)malloc(sizeof(NODE));
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
if (data < (node->data))
{
node->left = createtree(node->left, data);
}
else if (data > node->data)
{
node -> right = createtree(node->right, data);
}
return node;
}

NODE* search(NODE *node, int data)


{
if(node == NULL)
printf("\nElement not found");
else if(data < node->data)
{
node->left=search(node->left, data);
}
else if(data > node->data)

24
{
node->right=search(node->right, data);
}
else
printf("\nElement found is: %d", node->data);
return node;
}
void inorder(NODE *node)
{
if(node != NULL)
{
inorder(node->left);
printf("%d\t", node->data);
inorder(node->right);
}
}
void preorder(NODE *node)
{
if(node != NULL)
{
printf("%d\t", node->data);
preorder(node->left);
preorder(node->right);
}
}
void postorder(NODE *node)
{
if(node != NULL)
{
postorder(node->left);
postorder(node->right);
printf("%d\t", node->data);
}
}
NODE* findMin(NODE *node)
{
if(node==NULL)
{
return NULL;
}
if(node->left)
return findMin(node->left);
else

25
return node;
}

NODE* del(NODE *node, int data)


{
NODE *temp;
if(node == NULL)
{
printf("\nElement not found");
}
else if(data < node->data)
{
node->left = del(node->left, data);
}
else if(data > node->data)
{
node->right = del(node->right, data);
}
else
{ /* Now We can delete this node and replace with either minimum element in the
right sub tree or maximum element in the left subtree */
if(node->right && node->left)
{ /* Here we will replace with minimum element in the right sub tree */
temp = findMin(node->right);
node -> data = temp->data;
/* As we replaced it with some other node, we have to delete that node */
node -> right = del(node->right,temp->data);
}
else
{
/* If there is only one or zero children then we can directly remove it from the tree
and connect its parent to its child */
temp = node;
if(node->left == NULL)
node = node->right;
else if(node->right == NULL)
node = node->left;
free(temp); /* temp is longer required */
}
}
return node;
}

26
void main()
{
int data, ch, i, n; NODE *root=NULL;
clrscr();
while (1)
{
printf("\n1.Insertion in Binary Search Tree");
printf("\n2.Search Element in Binary Search Tree");
printf("\n3.Delete Element in Binary Search Tree");
printf("\n4.Inorder\n5.Preorder\n6.Postorder\n7.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1: printf("\nEnter N value: " );
scanf("%d", &n);
printf("\nEnter the values to create BST like(6,9,5,2,8,15,24,14,7,8,5,2)\n");
for(i=0; i<n; i++)
{
scanf("%d", &data);
root=createtree(root, data);
}
break;
case 2: printf("\nEnter the element to search: ");
scanf("%d", &data);
root=search(root, data); break;
case 3: printf("\nEnter the element to delete: ");
scanf("%d", &data);
root=del(root, data); break;
case 4: printf("\nInorder Traversal: \n");
inorder(root); break;
case 5: printf("\nPreorder Traversal: \n");
preorder(root); break;
case 6: printf("\nPostorder Traversal: \n");
postorder(root); break;
case 7: exit(0);
default:printf("\nWrong option");
break;
}
}
}

27
SAMPLE OUTPUT:-
1. Insertion in Binary Search Tree
2. Search Element in Binary Search Tree
3. Delete Element in Binary Search Tree
4. Inorder
5. Preorder
6. Postorder
7. Exit
Enter your choice: 1
Enter N value: 12
Enter the values to create BST like(6,9,5,2,8,15,24,14,7,8,5,2)
6 9 5 2 8 15 24 14 78 5 2
1. Insertion in Binary Search Tree
2. Search Element in Binary Search Tree
3. Delete Element in Binary Search Tree
4. Inorder
5. Preorder
6. Postorder
7. Exit
Enter your choice: 4
Inorder Traversal:
2 5 6 7 8 9 14 15 24
1. Insertion in Binary Search Tree
2. Search Element in Binary Search Tree
3. Delete Element in Binary Search Tree
4. Inorder
5. Preorder
6. Postorder
7. Exit
Enter your choice: 5
Preorder Traversal:
6 5 2 9 8 7 15 14 24
1.Insertion in Binary Search Tree
2.Search Element in Binary Search Tree
3.Delete Element in Binary Search Tree
4.Inorder
5.Preorder
6.Postorder
7.Exit
Enter your choice: 6
Postorder Traversal:
2 5 7 8 14 24 15 9 6

28
7. SEARCHING TECHNIQUES

7. PROGRAM:-
7A. Linear search:-
#include<stdio.h>
void main()
{
int a[20],i,x,n;
printf("How many elements?"); scanf("%d",&n);

printf("Enter array elements:\n"); for(i=0;i<n;++i)


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

printf("\nEnter element to search:"); scanf("%d",&x);

for(i=0;i<n;++i) if(a[i]==x)
break;

if(i<n)
printf("Element found at index %d",i); else
printf("Element not found");
getch();
}

Output:-
How many elements? 6 Enter array elements: 5
8

Enter element to search: 3


Element found at index 3

29
7B.Binary search:-
#include <stdio.h>
#include<conio.h>
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;

if (arr[mid] == x) return mid;

if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);

return binarySearch(arr, mid + 1, r, x);


}
return -1;
}
void main()
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int x,result;
clrscr();
printf("Enter a element u want to search:");
scanf("%d",&x);
result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? printf("Element is not present in array") : printf("Element is
present at index %d", result);
getch();
}

Output:-
Enter a element u want to search: 10
Element is present at index 3

30
8. QUICK SORT

8. PROGRAM:-
#include <stdio.h>
#include <conio.h>
int partition (int a[], int start, int end)
{
int pivot = a[end]; // pivot element
int i = (start - 1);
int j,t;

for ( j = start; j <= end - 1; j++)


{
// If current element is smaller than the pivot
if (a[j] < pivot)
{
i++; // increment index of smaller element
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
t = a[i+1];
a[i+1] = a[end];
a[end] = t;
return (i + 1);
}

/* function to implement quick sort */


void quick(int a[], int start, int end) /* a[] = array to be sorted, start = Starting
index, end = Ending index */
{
if (start < end)
{
int p = partition(a, start, end); //p is the partitioning index
quick(a, start, p - 1);
quick(a, p + 1, end);
}
}

/* function to print an array */


void printArr(int a[], int n)
{

31
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}
void main()
{
int a[] = { 24, 9, 29, 14, 19, 27 };
int n = sizeof(a) / sizeof(a[0]);
clrscr();
printf("Before sorting array elements are - \n");
printArr(a, n);
quick(a, 0, n - 1);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);

getch();
}

SAMPLE OUTPUT:-
Before sorting array elements are-
24 9 29 14 19 27
After sorting array elements are -
9 14 19 24 27 29

32
9. OPERATIONS ON GRAPHS USING BFS & DFS
9. PROGRAM:-
#include<stdio.h>
#include<conio.h>
int a[10][10], n, m, i, j, source, s[10], b[10];
int visited[10];
void create()
{
printf("\nEnter the number of vertices of the digraph: ");
scanf("%d", &n);
printf("\nEnter the adjacency matrix of the graph:\n");
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
scanf("%d", &a[i][j]);
}
void bfs()
{
int q[10], u, front=0, rear=-1;
printf("\nEnter the source vertex to find other nodes reachable or not: ");
scanf("%d", &source);
q[++rear] = source;
visited[source] = 1;
printf("\nThe reachable vertices are: ");
while(front<=rear)
{
u = q[front++];
for(i=1; i<=n; i++)
{
if(a[u][i] == 1 && visited[i] == 0)
{
q[++rear] = i;
visited[i] = 1;
printf("\n%d", i);
}
}
}
}
void dfs(int source)
{
int v, top = -1;
s[++top] = 1;
b[source] = 1;
for(v=1; v<=n; v++)

33
{
if(a[source][v] == 1 && b[v] == 0)
{
printf("\n%d -> %d", source, v);
dfs(v);
}
}
}
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n1.Create Graph\n2.BFS\n3.Check graph connected or not(DFS)\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: create();break;
case 2: bfs();
for(i=1;i<=n;i++)
if(visited[i]==0)
printf("\nThe vertex that is not rechable %d" ,i);
break;
case 3: printf("\nEnter the source vertex to find the connectivity: ");
scanf("%d",&source);
m=1;
dfs(source);
for(i=1;i<=n;i++)
{
if(b[i]==0)
m=0;
}
if(m==1)
printf("\nGraph is Connected");
else
printf("\nGraph is not Connected");
break;
default: exit(0);
}
}
}

34
SAMPLE OUTPUT:-
1. Create Graph 2.BFS 3.Check graph connected or not (DFS) 4.Exit
Enter your choice: 1
Enter the number of vertices of the digraph: 4
Enter the adjacency matrix of the graph:
0011
0000
0100
0100

1. Create Graph 2.BFS 3.Check graph connected or not (DFS) 4.Exit


Enter your choice: 2
Enter the source vertex to find other nodes reachable or not: 1
3
4
2
1. Create Graph 2.BFS 3.Check graph connected or not (DFS) 4.Exit Enter your
choice: 3
Enter the source vertex to find the connectivity: 1
1 -> 3
3 -> 2
1 -> 4
Graph is Connected
1. Create Graph 2.BFS 3.Check graph connected or not (DFS) 4.Exit
Enter your choice: 4

35
10. MERGE SORT
10.PROGRAM:-
#include <stdio.h>
#include <conio.h>
/* Function to merge the subarrays of a[] */
void merge(int a[], int beg, int mid, int end)
{
int i, j, k;
int b[10];
i = beg;
j = mid+1;
k = beg;
while (i <= mid && j <= end)
{
if(a[i] <= a[j])
{
b[k] = a[i];
i++;
}
else
{
b[k] = a[j];
j++;
}
k++;
}
if(i>mid)
{
while (j<=end)
{
b[k] = a[j];
j++;
k++;
}
}
else{

while (i<=mid)
{
b[k] = a[i];
i++;
k++;
}

36
}
for(k=beg;k<=end;k++)
a[k]=b[k];
}

void mergeSort(int a[], int beg, int end)


{
if (beg < end)
{
int mid = (beg + end) / 2;
mergeSort(a, beg, mid);
mergeSort(a, mid + 1, end);
merge(a, beg, mid, end);
}
}

/* Function to print the array */


void printArray(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}

void main()
{
int a[] = { 12, 31, 25, 8, 32, 17, 40, 42 };
int n = sizeof(a) / sizeof(a[0]);
clrscr();
printf("Before sorting array elements are - \n");
printArray(a, n);
mergeSort(a, 0, n - 1);
printf("After merge sorting array elements are - \n");
printArray(a, n);
getch();

37
SAMPLE OUTPUT:-
Before sorting array elements are-
12 31 25 8 32 17 40 42
After merge sorting array elements are –
8 12 17 25 31 32 40 42

38

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy