0% found this document useful (0 votes)
215 views36 pages

Lab Report On Data Structure Using C Lab Work

This document contains a lab report submitted by MD Sajjadul Islam to Mr. Khalil Ahmed for the Data Structure using C Lab work course at the Department of Computer Science and Engineering, Comilla University, Cumilla. The lab report includes 26 programs solving various problems related to arrays, linked lists, stacks, queues, sorting, and binary trees using C language. These programs demonstrate skills in implementing common data structures and algorithms.

Uploaded by

me homer
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)
215 views36 pages

Lab Report On Data Structure Using C Lab Work

This document contains a lab report submitted by MD Sajjadul Islam to Mr. Khalil Ahmed for the Data Structure using C Lab work course at the Department of Computer Science and Engineering, Comilla University, Cumilla. The lab report includes 26 programs solving various problems related to arrays, linked lists, stacks, queues, sorting, and binary trees using C language. These programs demonstrate skills in implementing common data structures and algorithms.

Uploaded by

me homer
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/ 36

Comilla University, Cumilla

Department of Computer Science and


Engineering
CSE-1202: Data Structure Lab
Second Semester

LAB REPORT on
Data Structure using C Lab work

Submitted by: Submitted to:


MD SAJJADUL ISLAM MR. KHALIL AHMED
ID: 12208040 Assistant Professor
Session: 2021-2022 Department of CSE
Department of CSE Comilla University, Cumilla
Comilla University, Cumilla

Submitted date: December 28, 2023


1. Write a C program for Deletion a element from k position
2. Write a C program for Insertion a element from k position
3. Write a C program for Matrix multiplication
4. Write a C program for Binary search
5. Write a C program for first, second and third maximum
6. Write a C program for first, second and third minimum
7. Write a C program for Linear search
8. Write a C program for Matrix addition
9. Write a C program for Matrix subtraction
10.Write a C program for Singly Linked list implementation
11.Write a C program for Doubly Linked list implementation
12.Write a C program for Circular Linked list implementation
13.Write a C program for array implementation of stack
14.Write a C program for linked implementation of stack
15.Write a C program for infix to postfix
16.Write a C program for evaluation of postfix (postfix to infix)
17.Write a C program for Tower of Hanoi using recursion
18.Write a C program for circular queue implementation
19.Write a C program for array implementation of queue
20.Write a C program for linked implementation of queue
21.Write a C program for insertion sort
22.Write a C program for selection sort
23.Write a C program for quick sort
24.Write a C program for merge sort
25.Write a C program for bubble sort
26.Write a C program for array implementation of tree operation- traverse
preorder, in-order, post-order)
Array

1. Write a C program for delete a element in k position


Ans:
#include <stdio.h>
int main() {
int array[10] = {-1, 3, 5, 22, 77};
int i, k, N, D;
N = 5;
printf("The contents of the array are:\n");
for (i = 0; i < N; i++)
printf("array[%d] = %d\n", i, array[i]);
printf("\nEnter the index location from where element is to be deleted:\n");
scanf("%d", &k);
D = array[k];
for (i = k; i <= N - 2; i++)
array[i] = array[i + 1];
N = N - 1;
printf("\n%d element deleted from index location %d\n", D, k);
printf("The contents of the array after array deletion are:\n");
for (i = 0; i < N; i++)
printf("array[%d] = %d\n", i, array[i]);
return 0;
}
2. Write a C program for insert a element in k position
Ans:
#include <stdio.h>
#include <stdlib.h>
int main() {
int array[10] = {-1, 3, 5, 22, 77};
int i, k, N, P;
N = 5;
printf("The contents of the array are:\n");
for (i = 0; i < N; i++)
printf("array[%d] = %d\n", i, array[i]);
printf("\nEnter the element to be inserted:\n");
scanf("%d", &P);
printf("\nEnter the index location where %d is to be inserted:\n", P);
scanf("%d", &k);
for (i = N; i >= k; i--)
array[i + 1] = array[i];
array[k] = P;
N = N + 1;
printf("\nThe contents of the array after array insertion are:\n");
for (i = 0; i < N; i++)
printf("array[%d] = %d\n", i, array[i]);
return 0;
}
3. Write a C program for Matrix multiplication
Ans:
#include<stdio.h>
void binary_search(int data[],int ub,int lb,int loc){
printf("enter the element to be searched :");
int n;
scanf("%d",&n);
while(lb<=ub){
int mid_index=(ub+lb)/2;
if(n==data[mid_index]){
printf("the position is %d",mid_index);
break;
}
if(n>data[mid_index]){
lb=mid_index+1;
}
else
ub=mid_index-1;
}
if(lb>ub){
printf("%d",loc);
}
}
int main(){
printf("enter the array size :");
int n;
scanf("%d",&n);
int data[n];
printf("enter the elements of array :");
for(int i=0;i<n;i++){
scanf("%d",&data[i]);
}
binary_search(data,n-1,0,-1);
}

