Program For Circular Queue Implementation Through Array

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 13

6.

Program for Circular Queue implementation through Array

#include<stdio.h> #include<ctype.h> # define maxsize 6 int cq[maxsize]; int front,rear; void main() { void add(int,int[],int,int,int); int del(int[],int,int,int); int will=1,i,num; front=0; rear=0; printf("\n program for circular queue through array \n \n"); while(will==1) { printf("\n press 1 to add element to circular queue\n"); printf("\n press 2 to delete element to circular queue\n"); scanf("%d",&will); switch(will) { case 1: printf("\n enter the data...\n"); scanf("%d",&num); rear++;

add(num,cq,maxsize,front,rear); break; case 2: i=del(cq,maxsize,front,rear); printf("\n the value retreived from circular queue is: %d",i); break; default:printf("invalid choice"); } printf("\n\n do you want to do more operations on circular queue\n"); printf("\n press 1 for yes,any other key to exit"); scanf("%d",&will); } } void add(int item,int q[],int max,int front,int rear) {rear=(rear%max); if(front==rear) { printf("cicular queue full"); return; } else { cq[rear]=item; printf("rear=%d front=%d",rear,front); }

} int del(int q[],int max,int front,int rear) { int a; if(front==rear) { printf("circular queue empty"); return(0); } else { front++; front=front%max; a=cq[front]; return(a); printf("rear=%d front=%d",rear,front); } }
7.

Write a C program to sorting the given array using bubble sort.

#include<stdio.h> void main() { int a[20], n, temp, i, j; printf("\nEnter the total number: "); scanf("%d",&n); printf("\nEnter the elements of the array:\n"); for(i=1; i<=n; i++) { scanf("%d", &a[i]); }

for(i=1; i<=n-1; i++) for(j=1; j<=n-i;j++) if(a[j]>a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } printf("\nTHE ASCENDING ORDER LIST IS:"); for(i=1; i<=n; i++) printf("\n%d",a[i]); }

Enter the total number: 5 Enter the elements of the array: 5 4 3 2 1


8.

Write a program to implement Selection sort.

#include<stdio.h> #include<conio.h> #define MAXSIZE 500 void selection(int elements[], int maxsize); int a[MAXSIZE],maxsize; int main() { int i;

printf("\nEnter the size of the array: "); scanf("%d",&maxsize); printf("\nEnter the values : "); for (i = 0; i < maxsize; i++) { scanf("%d",&a[i]); } printf("\nArray before sorting:\n"); for (i = 0; i < maxsize; i++) printf("%d\t",a[i]); printf ("\n"); selection(a, maxsize); printf("\nArray after sorting:\n"); for (i = 0; i < maxsize; i++) printf("%d\t", a[i]); } void selection(int a[], int array_size) { int i, j, k; int min, temp; for (i = 0; i < maxsize-1; i++) { min = i; for (j = i+1; j < maxsize; j++) {

if (a[j] < a[min]) min = j; } temp =a[i]; a[i] = a[min]; a[min] = temp; } }

9.

Write a program to implement Quick sort.

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

void quicksort(int [],int,int); int partition(int [],int,int); main() { int a[20],p,q,i,n; clrscr();

printf("Enter The number Of Elements\t: "); scanf("%d",&n); for(i=0;i< n;i++) { scanf("%d",&a[i]); } p=0; q=n-1; printf("\nArray Befor Sorting : "); for(i=0;i< n;i++) printf("%5d",a[i]); quicksort(a,p,q);

printf("\nArray After Sorting : "); for(i=0;i< n;i++) printf("%5d",a[i]); getch(); return 0; } void quicksort(int a[],int p,int q) { int j; if(p< q) { j=partition(a,p,q+1); quicksort(a,p,j-1); quicksort(a,j+1,q); } } int partition(int a[],int m,int p) { int v,i,j; int temp; v=a[m]; i=m;j=p; do { do

{ i += 1; } while(a[i]< v); do { j -= 1; } while(a[j]>v);

if(i< j) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } while(i< j); a[m] =a[j]; a[j] = v; return j; }
10. Write

a program to implement Shell sort.

#include<stdio.h> #include<conio.h> void shellsort(int a[],int n)

{ int j,i,m,mid; for(m = n/2;m>0;m/=2) { for(j = m;j< n;j++) { for(i=j-m;i>=0;i-=m) { if(a[i+m]>=a[i]) break; else { mid = a[i]; a[i] = a[i+m]; a[i+m] = mid; } } } } } main() { int a[10],i,n; clrscr(); printf("Enter The number Of Elements\t: "); scanf("%d",&n); for(i=0;i< n;i++) { printf("\nElement %d\t: ",i+1); scanf("%d",&a[i]); } printf("\nArray Befor Sorting : "); for(i=0;i< n;i++) printf("%5d",a[i]); shellsort(a,n); printf("\nArray After Sorting : "); for(i=0;i< n;i++) printf("%5d",a[i]); getch(); return 0; }

11. Write

a program to implement insertion sort.

#include<stdio.h> #include<conio.h> void main() { int A[20], N, Temp, i, j; clrscr(); printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); scanf("%d", &N); printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { gotoxy(25,11+i); scanf("\n\t\t%d", &A[i]); } for(i=1; i<N; 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("\n\tTHE ASCENDING ORDER LIST IS...:\n"); for(i=0; i<N; i++) printf("\n\t\t\t%d", A[i]); getch(); }

12. Write

a program to implement linear search.

#include<stdio.h> #include<conio.h> void main() { clrscr(); int i,n,item,a[20]; printf("\nEnter no of elements="); scanf("%d",&n); printf("\nEnter %d elements=",n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("\nEnter the element to be search="); scanf("%d",&item); for(i=0;i<n;i++) { if(a[i]==item) { printf("\n\t%d is present at position %d",a[i],i+1); break; } } if(i==n) printf("\nElement is not Present"); getch(); }
13. Write

a program to implement binary search.

#include <stdio.h> #define TRUE 0 #define FALSE 1 int main(void) { int array[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; int left = 0; int right = 10; int middle = 0; int number = 0; int bsearch = FALSE;

int i = 0; printf("ARRAY: "); for(i = 1; i <= 10; i++) printf("%d ", i*10); printf("\nSearch for Number: "); scanf("%d", &number); while(bsearch == FALSE && left <= right) { middle = (left + right) / 2; if(number == array[middle]) { bsearch = TRUE; printf("Number found in %d position of array\n",middle+1); } else { if(number < array[middle]) right = middle - 1; if(number > array[middle]) left = middle + 1; } } if(bsearch == FALSE) printf("-- Number Not found --\n"); return 0; }

You might also like