Showing posts with label DSA lab programs. Show all posts
Showing posts with label DSA lab programs. Show all posts

Monday, May 23, 2011

MDU DSA LAB PROGRAM. Write a program to implement Non-linear data structure i.e.: TREE, in which items are arranged in stored sequence. The tree type is binary tree, is a finite set of data item which either empty or consists of single item called the root and two disjoint binary tree called the left sub tree and the right sub tree.



#include
#include
Struct node
{
int info;
struct node *link,*right;
}*btree;

main()
{
int ele,opt;
char ans;
void inoder(struct node*);
void preorder(struct node*);
void postorder(struct node*);
struct node*insert(struct node*,int);
btree=NULL;
while(1)
{
printf("\n Main menu ");
printf("\n\t1. Create");
printf("\n\t2. inoder");
printf("\n\t3. Preorder");
printf("\n\t4. postorder");
printf("\n\t5. Exit");
printf("\n Enter ur choice");
scanf("%d",&opt);
switch(opt)
{
case 1:
while(1)
{
printf("\n Enter element");
scanf("%d",&ele);
btree=insert(btree,ele);
printf("\n want to continue(y/n)");
fflush(stdin);
scanf(“%c”,&ans);
if(ans==’y’||ans==’Y’)
continue;
else
break;
}
break;
case 2:
printf(“\n inorder traversal”);
inorder(btree);
break;
case 3:
printf(“\n preorder traversal”);
preorder(btree);
break;
case 4:
printf(“\n postorder traversal”);
postorder(btree);
break;
case 5:
exit(0);
break;
default :
printf("\n Wrong choice");
break;
}
}
}

struct node *insert(struct node *btree,int ele)
{
if(btree==NULL)
{
btree=(struct node*)malloc(sizeof(struct node));
btree->info=ele;
btree->left=NULL;
}
else
if(ele>btree->info)
btree->right=insert(btree->right,ele);
else
if(eleinfo)
btree->left=insert(btree->left,ele);
else
printf(“\n duplicate node”);
return(btree);
}

void inorder(struct node *btree)
{
if(btree!=NULL)
{
inorder(btree->left);
printf(“\t %d”,btree->info);
inorder(btree->right);
}
}
 void preorder(struct node *btree)
{
if(btree!=NULL)
{
printf(“\t %d”,btree->info);
preorder(btree->left);
preorder(btree->right);
}
}
 void inorder(struct node *btree)
{
if(btree!=NULL)
{
postrder(btree->left);
postorder(btree->right);
printf(“\t %d”,btree0->info);
}
}

Output:
Main menu
1.      Create
2.      inoder
3.      Preorder
4.      postorder
5.      Exit
Enter ur choice1

Enter element 424

want to continue(y/n)y

Enter element410

want to continue(y/n)y

Enter element440

want to continue(y/n)n

Main menu
1.      Create
2.      inoder
3.      Preorder
4.      postorder
5.      Exit
Enter ur choice2

inorder traversal          410      424      440
Main menu
1.      Create
2.      inoder
3.      Preorder
4.      postorder
5.      Exit
Enter ur choice3

Preorder traversal        424      410      440
Main menu
1.      Create
2.      inoder
3.      Preorder
4.      postorder
5.      Exit
Enter ur choice4

postorder traversal      410      440      424
Main menu
1.      Create
2.      inoder
3.      Preorder
4.      postorder
5.      Exit
Enter ur choice5

DSA LAB PROGRAM. Write a program to illustrate the working of a Queue which is implemented using pointers. But note that the memory space occupied by a node in a linked representation is not exactly twice the space used by an element of array representation.


#include
#include
Struct node
{
int info;
struct node *link;
}*front,*rear,*newptr,*save;

