Amxn DSA2
Amxn DSA2
03
NAME: ASHISH JOSHI
COURSE: BCA
SEMESTER: II
ROLL NO.: 30
1. OBJECTIVE:
Write a C program to demonstrate the stack operations. Use array to represent the
stack.
2. PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct stack{
int top;
int cap;
int *arr;
};
if(isempty(st)){
printf("Stack is empty \n");
return;
}
int data;
data = st -> arr[st -> top];
st -> top --;
printf("Element poped is %d : ",data);
return;
}
int main(){
int choice;
int size;
printf("Enter the size of the stack : ");
scanf("%d",&size);
printf("1.Push \n");
printf("2.Pop \n");
printf("3.Display \n");
while(true){
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice){
case 1:
push(st);
break;
case 2:
pop(st);
break;
case 3:
display(st);
break;
default:
printf("Wrong Inut \n");
break;
}
}
free(st -> arr);
free(st);
}
3. OUTPUT:
33 22 11
PROGRAM NO. 04
NAME: ASHISH JOSHI
COURSE: BCA
SEMESTER: II
ROLL NO.: 30
1. OBJECTIVE:
2. PROGRAM:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 50
int stack[MAX];
int top = -1;
void push(int value) {
if (top == MAX - 1) {
printf("Error: Stack overflow!\n");
return;
}
stack[++top] = value;
}
int pop() {
if (top == -1) {
printf("Error: Stack underflow!\n");
return -1;
}
return stack[top--];
}
int is_operator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
}
int perform_operation(int a, int b, char c) {
switch (c) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
default:
return -1;
}
}
int evaluate_postfix(char *postfix) {
int i, len = strlen(postfix);
char c;
int a, b;
for (i = 0; i < len; i++) {
c = postfix[i];
if (isdigit(c)) {
push(c - '0');
} else if (is_operator(c)) {
b = pop();
a = pop();
push(perform_operation(a, b, c));
} else {
printf("Invalid character: %c\n", c);
return -1;
}
}
return pop();
}
int main() {
char postfix[MAX];
printf("Enter a postfix expression: ");
scanf("%s", postfix);
int result = evaluate_postfix(postfix);
if (result != -1) {
printf("Result: %d\n", result);
}
return 0;
}
3. OUTPUT:
Result: 5
PROGRAM NO. 06
NAME: ASHISH JOSHI
COURSE: BCA
SEMESTER: II
ROLL NO.: 30
1. OBJECTIVE:
2. PROGRAM:
a).
#include <stdio.h>
#include <stdlib.h>
struct Queue {
int rear;
int front;
int cap;
int *arr;
};
return Q;
}
if (!isfull(Q)) {
if (isempty(Q) || d < Q->arr[Q->front]) {
Q->front = (Q->front - 1 + Q->cap) % Q->cap;
Q->arr[Q->front] = d;
} else {
Q->rear = (Q->rear + 1) % Q->cap;
Q->arr[Q->rear] = d;
}
} else {
printf("Queue is Overflowed\n");
}
}
int main() {
int size;
printf("Enter the size of Queue: ");
scanf("%d", &size);
printf("1.Insertion of Data\n");
printf("2.Deletion of Data\n");
printf("3.Size of Queue\n");
printf("4.Delete whole Queue\n");
printf("5.Display Queue\n"); // Added
int choice;
while (1) {
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insertion(Q);
break;
case 2:
deletion(Q);
break;
case 3:
sizes(Q);
break;
case 4:
whole_delete(Q);
exit(0);
case 5: // Added
display(Q);
break; // Added
default:
printf("Wrong Input\n");
break;
}
}
}
3. OUTPUT:
#include <stdio.h>
#define MAX 5
int circular_queue[MAX];
int front = -1, rear = -1;
void enqueue(int value) {
if((front == 0 && rear == MAX-1) || (front == rear+1)) {
printf("Circular queue is full\n");
return;
}
if(front == -1) {
front = 0;
rear = 0;
} else if(rear == MAX-1 && front != 0) {
rear = 0;
} else {
rear++;
}
circular_queue[rear] = value;
}
int dequeue() {
int item;
if(front == -1) {
printf("Circular queue is empty\n");
return -1;
}
item = circular_queue[front];
if(front == rear) {
front = -1;
rear = -1;
} else if(front == MAX-1) {
front = 0;
} else {
front++;
}
return item;
}
void printCircularQueue() {
int i = front;
if(front == -1) {
printf("Circular queue is empty\n");
return;
}
printf("Circular queue elements are: ");
if(front <= rear) {
while(i <= rear) {
printf("%d ", circular_queue[i]);
i++;
}
} else {
while(i <= MAX-1) {
printf("%d ", circular_queue[i]);
i++;
}
i = 0;
while(i <= rear) {
printf("%d ", circular_queue[i]);
i++;
}
}
printf("\n");
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
enqueue(50);
enqueue(60);
printCircularQueue();
printf("Deleted element: %d\n", dequeue());
printCircularQueue();
return 0;
}
OUTPUT:
1. OBJECTIVE:
2. PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL) {
*head_ref = new_node;
return;
}
last->next = new_node;
return;
}
if (*head_ref == NULL) {
printf("List is empty\n");
return;
}
if ((*head_ref)->next == NULL) {
free(*head_ref);
*head_ref = NULL;
return;
}
prev->next = NULL;
free(temp);
}
int main() {
struct Node* head = NULL;
int choice, data;
while (1) {
printf("\n1. Insert at the beginning\n2. Insert at the end\n3. Delete from the
beginning\n4. Delete from the end\n5. Display\n6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the data to be inserted: ");
scanf("%d", &data);
insertAtBeginning(&head, data);
break;
case 2:
printf("Enter the data to be inserted: ");
scanf("%d", &data);
insertAtEnd(&head, data);
break;
case 3:
deleteAtBeginning(&head);
break;
case 4:
deleteAtEnd(&head);
break;
case 5:
printf("The linked list is: ");
display(head);
break;
case 6:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
OUTPUT:
1. OBJECTIVE:
Write C program to create Binary Search Tree and perform the following
● Inorder traversal
● Preorder traversal
● Postorder traversal
2. PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* left;
struct node* right;
};
while(root!=NULL){
prev = root;
if(d==root->data){
printf("Cannot insert %d, already in BST", d);
return;
}
else if(d<root->data){
root = root->left;
}
else{
root = root->right;
}
}
if(d<prev->data){
prev->left = new_node;
}
else{
prev->right = new_node;
}
printf("Node Added \n");
}
if(root == NULL){
return NULL;
}
return root;
}
int main(){
int ch;
int data;
while(1){
printf("Enter your Choice : ");
scanf("%d",&ch);
switch(ch){
case 1:
preOrder(p);
break;
case 2:
postOrder(p);
case 3:
inOrder(p);
break;
case 4:
if(isBST(p)){
printf("Its BST \n");
}
else{
printf("Its Not BST \n");
}
break;
case 5:
printf("Enter the data to be searched : ");
scanf("%d",&data);
if(searchIter(p,data)){
printf("Data Present \n");
}
else{
printf("Data not Present \n");
}
break;
case 6:
printf("Enter the data : ");
scanf("%d",&data);
insert(p,data);
case 7:
printf("Enter the data you want to delete : ");
scanf("%d",&data);
delete_node(p,data);
break;
default:
printf("Wrong Input \n");
}
}
return 0;
}
3. OUTPUT:
1.Preorder Traversal
2.Postorder Traversal
3.Inorder Traversal
4.Is BST or Not
5.Search in BST
6.Insert In BST
7.Deletion Of Element
PROGRAM NO. 09
NAME: ASHISH JOSHI
COURSE: BCA
SEMESTER: II
ROLL NO.: 30
1. OBJECTIVE:
Write C program to implement the following
a) Linear search
b) Binary search
2. PROGRAM:
a).
#include <stdio.h>
int main() {
int n , search;
int flag = 0;
int arr[n];
if(flag == 1){
printf("The item is found : \n");
}
else if (flag == 0){
printf("Element not found \n");
}
}
3. OUTPUT:
b).
#include <stdio.h>
int main() {
int n , search , left , right , middle;
int arr[n];
OUTPUT:
PROGRAM NO. 10
NAME: ASHISH JOSHI
COURSE: BCA
SEMESTER: II
ROLL NO.: 30
OBJECTIVE:
Write C program to implement the following
a) Bubble sort
b) Quick sort
PROGRAM:
a).
#include <stdio.h>
int main() {
int n , temp;
printf("Enter the range of the array : ");
scanf("%d",&n);
int arr[n];
OUTPUT: