0% found this document useful (0 votes)
31 views32 pages

Amxn DSA2

The document contains C programs to demonstrate stack and queue operations using arrays. It includes programs for push, pop and display operations on a stack. It also includes programs for insertion, deletion, size and display operations on a queue as well as a circular queue.

Uploaded by

Ashish Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views32 pages

Amxn DSA2

The document contains C programs to demonstrate stack and queue operations using arrays. It includes programs for push, pop and display operations on a stack. It also includes programs for insertion, deletion, size and display operations on a queue as well as a circular queue.

Uploaded by

Ashish Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

PROGRAM NO.

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;
};

int isfull(struct stack *st){


if(st -> top +1 == st -> cap){
return 1;
}
else{
return 0;
}
}

int isempty(struct stack *st){


if(st -> top == -1){
return 1;
}
else{
return 0;
}
}
void push(struct stack *st){
if(!isfull(st)){
st -> top ++;
printf("Enter the data : ");
scanf("%d",&st->arr[st->top]);
printf("Data Added \n");
return;
}
else{
printf("Stack is full \n");
return;
}
}

void display(struct stack *st){


if(isempty(st)){
printf("Stack is empty\n");
return;
}
int i;
for(i = st->top; i >= 0; i--){
printf("%d \t", st->arr[i]);
}
printf("\n");
}

void pop(struct stack *st){

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);

struct stack *st = (struct stack*)malloc(sizeof(struct stack));

st -> top = -1;


st -> cap = size;
st -> arr = (int *)calloc(st->cap,sizeof(int));

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:

Enter the size of the stack : 5


1.Push
2.Pop
3.Display
Enter your choice : 1
Enter the data : 11
Data Added
Enter your choice : 1
Enter the data : 22
Data Added
Enter your choice : 1
Enter the data : 33
Data Added
Enter your choice : 1
Enter the data : 44
Data Added
Enter your choice : 1
Enter the data : 55
Data Added
Enter your choice : 1
Stack is full
Enter your choice : 2
Element poped is 55 : Enter your choice : 2
Element poped is 44 : Enter your choice : 3

33 22 11

PROGRAM NO. 04
NAME: ASHISH JOSHI
COURSE: BCA
SEMESTER: II
ROLL NO.: 30

1. OBJECTIVE:

Write a C program to evaluate the postfix expression.

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:

Enter a postfix expression: 23*4/5*4+56-25

Result: 5

PROGRAM NO. 06
NAME: ASHISH JOSHI
COURSE: BCA
SEMESTER: II
ROLL NO.: 30

1. OBJECTIVE:

Write C programs to demonstrate the following data structures using arrays.


a) Queue
b) Circular queue

2. PROGRAM:

a).
#include <stdio.h>
#include <stdlib.h>

struct Queue {
int rear;
int front;
int cap;
int *arr;
};

struct Queue *create_Queue(int size) {


struct Queue *Q = (struct Queue *)malloc(sizeof(struct Queue));
Q->cap = size;
Q->front = -1;
Q->rear = -1;
Q->arr = (int *)calloc(Q->cap, sizeof(int));

return Q;
}

int isfull(struct Queue *Q) {


if ((Q->rear + 1) % Q->cap == Q->front) {
return 1;
} else {
return 0;
}
}

int isempty(struct Queue *Q) {


if (Q->front == -1) {
return 1;
} else {
return 0;
}
}

void insertion(struct Queue *Q) {


int d;
printf("Enter the data: ");
scanf("%d", &d);

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");
}
}

void deletion(struct Queue *Q) {


if (!isempty(Q)) {
int d = Q->arr[Q->front];
if (Q->front == Q->rear) {
Q->rear = Q->front = -1;
} else {
Q->front = (Q->front + 1) % Q->cap;
}
printf("Deleted data is: %d\n", d);
} else {
printf("Queue is Empty or Underflowed\n");
}
}
void sizes(struct Queue *Q) {
int d;
d = (Q->rear - Q->front + Q->cap) % Q->cap;
printf("Size of Queue is: %d\n", d);
}

void whole_delete(struct Queue *Q) {


if (Q) {
if (Q->arr) {
free(Q->arr);
}
free(Q);
}
printf("Whole Queue is Deleted\n");
}

void display(struct Queue *Q) {


if (!isempty(Q)) {
int i = Q->front;
printf("Elements of the Queue are: ");
while (i != Q->rear) {
printf("%d ", Q->arr[i]);
i = (i + 1) % Q->cap;
}
printf("%d\n", Q->arr[i]);
} else {
printf("Queue is Empty\n");
}
}

