You are on page 1of 13

PROGRAM 3: QUEUE IMPLEMENTATION

#include<stdio.h> #include<conio.h> #include<alloc.h> struct node { int data; struct node *next; }*front,*rear; void createq(int); void insertq(int); void deleteq(); void displayq(struct node *); void main() { clrscr(); front=rear=NULL; createq(1); insertq(2); insertq(3); insertq(4); insertq(5); insertq(7); printf("\nElements in the circular queue: "); displayq(front); deleteq(); deleteq(); printf("\nElements in the circular queue after deletion: "); displayq(front); insertq(1); insertq(10); printf("\nElements in the circular queue after addition: "); displayq(front); getch(); } void createq(int num) { struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); temp->data=num; temp->next=NULL; front=rear=temp; } void insertq(int num) { struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); temp->data=num;

return NULL. } void deleteq() { struct node *temp. } if(front==rear) { front=NULL. front=front->next. rear=temp. front=front->next. temp=front. return(item).temp->next=front. rear=NULL. free(temp). int item. } } } . } else { if(rear!=front) { item=front->data. } printf("%d".front->data). if(front==NULL) { printf("queue is empty"). } void displayq(struct node *front) { while(front!=rear) { printf("%d ". rear->next=temp.front->data).

getch(). } if( arr[mid]>num) upper=mid-1 .mid=(lower+upper)/2) { if( arr[mid]==num) { printf("\nSearched number is at position %d in the array".mid.PROGRAM 4: SEARCHING Program for binary search #include<conio.h> void main() { int num. } if(flag) printf("\nElement is not present in the array").7.3.mid).6. int arr[10]={1. for(mid=((upper+lower)/2).&num).4.upper=9.8.h> #include<stdio.5. clrscr().lower=0. else lower=mid+1. break. flag=0.2.10}.lower<=upper.9. scanf("\n%d". } OUTPUT: .flag=1. printf("\nEnter the number to be searched: ").

else printf("\Number is not present in the array").&num). } OUTPUT: . scanf("\n%d".i++) { if ((arr[9]<num)||(arr[i]>=num)) { if (arr[i]==num) printf("\n searched number is at position %d in the array".3.h> #include<stdio.7.9. printf("\nEnter the number to be searched: ").Program for linear search #include<conio.4.i<=9. int arr[10]={1.h> void main() { int i.num. for (i=0.2.5. } } getch().6. clrscr().8.10}. break.i).

case 3: display(). main() { int choice. printf("4. clrscr().h> #include<stdio. break.Display\n"). printf("Enter your choice : "). printf("3. break.h> #define MAX 5 int top = -1.Pop\n").PROGRAM 2: STACK USING ARRAY AND LINKED LIST Program of stack using array #include<conio. default: printf("Wrong choice\n"). case 2: pop(). scanf("%d". break.Push\n").&choice). while(1) { printf("\n\n1. } } } push() { int pushed_item. printf("2. else . switch(choice) { case 1 : push(). int stack_arr[MAX] . case 4: exit(1). if(top == (MAX-1)) printf("Stack Overflow\n").Quit\n").

else { printf("\n\nStack elements :\n"). i >=0. } } . top=top-1. i--) printf("%d\n".{ printf("Enter the item to be pushed in stack : "). } } pop() { if(top == -1) printf("Stack Underflow\n").&pushed_item). top=top+1. scanf("%d".stack_arr[top]). stack_arr[i] ). if(top ==-1) printf("Stack is empty\n"). for(i = top. else { printf("Popped element is : %d\n". } } display() { int i. stack_arr[top] = pushed_item.

OUTPUT: .

data. scanf("%d". top=temp. } break.Push\n2. printf("\nEnter ur choice:").h> struct node { int data. if(temp==NULL) { printf("\nStack is empty\n").&choice). top=top->link. } else { printf("\nStack Underflow"). temp->data=data. case 3: temp=top.Pop\n3. switch(choice) { case 1: temp=(struct node *)malloc(sizeof(struct node)). .h> #include<conio.top->data). struct node *link. scanf("%d". break.*temp.h> #include<stdlib.Stack using linked list #include<stdio.Display\n4. temp->link=top. struct node *top=NULL. while(1)//infinite loop is used to insert/delete infinite number of nodes { printf("\n1.&data). clrscr().Exit\n"). case 2: if(top!=NULL) { printf("The poped element is %d". }. void main() { int choice. printf("Enter a node data :").

} while(temp!=NULL) { printf("->%d->".temp->data). temp=temp->link. case 4: exit(0). } } } OUTPUT: . } break.

2}. } OUTPUT: . for(i=0. for(i=0.i<=3.i<=4.j.arr[i]).17.temp.i++) printf("%d\t".31.13. clrscr().i<=4.i++) printf("%d\t".i++) { for(j=0.j<=3-i. arr[j+1]=temp.h> #include<conio.PROGRAM 5: SORTING APPROACHES Program for bubble sort #include<stdio. printf("before sorting:"). int i. } } } printf("\nAfter sorting: "). for(i=0.h> void main() { int arr[5]={25.j++) { if(arr[j]>arr[j+1]) { temp=arr[j]. arr[j]=arr[j+1]. getch().arr[i]).

i <= 4 . 17. temp .\n" ) . arr[j] = arr[i] . arr[k + 1] = temp . 13. } } } printf ( "\n\nArray after sorting:\n") .1] .Program for insertion sort #include <stdio. for ( i = 0 . for ( k = i . 2 } . clrscr( ) . printf ( "\nArray before sorting:\n") . 31. arr[i] ) . i++ ) printf ( "%d\t".h> #include <conio. j++ ) { if ( arr[j] > arr[i] ) { temp = arr[j] . j < i . i <= 4 . printf ( "Insertion sort. k > j . k-.h> void main( ) { int arr[5] = { 25. i++ ) { for ( j = 0 . k. for ( i = 1 . getch( ) . i++ ) printf ( "%d\t". j. } OUTPUT: . int i. for ( i = 0 . arr[i] ) .) arr[k] = arr[k . i <= 4 .

i++) printf("%d\t". fflush.i++) printf("%d\t". arr[i]=arr[j].j++) { if(arr[i]>arr[j]) { temp=arr[i].j. clrscr().17.i<=4. } OUTPUT: . printf("before sorting:").i<=3. for(i=0. int i.arr[i]).2}.h> #include<conio.j<=4.31. getch().arr[i]).temp.i<=4. } } } printf("\nAfter sorting: "). arr[j]=temp. for(i=0.Program for selection sort #include<stdio. for( i=0.i++) { for(j=i+1.13.h> void main() { int arr[5]={25.

PROGRAM 1: FACTORIAL USING RECURSION #include<stdio. return(f). if(x==1) return(1).fact.&num). getch().h> #include<conio. clrscr().fact). else f=x*rec(x-1).h> rec(int). } rec(int x) { int f. printf("Factorial of given number is %d". void main() { int num. } OUTPUT: . fact=rec(num). scanf("%d". printf("enter the number to find factorial: \n").