Part-B DSC Programs
Part-B DSC Programs
if (arr[i] == target)
return i;
return -1;
int main()
if (result != -1)
else
return 0;
}
Output :
10). Write a C program to search an element using binary search technique.
#include <stdio.h>
int binary_search(int arr[], int n, int target)
{
int left = 0;
int right = n - 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (arr[mid] == target)
return mid;
else if (arr[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
int main()
{
int arr[] = {2, 5, 8, 12, 16, 23, 38};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 23;
int result = binary_search(arr, n, target);
if (result != -1)
printf("Element found at index %d\n", result);
else
printf("Element not found\n");
return 0;
}
Output :
11). Write a C program to implement the stack operations.
#include <stdio.h>
#define MAX 5
int stack[MAX];
int top = -1;
void push(int value)
{
if (top == MAX - 1)
printf("Stack is full\n");
else
{
top++;
stack[top] = value;
}
}
void pop() {
if (top == -1)
printf("Stack is empty\n");
else {
printf("Popped: %d\n", stack[top]);
top--;
}
}
void display()
{
if (top == -1)
printf("Stack is empty\n");
else
{
printf("Stack: ");
for (int i = 0; i <= top; i++)
printf("%d ", stack[i]);
printf("\n");
}
}
int main()
{
push(1);
push(2);
push(3);
display();
pop();
display();
return 0;
}
Output :
12). Write a C program to convert an infix expressions into postfix.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 100
typedef struct
{
int top;
char items[MAX];
}
Stack;
void push(Stack* s, char c)
{
s->items[++(s->top)] = c;
}
char pop(Stack* s)
{
return s->items[(s->top)--];
}
char peek(Stack* s)
{
return s->items[s->top];
}
int precedence(char op)
{
switch (op)
{
case '+': case '-': return 1;
case '*': case '/': return 2;
case '^': return 3;
default: return 0;
}
}
void infixToPostfix(const char* infix, char* postfix)
{
Stack s = { .top = -1 };
int j = 0;
for (int i = 0; infix[i]; ++i)
{
char c = infix[i];
if (isalnum(c))
{
postfix[j++] = c;
}
else if (c == '(')
{
push(&s, c);
}
else if (c == ')')
{
while (peek(&s) != '(')
{
postfix[j++] = pop(&s);
}
pop(&s); // Remove '('
}
else
{
while (s.top >= 0 && precedence(c) <= precedence(peek(&s)))
{
postfix[j++] = pop(&s);
}
push(&s, c);
}
}
while (s.top >= 0)
{
postfix[j++] = pop(&s);
}
postfix[j] = '\0';
}
int main()
{
char infix[MAX] = "(a+b)*(c-d)";
char postfix[MAX];
infixToPostfix(infix, postfix);
printf("Infix: %s\n", infix);
printf("Postfix: %s\n", postfix);
return 0;
}
Output :
13). Write a C program to implementa circular queue.
#include <stdio.h>
#define MAX 5
int queue[MAX];
int front = 0;
int rear = 0;
void enqueue(int value)
{
if ((rear + 1) % MAX == front)
printf("Queue is full\n");
else
{
queue[rear] = value;
rear = (rear + 1) % MAX;
}
}
void dequeue()
{
if (front == rear)
printf("Queue is empty\n");
else
{
printf("Dequeued: %d\n", queue[front]);
front = (front + 1) % MAX;
}
}
void display()
{
if (front == rear)
printf("Queue is empty\n");
else
{
int i = front;
printf("Queue: ");
while (i != rear)
{
printf("%d ", queue[i]);
i = (i + 1) % MAX;
}
printf("\n");
}
}
int main()
{
enqueue(1);
enqueue(2);
enqueue(3);
display();
dequeue();
display();
return 0;
}
Output :
14). Write a C program to implement the operations of singly linked list.
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node* next;
}
Node;
Node* createNode(int data)
{
Node* newNode = (Node*) malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insert(Node** head, int data)
{
Node* newNode = createNode(data);
if (*head == NULL)
{
*head = newNode;
}
else
{
Node* temp = *head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
}
void printList(Node* head)
{
while (head != NULL)
{
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
int main()
{
Node* head = NULL;
insert(&head, 10);
insert(&head, 20);
insert(&head, 30);
printList(head);
return 0;
}
Output :
15). Write a C program to implement the operation of a circular linked list.
#include <stdio.h>
#include <stdlib.h>
int data;
Node;
newNode->data = data;
newNode->next = NULL;
return newNode;
if (*head == NULL)
*head = newNode;
newNode->next = newNode;
else
{
temp = temp->next;
temp->next = newNode;
newNode->next = *head;
do
temp = temp->next;
printf("\n");
int main()
insert(&head, 10);
insert(&head, 20);
insert(&head, 30);
printList(head);
return 0;
}
Output :
16). Write a C program to construct BST and implement the tree traversal.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int key;
struct node *left;
struct node *right;
};
struct node *getNode(int val)
{
struct node *newNode;
newNode = malloc(sizeof(struct node));
newNode->key = val;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node *insertNode(struct node *root, int val)
{
if(root == NULL)
return getNode(val);
if(root->key < val)
root->right = insertNode(root->right,val);
if(root->key > val)
root->left = insertNode(root->left,val);
return root;
}
void inorder(struct node *root)
{
if(root == NULL)
return;
inorder(root->left);
printf("%d ",root->key);
inorder(root->right);
}
int main()
{
struct node *root = NULL;
int data;
char ch;
/* Do while loop to display various options to select from to decide the input */
do
{
printf("\nSelect one of the operations::");
printf("\n1. To insert a new node in the Binary Tree");
printf("\n2. To display the nodes of the Binary Tree(via Inorder Traversal).\n");
int choice;
scanf("%d",&choice);
switch (choice)
{
case 1 :
printf("\nEnter the value to be inserted\n");
scanf("%d",&data);
root = insertNode(root,data);
break;
case 2 :
printf("\nInorder Traversal of the Binary Tree::\n");
inorder(root);
break;
default :
printf("Wrong Entry\n");
break;
}
printf("\nDo you want to continue (Type y or n)\n");
scanf(" %c",&ch);
} while (ch == 'Y'|| ch == 'y');
return 0;
}
Output :