int main() {
int size;
printf("Enter the size of Queue: ");
scanf("%d", &size);

struct Queue *Q = create_Queue(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:

Enter the size of Queue: 5


1.Insertion of Data
2.Deletion of Data
3.Size of Queue
4.Delete whole Queue
5.Display Queue
Enter your choice: 1
Enter the data: 11
Enter your choice: 1
Enter the data: 22
Enter your choice: 1
Enter the data: 33
Enter your choice: 1
Enter the data: 44
Enter your choice: 1
Enter the data: 55
Queue is Overflowed
Enter your choice: 3
Size of Queue is: 4
Enter your choice: 5
Elements of the Queue are: 11 0 22 33 44
Enter your choice: 2
Deleted data is: 11
Enter your choice: 2
Deleted data is: 0
Enter your choice: 2
Deleted data is: 22
Enter your choice: 5
Elements of the Queue are: 33 44
Enter your choice: 1
Enter the data: 85
Enter your choice: 5
Elements of the Queue are: 33 44 85
Enter your choice: 4
Whole Queue is Deleted
b).

#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:

Circular queue is full


Circular queue elements are: 10 20 30 40 50
Deleted element: 10
Circular queue elements are: 20 30 40 50
PROGRAM NO. 07
NAME: ASHISH JOSHI
COURSE: BCA
SEMESTER: II
ROLL NO.: 30

1. OBJECTIVE:

Write C programs to demonstrate the following operations on a linked list Creation


of a list
 Adding an element at the beginning of the list.
 Adding an element at the end of the list.
 Deleting the first element.
 Deleting the last element

2. PROGRAM:

#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

void insertAtBeginning(struct Node** head_ref, int new_data) {


struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}

void insertAtEnd(struct Node** head_ref, int new_data) {


struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
struct Node* last = *head_ref;

new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL) {
*head_ref = new_node;
return;
}

while (last->next != NULL)


last = last->next;

last->next = new_node;
return;
}

void deleteAtBeginning(struct Node **head_ref) {


struct Node* temp = *head_ref;
if (*head_ref != NULL) {
*head_ref = (*head_ref)->next;
free(temp);
}
}

void deleteAtEnd(struct Node **head_ref) {


struct Node* temp = *head_ref, *prev;

if (*head_ref == NULL) {
printf("List is empty\n");
return;
}

if ((*head_ref)->next == NULL) {
free(*head_ref);
*head_ref = NULL;
return;
}

while (temp->next != NULL) {


prev = temp;
temp = temp->next;
}

prev->next = NULL;
free(temp);
}

void display(struct Node* node) {


while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
}

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. Insert at the beginning


2. Insert at the end
3. Delete from the beginning
4. Delete from the end
5. Display
6. Exit
Enter your choice: 1
Enter the data to be inserted: 10

1. Insert at the beginning


2. Insert at the end
3. Delete from the beginning
4. Delete from the end
5. Display
6. Exit
Enter your choice: 1
Enter the data to be inserted: 20

1. Insert at the beginning


2. Insert at the end
3. Delete from the beginning
4. Delete from the end
5. Display
6. Exit
Enter your choice: 3

1. Insert at the beginning


2. Insert at the end
3. Delete from the beginning
4. Delete from the end
5. Display
6. Exit
Enter your choice: 5
The linked list is: 10
1. Insert at the beginning
2. Insert at the end
3. Delete from the beginning
4. Delete from the end
5. Display
6. Exit
Enter your choice:
PROGRAM NO. 08
NAME: ASHISH JOSHI
COURSE: BCA
SEMESTER: II
ROLL NO.: 30

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;
};

struct node* createNode(int data){


struct node *n; // creating a node pointer
n = (struct node *) malloc(sizeof(struct node)); // Allocating memory in the heap
n->data = data; // Setting the data
n->left = NULL; // Setting the left and right children to NULL
n->right = NULL; // Setting the left and right children to NULL
return n; // Finally returning the created node
}

void preOrder(struct node* root){


if(root!=NULL){
printf("%d ", root->data);
preOrder(root->left);
preOrder(root->right);
}
}

void postOrder(struct node* root){


if(root!=NULL){
postOrder(root->left);
postOrder(root->right);
printf("%d ", root->data);
}
}

void inOrder(struct node* root){


if(root!=NULL){
inOrder(root->left);
printf("%d ", root->data);
inOrder(root->right);
}
}

int isBST(struct node* root){


static struct node *prev = NULL;
if(root!=NULL){
if(!isBST(root->left)){
return 0;
}
if(prev!=NULL && root->data <= prev->data){
return 0;
}
prev = root;
return isBST(root->right);
}
else{
return 1;
}
}

struct node * searchIter(struct node* root, int key){


while(root!=NULL){
if(key == root->data){
return root;
}
else if(key<root->data){
root = root->left;
}
else{
root = root->right;
}
}
return NULL;
}

void insert(struct node *root,int d){

struct node *prev = NULL;


struct node *new_node = createNode(d);

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");
}

struct node *inorder_pre(struct node *root){


root = root -> left;
while(root -> right != NULL){
root = root -> right;
}
return root;
}

