You are on page 1of 31

Programs

/* Experiment no 1 : PROGRAM TO PERFORM ARRAY OPERATIONS */

#include<stdio.h>
#include<conio.h>
void insert(int *,int,int);
void del(int *,int);
void display(int *);
void main()
{
int a[5];
clrscr();
insert(a,1,11);
insert(a,2,12);
insert(a,3,13);
insert(a,4,14);
insert(a,5,15);
display(a);
del(a,3);
printf("\n");
display(a);
getch();
}
void insert(int *a,int pos,int num)
{
int i;
for(i=4;i>=pos;i--)
a[i]=a[i-1];
a[i]=num;

}
void del(int *a,int pos)
{
int i;
for(i=pos;i<5;i++)
a[i-1]=a[i];
a[i-1]=0;
}
void display(int *a)
{
int i;
for(i=0;i<=4;i++)
printf("%d\t",a[i]);
}
/* OUTPUT
11 12 13 14 15
11 12 14 15 0
*/
/* Experiment no 2: PROGRAM TO PERFORM LINEAR SEARCH */

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10]={1,2,3,4,5,6,7,8,9,10};
int i,x;
clrscr();
printf("enter the value to search = ");
scanf("%d",&x);
for(i=0;i<=9;i++)
{
if(x==a[i])
break;
}
if(i==10)
printf("not found");
else
printf("%d is found at %d location",x,i);
getch();
}
/* OUTPUT
enter the value to search = 4
4 is found at 3 location
*/
/* Experiment no 3: PROGRAM TO PERFORM BINARY SEARCH */

#include<stdio.h>
#include<conio.h>
void main()
{
int data[10]={1,2,3,4,5,6,7,8,9,10};
int mid,lower=0,upper=9,num,flag=1;
clrscr();
printf("enter no to search = ");
scanf("%d",&num);
for(mid=(lower+upper)/2;lower<=upper;mid=(lower+upper)/2)
{
if(data[mid]==num)
{
printf("\nThe no is at position %d in array",mid);
flag=0;
break;
}
if(data[mid]>num)
upper=mid-1;
else
lower=mid+1;
}
if(flag)
printf("\nElement is not present in the array.");
getch();
}
/* OUTPUT
enter no to search = 9

The no is at position 8 in array


*/
/* Experiment no 4: PROGRAM TO PERFORM BUBBLE SORT */
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5]={17,15,13,31,5};
int i,j,temp;
clrscr();
printf("Bubble Sort = \n");
printf("Array before Sorting\n");
for(i=0;i<=4;i++)
printf("%d\t",arr[i]);
for(i=0;i<=3;i++)
{
for(j=0;j<=3-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\n\n Array after Sorting:\n");
for(i=0;i<=4;i++)
printf("%d\t",arr[i]);
getch();
}
/* OUTPUT
Bubble Sort =
Array before Sorting
17 15 13 31 5

Array after Sorting:


5 13 15 17 31
*/
/* Experiment no 5: PROGRAM TO PERFORM SELECTION SORT */
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5]={25,17,31,13,2};
int i,j,temp;
clrscr();
printf("selection sort.\n");
printf("Array before sorting:\n");
for(i=0;i<=4;i++)
printf("%d\t",arr[i]);
for(i=0;i<=3;i++)
{
for(j=i+1;j<=4;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("\n\n Array after sorting:\n");
for(i=0;i<=4;i++)
printf("%d\t",arr[i]);
getch();
}
/* OUTPUT
selection Sort =
Array before Sorting
17 15 13 31 5

Array after Sorting:


5 13 15 17 31
*/
/* Experiment no 6: PROGRAM TO PERFORM INSERTION SORT */
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5]={25,17,31,13,2};
int i,j,k,temp;
clrscr();
printf("insertion sort.\n");
printf("\narray before sorting:\n");
for(i=0;i<=4;i++)
printf("%d\t",arr[i]);
for(i=1;i<=4;i++)
{
for(j=0;j<i;j++)
{
if(arr[j]>arr[i])
{
temp=arr[j];
arr[j]=arr[i];
for(k=i;k>j;k--)
arr[k]=arr[k-1];

arr[k+1]=temp;
}
}
}
printf("\n\narray after sorting:\n");
for(i=0;i<=4;i++)
printf("%d\t",arr[i]);
getch();
}
/* OUTPUT
insertion sort.
array before sorting:
25 17 31 13 2

array after sorting:


2 13 17 25 31 */
/* Experiment no 7: PROGRAM TO PERFORM STACK OPERATION */

