0% found this document useful (0 votes)
110 views54 pages

DS Programshj GFH DF

The document contains details of 8 experiments on data structures and algorithms: 1. Computing minimum and maximum of an array using C. 2. Implementing bubble sort to sort numbers in ascending/descending order. 3. Developing a menu-based program for array operations like deletion, insertion, and printing. 4. Implementing linear search to search an element in an array. 5. Searching an element in a 2D array. 6. Performing addition, subtraction, multiplication and transpose on matrices. 7. Developing a menu-based program for single linked list operations like insertion, deletion. 8. Writing a program for doubly linked list operations like insertion and deletion.

Uploaded by

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

DS Programshj GFH DF

The document contains details of 8 experiments on data structures and algorithms: 1. Computing minimum and maximum of an array using C. 2. Implementing bubble sort to sort numbers in ascending/descending order. 3. Developing a menu-based program for array operations like deletion, insertion, and printing. 4. Implementing linear search to search an element in an array. 5. Searching an element in a 2D array. 6. Performing addition, subtraction, multiplication and transpose on matrices. 7. Developing a menu-based program for single linked list operations like insertion, deletion. 8. Writing a program for doubly linked list operations like insertion and deletion.

Uploaded by

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

EXPERIMENTDETAILS

Experiment No. 1
Title: WAP to compute minimum/ maximum of a given array.

#include<stdio.h>
#include<conio.h>
Void main()
{
Inta[5],I,max,min=0;
Printf(enter the element of array);
For(i=0; i<5; i++)
{
Scanf(%d,&a[i]);
}
Max=a[0];
For(i=0; i<5; i++)
{
If(max<a[i])
Max=a[i];
Else if (max>a[i])
Min=a[i];
}
Printf(maximum element is: %d,max);
Printf(minimum element is : %d,min);
getch();
}

13