main()
{
int ele,opt;
void insert(int);
int delete(struct node*);
void display(struct node*);
rear=NULL;
front=NULL;
while(1)
{
printf("\n Main menu ");
printf("\n\t1. Insert");
printf("\n\t2. Delete");
printf("\n\t3. Display");
printf("\n\t4. Exit");
printf("\n Enter ur choice");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("\n Enter element to insert");
scanf("%d",&ele);
insert(ele);
break;
case 2:
ele=delete(front);
printf("\n Deleted element=%d",ele);
break;
case 3:
display(queue);
break;
case 4:
exit(0);
break;
default :
printf("\n Wrong choice");
break;
}
}
}

void insert(int ele)
{
newptr=(struct node*)malloc(sizeof(struct node));
newptr->info=ele;
newptr->link=NULL;
if(rear==NULL)
{
rear=newptr;
front=newptr;
}
else
rear->link=newptr;
}

int delete(struct node *front)
{
int ele;
if(front==NULL)
{
printf("\n queue is empty");
return(0);
}
else
{
ele=front->info;
if(front==rear)
{
front=NULL;
rear=NULL;
}
else
front=front->link;
return(ele);
}
}
 void display(struct node *front)
{
save=front;
while(save!=NULL)
{
printf(“\t%d”,save->info);
save=save->link;
}
}

Output:
Main menu
1.      Insert
2.      Delete
3.      Display
4.      Exit
Enter ur choice1

Enter element to insert424

Main menu
1.      Insert
2.      Delete
3.      Display
4.      Exit
Enter ur choice3
424
Main menu
1.      Insert
2.      Delete
3.      Display
4.      Exit
Enter ur choice2

Deleted element=424
Main menu
1.      Insert
2.      Delete
3.      Display
4.      Exit
Enter ur choice4

Write a program to delete a node from node from Doubly Link list.


#include
struct thenode
{
struct thenode *prev ;
int data ;
struct thenode * next ;
} ;

void d_append ( struct thenode **, int ) ;
void d_addatbeg ( struct thenode **, int ) ;
void d_addafter ( struct thenode *, int , int ) ;
void d_display ( struct thenode * ) ;
int d_count ( struct thenode *  ) ;
void d_delete ( struct thenode **, int ) ;
int main()
{
struct thenode *p ;
p = NULL ;
d_append ( &p , 21 ) ;
d_append ( &p , 2 ) ;
d_append ( &p , 14 ) ;
d_append ( &p , 17 ) ;
d_append ( &p , 99 ) ;
d_display ( p ) ;
printf ( "\nNo. of elements = %d\n", d_count ( p ) ) ;
d_addatbeg ( &p, 33 ) ;
d_addatbeg ( &p, 55 ) ;
d_display ( p ) ;
printf ( "\nNo. of elements = %d\n", d_count ( p ) ) ;
d_addafter ( p, 4, 66 ) ;
d_addafter ( p, 2, 96 ) ;
d_display ( p ) ;
printf ( "\nNo. of elements = %d\n", d_count ( p ) ) ;
d_delete ( &p, 55 ) ;
d_delete ( &p, 2 ) ;
d_delete ( &p, 99 ) ;
d_display ( p ) ;
printf ( "\nNo. of elements = %d\n", d_count ( p ) ) ;
}

void d_append ( struct thenode **s, int num )
{
struct thenode *r, *q = *s ;
if ( *s == '\0' )
{
fflush(stdin);
*s = ( struct thenode* ) malloc ( sizeof ( struct thenode ) ) ;
( *s ) -> prev = NULL ;
( *s ) -> data = num ;
( *s ) -> next = NULL ;
}
else
{
while ( q -> next != NULL )
q = q -> next ;
r = (struct thenode* ) malloc ( sizeof ( struct thenode ) ) ;
r -> data = num ;
r -> next = NULL ;
r -> prev = q ;
q -> next = r ;
}
}