#include<stdio.h>
#include<conio.h>
#define MAX 5
struct stack
{
int arr[MAX];
int top;
};
void initstack(struct stack*);
void push(struct stack*,int item);
int pop(struct stack*);
void main ()
{
struct stack s;
int i;
clrscr();
initstack(&s);
push(&s,1);
push(&s,2);
push(&s,3);
push(&s,4);
push(&s,5);
push(&s,6);
i=pop(&s);
printf("\n\n item popped is:%d",i);
i=pop(&s);
printf("\n item popped is:%d",i);
i=pop(&s);
printf("\n item popped is:%d",i);
i=pop(&s);
printf("\n item popped is:%d",i);
i=pop(&s);
printf("\n item popped is:%d",i);
i=pop(&s);
getch();
}
void initstack(struct stack *s)
{
s->top=-1;
}
void push(struct stack *s,int item)
{
if(s->top==4)
{
printf(" \n Stack Overflow ");
return;
}
s->top++;
s->arr[s->top]=item;
}
int pop(struct stack*s)
{
int data;
if(s->top==-1)
{
printf("\n Stack underflow ");
return NULL;
}
data=s->arr[s->top];
s->arr[s->top]=0;
s->top--;
return data;
}
/*OUTPUT

Stack Overflow

item popped is:5


item popped is:4
item popped is:3
item popped is:2
item popped is:1
Stack underflow
*/
/* EXPERIMENT NO 8 : LINEAR QUEUE */
#include<stdio.h>
#include<conio.h>
struct queue
{
int a[5];
int f,r;
}q;

void insert(struct queue*,int);


void del(struct queue*);
void display(struct queue*);
void init(struct queue*);

void main()
{
clrscr();
init(&q);
insert(&q,10);
insert(&q,20);
insert(&q,30);
insert(&q,40);
insert(&q,50);
insert(&q,60);
display(&q);
del(&q);
del(&q);
del(&q);
del(&q);
del(&q);
del(&q);
display(&q);
getch();
}
void init (struct queue *q)
{
q->f=q->r=-1;
}
void insert(struct queue*q,int num)
{
if(q->r==4)
{
printf("queue overflow \n");
return;
}
q->r++;
q->a[q->r]=num;
if (q->f==-1)
q->f=q->r=0;
}
void del(struct queue * q)
{
if (q->r==-1)
{
printf("\nqueue underflow\n");
return;
}
q->a[q->f]=0;
if (q->f==q->r)
q->f=q->r=-1;
else
q->f++;
}
void display (struct queue *q)
{
int i;
if (q->r==-1)
{
printf("\nqueue empty\n");
return;
}
for(i=q->f;i<=q->r;i++)
{
printf("%d \t", q->a[i]);
}
}
/* OUTPUT
queue overflow
10 20 30 40 50
queue underflow

queue empty */
/* experiment no 9 : Program that implements circular queue as an array. */

#include <stdio.h>
#include <conio.h>

#define MAX 10

void addq ( int *, int, int *, int * ) ;


int delq ( int *, int *, int * ) ;
void display ( int * ) ;

void main( )
{
int arr[MAX] ;
int i, front, rear ;

clrscr( ) ;

/* initialise data member */


front = rear = -1 ;
for ( i = 0 ; i < MAX ; i++ )
arr[i] = 0 ;

addq ( arr, 14, &front, &rear ) ;


addq ( arr, 22, &front, &rear ) ;
addq ( arr, 13, &front, &rear ) ;
addq ( arr, -6, &front, &rear ) ;
addq ( arr, 25, &front, &rear ) ;

printf ( "\nElements in the circular queue: " ) ;


display ( arr ) ;

i = delq ( arr, &front, &rear ) ;


printf ( "Item deleted: %d", i ) ;
i = delq ( arr, &front, &rear ) ;
printf ( "\nItem deleted: %d", i ) ;

printf ( "\nElements in the circular queue after deletion: " ) ;


display ( arr ) ;

addq ( arr, 21, &front, &rear ) ;


addq ( arr, 17, &front, &rear ) ;
addq ( arr, 18, &front, &rear ) ;
addq ( arr, 9, &front, &rear ) ;
addq ( arr, 20, &front, &rear ) ;

printf ( "Elements in the circular queue after addition: " ) ;


display ( arr ) ;

addq ( arr, 32, &front, &rear ) ;

printf ( "Elements in the circular queue after addition: " ) ;


display ( arr ) ;

getch( ) ;
}

/* adds an element to the queue */


void addq ( int *arr, int item, int *pfront, int *prear )
{
if ( ( *prear == MAX - 1 && *pfront == 0 ) || ( *prear + 1 == *pfront ) )
{
printf ( "\nQueue is full." ) ;
return ;
}

if ( *prear == MAX - 1 )
*prear = 0 ;
else
( *prear )++ ;

arr[*prear] = item ;
if ( *pfront == -1 )
*pfront = 0 ;
}