4. Write a C program for binary search


Ans:
#include<stdio.h>
void binary_search(int data[],int ub,int lb,int loc){
printf("enter the element to be searched :");
int n;
scanf("%d",&n);
while(lb<=ub){
int mid_index=(ub+lb)/2;
if(n==data[mid_index]){
printf("the position is %d",mid_index);
break;
}
if(n>data[mid_index]){
lb=mid_index+1;
}
else
ub=mid_index-1;
}
if(lb>ub){
printf("%d",loc);
}
}
int main(){
printf("enter the array size :");
int n;
scanf("%d",&n);
int data[n];
printf("enter the elements of array :");
for(int i=0;i<n;i++){
scanf("%d",&data[i]);
}
binary_search(data,n-1,0,-1);
}

5. Write a C program for finding first, second and third maximum


Ans:
#include <stdio.h>
#include <limits.h>
void findLargest(int arr[], int n) {
int first, second, third;
first = second = third = INT_MIN;
for (int i = 0; i < n; i++) {
if (arr[i] > first) {
third = second;
second = first;
first = arr[i];
}
else if (arr[i] > second && arr[i] < first) {
third = second;
second = arr[i];
}
else if (arr[i] > third && arr[i] < second) {
third = arr[i];
}
}
printf("First largest: %d\n", first);
printf("Second largest: %d\n", second);
printf("Third largest: %d\n", third);
}
int main() {
int numbers[] = {10, 5, 8, 20, 15, 10, 20, 10};
int n = sizeof(numbers) / sizeof(numbers[0]);

findLargest(numbers, n);

return 0;
}

6. Write a C program for finding first, second and third minimum


Ans:
#include <stdio.h>
#include <limits.h>
void findMinElements(int arr[], int n) {
int firstMin, secondMin, thirdMin;
firstMin = secondMin = thirdMin = INT_MAX;
for (int i = 0; i < n; i++) {
if (arr[i] < firstMin) {
thirdMin = secondMin;
secondMin = firstMin;
firstMin = arr[i];
}
else if (arr[i] < secondMin && arr[i] > firstMin) {
thirdMin = secondMin;
secondMin = arr[i];
}
else if (arr[i] < thirdMin && arr[i] > secondMin) {
thirdMin = arr[i];
}
}
printf("First minimum: %d\n", firstMin);
printf("Second minimum: %d\n", secondMin);
printf("Third minimum: %d\n", thirdMin);
}
int main() {
int numbers[] = {10, -1, 2, 5, 8, 20, 15, 10, 8, 8, 32, 20, 10};
int n = sizeof(numbers) / sizeof(numbers[0]);
findMinElements(numbers, n);
return 0;
}

7. Write a C program for linear search


Ans:
#include<stdio.h>
void solve(){
int n,count=0;
printf("enter the size of the array :");
scanf("%d",&n);
int a[n];
printf("enter the elements :");
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
int x;
printf("enter the element you want to find :");
scanf("%d",&x);
for(int i=0;i<n;i++){
if(a[i]==x){
printf("the position is %d\n",i);
count++;
break;
}
}
if(count==0) printf("the element is not found\n");
}
int main(){
solve();
return 0;

}
8. Write a C program for matrix addition
Ans:
#include <stdio.h>
void matrix_addition(int rows, int cols, int A[rows][cols], int B[rows][cols], int
C[rows][cols]) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
C[i][j] = A[i][j] + B[i][j];
}
}
}
void input_matrix(int rows, int cols, int matrix[rows][cols]) {
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
printf("Enter element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
}
void print_matrix(int rows, int cols, int matrix[rows][cols]) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int rows, cols;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int matrix_A[rows][cols];
int matrix_B[rows][cols];
int result_matrix[rows][cols];
printf("\nEnter elements for Matrix A:\n");
input_matrix(rows, cols, matrix_A);
printf("\nEnter elements for Matrix B:\n");
input_matrix(rows, cols, matrix_B);
matrix_addition(rows, cols, matrix_A, matrix_B, result_matrix);
printf("\nMatrix A:\n");
print_matrix(rows, cols, matrix_A);
printf("\nMatrix B:\n");
print_matrix(rows, cols, matrix_B);
printf("\nResultant Matrix (A + B):\n");
print_matrix(rows, cols, result_matrix);

return 0;
}
9. Write a C program for matrix subtraction
Ans:
#include <stdio.h>
void matrix_subtraction(int rows, int cols, int A[rows][cols], int B[rows][cols], int
C[rows][cols]) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
C[i][j] = A[i][j] - B[i][j];
}
}
}
void input_matrix(int rows, int cols, int matrix[rows][cols]) {
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
printf("Enter element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
}
void print_matrix(int rows, int cols, int matrix[rows][cols]) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int rows, cols;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int matrix_A[rows][cols];
int matrix_B[rows][cols];
int result_matrix[rows][cols];
printf("\nEnter elements for Matrix A:\n");
input_matrix(rows, cols, matrix_A);
printf("\nEnter elements for Matrix B:\n");
input_matrix(rows, cols, matrix_B);
matrix_subtraction(rows, cols, matrix_A, matrix_B, result_matrix);
printf("\nMatrix A:\n");
print_matrix(rows, cols, matrix_A);
printf("\nMatrix B:\n");
print_matrix(rows, cols, matrix_B);
printf("\nResultant Matrix (A - B):\n");
print_matrix(rows, cols, result_matrix);
return 0;
}

