0% found this document useful (0 votes)
12 views63 pages

Final Ds Lab File

The document discusses experiments performed on arrays and linked lists. It includes linear and binary search on an array, traversing an array and performing operations like insertion, deletion and reversal on an array. It also includes creating linked lists and performing operations like reversal, insertion and deletion on linked lists.

Uploaded by

Ishan xz
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)
12 views63 pages

Final Ds Lab File

The document discusses experiments performed on arrays and linked lists. It includes linear and binary search on an array, traversing an array and performing operations like insertion, deletion and reversal on an array. It also includes creating linked lists and performing operations like reversal, insertion and deletion on linked lists.

Uploaded by

Ishan xz
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/ 63

VIDIT GHAI CSE-B vips-tc

Practical file submitted in partial


fulfillment for the evaluation of

“Data Structures Lab”

Submitted By:
Student Name: ISHAN KHATRI
Enrolment no: 00217707223
Branch & Section: CSE A

Submitted To:
 Dr. Nivedita Palia

1
VIDIT GHAI CSE-B vips-tc

S.No Experiment Title Page Date Sign


No.

2
VIDIT GHAI CSE-B vips-tc

1.) Perform Linear Search and Binary Search on 4


an array by passing the array to a function and
then returning the position of the element from
the function else return -1 if the element is not
found
2.) Write a program to perform the following 8
array operations:
 Traversing in an array
 Inserting element at specific position in
an array
 Deleting element from specific position
in array
 Reversing the array
 Merging 2 arrays
3.) Create a linked list with nodes having 16
information about a student and perform:
 Reversal of that linked list.
 Insert a new node at specified position.
 Delete of a node with the roll no. of
specified student
4.) Stack Implementation using Linked list. 22
5.) Create doubly linked list with nodes having 27
information about an employee and perform
insertion at front of doubly linked list and
perform deletion at end of that doubly linked
list.
6.) Implement two stacks using in a single array. 33
7.) Create a linear queue using linked list and 38
implement different operations such as
insertion, deletion and display the queue
elements.
8.) Create a binary tree and perform tree traversal 43
using recursion.
9.) Write a program to evaluate 47
1. Prefix Expression
2. Postfix Expression

10.) Implement selection sort, bubble sort, insertion 54


sort and heap sort using array as a data
structure.

3
VIDIT GHAI CSE-B vips-tc

EXPERIMENT 01
Problem Statement:
4
VIDIT GHAI CSE-B vips-tc

Perform Linear Search and Binary Search on an array by passing the array to a
function and then returning the position of the element from the function else
return -1 if the element is not found.

Programme code:
#include<stdio.h>
int linearsearch(int arr[], int n, int num){
int i=0;
for(i; i<n; i++){
if(arr[i]==num){
return i;
}
}
return -1;
}
int binarysearch(int arr[], int start, int end, int num){
while(start<=end){
int mid=(start+end-1)/2;
if(arr[mid]==num){
return mid;
}
if(arr[mid]<num){
start=mid+1;
}else{
end=mid-1;
}
}
return -1;
}

5
VIDIT GHAI CSE-B vips-tc

int main(){
int n;
int i=0;
printf("Enter the number of elements in sorted array: ");
scanf("%d",&n);
int arr[n];
for(i; i<n; i++){
printf("Enter the %d element of array: ",i+1);
scanf("%d",&arr[i]);
}
int num;
printf("Enter the element for searching:\n ");
scanf("%d",&num);
int result=binarysearch(&arr[0], 0, n-1, num);
printf("Searched element by linear search:\n ");
if(result==-1){
printf("Element not found!");
}else{
printf("Element found at %d position",result+1);
}
printf("\n");
result=linearsearch(&arr[0], n, num);
printf("Searched element by linear search:\n ");
if(result==-1){
printf("Element not found!");
}else{
printf("Element found at %d position",result+1);

6
VIDIT GHAI CSE-B vips-tc

}
return 0;
}
Algorithm:
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
7
VIDIT GHAI CSE-B vips-tc

__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
Output:

Learning Outcome:
________________________________________________________
________________________________________________________
________________________________________________________

EXPERIMENT 02
Problem Statement:
8
VIDIT GHAI CSE-B vips-tc