void d_addatbeg ( struct thenode **s, int num )
{
struct thenode *q ;
q = ( struct thenode* ) malloc ( sizeof ( struct thenode ) ) ;
q -> prev = NULL ;
q -> data = num ;
q -> next = *s ;
( *s ) -> prev = q ;
*s = q ;
}

void d_addafter ( struct thenode *q, int loc, int num )
{
struct thenode *temp ;
int i ;
for ( i = 0 ; i < loc ; i++ )
{
q = q -> next ;
if ( q == NULL )
{
printf ( "\nThere are less than %d elements", loc );
return ;
}
}
q = q -> prev ;
temp = ( struct thenode* ) malloc ( sizeof ( struct thenode ) ) ;
temp -> data = num ;
temp -> prev = q ;
temp -> next = q -> next ;
temp -> next -> prev = temp ;
q -> next = temp ;
}

void d_display ( struct thenode *q )
{
printf ( "\n" ) ;
while ( q != NULL )
{
printf ( "%2d\t", q -> data ) ;
q = q -> next ;
}
}

int d_count ( struct thenode * q )
{
int c = 0 ;
while ( q != NULL )
{
q = q -> next ;
c++ ;
}
return c ;
}

void d_delete ( struct thenode **s, int num )
{
struct thenode *q = *s ;
while ( q != NULL )
{
if ( q -> data == num )
{
if ( q == *s )
{
*s = ( *s ) -> next ;
( *s ) -> prev = NULL ;
}
else
{
if ( q -> next == NULL )
q -> prev -> next = NULL ;
else
{
q -> prev -> next = q -> next ;
q -> next -> prev = q -> prev ;
}
free ( q ) ;
}
return ;
}
q = q -> next ; /* go to next node */
}
printf ( "\n%d not found.", num ) ;
return;
}

Output:

21       2      14      17      99
No. of elements = 5

55      33      11       2      14      17      99
No. of elements = 7

55      33      96      11       2      66      14      17      99
No. of elements = 9

33      96      11      66      14      17
No. of elements = 6

Write a program of Heap sort, it is a type of tree sort and a heap is defined to a binary tree with the key in each node such that all the leaves of the tree are on two adjacent levels. All leaves on lowest level occur to the left and all levels, except possible the lowest are filled.


#include
main()
{
int ar[100],i,n;
void heap(int[],int);
int pqmaxdelete(int[],int);
printf(“\n enter size of array”);
scanf(“%d”,&n);
for(i=0;i<=n-1;i++)
{
printf(“\n enter element”);
scanf(“%d”,&ar[i]);
}
printf(“\n elements before sorting”);
for(i=0;i<=n-1;i++)
scanf(“\n%d”,&ar[i]);
heap(ar,n);
printf(“\n elements after sorting”);
for(i=0;i<=n-1;i++)
scanf(“\n%d”,&ar[i]);
}

void heap(int ar[],int n)
{
int i,ele,c,p,x,j;
for(i=0;i<=n-1;i++)
{
ele=ar[i];
c=i;
p=(c-1)/2;
while(c>0&&ele>ar[p]);
{
ar[c]=ar[p];
c=p;
p=(c-1)/2;
}
ar[c]=ele;
}
for(i=0;i<=n-1;i++)
{
ar[i]=pqmaxdelete(ar,i+1);
}
}