Linked list

1. Write a C program for implementation of singly linked list


Ans:
#include <stdio.h>
#include <stdlib.h>
struct node{
int INFO;
struct node *NEXT;
};
struct node *FIRST = NULL;
struct node *LAST = NULL;
void insert(int);
int delete(int);
void print(void);
struct node *search(int);
int main()
{
int num1, num2, choice;
struct node *location;
while (1)
{
printf("Select an option\n");
printf("1 - Insert\n");
printf("2 - Delete\n");
printf("3 - Search\n");
printf("4 - Print\n");
printf("5 - Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter the element to be inserted into the linked list: ");
scanf("%d", &num1);
insert(num1);
printf("\n%d successfully inserted into the linked list\n", num1);
break;

case 2:
printf("\nEnter the element to be deleted from the linked list: ");
scanf("%d", &num1);
num2 = delete(num1);
if (num2 == -9999)
printf("\n%d is not present in the linked list\n", num1);
else
printf("\nElement %d successfully deleted from the linked list\n", num2);
break;

case 3:
printf("\nEnter the element to be searched: ");
scanf("%d", &num1);
location = search(num1);
if (location == NULL)
printf("\n%d is not present in the linked list\n", num1);
else
{
if (location == LAST)
printf("\nElement %d is the last element in the list", num1);
else
printf("\nElement %d is present before element %d in the linked list\n",
num1, (location->NEXT)->INFO);
}
break;

case 4:
print();
break;

case 5:
exit(0);
break;

default:
printf("\nIncorrect Choice, please try again.\n");
break;
}
}
return 0;
}
void insert(int value)
{
struct node *PTR = (struct node *)malloc(sizeof(struct node));
if (PTR == NULL){
printf("\nMemory allocation failed. Exiting...\n");
exit(1);
}
PTR->INFO = value;
if (FIRST == NULL){
FIRST = LAST = PTR;
PTR->NEXT = NULL;
}
else{
LAST->NEXT = PTR;
PTR->NEXT = NULL;
LAST = PTR;
}
}
int delete(int value)
{
struct node *LOC, *TEMP;
int i;
i = value;
LOC = search(i);
if (LOC == NULL)
return -9999;
if (LOC == FIRST)
{
if (FIRST == LAST)
FIRST = LAST = NULL;
else
FIRST = FIRST->NEXT;

return value;
}
for (TEMP = FIRST; TEMP->NEXT != LOC; TEMP = TEMP->NEXT);
TEMP->NEXT = LOC->NEXT;
if (LOC == LAST)
LAST = TEMP;
return LOC->INFO;
}
struct node *search(int value){
struct node *PTR;
if (FIRST == NULL)
return NULL;
for (PTR = FIRST; PTR != NULL && PTR->INFO != value; PTR = PTR-
>NEXT);
return PTR;
}
void print(){
struct node *PTR;
if (FIRST == NULL){
printf("\nEmpty list!!\n");
return;
}
printf("\nLinked list elements:\n");
for (PTR = FIRST; PTR != NULL; PTR = PTR->NEXT)
printf("\t%d", PTR->INFO);
printf("\n");
}
2. Write a C program for implementation of doubly linked list
Ans:
#include <stdio.h>
#include <stdlib.h>
struct dl_node{
int INFO;
struct dl_node *NEXT;
struct dl_node *PREVIOUS;
};
struct dl_node *FIRST = NULL;
struct dl_node *LAST = NULL;
void insert(int);
int delete(int);
void print(void);
struct dl_node *search(int);
int main(){
int num1, num2, choice;
struct dl_node *location;
while (1)
{
printf("\n\nSelect an option\n");
printf("1 - Insert\n");
printf("2 - Delete\n");
printf("3 - Search\n");
printf("4 - Print\n");
printf("5 - Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter the element to be inserted into the linked list: ");
scanf("%d", &num1);
insert(num1);
printf("%d successfully inserted into the linked list!\n", num1);
break;
case 2:
printf("\nEnter the element to be deleted from the linked list: ");
scanf("%d", &num1);
num2 = delete(num1);
if (num2 == -9999)
printf("\n%d is not present in the linked list\n", num1);
else
printf("\nElement %d successfully deleted from the linked list\n", num2);
break;
case 3:
printf("\nEnter the element to be searched: ");
scanf("%d", &num1);
location = search(num1);
if (location == NULL)
printf("\n%d is not present in the linked list\n", num1);
else
{
if (location == LAST)
printf("\nElement %d is the last element in the list\n", num1);
else
printf("\nElement %d is present before element %d in the linked list\n",
num1, (location->NEXT)->INFO);
}
break;
case 4:
print();
break;
case 5:
exit(0);
default:
printf("\nIncorrect choice. Please try again.\n");
break;
}
}
return 0;
}
void insert(int value){
struct dl_node *PTR = (struct dl_node *)malloc(sizeof(struct dl_node));
PTR->INFO = value;
if (FIRST == NULL){
FIRST = LAST = PTR;
PTR->NEXT = NULL;
PTR->PREVIOUS = NULL;
}
else{
LAST->NEXT = PTR;
PTR->NEXT = NULL;
PTR->PREVIOUS = LAST;
LAST = PTR;
}
}
int delete(int value){
struct dl_node *LOC, *TEMP;
int i;
i = value;
LOC = search(i);
if (LOC == NULL)
return -9999;
if (LOC == FIRST){
if (FIRST == LAST)
FIRST = LAST = NULL;
else{
FIRST->NEXT->PREVIOUS = NULL;
FIRST = FIRST->NEXT;
}
return value;
}
for (TEMP = FIRST; TEMP->NEXT != LOC; TEMP = TEMP->NEXT);
if (LOC == LAST){
LAST = TEMP;
TEMP->NEXT = NULL;
}
else{
TEMP->NEXT = LOC->NEXT;
LOC->NEXT->PREVIOUS = TEMP;
}
return LOC->INFO;
}
struct dl_node *search(int value){
struct dl_node *PTR;
if (FIRST == NULL)
return NULL;
if (FIRST == LAST && FIRST->INFO == value)
return FIRST;
for (PTR = FIRST; PTR != LAST; PTR = PTR->NEXT)
if (PTR->INFO == value)
return PTR;
if (LAST->INFO == value)
return LAST;
else
return NULL;
}
void print(){
struct dl_node *PTR;
if (FIRST == NULL){
printf("\nEmpty List!!\n");
return;
}
printf("\nDoubly linked list elements:\n");
if (FIRST == LAST){
printf("%d\n", FIRST->INFO);
return;
}
for (PTR = FIRST; PTR != LAST; PTR = PTR->NEXT)
printf("%d\t", PTR->INFO);
printf("%d\n", LAST->INFO);
}
3. Write a C program for implementation of circular linked list
Ans:
#include <stdio.h>
#include <stdlib.h>
struct cl_node{
int INFO;
struct cl_node *NEXT;
};
struct cl_node *FIRST = NULL;
struct cl_node *LAST = NULL;
void insert(int);
int delete(int);
void print(void);
struct cl_node *search(int);
int main(){
int num1, num2, choice;
struct cl_node *location;
while (1){
printf("\n\nSelect an option\n");
printf("1 - Insert\n");
printf("2 - Delete\n");
printf("3 - Search\n");
printf("4 - Print\n");
printf("5 - Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter the element to be inserted into the linked list: ");
scanf("%d", &num1);
insert(num1);
printf("\n%d successfully inserted into the linked list!\n", num1);
break;
case 2:
printf("\nEnter the element to be deleted from the linked list: ");
scanf("%d", &num1);
num2 = delete(num1);
if (num2 == -9999)
printf("\n%d is not present in the linked list\n", num1);
else
printf("\nElement %d successfully deleted from the linked list\n", num2);
break;
case 3:
printf("\nEnter the element to be searched: ");
scanf("%d", &num1);
location = search(num1);
if (location == NULL)
printf("\n%d is not present in the linked list\n", num1);
else
printf("\nElement %d is present before element %d in the circular linked
list\n", num1, (location->NEXT)->INFO);
break;
case 4:
print();
printf("Press Enter to continue...\n");
getchar();
break;
case 5:
exit(0);
default:
printf("\nIncorrect choice. Please try again.\n");
break;
}
}
return 0;
}
void insert(int value)
{
struct cl_node *PTR = (struct cl_node *)malloc(sizeof(struct cl_node));
PTR->INFO = value;

if (FIRST == NULL)
{
FIRST = LAST = PTR;
PTR->NEXT = FIRST;
}
else{
LAST->NEXT = PTR;
PTR->NEXT = FIRST;
LAST = PTR;
}
}
int delete(int value)
{
struct cl_node *LOC, *TEMP;
int i;
i = value;
LOC = search(i);
if (LOC == NULL)
return -9999;
if (LOC == FIRST){
if (FIRST == LAST){
FIRST = LAST = NULL;
}
else{
FIRST = FIRST->NEXT;
LAST->NEXT = FIRST;
}
return value;
}
for (TEMP = FIRST; TEMP->NEXT != LOC; TEMP = TEMP->NEXT);
if (LOC == LAST){
LAST = TEMP;
TEMP->NEXT = FIRST;
}
else{
TEMP->NEXT = LOC->NEXT;
}
return LOC->INFO;
}
struct cl_node *search(int value)
{
struct cl_node *PTR;
if (FIRST == NULL)
return NULL;
if (FIRST == LAST && FIRST->INFO == value)
return FIRST;
for (PTR = FIRST; PTR != LAST; PTR = PTR->NEXT)
if (PTR->INFO == value)
return PTR;
if (LAST->INFO == value)
return LAST;
else
return NULL;
}
void print()
{
struct cl_node *PTR;
if (FIRST == NULL){
printf("\nEmpty List!!\n");
return;
}
printf("\nCircular linked list elements:\n");
if (FIRST == LAST){
printf("\t%d\n", FIRST->INFO);
return;
}
for (PTR = FIRST; PTR != LAST; PTR = PTR->NEXT)
printf("\t%d", PTR->INFO);
printf("\t%d\n", LAST->INFO);
}