Experiment No: 2
Title: Write a program to sort given set of numbers in ascending/descending order using Bubble
Sort.
#include<stdio.h>
#include<conio.h>
int main()
{
ints,temp,i,j,a[20];
printf("Enter total numbers of elements: ");
scanf("%d",&s);
printf("Enter %d elements: ",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
//Bubble sorting algorithm
for(i=s-2;i>=0;i--){
for(j=0;j<=i;j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("After sorting: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
getch();
}
//Output:
//Enter total numbers of elements: 5
//Enter 5 elements: 6 2 0 11 9
//After sorting: 0 2 6 9 11

14

Experiment No: 3
Title: Write a menu-based program to perform array operations: deletion of an element from the
specified position, inserting an element at the specified position, printing the array elements.

#include<stdio.h>
#include<conio.h>
void insert_ele(int i,int ar[],int len){
int ele, loc;
printf("\nEnter element to be inserted : ");
scanf("%d",&ele);
printf("\nEnter location : ");
scanf("%d",&loc);
if(i==19){
printf("\nOverflow");
}
else{
while(i>=loc){
ar[i+1]=ar[i];
i--;
}
ar[loc]=ele;
len++;
printf("\nNew Array : ");
for(i=0;i<len;i++){
printf("\t%d",ar[i]);
}
}
}
void delete_ele(int i,int ar[],int len){
int loc;
printf("\nEnter location of element to be deleted : ");
scanf("%d",&loc);
if(len==0){
printf("\nUnderflow");
}
else{
for(i=loc;i<len;i++){
ar[i]=ar[i+1];
}
len--;
printf("\nNew Array : ");
for(i=0;i<len;i++){
printf("\t%d",ar[i]);
15

}
}
}
void find_ele(int i,int ar[],int len){
int ele, count=0;
printf("\nEnter element to be searched : ");
scanf("%d",&ele);
for(i=0;i<len;i++){
if(ar[i]==ele){
count++;
}
}
if(count == 0){
printf("\nElement does not exist");
}
else{
printf("\nElement found %d times",count);
}
}
void traverse(int i, int ar[], int len){
printf("\nTotal number of Elements are : %d",len);
printf("\nElements are : ");
for(i=0; i<len; i++){
printf("\t%d",ar[i]);
}
}
void main(){
int ar[20], i=0, len=0, ch;
char choice;
clrscr();
printf("\nEnter number of elements you want to insert : ");
scanf("%d",&len);
for(i=0; i<len; i++){
printf("Enter element %d : ",i);
scanf("%d",&ar[i]);
}
i--;
label:
printf("\nPress 1 to insert, 2 to delete, 3 to find any element");
printf("\n or 4 to traverse the array : ");
scanf("%d",&ch);
switch(ch){
case 1:insert_ele(i, ar, len);
break;
case 2:delete_ele(i, ar, len);
break;
case 3:find_ele(i, ar, len);
break;
case 4:traverse(i, ar, len);
break;
default:printf("\nInvalid Option");
16

break;
}
printf("\nPress y to continue or any other key to quit : ");
scanf("%s",&choice);
if(choice=='y' || choice=='Y'){
goto label;
}
getch();
}

Experiment No: 4
Title: Write a program to search an element in the array using linear search.
#include<stdio.h>
main()
{
int array[100], search, c, number;
printf("Enter the number of elements in array\n");
scanf("%d",&number);
printf("Enter %d numbers\n", number);
for ( c = 0 ; c < number ; c++ )
scanf("%d",&array[c]);
printf("Enter the number to search\n");
scanf("%d",&search);
for ( c = 0 ; c < number ; c++ )
{
if ( array[c] == search ) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if ( c == number )
printf("%d is not present in array.\n", search);
return 0;
}

17

Experiment No: 5
Title: Write a program to search an element in a 2-dimensional array.
#include<stdio.h>
main()
{
int array[100], search, c, number;
printf("Enter the number of elements in array\n");
scanf("%d",&number);
printf("Enter %d numbers\n", number);
for ( c = 0 ; c < number ; c++ )
scanf("%d",&array[c]);
printf("Enter the number to search\n");
scanf("%d",&search);
for ( c = 0 ; c < number ; c++ )
{
if ( array[c] == search ) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if ( c == number )
printf("%d is not present in array.\n", search);
return 0;
}

18

Experiment No: 6
Title: Write a program to perform following operations in matrix:
a. Addition
b. Subtraction
c. Multiplication
d. Transpose
#include<stdio.h>
main()
{
int array[100], search, c, number;
printf("Enter the number of elements in array\n");
scanf("%d",&number);
printf("Enter %d numbers\n", number);
for ( c = 0 ; c < number ; c++ )
scanf("%d",&array[c]);
printf("Enter the number to search\n");
scanf("%d",&search);
for ( c = 0 ; c < number ; c++ )
{
if ( array[c] == search ) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if ( c == number )
printf("%d is not present in array.\n", search);
19

return 0;
}
#include<stdio.h>
main()
{
int array[100], search, c, number;
printf("Enter the number of elements in array\n");
scanf("%d",&number);
printf("Enter %d numbers\n", number);
for ( c = 0 ; c < number ; c++ )
scanf("%d",&array[c]);
printf("Enter the number to search\n");
scanf("%d",&search);
for ( c = 0 ; c < number ; c++ )
{
if ( array[c] == search ) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if ( c == number )
printf("%d is not present in array.\n", search);
return 0;
}
#include<stdio.h>
main()
{
int array[100], search, c, number;
printf("Enter the number of elements in array\n");
scanf("%d",&number);
printf("Enter %d numbers\n", number);
for ( c = 0 ; c < number ; c++ )
scanf("%d",&array[c]);
printf("Enter the number to search\n");
scanf("%d",&search);
for ( c = 0 ; c < number ; c++ )
20

{
if ( array[c] == search ) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if ( c == number )
printf("%d is not present in array.\n", search);
return 0;
}

Experiment No: 7
Title: Write a menu-based program to perform following operations on single linked list:
a. To insert a node at the beginning of the list.
b. To insert a node at the end of the list.
c. To insert a node after a given node in the list.
d. To delete the first node from the list.
e. To delete the last node from the list.
f. To delete a node after a given node from the list.
g. To delete a node at a given position from the list.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
struct node
{
int info;
struct node *next;
}*head,*temp,*tail;
void insert_beg()
{
printf("enter the new node : ");
temp=(struct node *)malloc(sizeof(struct node));
scanf("%d",&temp->info);
temp->next=head;
head=temp;
}
21

void insert_end()
{
printf("enter the new node : ");
temp=(struct node *)malloc(sizeof(struct node));
scanf("%d",&temp->info);
temp->next=NULL;
tail->next=temp;
tail=temp;
}
void insert_pos(int n)
{
struct node *item,*temp2;
int i,c=0,pos;
temp=head;
printf("linked list is : \n");
for(i=1;i<=n;i++)
{
printf("%d-> ",temp->info);
temp=temp->next;
}
printf("\n\nenter the existing node after which you want insert a new node : ");
scanf("%d",&pos);
temp=head;
for(i=1;i<=n;i++)
{
if(temp->info==pos)
{
temp2=temp;
temp=temp->next;
printf("enter the new node : ");
item=(struct node *)malloc(sizeof(struct node));
scanf("%d",&item->info);
temp2->next=item;
item->next=temp;
c++;
}
temp=temp->next;
}
if(c==0)
printf("\nthe node not found");
}
void del_beg()
{
head=head->next;
}
void del_end(int n)
{
22

struct node *temp;


int i;
temp=head;
for(i=1;i<=n;i++)
{
if(temp->next==tail)
tail=temp;
temp=temp->next;
}
tail->next=NULL;
}
void del_pos(int n)
{
struct node *temp2;
int i,c=0,pos;
temp=head;
printf("linked list is : \n");
for(i=1;i<=n;i++)
{
printf("%d-> ",temp->info);
temp=temp->next;
}
printf("\nenter the existing node after which you want delete a node : ");
scanf("%d",&pos);
temp=head;
for(i=1;i<=n;i++)
{
if(temp->info==pos)
{
temp2=temp;
temp2=temp2->next;
temp2=temp2->next;
temp->next=temp2;
c++;
}
temp=temp->next;
}
if(c==0)
printf("\nthe node not found");
}
void display(int n)
{
temp=head;
printf("the list is : \n");
for(int i=1;i<=n;i++)
{
printf("%d-> ",temp->info);
temp=temp->next;
23

}
printf("NULL\n\nPress any Key");
}
void main()
{
int n,i,ch;
clrscr();
printf("enter the number of nodes : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nenter node %d : ",i);
temp=(struct node *)malloc(sizeof(struct node));
scanf("%d",&temp->info);
temp->next=NULL;
if(i==1)
head=tail=temp;
tail->next=temp;
tail=temp;
}
do
{
clrscr();
printf("\nPress 1 for insert a node at the beginning of the list");
printf("\nPress 2 for insert a node at the end of the list");
printf("\nPress 3 for insert a node after a given node of the list");
printf("\nPress 4 for delete a node at the beginning of the list");
printf("\nPress 5 for delete a node at the end of the list");
printf("\nPress 6 for delete a node after a given node of the list");
printf("\nPress 7 for Display");
printf("\nPress 8 for exit");
printf("\nEnter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_beg();
n++;
break;
case 2: insert_end();
n++;
break;
case 3: insert_pos(n);
n++;
break;
case 4: del_beg();
n--;
break;
case 5: del_end(n);
n--;
24

break;
case 6: del_pos(n);
n--;
break;
case 7: display(n);
getch();
break;
case 8: exit(0);
default: printf("wrong choice, enter again");
getch();
}
}
while(1);
}

Experiment No. 8
Title: Write a program to performs following operations on doubly linked lists:
a. To insert a node at the beginning of the list.
b. To insert a node at the end of the list.
c. To insert a node after a given node of the list.
d. To delete a node at the beginning of the list.
e. To delete a node at the end of the list.
f. To delete a node after a given node of the list.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
25

struct node
{
int info;
struct node *prev, *next;
}*head,*tail,*temp;
void insert_beg()
{
printf("enter the new node : ");
temp=(struct node *)malloc(sizeof(struct node));
scanf("%d",&temp->info);
temp->next=head;
head->prev=temp;
head=temp;
head->prev=NULL;
}
void insert_end()
{
printf("enter the new node : ");
temp=(struct node *)malloc(sizeof(struct node));
scanf("%d",&temp->info);
temp->next=NULL;
temp->prev=tail;
tail->next=temp;
tail=temp;
}
void insert_pos(int n)
{
struct node *item,*temp2;
int i,c=0,pos;
temp=head;
printf("linked list is : \n");
for(i=1;i<=n;i++)
{
printf("%d-> ",temp->info);
temp=temp->next;
}
printf("\n\nenter the existing node after which you want insert a new node : ");
scanf("%d",&pos);
temp=head;
for(i=1;i<=n;i++)
{
if(temp->info==pos)
{
temp2=temp;
temp=temp->next;
printf("enter the new node : ");
item=(struct node *)malloc(sizeof(struct node));
scanf("%d",&item->info);
item->prev=temp2;
item->next=temp;
temp2->next=item;
26

temp->prev=item;
c++;
}
temp=temp->next;
}
if(c==0)
printf("\nthe node not found");
}
void del_beg()
{
head=head->next;
head->prev=NULL;
}
void del_end()
{
temp=tail->prev;
temp->next=NULL;
tail=temp;
}
void del_pos(int n)
{
struct node *temp2;
int i,c=0,pos;
temp=head;
printf("linked list is : \n");
for(i=1;i<=n;i++)
{
printf("%d-> ",temp->info);
temp=temp->next;
}
printf("\nenter the existing node after which you want delete a node : ");
scanf("%d",&pos);
temp=head;
for(i=1;i<=n;i++)
{
if(temp->info==pos)
{
temp2=temp;
temp2=temp2->next;
temp2=temp2->next;
temp->next=temp2;
temp2->prev=temp;
c++;
}
temp=temp->next;
}
if(c==0)
printf("\nthe node not found");
27

}
void display()
{
int k;
printf("\nPress 1 for print the list from the begening");
printf("\nPress 2 for print the list from the end");
scanf("%d",&k);
printf("NULL<-");
if(k==1)
{
temp=head;
while(temp)
{
if(temp!=head)
printf("<=>");
printf("%d",temp->info);
temp=temp->next;
}
}
else
{
temp=tail;
while(temp)
{
if(temp!=tail)
printf("<=>");
printf("%d",temp->info);
temp=temp->prev;
}
}
printf("->NULL");
getch();
}
void main()
{
int n,i,ch;
clrscr();
printf("enter the number of nodes : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nenter the node %d : ",i);
temp=(struct node *)malloc(sizeof(struct node));
scanf("%d",&temp->info);
temp->next=temp->prev=NULL;
if(i==1)
{
temp->prev=NULL;
head=tail=temp;
}
else
28

{
tail->next=temp;
temp->prev=tail;
tail=temp;
}
}
do
{
clrscr();
printf("\nPress 1 for insert a node at the beginning of the list");
printf("\nPress 2 for insert a node at the end of the list");
printf("\nPress 3 for insert a node after a given node of the list");
printf("\nPress 4 for delete a node at the beginning of the list");
printf("\nPress 5 for delete a node at the end of the list");
printf("\nPress 6 for delete a node after a given node of the list");
printf("\nPress 7 for Display");
printf("\nPress 8 for exit");
printf("\nEnter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_beg();
n++;
break;
case 2: insert_end();
n++;
break;
case 3: insert_pos(n);
n++;
break;
case 4: del_beg();
n--;
break;
case 5: del_end();
n--;
break;
case 6: del_pos(n);
n--;
break;
case 7: display();
break;
case 8: exit(0);
default: printf("wrong choice, enter again");
getch();
}
}
while(1);
}

29

Experiment No.9
Title: Write a program to perform following operations on circular linked lists:
d. To insert a node at the beginning of the list.
e. To insert a node at the end of the list.
f. To insert a node after a given node of the list.
d. To delete a node at the beginning of the list.
e. To delete a node at the end of the list.
f. To delete a node after a given node of the list
#include <stdio.h>
#include <malloc.h>
struct node
{
struct node *prev;
int info;
struct node *next;
}*start;
void main()
{
int choice,n,m,po,i;
start=NULL;
while(1)
{
printf("1.Create List\n");
printf("2.Add at begining\n");
printf("3.Add after\n");
printf("4.Delete\n");
printf("5.Display\n");
printf("6.Count\n");
printf("7.Reverse\n");
printf("8.exit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("How many nodes you want : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the element : ");
scanf("%d",&m);
create_list(m);
}
break;
case 2:
printf("Enter the element : ");
scanf("%d",&m);
addatbeg(m);
break;
case 3:
30

printf("Enter the element : ");


scanf("%d",&m);
printf("Enter the position after which this element is inserted : ");
scanf("%d",&po);
addafter(m,po);
break;
case 4:
printf("Enter the element for deletion : ");
scanf("%d",&m);
del(m);
break;
case 5:
display();
break;
case 6:
count();
break;
case 7:
rev();
break;
case 8:
exit();
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
void create_list(int num)
{
struct node *q,*tmp;
tmp= malloc(sizeof(struct node));
tmp->info=num;
tmp->next=NULL;
if(start==NULL)
{
tmp->prev=NULL;
start->prev=tmp;
start=tmp;
}
else
{
q=start;
while(q->next!=NULL)
q=q->next;
q->next=tmp;
tmp->prev=q;
}
}/*End of create_list()*/
void addatbeg(int num)
{
struct node *tmp;
31

tmp=malloc(sizeof(struct node));
tmp->prev=NULL;
tmp->info=num;
tmp->next=start;
start->prev=tmp;
start=tmp;
}/*End of addatbeg()*/
void addafter(int num,int c)
{
struct node *tmp,*q;
int i;
q=start;
for(i=0;i<c-1;i++)
{
q=q->next;
if(q==NULL)
printf("There are less than %d elements\n",c);
}
tmp=malloc(sizeof(struct node) );
tmp->info=num;
q->next->prev=tmp;
tmp->next=q->next;
tmp->prev=q;
q->next=tmp;
}/*End of addafter() */
void del(int num)
{
struct node *tmp,*q;
if(start->info==num)
{
tmp=start;
start=start->next; /*first element deleted*/
start->prev = NULL;
free(tmp);
return;
}
q=start;
while(q->next->next!=NULL)
{
if(q->next->info==num) /*Element deleted in between*/
{
tmp=q->next;
q->next=tmp->next;
tmp->next->prev=q;
free(tmp);
}
q=q->next;
}
if(q->next->info==num) /*last element deleted*/
{ tmp=q->next;
free(tmp);
32

q->next=NULL;
}
printf("Element %d not found\n",num);
}/*End of del()*/
void display()
{
struct node *q;
if(start==NULL)
printf("List is empty\n");
}
q=start;
printf("List is :\n");
while(q!=NULL)
{
printf("%d ", q->info);
q=q->next;
}
printf("\n");
}/*End of display() */
void count()
{ struct node *q=start;
int cnt=0;
while(q!=NULL)
{
q=q->next;
cnt++;
}
printf("Number of elements are %d\n",cnt);
}/*End of count()*/
void rev()
{
struct node *p1,*p2;
p1=start;
p2=p1->next;
p1->next=NULL;
p1->prev=p2;
while(p2!=NULL)
{
p2->prev=p2->next;
p2->next=p1;
p1=p2;
p2=p2->prev; /*next of p2 changed to prev */
}
start=p1;
}/*End of rev()*/.

33

Experiment No: 10
Title: Write a menu-based program to implement stack operations: PUSH, POP using array
implementation of stack.
#include<stdio.h>
#include<conio.h>
#define max 10
int a[max],top=-1;
void push();
void pop();
void display();
void peek();
void main()
{
int ch;
clrscr();
printf("STACK USING ARRAY\n\n");
while(1)
{
printf("Enter 1 for push\n");
printf("Enter 2 for pop\n");
printf("Enter 3 for display\n");
printf("Enter 4 for peek\n");
printf("Enter 5 for exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1 : push();
break;
case 2 : pop();
break;
case 3 : display();
break;
case 4 : peek();
34

break;
case 5 : exit(1);
default : printf("Wrong choice please enter correct\n\n");
}
}
}
void push()
{
int n,s,i;
if(top==(max-1))
{
printf("stack is full\n");
}
else
{
printf("How many no you want insert: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("Enter any no: ");
scanf("%d",&s);
++top;
a[top]=s;
}
}
}
void pop()
{
int temp;
if(top==-1)
{
printf("stack is empty\n\n");
}
else
{
temp=a[top];
--top;
printf("Element %d is pop\n\n",temp);
free(temp);
}
}
void display()
{
int i;//top,a[max];
printf("Element is:\n\n");
for(i=0;i<=top;++i)
{
printf("%d\n",a[i]);
35

}
}
void peek()
{
printf("%d",a[top]);
}

Experiment No: 11
Title:Write a menu-based program using functions to implement stack operations: PUSH, POP
using linked implementation of stack.
#include<stdio.h>
#include<conio.h>
struct stack
{
int num;
struct stack *next;
};
struct stack *top=NULL;
//struct stack *pt1;//=NULL;
void push();
void pop();
void display();
void peek();
void main()
{
int i,ch;
clrscr();
printf("STACK USING LINK LIST\n\n");
while(1)
{
printf("Enter 1 for push\n");
printf("Enter 2 for pop\n");
printf("Enter 3 for display\n");
printf("Enter 4 for peek\n");
printf("Enter 5 for exit\n");
scanf("%d",&ch);
36

switch(ch)
{
case 1 : push();
break;
case 2 : pop();
break;
case 3 : display();
break;
case 4 : peek();
break;
case 5 : exit(0);
}
}
}
void push()
{
struct stack *temp,*pt1;
int n,i;
//clrscr();
printf("How many no you want insert: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("enter the no: ");
temp=(struct stack*)malloc(sizeof(struct stack));
scanf("%d",&temp->num);
temp->next=NULL;
if(top==NULL)
{
top=temp;
pt1=temp;
}
else
{
top->next=temp;
top=temp;
}
}
printf("\ntemp->num:%d\npt1->num:%d\n",temp->num,pt1->num);
}
void pop()
{
struct stack *pt1,*pt,*temp;
pt=pt1;
//clrscr();
printf("\n@%d**",pt1->num);
/*while(pt->next!=top)
{
pt=pt->next;
37

}
temp=pt->next;
pt->next=NULL;
top=pt;
printf("Element %d is pop\n",temp->num);
free(temp);*/
}
void display()
{
struct stack *pt,*pt1;
pt=pt1;
//clrscr();
if(top==NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Element is:\n");
while(pt!=NULL)
{
printf("%d\n",pt->num);
pt=pt->next;
}
//printf("%d\n",pt->num);
}
}
void peek()
{
//clrscr();
if(top==NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Top element is %d\n",top->num);
}
}

38

Experiment No: 12
Title: Write a program to convert infix expression into postfix expression and then to evaluate
resultant postfix expression.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define N 64
#define LP 10
#define RP 20
#define OPERATOR 30
#define OPERAND 40
#define LPP 0
#define AP 1
#define SP AP
39

#define MP 2
#define DP MP
#define REMP 2
#define NONE 9
static char infix[N+1],stack[N],postfix[N+1];
static int top;
void infixtopostfix(void);
int gettype(char);
void push(char);
char pop(void);
int getprec(char);
void main()
{
char ch;
do
{
top=-1;
printf("\nEnter an infix expression\n");
fflush(stdin);
gets(infix);
infixtopostfix();
printf("\ninfix = %s\npost fix =%s\n",infix,postfix);
printf("\nDo you wish to continue(y/n):");
ch=getche();
} while(ch=='Y' || ch=='y');
}
void infixtopostfix(void)
{
int i,p,l,type,prec;
char next;
i=p=0;
l=strlen(infix);
while(i<l)
{
type=gettype(infix[i]);
switch(type)
{
case LP:
push(infix[i]);
break;
case RP:
while((next=pop())!='(')
postfix[p++]=next;
break;
case OPERAND:
40

postfix[p++]=infix[i];
break;
case OPERATOR:
prec=getprec(infix[i]);
while(top>-1 && prec <= getprec(stack[top]))
postfix[p++]=pop();
push(infix[i]);
break;
}
i++; }
while(top>-1)
postfix[p++]=pop();
postfix[p]='\0';
}
int gettype(char sym)
{
switch(sym)
{
case '(':
return(LP);
case ')':
return(RP);
case '+':
case '-':
case '*':
case '/':
case '%':
return(OPERATOR);
default :
return(OPERAND);
}}
void push(char sym)
{
if(top>N)
{ printf("\nStack is full\n");
exit(0);
}
else
stack[++top]=sym;
}
char pop(void)
{
if(top<=-1)
{
printf("\nStack is empty\n");
41

exit(0);
} else
return(stack[top--]);
}
int getprec(char sym)
{
switch(sym)
{
case '(':
return(LPP);
case '+':
return(AP);
case '-':
return(SP);
case '*':
return(MP);
case '/':
return(DP);
case '%':
return(REMP);
default :
return(NONE);
}
}
OUTPUT
Enter an infix expression
a*b+c*d/e
infix = a*b+c*d/e
post fix =ab*cd*e/+

Experiment No:13
Title: Write a program to print Fibonacci series using Recursion.
#include<stdio.h>
void printFibonacci(int);
int main(){
int k,n;
long int i=0,j=1,f;
printf("Enter the range of the Fibonacci series: ");
42

scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n);
return 0;
}
void printFibonacci(int n){
static long int first=0,second=1,sum;
if(n>0){
sum = first + second;
first = second;
second = sum;
printf("%ld ",sum);
printFibonacci(n-1);
}
}
Sample output:
Enter the range of the Fibonacci series: 10
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89

Experiment No: 14
Title: Write a program to solve Towers of Hanoi Problem.
#include<stdio.h>
#include<conio.h>
43

#include<math.h>
void hanoi(int x, char from, char to, char aux)
{
if(x==1)
printf("Move Disk From %c to %c\n",from,to);
else
{
hanoi(x-1,from,aux,to);
printf("Move Disk From %c to %c\n",from,to);
hanoi(x-1,aux,to,from);
}
}
void main( )
{
int disk;
int moves;
clrscr();
printf("Enter the number of disks you want to play with:");
scanf("%d",&disk);
moves=pow(2,disk)-1;
printf("\nThe No of moves required is=%d \n",moves);
hanoi(disk,'A','C','B');
getch( );
}
Output:Enter the number of disks you want to play with: 3
The No of moves required is=7
Move Disk from A to C
Move Disk from A to B
Move Disk from C to B
Move Disk from A to C
Move Disk from B to A
Move Disk from B to C
Move Disk from A to C

Experiment No: 15
44

Title: Write a menu-based program to implement linear queue operations: INSERTION,


DELETION using array implementation of queue.
#include<stdio.h>
#include<conio.h>
#define max 10
int front=-1,rear=-1,a[max];
void creat();
void display();
void delet();
void peek();
void main()
{
int ch;
printf("***QUEUE USING ARRAY***\n");
while(1)
{
printf("Enter 1 for creat\n");
printf("Enter 2 for display\n");
printf("Enter 3 for delet\n");
printf("Enter 4 for peek\n");
printf("Enter 5 for exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1 : creat();
break;
case 2 : display();
break;
case 3 : delet();
break;
case 4 : peek();
break;
case 5 : exit(0);
}
}
}
void creat()
{
int i,n1,n;
if(rear==(max-1))
{
printf("queue is full\n");
}
else
{
printf("How many no you want insert: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("Enter the no: ");
45

scanf("%d",&n1);
if((front==-1)&&(rear==-1))
{
++front;
++rear;
a[front]=n1;
a[rear]=n1;
}
else
{
++rear;
a[rear]=n1;
}
}
}
}
void display()
{
int n2;
if(front==-1&&rear==-1)
{
printf("queue is empty\n");
}
else
{
n2=front;
printf("Data is:\n");
while(a[n2]!=NULL)
{
printf("%d\n",a[n2]);
++n2;
}
}
}
void delet()
{
int temp;
temp=front;
front=++front;
printf("%d element is delet succesfull\n",a[temp]);
free(temp);
}
void peek()
{
printf("Front element is: %d\n",a[front]);
}

46

Experiment No: 16
Title: Write a menu-based program to implement linear queue operations: INSERTION,
DELETION using linked list implementation of queue.
#include<stdio.h>
#include<conio.h>
struct queue
{
int num;
struct queue *next;
};
struct queue *front=NULL;
struct queue *rear=NULL;
void creat();
void delet();
void show();
void peek();
void main()
{
int ch;
printf("***QUEUE USING LINK LIST***\n");
while(1)
{
printf("\n\nEnter 1 for creat\n");
printf("Enter 2 for delet\n");
printf("Enter 3 for show\n");
printf("Enter 4 for peek\n");
printf("enter 5 for exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1 : creat();
break;
case 2 : delet();
break;
case 3 : show();
break;
case 4 :peek();
break;
case 5 : exit(0);
}
}
}
47

void creat()
{
struct queue *temp;
int n,i;
clrscr();
printf("How many no you want insert: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("Enter the no: ");
temp=(struct queue*)malloc(sizeof(struct queue));
scanf("%d",&temp->num);
temp->next=NULL;
if(front==NULL&&rear==NULL)
{
front=temp;
rear=temp;
}
else
{
rear->next=temp;
rear=temp;
}
}
}
void show()
{
struct queue *pt;
clrscr();
pt=front;
//printf("%d**%d",pt->num,rear->num);
if(front==NULL&&rear==NULL)
{
printf("queue is empty\n");
}
else
{
printf("Data is:\n");
while(pt!=NULL)
{
printf("%d\n",pt->num);
pt=pt->next;
}
}
}
void delet()
{
struct queue *temp;
clrscr();
48

if(front==NULL&&rear==NULL)
{
printf("Queue is empty\n");
}
else if(front==rear)
{
temp=front;
front=NULL;
rear=NULL;
printf("%d element is delet\n",temp->num);
free(temp);
}
else
{
temp=front;
front=front->next;
printf("%d element is delet\n",temp->num);
free(temp);
}
}
void peek()
{
clrscr();
printf("Front element is:%d",front->num);
}

49

Experiment No: 17
Title:Write a menu-based program to implement circular queue operations: INSERTION,
DELETION.
#include<stdio.h>
#define max 3
int q[10],front=0,rear=-1;
void main()
{
int ch;
void insert();
void delet();
void display();
clrscr();
printf("\nCircular Queue operations\n");
printf("1.insert\n2.delete\n3.display\n4.exit\n");
while(1)
{
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delet();
break;
case 3:display();
break;
case 4:exit();
default:printf("Invalid option\n");
}
}
}
void insert()
{
50

int x;
if((front==0&&rear==max-1)||(front>0&&rear==front-1))
printf("Queue is overflow\n");
else
{
printf("Enter element to be insert:");
scanf("%d",&x);
if(rear==max-1&&front>0)
{
rear=0;
q[rear]=x;
}
else
{
if((front==0&&rear==-1)||(rear!=front-1))
q[++rear]=x;
}
}
}
void delet()
{
int a;
if((front==0)&&(rear==-1))
{
printf("Queue is underflow\n");
getch();
exit();
}
if(front==rear)
{
a=q[front];
rear=-1;
front=0;
}
else
if(front==max-1)
{
a=q[front];
front=0;
}
else a=q[front++];
printf("Deleted element is:%d\n",a);
}
void display()
{
int i,j;
if(front==0&&rear==-1)
{
printf("Queue is underflow\n");
getch();
51

exit();
}
if(front>rear)
{
for(i=0;i<=rear;i++)
printf("\t%d",q[i]);
for(j=front;j<=max-1;j++)
printf("\t%d",q[j]);
printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]);
}
else
{
for(i=front;i<=rear;i++)
{
printf("\t%d",q[i]);
}
printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]);
}
printf("\n");
}
getch();
Experiment No: 18
Title:Write a program to traverse a binary tree using PRE-ORDER, IN-ORDER, POST-ORDER
traversal techniques.
#include <iostream>
using namespace std;
// Node class
class Node {
int key;
Node* left;
Node* right;
public:
Node() { key=-1; left=NULL; right=NULL; };
void setKey(int aKey) { key = aKey; };
void setLeft(Node* aLeft) { left = aLeft; };
void setRight(Node* aRight) { right = aRight; };
int Key() { return key; };
Node* Left() { return left; };
Node* Right() { return right; };
};
// Tree class
class Tree {
Node* root;
public:
Tree();
52

~Tree();
Node* Root() { return root; };
void addNode(int key);
void inOrder(Node* n);
void preOrder(Node* n);
void postOrder(Node* n);
private:
void addNode(int key, Node* leaf);
void freeNode(Node* leaf);
};
// Constructor
Tree::Tree() {
root = NULL;
}
// Destructor
Tree::~Tree() {
freeNode(root);
}
// Free the node
void Tree::freeNode(Node* leaf)
{
if ( leaf != NULL )
{
freeNode(leaf->Left());
freeNode(leaf->Right());
delete leaf;
}
}
// Add a node
void Tree::addNode(int key) {
// No elements. Add the root
if ( root == NULL ) {
cout << "add root node ... " << key << endl;
Node* n = new Node();
n->setKey(key);
root = n;
}
else {
cout << "add other node ... " << key << endl;
addNode(key, root);
}
}
// Add a node (private)
void Tree::addNode(int key, Node* leaf) {
if ( key <= leaf->Key() ) {
if ( leaf->Left() != NULL )
53

addNode(key, leaf->Left());
else {
Node* n = new Node();
n->setKey(key);
leaf->setLeft(n);
}
}
else {
if ( leaf->Right() != NULL )
addNode(key, leaf->Right());
else {
Node* n = new Node();
n->setKey(key);
leaf->setRight(n);
}
}
}
// Print the tree in-order
// Traverse the left sub-tree, root, right sub-tree
void Tree::inOrder(Node* n) {
if ( n ) {
inOrder(n->Left());
cout << n->Key() << " ";
inOrder(n->Right());
}
}
// Print the tree pre-order
// Traverse the root, left sub-tree, right sub-tree
void Tree::preOrder(Node* n) {
if ( n ) {
cout << n->Key() << " ";
preOrder(n->Left());
preOrder(n->Right());
}
}
// Print the tree post-order
// Traverse left sub-tree, right sub-tree, root
void Tree::postOrder(Node* n) {
if ( n ) {
postOrder(n->Left());
postOrder(n->Right());
cout << n->Key() << " ";
}
}
// Test main program
int main() {
Tree* tree = new Tree();
tree->addNode(30);
54

tree->addNode(10);
tree->addNode(20);
tree->addNode(40);
tree->addNode(50);
cout << "In order traversal" << endl;
tree->inOrder(tree->Root());
cout << endl;
cout << "Pre order traversal" << endl;
tree->preOrder(tree->Root());
cout << endl;
cout << "Post order traversal" << endl;
tree->postOrder(tree->Root());
cout << endl;
delete tree;
return 0;
}
.
OUTPUT:add root node ... 30
add other node ... 10
add other node ... 20
add other node ... 40
add other node ... 50
In order traversal
10 20 30 40 50
Pre order traversal
30 10 20 40 50
Post order traversal
20 10 50 40 30
Experiment No: 19
Title: Write a menu-based program to perform operations for a binary search tree (BST).
a. Search an element
b. Find minimum
c. Find maximum
d. Insertion
e. Deletion
#include<stdlib.h>
#include<stdio.h>
struct bin_tree {
int data;
struct bin_tree * right, * left;
};
55

typedef struct bin_tree node;


void insert(node ** tree, int val)
{
node *temp = NULL;
if(!(*tree))
{
temp = (node *)malloc(sizeof(node));
temp->left = temp->right = NULL;
temp->data = val;
*tree = temp;
return;
}
if(val < (*tree)->data)
{
insert(&(*tree)->left, val);
}
else if(val > (*tree)->data)
{
insert(&(*tree)->right, val);
}
}
void print_preorder(node * tree)
{
if (tree)
{
printf("%d\n",tree->data);
print_preorder(tree->left);
print_preorder(tree->right);
}
}
void print_inorder(node * tree)
{
if (tree)
{
print_inorder(tree->left);
printf("%d\n",tree->data);
print_inorder(tree->right);
}
}
void print_postorder(node * tree)
{
if (tree)
{
print_postorder(tree->left);
56

print_postorder(tree->right);
printf("%d\n",tree->data);
}
}
void deltree(node * tree)
{
if (tree)
{
deltree(tree->left);
deltree(tree->right);
free(tree);
}
}
node* search(node ** tree, int val)
{
if(!(*tree))
{
return NULL;
}
if(val < (*tree)->data)
{
search(&((*tree)->left), val);
}
else if(val > (*tree)->data)
{
search(&((*tree)->right), val);
}
else if(val == (*tree)->data)
{
return *tree;
}
}
void main()
{
node *root;
node *tmp;
//int i;
root = NULL;
/* Inserting nodes into tree */
insert(&root, 9);
insert(&root, 4);
insert(&root, 15);
insert(&root, 6);
insert(&root, 12);
insert(&root, 17);
insert(&root, 2);
57

/* Printing nodes of tree */


printf("Pre Order Display\n");
print_preorder(root);
printf("In Order Display\n");
print_inorder(root);
printf("Post Order Display\n");
print_postorder(root);
/* Search node into tree */
tmp = search(&root, 4);
if (tmp)
{
printf("Searched node=%d\n", tmp->data);
}
else
{
printf("Data Not found in tree.\n");
}
/* Deleting all nodes of tree */
deltree(root);
}

Experiment No: 20
Title: Write a program to traverse a graph using breadth-first search (BFS), depth-first search
(DFS).
// BFS
#include<stdio.h>
58

#include<conio.h>
int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;
void bfs(int v)
{
for(i=1;i<=n;i++)
if(a[v][i] && !visited[i])
q[++r]=i;
if(f<=r)
{
visited[q[f]]=1;
bfs(q[f++]);
}
}
void main()
{
int v;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
q[i]=0;
visited[i]=0;
}
printf("\n Enter graph data in matrix form:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the starting vertex:");
scanf("%d",&v);
bfs(v);
printf("\n The node which are reachable are:\n");
for(i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
else
printf("\n Bfs is not possible");
getch();
}
//DFS
#include<stdio.h>
#include<assert.h>
/* maxVertices represents maximum number of vertices that can be present in the graph. */
#define maxVertices 100
void Dfs(int graph[][maxVertices], int *size, int presentVertex,int *visited)
{
printf("Now visiting vertex %d\n",presentVertex);
visited[presentVertex] = 1;
/* Iterate through all the vertices connected to the presentVertex and perform dfs on those
vertices if they are not visited before */
59

int iter;
for(iter=0;iter<size[presentVertex];iter++)
{
if(!visited[graph[presentVertex][iter]])
{
Dfs(graph,size,graph[presentVertex][iter],visited);
}
}
return;
}
/* Input Format: Graph is directed and unweighted. First two integers must be number of vertces
and edges
which must be followed by pairs of vertices which has an edge between them. */
int main()
{
int graph[maxVertices][maxVertices],size[maxVertices]={0},visited[maxVertices]={0};
int vertices,edges,iter;
/* vertices represent number of vertices and edges represent number of edges in the graph. */
scanf("%d%d",&vertices,&edges);
int vertex1,vertex2;
for(iter=0;iter<edges;iter++)
{
scanf("%d%d",&vertex1,&vertex2);
assert(vertex1>=0 && vertex1<vertices);
assert(vertex2>=0 && vertex2<vertices);
graph[vertex1][size[vertex1]++] = vertex2;
}
int presentVertex;
for(presentVertex=0;presentVertex<vertices;presentVertex++)
{
if(!visited[presentVertex])
{
Dfs(graph,size,presentVertex,visited);
}
}
return 0;
}

60

Experiment No: 21
Title: Write a program to sort given set of numbers in ascending/descending order using insertion
sort and also search a number using binary search.
//Insertion Sort
#include<stdio.h>
#include<conio.h>
void main()
{
inti,j,s,temp,a[20];
printf("Enter total elements: ");
scanf("%d",&s);
printf("Enter %d elements: ",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=1;i<s;i++){
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0)){
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("After sorting: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
getch();
}
//Output:
//Enter total elements: 5
//Enter 5 elements: 3 7 9 0 2
//After sorting: 0 2 3 7 9
//Binary Search
void main()
{
int arr[30],len,low,high,mid,item,flag=0,i;
clrscr();
pf("enter the length");
sf("%d",&len);
61

low=0;
high=len-1;
pf("\n unput elements");
pf("enter item");
sf("%d",&item);
for(i=0;i<len;i++)
sf("%d",&arr[i]);
while(low<=high)
{
mid=(high+low)/2;
if(item>arr[mid])
low=mid+1;
elseif(item<arr[mid])
high=mid-1;
else
{flag=1;
pf("%d",mid);
break;
}
if(flag==0)
pf("doesn't exists");
getch();
}

62

Experiment No: 22
Title: Write a program to sort given set of numbers in ascending/descending order using Quick sort
and selection sort. Also record the time taken by these two programs and compare them.
//Quick Sort
#include<stdio.h>
#include<conio.h>
void quicksort(int [10],int,int);
void main()
{
int x[20],size,i;
printf("Enter size of the array: ");
scanf("%d",&size);
printf("Enter %d elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("Sorted elements: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);
getch();
}
void quicksort(int x[10],intfirst,int last){
intpivot,j,temp,i;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
63

if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
//Output:
//Enter size of the array: 5
//Enter 5 elements: 3 8 0 1 2
//Sorted elements: 0 1 2 3 8
// Selection Sort
#include<stdio.h>
#include<conio.h>
void main()
{
ints,i,j,temp,a[20];
printf("Enter total elements: ");
scanf("%d",&s);
printf("Enter %d elements: ",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=0;i<s;i++){
for(j=i+1;j<s;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("After sorting is: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
64

getch();
}
// Output:
//Enter total elements: 5
//Enter 5 elements: 4 5 0 21 7
//The array after sorting is: 0 4 5 7 21

Experiment No: 23
Title: Write a program to sort given set of numbers in ascending/descending order using Merge
sort.
WAP for insertion of an element at the specified location.
#include<stdio.h>
#define MAX 50
void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);
int main(){
int merge[MAX],i,n;
printf("Enter the total number of elements: ");
scanf("%d",&n);
printf("Enter the elements which to be sort: ");
for(i=0;i<n;i++){
scanf("%d",&merge[i]);
}
partition(merge,0,n-1);
printf("After merge sorting elements are: ");
for(i=0;i<n;i++){
printf("%d ",merge[i]);
}
return 0;
}
void partition(int arr[],int low,int high){
int mid;
if(low<high){
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}
65

void mergeSort(int arr[],int low,int mid,int high){


int i,m,k,l,temp[MAX];
l=low;
i=low;
m=mid+1;
while((l<=mid)&&(m<=high)){
if(arr[l]<=arr[m]){
temp[i]=arr[l];
l++;
}
else{
temp[i]=arr[m];
m++;
}
i++;
}
if(l>mid){
for(k=m;k<=high;k++){
temp[i]=arr[k];
i++;
} }
else{
for(k=l;k<=mid;k++){
temp[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++){
arr[k]=temp[k];
}
}
Sample output:
Enter the total number of elements: 5
Enter the elements which to be sort: 2 5 0 9 1
After merge sorting elements are: 0 1 2 5 9

66

You might also like