/* removes an element from the queue */


int delq ( int *arr, int *pfront, int *prear )
{
int data ;

if ( *pfront == -1 )
{
printf ( "\nQueue is empty." ) ;
return NULL ;
}

data = arr[*pfront] ;
arr[*pfront] = 0 ;

if ( *pfront == *prear )
{
*pfront = -1 ;
*prear = -1 ;
}
else
{
if ( *pfront == MAX - 1 )
*pfront = 0 ;
else
( *pfront )++ ;
}
return data ;
}

/* displays element in a queue */


void display ( int * arr )
{
int i ;
printf ( "\n" ) ;
for ( i = 0 ; i < MAX ; i++ )
printf ( "%d\t", arr[i] ) ;
printf ( "\n" ) ;
}
/* OUTPUT
Elements in the circular queue:
14 22 13 -6 25 0 0 0 0 0

Item deleted: 14
Item deleted: 22
Elements in the circular queue after deletion:
0 0 13 -6 25 0 0 0 0 0

Elements in the circular queue after addition:


0 0 13 -6 25 21 17 18 9 20

Elements in the circular queue after addition:


32 0 13 -6 25 21 17 18 9 20
*/
/* Experiment no 10: Program to maintain a Single linked list */

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

/* structure containing a data part and link part */


struct node
{
int data ;
struct node * link ;
};

void append ( struct node **, int ) ;


void addatbeg ( struct node **, int ) ;
void addafter ( struct node *, int, int ) ;
void display ( struct node * ) ;
int count ( struct node * ) ;
void delete ( struct node **, int ) ;
void main( )
{
struct node *p ;
p = NULL ; /* empty linked list */

printf ( "\nNo. of elements in the Linked List = %d", count ( p ) ) ;


append ( &p, 14 ) ;
append ( &p, 30 ) ;
append ( &p, 25 ) ;
append ( &p, 42 ) ;
append ( &p, 17 ) ;

display ( p ) ;

addatbeg ( &p, 999 ) ;


addatbeg ( &p, 888 ) ;
addatbeg ( &p, 777 ) ;

display ( p ) ;

addafter ( p, 7, 0 ) ;
addafter ( p, 2, 1 ) ;
addafter ( p, 5, 99 ) ;

display ( p ) ;
printf ( "\nNo. of elements in the Linked List = %d", count ( p ) ) ;

delete ( &p, 99 ) ;
delete ( &p, 1 ) ;
delete ( &p, 10 ) ;

display ( p ) ;
printf ( "\nNo. of elements in the Linked List = %d", count ( p ) ) ;
}

/* adds a node at the end of a linked list */


void append ( struct node **q, int num )
{
struct node *temp, *r ;

if ( *q == NULL ) /* if the list is empty, create first node */


{
temp = malloc ( sizeof ( struct node ) ) ;
temp -> data = num ;
temp -> link = NULL ;
*q = temp ;
}
else
{
temp = *q ;

/* go to last node */
while ( temp -> link != NULL )
temp = temp -> link ;

/* add node at the end */


r = malloc ( sizeof ( struct node ) ) ;
r -> data = num ;
r -> link = NULL ;
temp -> link = r ;
}
}

/* adds a new node at the beginning of the linked list */


void addatbeg ( struct node **q, int num )
{
struct node *temp ;

/* add new node */


temp = malloc ( sizeof ( struct node ) ) ;

temp -> data = num ;


temp -> link = *q ;
*q = temp ;
}

/* adds a new node after the specified number of nodes */


void addafter ( struct node *q, int loc, int num )
{
struct node *temp, *r ;
int i ;

temp = q ;
/* skip to desired portion */
for ( i = 0 ; i < loc ; i++ )
{
temp = temp -> link ;

/* if end of linked list is encountered */


if ( temp == NULL )
{
printf ( "\nThere are less than %d elements in list", loc ) ;
return ;
}
}

/* insert new node */


r = malloc ( sizeof ( struct node ) ) ;
r -> data = num ;
r -> link = temp -> link ;
temp -> link = r ;
}

/* displays the contents of the linked list */


void display ( struct node *q )
{
printf ( "\n" ) ;

/* traverse the entire linked list */


while ( q != NULL )
{
printf ( "%d ", q -> data ) ;
q = q -> link ;
}
}
/* counts the number of nodes present in the linked list */
int count ( struct node * q )
{
int c = 0 ;

/* traverse the entire linked list */


while ( q != NULL )
{
q = q -> link ;
c++ ;
}

return c ;
}

/* deletes the specified node from the linked list */