Stack:

1. Write a C program for array implementation of stack


Ans:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int stack_array[MAX];
int top = -1;
void push(int data) {
if (isFull()) {
printf("Stack overflow!\n");
return;
}
top = top + 1;
stack_array[top] = data;
}
int pop() {
int value;
if (isEmpty()) {
printf("Stack Underflow!\n");
exit(1);
}
value = stack_array[top];
top = top - 1;
return value;
}
int peek() {
if (isEmpty()) {
printf("Stack Underflow!\n");
exit(1);
}
return stack_array[top];
}
void print() {
if (isEmpty()) {
printf("Stack Underflow!\n");
return;
}
for (int i = top; i >= 0; i--) {
printf("%d\n", stack_array[i]);
}
}
int isFull() {
return top == MAX - 1;
}
int isEmpty() {
return top == -1;
}
int main() {
int choice, data;
while (1) {
printf("\n1. Push\n");
printf("2. Pop\n");
printf("3. Print the top element\n");
printf("4. Print all the elements of the stack\n");
printf("5. Quit\n");
printf("Please enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the element to be pushed: ");
scanf("%d", &data);
push(data);
break;
case 2:
data = pop();
printf("Deleted element is %d\n", data);
break;
case 3:
printf("The topmost element of the stack is %d\n", peek());
break;
case 4:
print();
break;
case 5:
exit(0);
}
}
return 0;
}