int pqmaxdelete(int ar[],int n)
{
int x,a,ele,j,i=n-1;
x=ar[0];
ar[0]=ar[i];
ele=ar[0];
j=1;
while(j<=i-1)
{
if(j
j++;
if(ele>=ar[j])
break;
ar[(j-1)/2]=ar[j];
j=2*j+1;
}
ar[(j-1)/2]=ele;
return x;
}

Output:

enter size of array5
enter element33
enter element55
enter element11
enter element44
enter element22

elements before sorting
33
55
11
44
22
elements after sorting
11
22
33
44
55

Write a program to sort a user inputted list of integers using Bubble sort. In this method of sorting multiple swapping take place in one pass and required (n-1) power to sort an array of n elements. Smaller elements move to top of list, which is also known as bubble up.


#include
main()
{
int ar[100],i,n;
void bubble(int[],int);
printf(“\n enter size of array”);
scanf(“%d”,&n);
for(i=0;i<=n-1;i++)
{
printf(“\n enter element”);
scanf(“%d”,&ar[i]);
}
printf(“\n elements before sorting”);
for(i=0;i<=n-1;i++)
scanf(“\n%d”,&ar[i]);
bubble(ar,n);
printf(“\n elements after sorting”);
for(i=0;i<=n-1;i++)
scanf(“\n%d”,&ar[i]);
}

void bubble(int ar[],int n)
{
int i,temp,j;
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-2;j++)
{
if(ar[j]>ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
}


Output:

enter size of array5
enter element55
enter element33
enter element22
enter element11
enter element44

elements before sorting
55
33
22
11
44
elements after sorting
11
22
33
44
55

Write a program to search an element in unsorted list or sorted list. In this case a sorted list the search starts from the element of 0th location and continue until the element is found or an element whose value is greater than the value being searched is reached.

#include
main()
{
int a[10],i,n,m,c=0,loc=0;
printf("Enter the size of an array");
scanf("%d",&n);
printf("\nEnter the elements of the array");
for(i=0;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the element to search");
scanf("%d",&m);
for(i=0;i<=n;i++)
{
if(a[i]==m)
{
c=1;
loc=i+1;
break;
}
}
if(c==0)
printf("\nThe number is not in the list");
else
printf("\nThe number is found at location %d",loc);
}


Output:

Enter the size of an array5

Enter the elements of the array10
20
30
40
50
enter the element to search22
The number is not in the list
Enter the size of an array5

Enter the elements of the array10
20
30
40
50
enter the element to search20

The number is found at location 1

Write a program of Binary tree Traversal Tree After visiting the tree, students are required to traverse this tree in Pre-order, Post-order and in-order Traversal.

#include
struct dsa
{
struct dsa *leftchild;
int data;
struct dsa *rightchild;
};
void insert(struct dsa **,int);
void inorder(struct dsa *);
void preorder(struct dsa *);
void postorder(struct dsa *);

int main()
{
struct dsa *bt;
int req,i=1,num;
bt=NULL;
printf("enter the number of items to be inserted");
scanf("%d",&req);
while(i++<=req) { printf("enter the data"); scanf("%d",&num); insert(&bt,num); } printf("\n in order traversal"); inorder(bt); printf("\n pre order traversal"); preorder(bt); printf("\n postorder traversal"); postorder(bt); return 0; } void insert(struct dsa **sr,int num) { if (*sr==NULL) { *sr=malloc(sizeof(struct dsa)); (*sr)->leftchild=NULL;
(*sr)->rightchild=NULL;
(*sr)->data=num;
return;
}
else
{
if(num<(*sr)->data)
insert(&((*sr)->leftchild),num);
else
insert(&((*sr)->rightchild),num);
}
return;
}

void inorder(struct dsa *sr)
{
if(sr!=NULL)
{
inorder(sr->leftchild);
printf("\t%d",sr->data);
inorder(sr->rightchild);
}
else
return;
}

void preorder(struct dsa *sr)
{
if(sr!=NULL)
{
printf("\t%d",sr->data);
preorder(sr->leftchild);
preorder(sr->rightchild);
}
else
return;
}

void postorder(struct dsa *sr)
{
if(sr!=NULL)
{
postorder(sr->leftchild);
postorder(sr->rightchild);
printf("\t%d",sr->data);
}
else
return;
}


Output:

enter the number of items to be inserted5

enter the data1

enter the data2

enter the data3

enter the data4

enter the data5

in order traversal 1 2 3 4 5
pre order traversal 1 2 3 4 5
postorder traversal 5 4 3 2 1

Write a program to demonstrate the use of stack in implementing quick sort algorithm to sort an array of integers in ascending order.


#include
main()
{
int ar[100],i,n;
void quickshort(int[],int,int);
printf(“\n Enter size of array”);
scanf(“%d”,&n);
for(i=0; i<=n-1; i++)
{
printf(“\n Enter element”);
scanf(“%d”,&ar[i]);
}
printf(“\n array element before sorting”);
for(i=0; i<=n-1; i++)
{
printf(“\t%d”,ar[i]);
}
quicksort(ar,0,n-1);
printf(“\n array element after sorting”);
for(i=0; i<=n-1; i++)
{
printf(“\t%d”,ar[i]);
}
}
 void quicksort(int ar[],int first,int last)
{
int v,temp,l,r;
l=first;
r=last-1;
v=ar[last];
while(i<=r)
{
While(ar[i]
l++;
while(ar[r]>v)
r--;
if(l<=r)
{
temp=ar[l];
ar[l]=ar[r];
ar[r]=temp;
l++;
r--;
}
}

Output:

Enter size of array5
Enter element402
Enter element401
Enter element403
Enter element405
Enter element404

array element before sorting402         401      403      405      404
array element before sorting401         402      403      404      405

Write a program to input 15 numbers and then search any number with the help of Binary search.


#include
main()
    {
    int ar[15],i,n,ele,pos;
    int search(int[],int);
    for(i=0;i<=14;i++)
    {
    printf("Enter element");
    scanf("%d",&ar[i]);
    }
    printf("\n Enter element to be searched");
    scanf("%d",&ele);
    pos=search(ar,ele);
    printf("position=%d",pos);
    }

int search(int ar[],int ele)
    {
    int beg,end,mid,pos;
    pos=0;
    beg=0;
    end=14;
    while(beg<=end)
    {
    mid=(beg+end)/2;
    if(ar[mid]==ele)
        {
        pos=mid+1;
        return(pos);
        }
        else
        if(ele>ar[mid])
        beg=mid+1;
        else
        end=mid-1;
    }
    }

Output:

Enter element401
Enter element402
Enter element403
Enter element404
Enter element405
Enter element406
Enter element407
Enter element408
Enter element409
Enter element410
Enter element411
Enter element412
Enter element413
Enter element414
Enter element415
Enter element to be searched413
position=13

Sunday, May 22, 2011

Write a program to sort a list which is containing integers using Insertion Sort.

#include /* header file to read printf and scanf functions*/
int main()
{
int a[100],n,k,i,j,temp;
printf("How many elements\n");
scanf("%d",&n);
printf("Enter the element of array");
for(i=0;i<=n-1;i++)

{
scanf("%d",&a[i]);
}
for(k=1;k<=n-1;k++) { temp=a[k]; j=k-1; while((temp=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("Element of array after sorting\n"); /*statement to be displayed*/
for(i=0;i<=n-1;i++)
{
printf("%d\n",a[i]);
}
return 0;
}

OUTPUT

How many elements
3
Enter the element of array
1
7
4
Element of array after sorting
1
4
7

Write a program to sort a user inputted list of integers using Bubble sort. In this method of sorting multiple swapping take place in one pass and required (n-1) power to sort an array of n elements. Smaller elements move to top of list, which is also known as bubble up.

#include
#include
#define MAX 30
void bubblesort(int[],int);
main()
{
int arr[MAX],eleM,var;
printf("\n enter the size of array");
scanf("%d",&eleM);
for(var=1;var<=eleM;var++) { printf("\nenter the elements"); scanf("%d",&arr[var]); } printf("sorted array is"); bubblesort(arr,eleM); return 0; } void bubblesort(int arr[],int eleM) { int count,v,var; for(count=0;count<=eleM-1;count++) { for(v=0;v<=eleM-count-1;v++) { if(arr[v]>arr[v+1])
{
var=arr[v];
arr[v]=arr[v+1];
arr[v+1]=var;
}
}
}
for(count=0;count<=eleM-1;count++)
{
printf("\t%d",arr[count]);
}
}


OUTPUT

enter the size of array 6
enter the elements 3
enter the elements 5
enter the elements 1
enter the elements 0
enter the elements 4
enter the elements 6
sorted array is 0 1 3 4 5 6

Write a program for following operations in circular linked list of strings:- a.Creation of linked list b.Insertion at beginning c.Insertion at last d.Insertion at specified position e.Exit

#include /*Header file */
#include
struct node /* structure having a info part and a link part */
{
int info ;
struct node *link ;
} ;
int insertbeg ( struct node **, int ) ; /*insertion*/
int opposite ( struct node ** ) ; /* opposite*/
int display ( struct node * ) ;
int count ( struct node * ) ;

int main( )
{
struct node *opt ;
opt = NULL ;

insertbeg ( &opt, 8 ) ;
insertbeg ( &opt, 24 ) ;
insertbeg ( &opt, 10 ) ;
insertbeg ( &opt, 5 ) ;
insertbeg ( &opt, 3 ) ;
insertbeg ( &opt, 2 ) ;

display ( opt ) ;
printf ( "\nNo. of elements in linked list = %d", count ( opt ) ) ;
opposite ( &opt ) ;
display ( opt ) ;
printf ( "\nNo. of elements in linked list = %d", count ( opt ) ) ;
}

int insertbeg ( struct node **vtx, int var )
{
struct node *item ;
item = malloc ( sizeof ( struct node ) ) ; /*malloc for memory allocation*/
item -> info = var ;
item -> link = *vtx ;
*vtx = item ;
}

int opposite ( struct node **rtx )
{
struct node *vtx, *anode, *bnode ;
vtx = *rtx ;
anode = NULL ;
while ( vtx != NULL )
{
bnode = anode ;
anode = vtx ;
vtx = vtx -> link ;
anode -> link = bnode ;
}

*rtx = anode ;
}

int display ( struct node *vtx )
{
printf ( "\n" ) ;
while ( vtx != NULL)
{
printf ( "%d ", vtx -> info ) ;
vtx = vtx -> link ;
}
}

int count ( struct node * vtx)
{
int cont = 0 ;
while ( vtx != NULL )
{
vtx = vtx -> link ;
cont++ ;
}

return cont ;
}


OUTPUT

2 3 5 10 24 8
No. of elements in linked list = 6
8 24 10 5 3 2
No. of elements in linked list = 6

MDU DSA LAB PROGRAMS Write a program to illustrate the working of a Queue which is implemented using pointers. But note that the memory space occupied by a node in a linked representation is not exactly twice the space used by an element of array representation.

#include /*HEADER FILE*/
struct node
{
int inf; /*DECLARATION OF VARIABLES*/
struct node *link;
}

*front, *rear, *newval, *load;

void insert(int);
int del(struct node *);
void display(struct node *);

main()
{
int var,choice; /*VARIABLE DECLARED IN MAIN */
rear='\0';
front='\0';

while(1)
{
printf("\n MAIN MENU");
printf("\n 1.INSERT");
printf("\n 2.DELETE");
printf("\n 3.DISPLAY");
printf("\n 4.ENTER THE CHOICE: ");
scanf("%d",&choice);

switch(choice)
{
case 1: printf("\n enter the element: ");
scanf("%d",&var);
insert(var);
display(front);
break;
case 2: var=del(front);
printf("\n deleted element is %d",var);
display(front);
break;
case 3: display(front);
break;
case 4: exit(0);
break;
default: printf("\n you entered a wrong choice");
break;
}
}
}

void insert(int var) /*NODE INSERTION*/
{
newval=(struct node *)malloc(sizeof(struct node));
newval->inf=var;
newval->link='\0';
if(rear=='\0')
{
rear=newval;
front=newval;
}
else
{
rear->link=newval;
}
}

int del(struct node *front) /*NODE DELETION*/
{
int var;
if (front=='\0')
{
printf("\n queue is empty");
return(0);
}
else
{
var=front->inf;
if(front==rear)
{
front='\0';
rear='\0';
}
else
{
front=front->link;
}
}
return(var);
}
void display(struct nod *front) /*DISPLAY FUNCTION*/
{
load=front;
while(rear->link!='\0')
{
printf("\t %d",load->inf);
load=load->link;
}
}


OUTPUT


MAIN MENU
1.insert
2.delete
3.display
4.enter your choice: 1

enter the element: 528
528
MAIN MENU
1.insert
2.delete
3.display
4.enter your choice: 1
enter the element: 828
528 828

MAIN MENU
1.insert
2.delete
3.display
4.enter your choice: 2
element to be deleted 528
828
MAIN MENU
1.insert
2.delete
3.display
4.enter your choice: 3
828
MAIN MENU
1.insert
2.delete
3.display
4.enter your choice: 4

Program to implement the following with the help of linear queue of integers: a) Insert the element in queue. b) Delete the element from the queue. c) Display queue after insert and delete operations d) Exit.

#include /*Header File*/
# include /*Header File*/
# define size 10 /*Header File*/
int front,rear;
main()
{
int Queue[size],elem,ch; /*Declaration of variables*/
void disp (int[]);
void insert (int[],int);
rear=-1;
front=-1;
while(1)
{
printf ("\n Main Menu");
printf ("\n1. Insert");
printf("\n2. Delete");
printf("\n3. Display");
printf("\n4. Exit");
printf("\n Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter element: ");
scanf("%d",&elem);
insert(Queue,elem);
break;
case 2: ele=delete (Queue);
printf("\n Deleted element is %d",elem);
break;
case 3: display (Queue);
break;
case 4: exit(0);
break;
default: printf("\n Wrong Choice"); /*Default Statement*/
break;
}
}
}

void insert (int Queue[],int elem)
{
if (rear==size-1)
printf("\n Queue is full");
else
if (rear==-1)
{
rear=0; front=0;
Queue[rear]=elem;
}
else
rear=rear+1;
Queue[rear]=elem;
}

int delete (int Queue[])
{
int elem;
if (front==-1)
{
printf("\n Queue is empty");
return(0);
}
else
{
elem =Queue[front];
if (front==rear)
{
front=-1;
rear=-1;
}
else
front=front+1;
return(elem);
}
}

void disp(int Queue[])
{
int count;
for ( count=front; count<=rear;count++)
printf("\t%d", Queue[count]);
}

OUTPUT

Main Menu
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1

Enter the value of item: 34
Element inserted
Main Menu
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
Elements are: 34
Main Menu
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2

Element deleted is: 34
Main Menu
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 4

Write a recursive program for calculating ab or a-b and also find the greatest common divisor of 2 positive integers with the help of Euclid’s algorithm. The definition of Euclid’s algorithm is ---- GCD(n,m) –{m if n>m sw n mod m=0 {Otherwise GCD(m,n mod m)

#include /*Header File*/
#include /*Header File*/

int a,b,c,m,n,l,k,num; /*Declaration of variables*/
int recursion(int m,int n); /*Fuunction declaration*/
int GCD(int l,int k); /*Fuunction declaration*/

main()
{
int choice,g1; /*Declaration of variables*/
printf("\nEnter the value of a and b:\n");
printf("a=");
scanf("%d",&a);
printf("b=");
scanf("%d",&b);
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\ncalling of recyoursive ():\n");
num=recursion(a,b);
printf("The recursive value is:%d",num);
break;
case 2: printf("Calling of GCD function:\n");
g1=GCD(a,b);
printf("%d",g1);
break;
default: printf("Wrong choice");
}

}

int recursion(int m,int n) /*Function definition*/
{
int count;
for(count=1;count {
m=(m*a);
}
return m;
}

int GCD(int l,int k) /*Function definition*/
{
int c=0;
int g=0;
c=k%l;
printf("c=%d",c);

if((c==0)&&(l {
g=l;
printf("\ngcd=");
}
else
GCD(c,l);
return g;
}


OUTPUT

Enter the value of a and b:
a=2
b=8

Enter your choice: 2
Calling of GCD function:
c=0
gcd=2

Write a program to demonstrate the use of stack in implementing quick sort algorithm to sort an array of integers in ascending order.

#include /* Header File*/
int quick ( int*, int, int ) ;
int main( )
{
int arr[10] = { 11, 10, 9, 15, 3, 26, 19, 1, 7, 28 } ; /*Declaration of Array*/
int count1 ;
int Quicksort ( int *, int, int ) ;
printf ( "Quick sort:\n" ) ;
printf ( "\nArray before sorting:\n") ;
for ( count1 = 0 ; count1 <= 9 ; count1++ ) printf ( "%d", arr[count1] ) ; Quicksort ( arr, 0, 9 ) ; printf ( "\nArray after sorting:\n") ; for ( count1 = 0 ; count1 <= 9 ; count1++ ) printf ( "%d", arr[count1] ) ; } int Quicksort ( int arr[ ], int low, int high ) { int n ; if ( high > low )
{
n = quick ( arr, low, high ) ;
Quicksort ( arr, low, n - 1 ) ;
Quicksort ( arr, n + 1, high ) ;
}
}

int quick ( int arr[ ], int low, int high )
{
int n, opt, value, temp ;
opt = low + 1 ;
value= high ;
n = arr[low] ;
while ( value >= opt )
{
while ( arr[opt] < n ) opt++ ; while ( arr[value] > n )
value-- ;
if ( value> opt )
{
item = arr[opt] ;
arr[opt] = arr[value] ;
arr[value] = item ;
}
}
temp = arr[low] ;
arr[low] = arr[value] ;
arr[value] = temp ;
return value;
}

OUTPUT


Quick sort:

Array before sorting:

11 10 9 15 3 26 19 1 7 28

Array after sorting:

1 3 7 9 10 11 15 19 26 28

Program to demonstrate the PUSH,POP,DISPLAY,EXIT operations on stack.

#include /*Header File*/
#define maximum 9 /*Header File*/
int top=-1; /*Declaration of variables*/

void push(int [],int);
int pop(int []);
void dis(int []);
void exit();

int main()
{
int stack[maximum]; /*Declaration of variables*/
int item; /*Declaration of variables*/
int ch; /*Declaration of variables*/
int m; /*Declaration of variables*/
while(1)
{
printf("\nMain Menu");
printf("\n1. Push");
printf("\n2. Pop");
printf("\n3. Display");
printf("\n4. Exit");
printf("\n Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("\n Enter the value of item: ");
scanf("%d",&item);
printf("Element inserted");
push(stack,item);
break;
case 2 : m=pop(stack);
printf("\nElement deleted is: ");
printf("%d",m);
break;
case 3 : printf("Elements are: ");
disp(stack);
break;
case 4 : exit();
break;
default: printf("\nWrong choice entered");
break;
}
}
return 0;
}

void push(int stack[],int item)
{
if(top==maximum)
{
printf("\n Overflow Error");
}
else
{
top=top+1;
stack[top]=item;
}
}

int pop(int stack[])
{
int x;
x=stack[top];
top=top-1;
return x;
}

void display(int stack[])
{
int count; /*Declaration of variables*/

for( count=0;count<=top;count++)

{
printf("%d",stack[count]);
}

}

void exit()
{
exit(0);
}



OUTPUT

Main Menu
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1

Enter the value of item: 14
Element inserted
Main Menu
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
Elements are: 14
Main Menu
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2

Element deleted is: 14
Main Menu
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 4