Write a program to perform the following array operations:


1. Traversing in an array
2. Inserting element at specific position in array
2. Deleting element from specific position in array
3. Reversing the array
4. Merging 2 arrays

Programme Code:
#include <stdio.h>
#include <stdlib.h>

int reverse(int n, int arr[]){


int start = 0;
int end = n-1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
printf("\nReversed array: ");
int i;
for (i = 0; i < n; i++) {
printf("%d\t", arr[i]);
}
}
int main() {
int n,i;
9
VIDIT GHAI CSE-B vips-tc

printf("Enter number of elements of array : ");


scanf("%d", &n);
int array[n];
for(i=0; i<n; i++){
printf("Enter the %d element of array : ",i+1);
scanf("%d",&array[i]);
}

// Traversal of Array.
printf("Entered array : ");
for(i=0; i<n; i++){
printf("%d\t", array[i]);
}

// Insertion of an element at kth position.


int val,k;
printf("\nEnter element to insert : ");
scanf("%d", &val);
printf("Enter the position of insertion : ");
scanf("%d", &k);
for(i=n; i>=k; i--){
array[i+1] = array[i];
}
n=n+1;
array[k] = val;
printf("Updated array : ");
for(i=0; i<n; i++){
printf("%d\t", array[i]);
10
VIDIT GHAI CSE-B vips-tc

// Deletion of an element at a given position.


int k2, val2;
printf("\nEnter the position you wish to delete : ");
scanf("%d", &k2);
for(i = k2; i <= n ; i++){
array[i] = array[i+1];
}
n=n-1;
printf("Updated array : ");
for(i=0; i<n; i++){
printf("%d\t", array[i]);
}

// Reversal of Array
reverse(n, array);

return 0;
}

Algorithm:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
11
VIDIT GHAI CSE-B vips-tc

________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
Output:

12
VIDIT GHAI CSE-B vips-tc

Learning Outcome:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________

Problem Statement:
Merging 2 arrays.

Programme Code:
#include<stdio.h>
void mergeArrays(int arr1[], int arr2[], int n1, int n2, int arr3[]){
int i = 0, j = 0, k = 0;
while (i<n1 && j <n2){
if (arr1[i] < arr2[j]){
arr3[k++] = arr1[i++];

13
VIDIT GHAI CSE-B vips-tc

}else{
arr3[k++] = arr2[j++];
}
}
while (i < n1){
arr3[k++] = arr1[i++];
}
while (j < n2){
arr3[k++] = arr2[j++];
}
printf("Arrays after merging: \t");
for (i=0; i < n1+n2; i++){
printf("%d\t",arr3[i]);
}
}
int main(){
int n,k,i,j;
printf("Enter the number of elements in first array : ");
scanf("%d",&n);
printf("Enter the number of elements in second array : ");
scanf("%d",&k);
int arr1[n],arr2[k];
for(i; i<n; i++){
printf("Enter the %d element of first array : ",i+1);
scanf("%d",&arr1[i]);
}
for(i=0; i<k; i++){

14
VIDIT GHAI CSE-B vips-tc

printf("Enter the %d element of second array : ",i+1);


scanf("%d",&arr2[i]);
}
int arr3[n+k];
mergeArrays(arr1, arr2, n, k, arr3);
return 0;
}

Algorithm:
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
15
VIDIT GHAI CSE-B vips-tc

__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
Output:

Learning Outcome:
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
EXPERIMENT 03

16
VIDIT GHAI CSE-B vips-tc

Problem Statement:
Create a linked list with nodes having information about a student and
perform:
 Reversal of that linked list.
 Insert a new node at specified position.
 Delete of a node with the roll no. of specified student.

Programme Code:
#include <stdio.h>
#include <stdlib.h>
struct node{
char name[20];
int rollno;
struct node * next;
};

void linkedlisttraversal(struct node * ptr){


while(ptr!=NULL){
printf("name: %s\n",ptr->name);
printf("roll no: %d\n",ptr->rollno);
ptr=ptr->next;
}
}

struct node* linkedlistreversal(struct node*p){


struct node*s=p;
struct node* prev=NULL;
struct node* current;

17
VIDIT GHAI CSE-B vips-tc

struct node* next=NULL;


current=s;
prev=NULL;
while(current!=NULL){
next=current->next;
current->next=prev;
prev=current;
current=next;
}
return prev;
}

struct node* insertAtIndex(struct node* head,int index){


struct node* q=(struct node*)malloc(sizeof(struct node));
struct node* w=head;
printf("enter name of student you want to insert: ");
scanf("%s",&q->name);
printf("enter roll number of 4th student: ");
scanf("%d",&q->rollno);
int i=0;
while(i!=index-1){
w=w->next;
i++;
}
q->next=w->next;
w->next=q;
return head;

18
VIDIT GHAI CSE-B vips-tc

struct node* deleteAtValue(struct node* head, int value){


struct node*t=head;
struct node*s=head->next;
while(s->rollno!=value && s->next!=NULL){
t=t->next;
s=s->next;
}
if(s->rollno==value){
t->next=s->next;
free(s);
}
return head;
}

int main() {
int pos,roll;
struct node* head;
struct node* s1;
struct node* s2;
head = (struct node *)malloc (sizeof(struct node));
s1 = (struct node *)malloc (sizeof(struct node));
s2 = (struct node *)malloc (sizeof(struct node));
printf("Enter the name of first student: ");
scanf("%s",&head->name);
head->rollno=1;

19
VIDIT GHAI CSE-B vips-tc

head->next=s1;
printf("Enter the name of second student: ");
scanf("%s",&s1->name);
s1->rollno=2;
s1->next=s2;
printf("Enter the name of third student: ");
scanf("%s",&s2->name);
s2->rollno=3;
s2->next=NULL;
printf("Before reversal\n");
linkedlisttraversal(head);
printf("After reversal\n");
head=linkedlistreversal(head);
linkedlisttraversal(head);
printf("Insertion\n");
printf("Enter index number where you want to insert details of 4th student: ");
scanf("%d",&pos);
head=insertAtIndex(head,pos);
linkedlisttraversal(head);
printf("Deletion\n");
printf("Enter roll number of the student,to remove his/her information: ");
scanf("%d",&roll);
head=deleteAtValue(head,roll);
linkedlisttraversal(head);
return 0;
}

20
VIDIT GHAI CSE-B vips-tc

Algorithm:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________

21
VIDIT GHAI CSE-B vips-tc

Output:

Learning Outcome:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________

EXPERIMENT 04
Problem Statement:

22
VIDIT GHAI CSE-B vips-tc

Implement STACK using linked list.


Programme Code:
#include<stdio.h>
#include<stdlib.h>
// stack implementation using linked list.
struct node{
int data;
struct node* next;
};
struct node* push(struct node* top){
struct node* newnode;
newnode=(struct node*)malloc(sizeof(struct node));
if (newnode==NULL){
printf("Overflow\n");
}else{
printf("Enter the element to be added: ");
scanf("%d",&newnode->data);
newnode->next=top;
top=newnode;
return top;
}
};
void display(struct node* top){
struct node *temp=top;
if(temp==NULL){
printf("Stack is empty\n");
}else{

23
VIDIT GHAI CSE-B vips-tc

while(temp!=NULL){
printf("%d\t",temp->data);
temp=temp->next;
}
printf("\n");
}
};
void peek(struct node* top){
printf("Element at the top is: %d",top->data);
};
struct node* pop(struct node* top){
struct node* temp=top;
if (temp==NULL){
printf("Underflow\n");
}else{
top=top->next;
free(temp);
return top;
}
};
int main(){
struct node *top,*node1,*node2;
top=NULL;
printf("Pushing Elements into stack...\n");
top=push(top);
top=push(top);
top=push(top);

24
VIDIT GHAI CSE-B vips-tc

top=push(top);
top=push(top);
printf("Displaying stack...\n");
display(top);
printf("Popping stack...\n");
top=pop(top);
printf("Displaying stack after popping...\n");
display(top);
peek(top);
return 0;
}
Algorithm:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________

25
VIDIT GHAI CSE-B vips-tc

________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________

Output:

Learning Outcome:

26
VIDIT GHAI CSE-B vips-tc

________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________

EXPERIMENT 05
27
VIDIT GHAI CSE-B vips-tc

Problem Statement:
Create doubly linked list with nodes having information about an employee and
perform insertion at front of doubly linked list and perform deletion at end of
that doubly linked list.
Programme Code:
#include<stdio.h>
#include<stdlib.h>

struct employee{
struct employee *prev;
int id;
char name[20];
float sal;
struct employee *next;
};

typedef struct employee Emp;

void printll(Emp *ptr){


printf("ID Name Salary\n");
while(ptr != NULL){
printf("%d %s %f\n", ptr->id, ptr->name, ptr-
>sal);
ptr= ptr->next;
}
return;
}

void prin(Emp *ptr){


printf("ID Name Salary\n");
while(ptr != NULL){
printf("%d %s %f\n", ptr->id, ptr->name, ptr-
>sal);
ptr= ptr->prev;
}
return;
}

28
VIDIT GHAI CSE-B vips-tc

void insert(Emp **sptr, Emp **lptr){


Emp *new = (Emp*)malloc(sizeof(Emp));
if (new == NULL){
printf("Unable to allocate memory!!!\n");
return;
}else{
if (*sptr == NULL){
printf("Enter Employee ID: ");
scanf("%d",&(new->id));
printf("Enter Employee Name: ");
scanf("%s", new->name);
printf("Enter Employee Salary: ");
scanf("%f",&(new->sal));
new->next = NULL;
new->prev = NULL;
*sptr = new;
*lptr = new;
}else{
printf("Enter Employee ID: ");
scanf("%d",&(new->id));
printf("Enter Employee Name: ");
scanf("%s", new->name);
printf("Enter Employee Salary: ");
scanf("%f",&(new->sal));
(*sptr)->prev = new;
new->next = *sptr;
new->prev = NULL;
*sptr = new;
}
}
return;
}

void delete(Emp **lptr, Emp **sptr){


if(*lptr == NULL){
printf("Underflow!!!\n");
return;

29
VIDIT GHAI CSE-B vips-tc

}else{
if ((*lptr)->prev != NULL) {
Emp *temp = *lptr;
*lptr = (*lptr)->prev;
free(temp);
(*lptr)->next = NULL;
printf("Entry deleted from last!!!\n");
} else {
free(*lptr);
*lptr = NULL;
*sptr = NULL;
printf("Entry deleted from last!!!\n");
}

}
}

int main(){
Emp *start = NULL;
Emp *last = NULL;
printll(start);

insert(&start, &last);
insert(&start, &last);
insert(&start, &last);
printll(start);

delete(&last, &start);
printll(start);
delete(&last, &start);
delete(&last, &start);
delete(&last, &start);
printll(start);

return 0;
}
Algorithm:

30
VIDIT GHAI CSE-B vips-tc

________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
31
VIDIT GHAI CSE-B vips-tc

________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
Output:

Learning Outcome:

32
VIDIT GHAI CSE-B vips-tc

________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________

EXPERIMENT 06

33
VIDIT GHAI CSE-B vips-tc

Problem Statement:
Implement two stacks using in a single array.
Programme Code:
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,top1,top2,ch=1,a,i,arr[100];
printf("Enter size of array you want to use\n");
scanf("%d",&n);
top1=-1;
top2=n;
while(ch!=0){
printf("What do u want to do?\n");
printf("Enter 1 to Push element in stack 1\n");
printf("Enter 2 to Push element in stack 2\n");
printf("Enter 3 to Pop element from stack 1\n");
printf("Enter 4 to Pop element from stack 2\n");
printf("Enter 5 to Display stack 1\n");
printf("Enter 6 to Display stack 2\n");
printf("Enter 0 to EXIT\n");
scanf("%d",&ch);
switch(ch){
case 1:
printf("Enter the element\n");
scanf("%d",&a);
if(top1!=(top2-1))
arr[++top1]=a;
else
printf("Overflow\n");
break;
case 2:
printf("Enter the element\n");
scanf("%d",&a);
if(top2!=(top1+1))
arr[--top2]=a;
else
printf("Overflow\n");

34
VIDIT GHAI CSE-B vips-tc

break;
case 3:
if(top1==-1)
printf("Stack1 is empty\n");
else{
a=arr[top1--];
printf("%d\n",a);
}
break;
case 4:
if(top2==n)
printf("Stack2 is empty\n");
else{
a=arr[top2++];
printf("%d\n",a);
}
break;
case 5:
if(top1==-1)
printf("Stack1 is empty\n");
else{
printf("Stack1 is-->>\n");
for(i=0;i<=top1;i++)
printf("%d ",arr[i]);
printf("\n");
}
break;
case 6:
if(top2==n)
printf("Stack2 is empty\n");
else{
printf("Stack2 is-->>\n");
for(i=(n-1);i>=top2;i--)
printf("%d ",arr[i]);
printf("\n");
}
break;
case 0:

35
VIDIT GHAI CSE-B vips-tc

break;
}
}
}

Algorithm:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________

36
VIDIT GHAI CSE-B vips-tc

________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
Output:

37
VIDIT GHAI CSE-B vips-tc

Learning outcome:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________

38
VIDIT GHAI CSE-B vips-tc

EXPERIMENT 07

Problem Statement:
Create a linear queue using linked list and implement different operations such
as insertion, deletion and display the queue elements.
Programme Code:
#include <stdio.h>
#include <malloc.h>
struct node{
int data;
struct node *next;
};
struct queue{
struct node *front;
struct node *rear;
};
struct queue *insert(struct queue *q,int val){
struct node *ptr;
ptr = (struct node*)malloc(sizeof(struct node));
ptr -> data = val;
if(q -> front == NULL){
q -> front = ptr;
q -> rear = ptr;
q -> front -> next = q -> rear -> next = NULL;
}else{
q -> rear -> next = ptr;
q -> rear = ptr;
q -> rear -> next = NULL;
}
return q;
}
struct queue *display(struct queue *q){
struct node *ptr;
ptr = q -> front;
if(ptr == NULL)
printf("\nQUEUE IS EMPTY");

39
VIDIT GHAI CSE-B vips-tc

else{
printf("\n");
while(ptr!=q -> rear){
printf("%d\t", ptr -> data);
ptr = ptr -> next;
}
printf("%d\t", ptr -> data);
}
return q;
}
struct queue *delete_element(struct queue *q){
struct node *ptr;
ptr = q -> front;
if(q -> front == NULL)
printf("\nUNDERFLOW");
else{
q -> front = q -> front -> next;
printf("\nThe value being deleted is : %d", ptr ->
data);
free(ptr);
}
return q;
}
int main(){
int val, option;
struct queue *q;
q -> rear = NULL;
q -> front = NULL;
do{
printf("\n*****MAIN MENU*****");
printf("\nEnter 1 for INSERTION");
printf("\nEnter 2 for DELETION");
printf("\nEnter 3 to DISPLAY");
printf("\nEnter 4 to EXIT");
printf("\nEnter your option : ");
scanf("%d", &option);
switch(option){
case 1:

40
VIDIT GHAI CSE-B vips-tc

printf("Enter the number to insert in the


queue:");
scanf("%d", &val);
q = insert(q,val);
break;
case 2:
q = delete_element(q);
break;
case 3:
q = display(q);
break;
}
}while(option != 4);
return 0;
}

Algorithm:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________

41
VIDIT GHAI CSE-B vips-tc

________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________

42
VIDIT GHAI CSE-B vips-tc

Output:

Learning Outcome:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________

43
VIDIT GHAI CSE-B vips-tc

EXPERIMENT 08
Problem Statement:
Create a binary tree and perform tree traversal using recursion.
Programme Code:
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
struct node{
int data;
struct node *left;
struct node *right;
};
void preorderTraversal(struct node *root){
if(root != NULL){
printf("\t%d\t", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}
void inorderTraversal(struct node *root){
if(root != NULL){
inorderTraversal(root->left);
printf("\t%d\t", root->data);
inorderTraversal(root->right);
}
}
void postorderTraversal(struct node *root){
if(root != NULL){
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("\t%d\t", root->data);
}
}
int main(){
struct node *root , *one ,*two ,*three , *four ,*seven;
root = (struct node *)malloc(sizeof(struct node));

44
VIDIT GHAI CSE-B vips-tc

one = (struct node *)malloc(sizeof(struct node));


two = (struct node *)malloc(sizeof(struct node));
three = (struct node *)malloc(sizeof(struct node));
four = (struct node *)malloc(sizeof(struct node));
seven = (struct node *)malloc(sizeof(struct node));
root = one ;
one -> left = two;
one ->right = three;
one -> data = 1;
two -> left = four;
two ->right = NULL;
two -> data = 2;
three -> left = NULL;
three ->right = seven;
three -> data = 3;
four -> left = NULL;
four ->right = NULL;
four -> data = 4;
seven -> left = NULL;
seven ->right = NULL;
seven -> data = 7;
printf("\nPreorder Traversal: ");
preorderTraversal(root);
printf("\nInorder Traversal: ");
inorderTraversal(root);
printf("\nPostorder Traversal: ");
postorderTraversal(root);
}
Algorithm:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________

45
VIDIT GHAI CSE-B vips-tc

________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
46
VIDIT GHAI CSE-B vips-tc

Output:

Learning Outcome:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________

47
VIDIT GHAI CSE-B vips-tc

EXPERIMENT 09
Problem statement:
Write a program to evaluate
3. Prefix Expression
4. Postfix Expression
Programme Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
#define MAX 100 // maximum size of the stack

// stack structure
struct stack {
int top;
int items[MAX];
};

// push operation
void push(struct stack *s, int value) {
if (s->top == MAX - 1) {
printf("Stack is full\n");
exit(1);
}else {
s->top++;
s->items[s->top] = value;
}
}

// pop operation
int pop(struct stack *s) {
if (s->top == -1) {
printf("Stack is empty\n");
exit(1);

48
VIDIT GHAI CSE-B vips-tc

}else {
int value = s->items[s->top];
s->top--;
return value;
}
}

// evaluate postfix expression


int evaluatePostfixExp(char *exp) {
struct stack s;
s.top = -1;
int i = 0;
while (exp[i] != '\0') {
char c = exp[i];
if (isdigit(c)) {
int num = c - '0';
push(&s, num);
}
else if (c == '+' || c == '-' || c == '*' || c ==
'/' || c == '^') {
int op2 = pop(&s);
int op1 = pop(&s);
int result;
switch (c) {
case '+':
result = op1 + op2;
break;
case '-':
result = op1 - op2;
break;
case '*':
result = op1 * op2;
break;
case '/':
result = op1 / op2;
break;
case '^':
result = pow(op1, op2);

49
VIDIT GHAI CSE-B vips-tc

break;
}
push(&s, result);
}else if (c == ' ') {

}else{
printf("Invalid character\n");
exit(1);
}
i++;
}
return pop(&s);
}

// evaluate prefix expression


int evaluatePrefixExp(char *exp) {
struct stack s;
s.top = -1;
int i = strlen(exp) - 1;
while (i >= 0) {
char c = exp[i];
if (isdigit(c)) {
int num = c - '0';
push(&s, num);
}
else if (c == '+' || c == '-' || c == '*' || c ==
'/' || c == '^') {
int op1 = pop(&s);
int op2 = pop(&s);
int result;
switch (c) {
case '+':
result = op1 + op2;
break;
case '-':
result = op1 - op2;
break;
case '*':

50
VIDIT GHAI CSE-B vips-tc

result = op1 * op2;


break;
case '/':
result = op1 / op2;
break;
case '^':
result = pow(op1, op2);
break;
}
push(&s, result);
}
else if (c == ' ') {

}
else {
printf("Invalid character\n");
exit(1);
}
i--;
}
return pop(&s);
}
int main(){
int val, ch;
char exp[100];
do{
printf("\n****MENU****");
printf("\n1.Evaluate Postfix Expression");
printf("\n2.Evaluate Prefix Expression");
printf("\n3.Exit");
printf("\nEnter choice : ");
scanf(" %d",&ch);
switch(ch){
case 1:
printf("\nEnter any postfix expression : ");
scanf(" %[^\n]",&exp);
val = evaluatePostfixExp(exp);

51
VIDIT GHAI CSE-B vips-tc

printf("Value of the postfix expression =


%d",val);
break;

case 2:
printf("\nEnter any prefix expression : ");
scanf(" %[^\n]",&exp);
val = evaluatePrefixExp(exp);
printf("Value of the prefix expression =
%d",val);
break;
case 3:
break;
default:
printf("\nINVALID CHOICE");
}
printf("\n");
}while(ch!=3);
return 0;
}

Algorithm:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
52
VIDIT GHAI CSE-B vips-tc

________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
53
VIDIT GHAI CSE-B vips-tc

Output:

Learning Outcome:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________

54
VIDIT GHAI CSE-B vips-tc

EXPERIMENT 10
Problem Statement:
Implement selection sort, bubble sort, insertion sort and heap sort using array as
a data structure.
Programme Code:
#include<stdio.h>
#include<stdlib.h>

int * Bubblesort(int arr[], int size){


int i,j,t;
for(i=0;i<size-1;i++){
for(j=0;j<size-1-i;j++){
if(arr[j]>arr[j+1]){
t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
}
return arr;
}

int *Insertionsort(int arr[], int size){


int i,j,t;
for(i=1;i<size;i++){
j=i;
while((j>=1)&&(arr[j]<arr[j-1])){
t=arr[j];
arr[j]=arr[j-1];
arr[j-1]=t;
j--;
}
}
return arr;
}

54
VIDIT GHAI CSE-B vips-tc

int *Selectionsort(int arr[],int size){


int i, min, loc, j, t;
for(i=0;i<size-1;i++){
min=arr[i];
loc=i;
for(j=i;j<size;j++){
if (arr[j]<min){
min=arr[j];
loc=j;
}}
t=arr[loc];
arr[loc]=arr[i];
arr[i]=t;
}
return arr;
}

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

void heapify(int arr[], int size, int index) {


int largest = index;
int left = 2 * index + 1;
int right = 2 * index + 2;

if (left < size && arr[left] > arr[largest])


largest = left;

if (right < size && arr[right] > arr[largest])


largest = right;

if (largest != index) {
swap(&arr[index], &arr[largest]);
heapify(arr, size, largest);
}

55
VIDIT GHAI CSE-B vips-tc

int *Heapsort(int arr[], int size){


for (int i = size / 2 - 1; i >= 0; i--)
heapify(arr, size, i);

for (int i = size - 1; i >= 0; i--) {


swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
return arr;
}

void printchoice(){
printf("\nSORTING METHODS AVAILABLE:\n");
printf("1.Bubble sort\n2.Insertion sort\n3.Selection
sort\n4.Heap sort\n0.Exit\n");
}

int * createArray(int *size){


printf("Enter size of array: ");
scanf("%d",size);
int *arr=(int*)malloc(*size*sizeof(int));
printf("Enter array: ");
for(int i=0;i<*size;i++){
scanf("%d",&arr[i]);
}
return arr;
}

void printArray(int arr[], int size){


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

int main(){

56
VIDIT GHAI CSE-B vips-tc

int choice;
int *array;
int size;
do{
printchoice();
printf("Enter your choice : ");
scanf("%d",&choice);

switch (choice){

case 1:
array=createArray(&size);
printf("Initial array:\n");
printArray(array,size);
Bubblesort(array,size);
printf("Sorted array:\n");
printArray(array,size);
free(array);
break;

case 2:
array=createArray(&size);
printf("Initial array:\n");
printArray(array,size);
Insertionsort(array,size);
printf("Sorted array:\n");
printArray(array,size);
free(array);
break;

case 3:
array=createArray(&size);
printf("Initial array:\n");
printArray(array,size);
Selectionsort(array,size);
printf("Sorted array:\n");
printArray(array,size);
free(array);

57
VIDIT GHAI CSE-B vips-tc

break;

case 4:
array=createArray(&size);
printf("Initial array:\n");
printArray(array,size);
Heapsort(array,size);
printf("Sorted array:\n");
printArray(array,size);
free(array);
break;

case 0:
break;

default:
printf("Invalid choice...\n");
break;
}
}while(choice!=0);
return 0;
}

Algorithm:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________

58
VIDIT GHAI CSE-B vips-tc

________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
59
VIDIT GHAI CSE-B vips-tc

________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
60
VIDIT GHAI CSE-B vips-tc

Output:

Learning Outcome:
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________

61
VIDIT GHAI CSE-B vips-tc

62

You might also like