2. Write a C program for linked implementation of stack


Ans:
#include <stdio.h>
#include <stdlib.h>
struct stack {
int element;
struct stack *next;
}*top;
void push(int);
int pop();
void display();
int main() {
int num1, num2, choice;
while (1) {
printf("Select a choice from the following:\n");
printf("[1] Push an element into the stack\n");
printf("[2] Pop out an element from the stack\n");
printf("[3] Display the stack elements\n");
printf("[4] Exit\n");
printf("Your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the element to be pushed into the stack: ");
scanf("%d", &num1);
push(num1);
break;

case 2:
num2 = pop();
printf("%d element popped out of the stack\n", num2);
break;

case 3:
display();
break;

case 4:
exit(1);

default:
printf("Invalid choice!\n");
}
}
return 0;
}
void push(int value) {
struct stack *ptr = (struct stack*)malloc(sizeof(struct stack));
ptr->element = value;
ptr->next = top;
top = ptr;
}
int pop() {
if (top == NULL) {
printf("Stack is Empty.\n");
exit(1);
} else {
int temp = top->element;
top = top->next;
return temp;
}
}
void display() {
struct stack *ptr1 = top;
printf("The various stack elements are:\n");
while (ptr1 != NULL) {
printf("%d\t", ptr1->element);
ptr1 = ptr1->next;
}
}

3. Write a C program to for infix to postfix using stack


