Data Structures Lab Using C
Data Structures Lab Using C
1.Write a C program to construct a stack of integers & to perform the following operations on it: a. Push b. pop. c. Display. The program should print appropriate messages for stack overflow, stack underflow & stack empty. #include<stdio.h> #include<conio.h> #define MAXSIZE 3 void push(); void pop(); void display(); int stk[MAXSIZE]; int top=-1; void main() { char ch; int choice; clrscr(); do { printf("\n*************** Stack Operations **************\n"); printf("\t\t 1-> Push\n"); printf("\t\t 2-> Pop\n"); printf("\t\t 3-> Display\n"); printf("\t\t 4-> Exit\n"); printf("\t\t Enter your choice:"); scanf("%d",&choice); switch(choice) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); break; default: printf("\t\tInvalid choice\n"); break; } } while(choice<=4); getch(); } void push() { int item; M.S.E.C Dept Of MCA Page 1
M.S.E.C
Dept Of MCA
Page 4
M.S.E.C
Dept Of MCA
Page 5
Program to convert infix to postfix expression ---------------------------------------------Enter the infix expression a+(b*c-(d/e^f)*g)*h The converted postfix expression is abc*def^/g*-h*+
M.S.E.C
Dept Of MCA
Page 6
default: printf("\t\t Invalid operation\n"); } push(res); } } printf("\t\t Result of above postfix expression is: %d",res); getch(); } void push(int item) { stk[++top]=item; } int pop() { return(stk[top--]); } Program to evaluate postfix expression ----------------------------------------Enter the postfix expression 623+-382/+*2^3+ Result of above postfix expression is: 52
M.S.E.C
Dept Of MCA
Page 8
M.S.E.C
Dept Of MCA
Page 9
M.S.E.C
Dept Of MCA
Page 10
c.
#include<stdio.h> #include<conio.h> int binsearch(int a[],int key,int first,int last); void main() { int a[50],n,i,key,result; clrscr(); printf("Enter the total no of elements\n"); scanf("%d",&n); printf("Enter the elements one by one\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter search element\n"); scanf("%d",&key); result=binsearch(a,key,0,n-1);
M.S.E.C
Dept Of MCA
Page 11
int binsearch(int a[],int key,int first,int last) { int mid; if(first>last) return -1; else { mid=(first+last)/2; if(key<a[mid]) binsearch(a,key,first,mid-1); else if(key>a[mid]) binsearch(a,key,mid+1,last); else return mid; } }
M.S.E.C
Dept Of MCA
Page 12
#include<stdio.h> #include<conio.h> #define MAXSIZE 4 void qinsert(); void qdelete(); void display(); int queue[MAXSIZE],front=0,rear=-1,item;
M.S.E.C
Dept Of MCA
Page 14
} while(ch!='n'); getch(); }
void qinsert() { int item; if(rear==MAXSIZE-1) printf("\t\t Queue Overflows\n"); else { printf("\t\tEnter the element:"); scanf("%d",&item); rear++; queue[rear]=item; } } void qdelete() { int item; if(rear==front-1) printf("\t\t Queue Underflows\n"); else if(rear==front) {
M.S.E.C
Dept Of MCA
Page 15
void display() { int i; if(rear==front-1) printf("\t\t Queue Underflows\n:"); else { printf("\t\tThe elements in Queue are\n"); printf("\t"); for(i=front;i<=rear;i++) printf("\t%d",queue[i]); printf("\n"); } }
M.S.E.C
Dept Of MCA
Page 16
*/--------------------output------------------------------
*************** Queue Operations ************** 1-> QInsert 2-> QDelete 3-> Display 4-> Exit Enter your choice:1 Enter the element:4 Do you want to continue(type y or n):y
*************** Queue Operations ************** 1-> QInsert 2-> QDelete 3-> Display 4-> Exit Enter your choice:1 Enter the element:45 Do you want to continue(type y or n):y
*************** Queue Operations ************** 1-> QInsert 2-> QDelete 3-> Display 4-> Exit
M.S.E.C
Dept Of MCA
Page 17
*************** Queue Operations ************** 1-> QInsert 2-> QDelete 3-> Display 4-> Exit Enter your choice:2 The deleted element is 4 Do you want to continue(type y or n):y
*************** Queue Operations ************** 1-> QInsert 2-> QDelete 3-> Display 4-> Exit Enter your choice:4 */
M.S.E.C
Dept Of MCA
Page 18
6. Write a C program to simulate the working of a circular queue of integers using an array. Provide the following operations: a. Insert b. Delete c. Display #include<stdio.h> #include<conio.h> #define N 4 void cqinsert(); void cqdelete(); void display(); int cqueue[N],front=-1,rear=-1,item;
void main() { char ch; int choice; M.S.E.C Dept Of MCA Page 19
M.S.E.C
Dept Of MCA
Page 20
} while(ch!='n'); getch(); }
void cqinsert() { int item; if(front==(rear+1)%N) printf("\t\t CQueue Overflows\n"); else { printf("\t\tEnter the element:"); scanf("%d",&item); if(front==-1) front=rear=0; else rear=(rear+1)%N; cqueue[rear]=item; } } void cqdelete() { int item; if(front==-1) printf("\t\t CQueue Underflows\n");
M.S.E.C
Dept Of MCA
Page 21
void display() { int i; if(front==-1) printf("\t\t CQueue Underflows\n:"); else { printf("\t\tThe elements in Queue are\n"); printf("\t"); if(front<=rear) { for(i=front;i<=rear;i++) printf("\t%d",cqueue[i]); } if(front>rear)
M.S.E.C
Dept Of MCA
Page 22
*/*************** Circular Queue Operations ************** 1-> CQInsert 2-> CQDelete 3-> Display 4-> Exit Enter your choice:1 Enter the element:23 Do you want to continue(type y or n):y
*************** Circular Queue Operations ************** 1-> CQInsert 2-> CQDelete 3-> Display 4-> Exit Enter your choice:1 Enter the element:34
M.S.E.C
Dept Of MCA
Page 23
*************** Circular Queue Operations ************** 1-> CQInsert 2-> CQDelete 3-> Display 4-> Exit Enter your choice:2 The deleted element is 23: Do you want to continue(type y or n):y
*************** Circular Queue Operations ************** 1-> CQInsert 2-> CQDelete 3-> Display 4-> Exit Enter your choice:3 The elements in Queue are 34 Do you want to continue(type y or n):n
*/
M.S.E.C
Dept Of MCA
Page 24
7. Write a program to design a priority queue which is maintained as a set of queues(assume a maximum of 3 queues). The elements are inserted based upon the given priority. The deletion of an element is to be done starting from the 1st queue, if it is not empty. If it is empty, the elements from the 2nd queue will be deleted & so on. #include<stdio.h> #include<conio.h> #define MAXSIZE 4 void pqinsert(); void pqdelete(); void display(); int pqueue[3][MAXSIZE]; int front[3]={0,0,0}; int rear[3]={-1,-1,-1}; int item,pr; void main() { char ch; M.S.E.C Dept Of MCA Page 25
M.S.E.C
Dept Of MCA
Page 26
} while(ch!='n'); getch(); }
void pqinsert(int pr) { int item; if(rear[pr]==MAXSIZE-1) printf("\t\t PQueue Overflows\n"); else { printf("\t\t Enter the element:"); scanf("%d",&item); rear[pr]++; pqueue[pr][rear[pr]]=item; } } void pqdelete() {
M.S.E.C
Dept Of MCA
Page 27
void display() { int i,j; for(i=0;i<3;i++) { if(rear[i]==front[i]-1) printf("\t\t Queue %d Underflows\n:",i+1); else { printf("\t\tThe elements in Queue %d are\n",i+1); printf("\t"); for(j=front[i];j<=rear[i];j++)
M.S.E.C
Dept Of MCA
Page 28
/* OUTPUT: *************** Queue Operations ************** 1-> PQInsert 2-> PQDelete 3-> Display 4-> Exit Enter your choice:1 Enter the priority:2 Enter the element:15 Do you want to continue(type y or n):y
M.S.E.C
Dept Of MCA
Page 29
*************** Queue Operations ************** 1-> PQInsert 2-> PQDelete 3-> Display 4-> Exit Enter your choice:1 Enter the priority:2 Enter the element:87 Do you want to continue(type y or n):y
*************** Queue Operations ************** 1-> PQInsert 2-> PQDelete 3-> Display 4-> Exit Enter your choice:3 Queue 1 Underflows The elements in Queue 2 are
M.S.E.C
Dept Of MCA
Page 30
8. Write a C program using dynamic variables & pointers, to construct a singly linked list consisting of the following information in each node; student id(integer), Student name(character string) & semester (integer). The operations to be supported are: a. The insertion operation 1. At the front of a list 2. At the back of the list 3. At any position in the list b. Deleting a node based on student id. If the specified node is not present in the list an error message should be displayed. Both the options should be demonstrated. c. Searching a node based on student id & update the information content. If the specified node is not present in the list an error message should be displayed. Both situations should be displayed. d. Displaying all the nodes in a list. (Note: only either (a, b,& d)or(a, c &d) may be asked in the examination)
M.S.E.C
Dept Of MCA
Page 31
int choice; start=(NODE*)malloc(sizeof(NODE)); clrscr(); do { printf("\n*************** Linked List Operations **************\n"); printf("\t\t 1-> Creation\n"); printf("\t\t 2-> Insertion\n");
M.S.E.C
Dept Of MCA
Page 32
M.S.E.C
Dept Of MCA
Page 33
} while(choice!=7); getch(); }
void insert_node() { int ch; clrscr(); while(1) { printf("\t\t ****************Insert Operations************\n"); printf("\t\t 1-> Insert at begining\n"); printf("\t\t 2-> Insert at position\n"); printf("\t\t 3-> Insert at end\n"); printf("\t\t 4-> Exit to Main menu\n"); printf("\t\t Enter your choice:"); scanf("%d",&ch); switch(ch) { case 1: start=insert_beg(start); break; case 2: insert_pos(start); break; case 3: insert_end(start); break;
M.S.E.C
Dept Of MCA
Page 34
void delete_node() { int item,found=0; NODE *temp; NODE *currptr=start; printf("\t\t Enter the student id to be deleted:"); scanf("%d",&item); if(start->id==item) { found=1; if(start->link==NULL) start=NULL; else start=start->link; } else { while(currptr!=NULL) { temp=currptr->link;
M.S.E.C
Dept Of MCA
Page 35
void search_list() { int item,found=0,count=1; NODE *temp=start; printf("\t\t Enter the student id to be searched:"); scanf("%d",&item); while(temp!=NULL) { if(temp->id==item) { found=1; break;
M.S.E.C
Dept Of MCA
Page 36
} void create_list(NODE *currptr) { NODE *newnode; char ch; while(1) { printf("\t\t Enter the student id :"); scanf("%d",&currptr->id); printf("\t\t Enter the student name :"); scanf("%s",&currptr->name); printf("\t\t Enter the semester"); scanf("%d",&currptr->sem); printf("\t\t Do you wish to continue(type y):"); fflush(stdin); ch=getchar(); if(toupper(ch)=='Y')
M.S.E.C
Dept Of MCA
Page 37
M.S.E.C
Dept Of MCA
Page 38
void insert_end(NODE *currptr) { NODE *newnode; newnode=(NODE*)malloc(sizeof(NODE)); printf("Enter the student id:"); scanf("%d",&newnode->id); printf("\t\t Enter the student name :"); scanf("%s",&newnode->name); printf("\t\t Enter the semester:"); scanf("%d",&newnode->sem); while(currptr->link!=NULL) { currptr=currptr->link; }
M.S.E.C
Dept Of MCA
Page 39
void insert_pos(NODE *currptr) { NODE *newnode,*temp; int i,pos; newnode=(NODE*)malloc(sizeof(NODE)); printf("\t\t Enter the position:"); scanf("%d",&pos); printf("Enter the student id:"); scanf("%d",&newnode->id); printf("\t\t Enter the student name :"); scanf("%s",&newnode->name); printf("\t\t Enter the semester:"); scanf("%d",&newnode->sem); for(i=0;i<pos-1;i++) { temp=currptr; currptr=currptr->link; } temp->link=newnode; newnode->link=currptr; }
M.S.E.C
Dept Of MCA
Page 40
*/-------------------------------output---------------------------------****************Insert Operations************ 1-> Insert at begining 2-> Insert at position 3-> Insert at end 4-> Exit to Main menu Enter your choice:2 Enter the position:2 Enter the student id:2 Enter the student name :DA Enter the semester:2 ****************Insert Operations************ 1-> Insert at begining 2-> Insert at position 3-> Insert at end 4-> Exit to Main menu Enter your choice:4
*************** Linked List Operations ************** 1-> Creation 2-> Insertion 3-> Deletion 4-> Search 5-> Display 6-> Exit
M.S.E.C
Dept Of MCA
Page 41
NULL *************** Linked List Operations ************** 1-> Creation 2-> Insertion 3-> Deletion 4-> Search 5-> Display 6-> Exit Enter your choice:6
----------------------------------------------------------------------------------*/
M.S.E.C
Dept Of MCA
Page 42
9. Write a C program using dynamic variables & pointers, to construct an ordered(ascending) singly linked list based on the rank of the student, where each node consists of the following information : Student id?(integer), Student name(character string), & rank(integer). #include<stdio.h> #include<conio.h> #include<alloc.h> struct node { int info; struct node *link; }; typedef struct node NODE; NODE *start=NULL; void disp_list(); void insert_beg(); void delete_beg(); void main() M.S.E.C Dept Of MCA Page 43
int choice; clrscr(); do { printf("\n*************** Stack Operations **************\n"); printf("\t\t 1-> PUSH\n"); printf("\t\t 2-> POP\n"); printf("\t\t 3-> DISPLAY\n"); printf("\t\t 4-> EXIT\n"); printf("\t\t Enter your choice:"); scanf("%d",&choice); switch(choice) { case 1: insert_beg(); break; case 2: if(start!=NULL) printf("\t\t The deleted element is %d:",start->info); delete_beg(); disp_list(); break; case 3: disp_list(); break; case 4: exit(0); break;
M.S.E.C
Dept Of MCA
Page 44
} while(choice!=5); getch(); }
void disp_list() { NODE *currptr=start; if(start==NULL) printf("\t\t Stack is empty\n"); else { printf("\n\t\t The elements of stack are as follows\n"); while(currptr!=NULL) { printf("\t\t %d\t->\n",currptr->info); currptr=currptr->link; } printf("\t\t NULL"); }
M.S.E.C
Dept Of MCA
Page 45
void insert_beg() { NODE *newnode; newnode=(NODE*)malloc(sizeof(NODE)); printf("\t\t Enter element:"); scanf("%d",&newnode->info); newnode->link=start; start=newnode; }
void delete_beg() { NODE *currptr; if(start==NULL) printf("\t\t Stack Underflows\n"); else { currptr=start; start=start->link; free(currptr); } } /*---------------------------------OUTPUT----------------------------------------------
M.S.E.C
Dept Of MCA
Page 46
*************** Stack Operations ************** 1-> PUSH 2-> POP 3-> DISPLAY 4-> EXIT Enter your choice:1 Enter element:34
*************** Stack Operations ************** 1-> PUSH 2-> POP 3-> DISPLAY 4-> EXIT Enter your choice:2 The deleted element is 34: The elements of stack are as follows 23 12 -> ->
NULL *************** Stack Operations ************** 1-> PUSH 2-> POP 3-> DISPLAY 4-> EXIT Enter your choice:1
M.S.E.C
Dept Of MCA
Page 47
*************** Stack Operations ************** 1-> PUSH 2-> POP 3-> DISPLAY 4-> EXIT Enter your choice:3
NULL *************** Stack Operations ************** 1-> PUSH 2-> POP 3-> DISPLAY 4-> EXIT Enter your choice:4 ---------------------------------------------------------------------------------------------*/
M.S.E.C
Dept Of MCA
Page 48
10. Write a C program using dynamic variables & pointers to construct a stack of integers using singly linked list & to perform the following operations: a. Push b. Pop. c. Display The program should print appropriate messages for stack overflow & stack empty. #include<stdio.h> #include<conio.h> #include<alloc.h> struct node { int info; struct node *link; }; typedef struct node NODE; NODE *front=NULL,*rear=NULL,*newnode; void disp_list(); M.S.E.C Dept Of MCA Page 49
int choice; clrscr(); do { printf("\n*************** Queue Operations **************\n"); printf("\t\t 1-> Insert\n"); printf("\t\t 2-> Delete\n"); printf("\t\t 3-> Display\n"); printf("\t\t 4-> exit\n"); printf("\t\t Enter your choice:"); scanf("%d",&choice); switch(choice) { case 1: insert_end(); break; case 2: delete_beg(); break; case 3: disp_list(); break; case 4: exit(0); break;
M.S.E.C
Dept Of MCA
Page 50
} while(choice!=5); getch(); }
void disp_list() { NODE *currptr=front; if(front==NULL) printf("\t\t Queue is empty\n"); else { printf("\n\t\t The elements of queue are as follows\n"); printf("\t\t"); while(currptr!=NULL) { printf(" %d\t->",currptr->info); currptr=currptr->link; } printf("\t NULL\n");
M.S.E.C
Dept Of MCA
Page 51
void insert_end() { if(rear==NULL) { newnode=(NODE*)malloc(sizeof(NODE)); printf("\t\t Enter element:"); scanf("%d",&newnode->info); front=newnode; newnode->link=NULL; rear=newnode; } else { newnode=(NODE*)malloc(sizeof(NODE)); printf("\t\t Enter element:"); scanf("%d",&newnode->info); newnode->link=NULL; rear->link=newnode; rear=newnode; } }
void delete_beg()
M.S.E.C
Dept Of MCA
Page 52
*/--------------------------------output-----------------------------------
M.S.E.C
Dept Of MCA
Page 53
After deletion
*************** Queue Operations ************** 1-> Insert 2-> Delete 3-> Display 4-> exit Enter your choice:2 Status of Queue before deletion
M.S.E.C
Dept Of MCA
Page 54
*************** Queue Operations ************** 1-> Insert 2-> Delete 3-> Display 4-> exit Enter your choice:3 Queue is empty
*************** Queue Operations ************** 1-> Insert 2-> Delete 3-> Display 4-> exit Enter your choice:4 ------------------------------------------------------------------------------------------------------*/
M.S.E.C
Dept Of MCA
Page 55
11. Write a C program to support the following operations on a doubly linked list where each node consists of integers: a. Create a doubly linked list by adding each node at the front. b. Insert a new node to the left of the node whose key value is read as an input. c. Delete the node of a given data, if it is found, otherwise display appropriate message. d. Display the contents of the list. (Note: only either(a, b & d) or (a, c & d) may be asked in the examination)
#include<stdio.h> #include<conio.h> #include<alloc.h> struct node { int info; struct node *back; struct node *forw; }; M.S.E.C Dept Of MCA Page 56
int i,n,choice; clrscr(); do { printf("\n*************** Doubly Linked List Operations **************\n"); printf("\t\t 1-> Insert at front\n"); printf("\t\t 2-> Insert Left\n"); printf("\t\t 3-> Delete\n"); printf("\t\t 4-> Display\n"); printf("\t\t 5-> Exit\n"); printf("\t\t Enter your choice:"); scanf("%d",&choice); switch(choice) { case 1: start=create(start); break; case 2: insert_node();
M.S.E.C
Dept Of MCA
Page 57
void delete_node() { int item,found=0; if(start==NULL) printf("\t\t The linked list is empty\n");
printf("\t\t Enter the data of the node to be deleted:"); scanf("%d",&item); temp=start; while(temp!=NULL) { if(temp->info==item)
M.S.E.C
Dept Of MCA
Page 58
M.S.E.C
Dept Of MCA
Page 59
if(found==1) printf("\t\t Item found and deleted\n"); else printf("\t\t Item not found\n"); }
NODE *create(NODE *currptr) { // NODE *newnode; newnode=(NODE*)malloc(sizeof(NODE)); printf("\t\t Read the information for the newnode:"); scanf("%d",&newnode->info); if(currptr==NULL) { newnode->back=NULL; newnode->forw=NULL; return(newnode); } else { newnode->forw=currptr;
M.S.E.C
Dept Of MCA
Page 60
void disp_list(NODE *currptr) { if(currptr==NULL) printf("\t\t NULL"); else { printf("\t\t The elements of list are as follows\n"); while(currptr!=NULL) { printf("\t\t %4d",currptr->info); currptr=currptr->forw; } } }
M.S.E.C
Dept Of MCA
Page 61
M.S.E.C
Dept Of MCA
Page 62
/* ----------------------------------output------------------------------------
*************** Doubly Linked List Operations ************** 1-> Insert at front 2-> Insert Left 3-> Delete 4-> Display 5-> Exit Enter your choice:1 Read the information for the newnode:12
*************** Doubly Linked List Operations ************** 1-> Insert at front 2-> Insert Left 3-> Delete 4-> Display 5-> Exit Enter your choice:1 Read the information for the newnode:22
M.S.E.C
Dept Of MCA
Page 63
*************** Doubly Linked List Operations ************** 1-> Insert at front 2-> Insert Left 3-> Delete 4-> Display 5-> Exit Enter your choice:4 The elements of list are as follows 33 22 12
*************** Doubly Linked List Operations ************** 1-> Insert at front 2-> Insert Left 3-> Delete 4-> Display 5-> Exit Enter your choice:5
M.S.E.C
Dept Of MCA
Page 64
-----------------------------------------------------------------------------------*/
12.Write a C program a. To construct a binary search tree of integers. b. To traverse the tree using all the methods i.e.inorder, preorder & postorder. c. To display the elements in the tree. #include<stdio.h> #include<conio.h> #include<alloc.h> #include<ctype.h> struct node { int info; struct node *left; struct node *right; }; typedef struct node NODE; NODE *root=NULL;
M.S.E.C
Dept Of MCA
Page 65
int i,n,choice; clrscr(); do { printf("\n*************** BST and Traversal Operations **************\n"); printf("\t\t 1-> Create BST\n"); printf("\t\t 2-> Inorder\n"); printf("\t\t 3-> Preorder\n"); printf("\t\t 4-> Postorder\n"); printf("\t\t 5-> Display\n"); printf("\t\t 6-> Exit\n"); printf("\t\t Enter your choice:"); scanf("%d",&choice); switch(choice) { case 1: create_bst(); break;
M.S.E.C
Dept Of MCA
Page 66
void create_bst() { char ch; NODE *newnode,*currptr,*temp; do { newnode=(NODE*)malloc(sizeof(NODE)); printf("\t\t Read the information for the newnode:"); scanf("%d",&newnode->info);
M.S.E.C
Dept Of MCA
Page 67
M.S.E.C
Dept Of MCA
Page 68
void display_tree(NODE *ptr,int level) { int i; if(ptr!=NULL) { display_tree(ptr->right,level+1); for(i=0;i<level;i++) printf(" "); printf("%2d\n",ptr->info); display_tree(ptr->left,level+1); } }
M.S.E.C
Dept Of MCA
Page 69
/*
*************** BST and Traversal Operations ************** 1-> Create BST 2-> Inorder 3-> Preorder 4-> Postorder
M.S.E.C
Dept Of MCA
Page 70
*************** BST and Traversal Operations ************** 1-> Create BST 2-> Inorder 3-> Preorder 4-> Postorder 5-> Display 6-> Exit Enter your choice:4 1312 *************** BST and Traversal Operations ************** 1-> Create BST 2-> Inorder 3-> Preorder 4-> Postorder 5-> Display 6-> Exit Enter your choice:3 1213
M.S.E.C
Dept Of MCA
Page 71
M.S.E.C
Dept Of MCA
Page 72
13. Write a C program for searching an element on a given list of integers using the a. Binary Search. b. Linear Search. #include<stdio.h> #include<conio.h> #include<process.h> void l_search(int a[],int); void b_search(int a[],int); void sort(int a[],int); void main() { int ch,i,n,a[50],key; clrscr(); printf("Enter the total no of elements:"); scanf("%d",&n); printf("Enter the elements one by one:\n");
M.S.E.C
Dept Of MCA
Page 73
void l_search(int a[],int n) { int i,flag=0,key; printf("Enter the key element:"); scanf("%d",&key);
M.S.E.C
Dept Of MCA
Page 74
void b_search(int a[],int n) { int mid,first,last,key,i; sort(a,n); printf("The elements in sorted order\n"); for(i=0;i<n;i++) printf("%d\n",a[i]); printf("Enter the key element:"); scanf("%d",&key); first=0;last=n-1; mid=(first+last)/2; while(key!=a[mid] && first<=last) { if(key>a[mid]) first=mid+1;
M.S.E.C
Dept Of MCA
Page 75
mid=(first+last)/2; } if(key==a[mid]) printf("%d is found at %d location\n",key,mid+1); if(first>last) printf("%d is not found in the given list\n",key); }
void sort(int a[],int n) { int temp,p,j; for(p=1;p<n;p++) for(j=0;j<=n-p-1;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } }
M.S.E.C
Dept Of MCA
Page 76
M.S.E.C
Dept Of MCA
Page 77
M.S.E.C
Dept Of MCA
Page 78
14. write a C program to sort a list of N integers using the quick sort algorithm. #include<stdio.h> #include<conio.h> void quick_sort(int a[],int low,int high); int partition(int a[],int low,int high); void main() { int i,n,a[20];
M.S.E.C
Dept Of MCA
Page 79
void quick_sort(int a[],int low,int high) { int j; if(low<high) { j=partition(a,low,high); quick_sort(a,low,j-1); quick_sort(a,j+1,high); } } int partition(int a[],int low,int high) {
M.S.E.C
Dept Of MCA
Page 80
M.S.E.C
Dept Of MCA
Page 81
Enter the total no of elements 5 Enter the elements one by one 4 6 8 2 9 The sorted elements are 2 4 6 8 9
------------------------------------------------------------*/ 15. Write a C program to traverse the nodes in a graph using Breath First Search. #include<stdio.h> int a[10][10],q[10],visited[10]; int i,j,n,v; int f=0,r=1; void bfsearch(int v) { for(i=0;i<=n;i++)
M.S.E.C
Dept Of MCA
Page 82
void main() { clrscr(); printf("\n Enter the vertexs:"); scanf("%d",&n); for(i=1;i<=n;i++) { visited[i]=0; q[i]=0; } printf("\nEnter the graphical order moment:"); for(i=1;i<=n;i++) for(j=1;j<=n;j++) { printf("\n The sdjecent of %d b/w %d is: ", i,j); scanf("%d",&a[i][j]); }
M.S.E.C
Dept Of MCA
Page 83
M.S.E.C
Dept Of MCA
Page 84
M.S.E.C
Dept Of MCA
Page 85
M.S.E.C
Dept Of MCA
Page 86