void delete ( struct node **q, int num )
{
struct node *old, *temp ;

temp = *q ;

while ( temp != NULL )


{
if ( temp -> data == num )
{
/* if node to be deleted is the first node in the linked list */
if ( temp == *q )
*q = temp -> link ;

/* deletes the intermediate nodes in the linked list */


else
old -> link = temp -> link ;

/* free the memory occupied by the node */


free ( temp ) ;
return ;
}
/* traverse the linked list till the last node is reached */
else
{
old = temp ; /* old points to the previous node */
temp = temp -> link ; /* go to the next node */
}
}

printf ( "\nElement %d not found", num ) ;


}

/* OUTPUT

No. of elements in the Linked List = 0


14 30 25 42 17
777 888 999 14 30 25 42 17
777 888 999 1 14 30 99 25 42 17 0
No. of elements in the Linked List = 11
Element 10 not found
777 888 999 14 30 25 42 17 0
No. of elements in the Linked List = 9
*/
// Experiment No 11 :- operation on circular link list//

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node * link;
};
void cinsert(struct node **,struct node **,int );
int cdel(struct node **,struct node **);
void cdisplay(struct node *);
void cerase(struct node **,struct node **);
void main()
{

struct node *front,*rear;


front=rear=NULL;
clrscr();
cinsert(&front,&rear,10);
cinsert(&front,&rear,20);
cinsert(&front,&rear,30);
cinsert(&front,&rear,40);
cinsert(&front,&rear,50);
printf("\n before deletion = ");
cdisplay(front);
cdel(&front,&rear);
cdel(&front,&rear);
printf("\n after deletion = ");
cdisplay(front);
cerase(&front,&rear);
getch();
}
void cinsert(struct node**f,struct node **r,int item)
{
struct node *q;
q=malloc(sizeof(struct node));
q->data=item;
if(*f==NULL)
*f=q;
else
(*r)->link=q;
*r=q;
(*r)->link=*f;
}
int cdel(struct node **f,struct node **r)
{
struct node *q;
int item;
if(*f==NULL)
printf("CLL is empty");
else
{
if(*f==*r)
{

item=(*f)->data;
free(*f);
*f=*r=NULL;
}
else
{
q=*f;
item=q->data;
*f=(*f)->link;
(*r)->link=*f;
free(q);
}
return(item);
}
return NULL;
}
void cdisplay(struct node *f)
{
struct node *q=f,*p=NULL;
while(q!=p)
{
printf("%d\t",q->data);
q=q->link;
p=f;
}
}
void cerase(struct node **f,struct node **r)
{
struct node *temp;
while(*f!=*r)
{
temp=*f;
*f=(*f)->link;
(*r)->link=*f;
free(temp);
}
temp=*f;
free(temp);
*f=*r=NULL;

/* OUTPUT
before deletion = 10 20 30 40 50
after deletion = 30 40 50
*/
/* Experiment no 12 : PROGRAM TO PERFORM TREE TRAVERSING */

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct tnode
{
struct tnode *lc;
struct tnode*rc;
int data;
};
void insert(struct tnode **,int);
void inorder(struct tnode *);
void preorder(struct tnode *);
void postorder(struct tnode *);
void main()
{
struct tnode *bt;
int req,i=1,num;
bt=NULL;
clrscr();
printf("\n enter no of nodes = ");
scanf("%d",&req);
while(i++<=req)
{
printf("enter data=");
scanf("%d",&num);
insert(& bt,num);
}
printf("\nInorder Traversal =");
inorder(bt);
printf("\npreorder Traversal =");
preorder(bt);
printf("\n postorder Traversal =");
postorder(bt);
getch();
}
void insert(struct tnode **sr,int num)
{
if(*sr==NULL)
{
*sr=malloc(sizeof(struct tnode));
(*sr)->lc=NULL;
(*sr)->data=num;
(*sr)->rc=NULL;
}
else
{
if(num<(*sr)->data)
insert (&((*sr)->lc),num);
else
insert(&((*sr)->rc),num);
}
return;
}
void inorder(struct tnode *sr)
{
if(sr!=NULL)
{
inorder(sr->lc);
printf("%d\t",sr->data);
inorder(sr->rc);
}
else
return;
}
void preorder(struct tnode*sr)
{
if(sr!=NULL)
{
printf("%d\t",sr->data);
preorder(sr->lc);
preorder(sr->rc);
}
else
return;
}
void postorder(struct tnode *sr)
{
if(sr!=NULL)
{
postorder(sr->lc);
postorder(sr->rc);
printf("%d\t",sr->data);
}
else
return;
}
/* OUTPUT
enter no of nodes = 5
enter data=22
enter data=6
enter data=24
enter data=77
enter data=33

Inorder Traversal =6 22 24 33 77
preorder Traversal =22 6 24 77 33
postorder Traversal =6 33 77 24 22
*/

You might also like