Ans:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_SIZE 100
typedef struct {
char data[MAX_SIZE];
int top;
} Stack;
void initialize(Stack *stack) {
stack->top = -1;
}
int isEmpty(Stack *stack) {
return stack->top == -1;
}
void push(Stack *stack, char item) {
if (stack->top == MAX_SIZE - 1) {
printf("Stack Overflow\n");
exit(EXIT_FAILURE);
}
stack->data[++stack->top] = item;
}
char pop(Stack *stack) {
if (isEmpty(stack)) {
printf("Stack Underflow\n");
exit(EXIT_FAILURE);
}
return stack->data[stack->top--];
}
int getPrecedence(char operator) {
if (operator == '+' || operator == '-')
return 1;
else if (operator == '*' || operator == '/')
return 2;
else
return 0; }
void infixToPostfix(char *infix, char *postfix) {
Stack stack;
initialize(&stack);
push(&stack, '(');
strcat(infix, ")");
int i = 0, j = 0;
while (!isEmpty(&stack)) {
char current = infix[i];
if (isalnum(current)) {
postfix[j++] = current;
} else if (current == '(') {
push(&stack, '(');
} else if (current == ')') {
while (stack.data[stack.top] != '(') {
postfix[j++] = pop(&stack);
}
pop(&stack);
} else {
while (getPrecedence(current) <= getPrecedence(stack.data[stack.top])) {
postfix[j++] = pop(&stack);
}
push(&stack, current);
}
i++;
}
postfix[j] = '\0';
int main() {
char infix[MAX_SIZE];
char postfix[MAX_SIZE];
printf("Enter the infix expression: ");
scanf("%s", infix);
infixToPostfix(infix, postfix);
printf("Postfix expression: %s\n", postfix);
return 0;
}
4. Write a C program for evaluation of postfix ( postfix to infix )
Ans:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct Stack {
int *data;
int top;
int capacity;
} Stack;
Stack *createStack(int capacity) {
Stack *stack = (Stack *)malloc(sizeof(Stack));
stack->data = (int *)malloc(capacity * sizeof(int));
stack->top = -1;
stack->capacity = capacity;
return stack;
}
void push(Stack *stack, int item) {
if (stack->top == stack->capacity - 1) {
printf("Stack overflow\n");
exit(1);
}
stack->top++;
stack->data[stack->top] = item;
}
int pop(Stack *stack) {
if (stack->top == -1) {
printf("Stack underflow\n");
exit(1);
}
int item = stack->data[stack->top];
stack->top--;
return item;
}
int evaluatePostfix(char *expression) {
Stack *operandStack = createStack(100);
for (char *symbol = expression; *symbol != '\0'; symbol++) {
if (isdigit(*symbol)) {
push(operandStack, *symbol - '0');
} else if (*symbol == '+' || *symbol == '-' || *symbol == '*' || *symbol == '/') {
int A = pop(operandStack);
int B = pop(operandStack);
int result;
switch (*symbol) {
case '+':
result = B + A;
break;
case '-':
result = B - A;
break;
case '*':
result = B * A;
break;
case '/':
result = B / A;
break;
}
push(operandStack, result);
}
}
int value = pop(operandStack);
free(operandStack->data);
free(operandStack);
return value;
}
int main() {
char postfixExpression[100];
printf("Enter the postfix expression: ");
scanf("%s", postfixExpression);
int result = evaluatePostfix(postfixExpression);
printf("The value of the postfix expression %s is: %d\n", postfixExpression, result);
return 0;
}

5. Write a C program for Tower of Hanoi ( recursive )


Ans:
#include <stdio.h>
void towerOfHanoi(int n, char source, char destination, char auxiliary) {
if (n == 1) {
printf("Move disk 1 from %c to %c\n", source, destination);
return;
}
towerOfHanoi(n - 1, source, auxiliary, destination);
printf("Move disk %d from %c to %c\n", n, source, destination);
towerOfHanoi(n - 1, auxiliary, destination, source);
}
int main() {
int numDisks;
printf("Enter the number of disks: ");
scanf("%d", &numDisks);
towerOfHanoi(numDisks, 'A', 'C', 'B');
return 0;
}

Queue

1. Write a C program for implementation of circular queue


Ans:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
int queue[MAX_SIZE];
int front = -1;
int rear = -1;
void insert(int);
int del();
void display();
int main() {
int choice;
int num1 = 0, num2 = 0;
while (1) {
printf("\nSelect a choice from the following:");
printf("\n[1] Add an element into the queue");
printf("\n[2] Remove an element from the queue");
printf("\n[3] Display the queue elements");
printf("\n[4] Exit\n");
printf("\n\tYour choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\n\tEnter the element to be added to the queue: ");
scanf("%d", &num1);
insert(num1);
break;
case 2:
num2 = del();
if (num2 != -9999)
printf("\n\t%d element removed from the queue\n\t", num2);
break;

case 3:
display();
break;

case 4:
exit(0);

default:
printf("\nInvalid choice!\n");
break;
}
}
return 0;
}
void insert(int element) {
if ((front == 0 && rear == MAX_SIZE - 1) || (front == rear + 1)) {
printf("\tQueue is Full. Element %d cannot be added into the queue\n", element);
return;
}
if (front == -1) {
front = 0;
rear = 0;
} else if (rear == MAX_SIZE - 1) {
rear = 0;
} else {
rear = rear + 1;
}
queue[rear] = element;
}
int del() {
if (front == -1) {
printf("\n\tQueue is Empty.\n");
return -9999;
}
int removedElement = queue[front];
if (front == rear) {
front = -1;
rear = -1;
} else if (front == MAX_SIZE - 1) {
front = 0;
} else {
front = front + 1;
}
return removedElement;
}
void display() {
if (front == -1) {
printf("\n\tQueue is Empty!\n");
return;
}
printf("\n\tThe various queue elements are:\n");
int i = front;
while (1) {
printf("\t%d", queue[i]);
if (i == rear) {
break;
}
if (i == MAX_SIZE - 1) {
i = 0;
} else {
i = i + 1;
}
}
printf("\n");
}

2. Write a C program for array implementation of queue