struct node *delete_node(struct node *root , int d){

struct node *ipre;

if(root == NULL){
return NULL;
}

//case 1 : Node with no children (leaf node)


if(root -> left == NULL && root -> right == NULL){
free(root);
return NULL;
}

if(d < root -> data){


root -> left = delete_node(root -> left , d);
}
else if(d > root -> data){
root -> right = delete_node(root -> right , d);
}
//deletion when node is found
else{
if(root -> left == NULL){
struct node *temp = root -> right;
free(root);
return temp;
}
else if(root->right == NULL){
struct node *temp = root -> left;
free(root);
return temp;
}
ipre = inorder_pre(root);
root -> data = ipre->data;
root -> left = delete_node(root->left,ipre->data);
}

return root;
}

int main(){

// Constructing the root node - Using Function (Recommended)


struct node *p = createNode(5);
struct node *p1 = createNode(3);
struct node *p2 = createNode(6);
struct node *p3 = createNode(1);
struct node *p4 = createNode(4);
// Finally The tree looks like this:
// 5
// / \
// 3 6
// / \
// 1 4

// Linking the root node with left and right children


p->left = p1;
p->right = p2;
p1->left = p3;
p1->right = p4;

int ch;
int data;

printf("1.Preorder Traversal \n");


printf("2.Postorder Traversal \n");
printf("3.Inorder Traversal \n");
printf("4.Is BST or Not \n");
printf("5.Search in BST \n");
printf("6.Insert In BST \n");
printf("7.Deletion Of Element \n");

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

Enter your Choice : 1


53146
Enter your Choice : 2
1436513456
Enter your Choice : 3
13456
Enter your Choice : 4
Its BST

Enter your Choice : 5


Enter the data to be searched : 5
Data Present

Enter your Choice : 6


Enter the data : 56
Node Added
Enter the data you want to delete : 0

Enter your Choice : 3


3 4 5 6 56
Enter your Choice : 7
Enter the data you want to delete : 4

Enter your Choice : 3


3 5 6 56

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>

void linear_search(int arr[] , int n , int search , int *flag ){


for(int i=0 ; i<n ; i++){
if(arr[i] == search ){
*flag = 1;
printf("The Element is found at postion %d : ",i+1);
break;
}
else{
*flag = 0;
}
}
}

int main() {

int n , search;
int flag = 0;

printf("Enter how many number you want to store : ");


scanf("%d",&n);

int arr[n];

for(int i=0 ; i<n ; i++){


printf("Enter the element %d : ",i+1);
scanf("%d",&arr[i]);
}

printf("Enter The Number you want to search : ");


scanf("%d",&search);
linear_search(arr,n,search,&flag);

if(flag == 1){
printf("The item is found : \n");
}
else if (flag == 0){
printf("Element not found \n");
}
}

3. OUTPUT:

Enter how many number you want to store : 5


Enter the element 1 : 12
Enter the element 2 : 13
Enter the element 3 : 14
Enter the element 4 : 63
Enter the element 5 : 16
Enter The Number you want to search : 13
The Element is found at postion 2 : The item is found

b).
#include <stdio.h>

int main() {
int n , search , left , right , middle;

printf("Enter how many number you want to store : ");


scanf("%d",&n);

int arr[n];

for(int i=0 ; i<n ; i++){


printf("Enter the element %d : ",i+1);
scanf("%d",&arr[i]);
}
left = 0;
right = n-1;

printf("Enter the number you want to search : ");


scanf("%d",&search);

while(left <= right){


middle = (left + right) / 2;
if(arr[middle] == search){
printf("Number is found at postion %d : ",middle+1); //10 20 30 40
break;
}
else if(arr[middle] < search){
left = middle + 1 ;
}
else if(arr[middle] > search){
right = middle - 1;
}
}
if(left > right){
printf("Number not found ! \n");
}

OUTPUT:

Enter how many number you want to store : 5


Enter the element 1 : 11
Enter the element 2 : 22
Enter the element 3 : 33
Enter the element 4 : 44
Enter the element 5 : 55
Enter the number you want to search : 33
Number is found at postion 3

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];

for(int i=0 ; i<n ; i++){


printf("Enter the elment %d : ",i+1);
scanf("%d",&arr[i]);
}

for(int i=0 ; i<n ; i++){


for(int j=i+1 ; j<n ; j++){
if(arr[i] > arr[j]){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("The sorted array is : ");
for(int i=0 ; i<n ; i++){
printf("%d \t",arr[i]);
}
}

OUTPUT:

Enter the range of the array : 5


Enter the elment 1 : 56
Enter the elment 2 : 1
Enter the elment 3 : 745
Enter the elment 4 : 23
Enter the elment 5 : 8
The sorted array is : 1 8 23 56 745

You might also like