Ans:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int queue[100];
int front=-1;
int rear=-1;
void insert(int);
int del();
void display();
void main()
{
int choice;
int num1=0,num2=0;
while(1)
{

printf("\nSelect a choice from the following:");


printf("\n[1] Add an element into the queue");
printf("\n[2] Remove an element from the queue");
printf("\n[3] Display the queue elements");
printf("\n[4] Exit\n");
printf("\n\tYour choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
printf("\n\tEnter the element to be added to the queue: ");
scanf("%d",&num1);
insert(num1);
break;
}
case 2:
{
num2=del();
if(num2==-9999)
;
else
printf("\n\t%d element removed from the queue\n\t",num2);

break;
}
case 3:
{
display();

break;
}
case 4:
exit(1);
break;
default:
printf("\nInvalid choice!\n");
break;
}
}
}

void insert(int element)


{
if(front==-1)
{
front = rear = front+1;
queue[front] = element;
return;
}
if(rear==99)
{
printf("Queue is Full.\n");

return;
}
rear=rear+1;
queue[rear]=element;
}

int del()
{
int i;
if(front==-1 && rear==-1)
{
printf("\n\tQueue is Empty.\n");

return (-9999);
}
if(front!=-1 && front==rear)
{
i=queue[front];
front=-1;
rear=-1;
return(i);
}
return(queue[front++]);
}

void display()
{
int i;
if(front==-1)
{
printf("\n\tQueue is Empty!\n");
return;
}
printf("\n\tThe various queue elements are:\n");
for(i=front;i<=rear;i++)
printf("\t%d",queue[i]);
}
3. Write a C program for linked implementation of queue
Ans:
#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
struct queue
{
int element;;
struct queue *next;
};
struct queue *front=NULL;
struct queue *rear = NULL;
void insert(int);
int del();
void display(void);
void main()
{
int num1, num2, choice;
while(1)
{

printf("\n\nSelect an option\n");
printf("\n1 - Insert an element into the Queue");
printf("\n2 - Remove an element from the Queue ");
printf("\n3 - Display all the elements in the Queue");
printf("\n4 - Exit");
printf("\n\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
{
printf("\nEnter the element to be inserted into the queue ");
scanf("%d",&num1);
insert(num1);
break;
}
case 2:
{
num2=del();
if(num2==-9999)
printf("\n\tQueue is empty!!");
else
printf("\n\t%d element removed from the queue\n\t",num2);

break;
}
case 3:
{
display();
break;
}
case 4:
{
exit(1);
break;
}
default:
{
printf("\nInvalid choice.");

break;
}
}
}
}

void insert(int value)


{
struct queue *ptr = (struct queue*)malloc(sizeof(struct queue));
ptr->element = value;
if(front==NULL)
{
front = rear = ptr;
ptr->next=NULL;
}

else
{
rear->next = ptr;
ptr->next = NULL;
rear = ptr;
}
}

int del()
{
int i;
if(front==NULL)
return(-9999);
else
{
i=front->element;
front = front->next;
return(i);
}
}

void display()
{
struct queue *ptr=front;
if(front==NULL)
{
printf("\n\tQueue is Empty!!");
return;
}
else
{
printf("\nElements present in the Queue are:\n");

while(ptr!=rear)
{
printf("\t%d",ptr->element);
ptr=ptr->next;
}
printf("\t%d",rear->element);
}
}

Sorting
1. Write a C program for Insertion sort
Ans:
#include <stdio.h>
#include <stdlib.h>
void insertion(int[], int);
int main() {
int *arr;
int i, N;
printf("Enter the number of elements in the array:\n");
scanf("%d", &N);
arr = (int *)malloc(sizeof(int) * N);
if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
printf("Enter the %d elements to sort:\n", N);
for (i = 0; i < N; i++)
scanf("%d", &arr[i]);
insertion(arr, N);
printf("\nThe sorted elements are:\n");
for (i = 0; i < N; i++)
printf("%d\n", arr[i]);
return 0;
}
void insertion(int array[], int size) {
int i, j, temp;
for (i = 1; i < size; i++) {
temp = array[i];
j = i - 1;
for (j ; j >= 0; j--) {
if (array[j] > temp)
array[j + 1] = array[j];
else
break;
}
array[j + 1] = temp;
}
}

2. Write a C program for Selection sort


Ans:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void selection(int*, int);

int Min(int*,int,int);
void main()
{
int *arr;
int i, N;

printf("Enter the number of elements in the array:\n");


scanf("%d",&N);
arr = (int*) malloc(sizeof(int));
printf("Enter the %d elements to sort:\n",N);
for (i=0;i<N;i++)
scanf("%d",&arr[i]);
selection(arr,N);
printf("\nThe sorted elements are:\n");
for(i=0;i<N;i++)
printf("%d\n",arr[i]);
getch();
}
void selection(int *a, int size)
{
int i=0,loc=0,temp=0;
for(i=0;i<size;i++)
{
loc=Min(a,i,size);
temp=a[loc];
a[loc]=a[i];
a[i]=temp;
}
}
int Min(int *array, int LB, int UB)
{
int m=LB;

while(LB<UB)
{
if(array[LB]<array[m])
m=LB;
LB++;
}
return(m);
}

3. Write a C program for quick sort


Ans:
#include <stdio.h>
#include <stdlib.h>
void quick(int [], int, int);
int main() {
int *arr;
int i, N;
printf("Enter the number of elements in the array:\n");
scanf("%d", &N);
if (N <= 0) {
printf("Invalid number of elements. Exiting...\n");
return 1; // Return an error code
}
arr = (int*) malloc(sizeof(int) * N);
printf("Enter the %d elements to sort:\n", N);
for (i = 0; i < N; i++)
scanf("%d", &arr[i]);
quick(arr, 0, N - 1);
printf("\nThe sorted elements are:\n");
for (i = 0; i < N; i++)
printf("%d\n", arr[i]);
return 0;
}
void quick(int array[], int LB, int UB) {
int pivot, nxt_pvt, left, right;
left = LB;
right = UB;
pivot = array[left];
while (LB < UB) {
while ((array[UB] >= pivot) && (LB < UB))
UB--;
if (LB != UB) {
array[LB] = array[UB];
LB++;
}
while ((array[LB] <= pivot) && (LB < UB))
LB++;
if (LB != UB) {
array[UB] = array[LB];
UB--;
}
}
array[LB] = pivot;
nxt_pvt = LB;
LB = left;
UB = right;
if (LB < nxt_pvt)
quick(array, LB, nxt_pvt - 1);
if (UB > nxt_pvt)
quick(array, nxt_pvt + 1, UB);
}

4. Write a C program for Merge sort


Ans:
#include <stdio.h>
#include <stdlib.h>
void mergesort(int*, int);
void merge(int*, int, int*, int);
int main() {
int *arr;
int i, N;

printf("Enter the number of elements in the array:\n");


scanf("%d", &N);

arr = (int*) malloc(sizeof(int) * N);

printf("Enter the %d elements to sort:\n", N);


for (i = 0; i < N; i++)
scanf("%d", &arr[i]);

mergesort(arr, N);

printf("\nThe sorted elements are:\n");


for (i = 0; i < N; i++)
printf("%d\n", arr[i]);
}

void mergesort(int *array, int size) {


int mid;

if (size == 1)
return;
else {
mid = size / 2;
mergesort(array, mid);
mergesort(array + mid, size - mid);
merge(array, mid, array + mid, size - mid);
}
}

void merge(int *a, int s1, int *b, int s2) {


int i, j, k, temp_arr[s1 + s2];
i = j = k = 0;

while (i < s1 && j < s2)


temp_arr[k++] = (a[i] < b[j]) ? a[i++] : b[j++];

while (i < s1)


temp_arr[k++] = a[i++];

while (j < s2)


temp_arr[k++] = b[j++];

for (i = 0; i < k; i++)


a[i] = temp_arr[i];
}
5. Write a C program for Bubble sort
Ans:
#include <stdio.h>
void bubble(int arr[], int size);
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
bubble(arr, size);
printf("Sorted array: ");
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
return 0;
}
void bubble(int arr[], int size) {
int i, j, temp;
for (i = size; i > 1; i--) {
for (j = 0; j < i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap the elements
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
}

Tree:

1. Write a C program for array implementation of tree operation- traverse preorder, in-
order, post-order)
Ans:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct bin_tree {
int INFO;
}node;
node tree[MAX_SIZE];
int count = 0;
int insert(int n) {
if (count < MAX_SIZE) {
tree[count].INFO = n;
count = count + 1;
return 1;
}
else {
printf("Tree is full. Cannot insert node.\n");
return 0;
}
}
void preorder(int index) {
if (index < count) {
printf("%d\n", tree[index].INFO);
preorder(2 * index + 1);
preorder(2 * index + 2);
}
}
void inorder(int index) {
if (index < count) {
inorder(2 * index + 1);
printf("%d\n", tree[index].INFO);
inorder(2 * index + 2);
}
}
void postorder(int index) {
if (index < count) {
postorder(2 * index + 1);
postorder(2 * index + 2);
printf("%d\n", tree[index].INFO);
}
}
int main() {
int element, choice;
while (1) {
printf("Select an option\n");
printf("\n1 - Insert");
printf("\n2 - Preorder");
printf("\n3 - Inorder");
printf("\n4 - Postorder");
printf("\n5 - Exit");
printf("\n\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:

printf("\n\nEnter the node value: ");


scanf("%d", &element);
if (insert(element))
printf("Node inserted successfully.\n");
break;

case 2:
printf("Preorder Traversal:\n");
preorder(0);
break;

case 3:
printf("Inorder Traversal:\n");
inorder(0);
break;

case 4:
printf("Postorder Traversal:\n");
postorder(0);
break;

case 5:
exit(0);
break;

default:
printf("\nIncorrect choice. Please try again.");
break;
}
}
return 0;
}

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