10MCA27-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
10MCA27-DATA STRUCTURES LAB USING C
if(top==MAXSIZE-1) printf("\t\tStack Overflows\n"); else { top=top+1; printf("\t\tEnter the element:"); scanf("%d",&item); stk[top]=item; } } void pop() { int item; if(top==-1) printf("\t\tStack Underflows\n"); else { item=stk[top]; top=top-1; printf("\t\tThe deleted element is %d:\n",item); } } void display() { int i; if(top==-1) printf("\t\tStack Underflows\n:"); else { printf("\t\tThe elements in stack are\n"); for(i=top;i>=0;i--) printf("\t\t%d\n",stk[i]); } } OUTPUT: *************** Stack Operations ************** 1-> Push 2-> Pop 3-> Display 4-> Exit Enter your choice:2 Stack underflows *************** Stack Operations ************** 1-> Push 2-> Pop 3-> Display 4-> Exit Enter your choice:3 Stack Underflows M.S.E.C Dept Of MCA Page 2
10MCA27-DATA STRUCTURES LAB USING C
*************** Stack Operations ************** 1-> Push 2-> Pop 3-> Display 4-> Exit Enter your choice:1 Enter the element:10 *************** Stack Operations ************** 1-> Push 2-> Pop 3-> Display 4-> Exit Enter your choice:1 Enter the element:20 *************** Stack Operations ************** 1-> Push 2-> Pop 3-> Display 4-> Exit Enter your choice:1 Enter the element:30 *************** Stack Operations ************** 1-> Push 2-> Pop 3-> Display 4-> Exit Enter your choice:1 Stack Overflows *************** Stack Operations ************** 1-> Push 2-> Pop 3-> Display 4-> Exit Enter your choice:3 The elements in stack are 30 20 10 *************** Stack Operations ************** 1-> Push 2-> Pop 3-> Display 4-> Exit Enter your choice:2 The deleted element is 30: *************** Stack Operations ************** 1-> Push 2-> Pop 3-> Display 4-> Exit M.S.E.C Dept Of MCA Page 3
10MCA27-DATA STRUCTURES LAB USING C
Enter your choice:3 The elements in stack are 20 10 *************** Stack Operations ************** 1-> Push 2-> Pop 3-> Display 4-> Exit Enter your choice:4 */
M.S.E.C
Dept Of MCA
Page 4
10MCA27-DATA STRUCTURES LAB USING C
2. Write a C program to convert & print a given valid parenthesized infix arithmetic expression to postfix expression. The expression consists of single character operands & the binary operations +(plus), -(minus), *(multiply), & /(divide). #include<stdio.h> #include<conio.h> #include<ctype.h> #include<stdlib.h> #define MAXSIZE 25 void push(char); char pop(); int precedence(char); int stk[MAXSIZE],top=-1; void main() { char infix[MAXSIZE],postfix[MAXSIZE],ch,elem; int i=0,j=0; clrscr(); printf("\n\t\t Program to convert infix to postfix expression\n"); printf("\t\t ----------------------------------------------\n"); printf("\t\t Enter the infix expression\n\t\t"); scanf("%s",&infix); push('#'); for(i=0;i<strlen(infix);i++) { ch=infix[i]; if(isalnum(ch)) postfix[j++]=ch; else if(ch=='(') push(ch); else if(ch==')') { while(stk[top]!='(') postfix[j++]=pop(); elem=pop(); } else { while(precedence(stk[top])>=precedence(ch)) postfix[j++]=pop(); push(ch); } }
M.S.E.C
Dept Of MCA
Page 5
10MCA27-DATA STRUCTURES LAB USING C
while(stk[top]!='#') postfix[j++]=pop(); postfix[j]='\0'; printf("\t\t The converted postfix expression is %s",postfix); getch(); } void push(char item) { stk[++top]=item; } char pop() { return(stk[top--]); } int precedence(char item) { switch(item) { case '+': case '-': return(1); case '*': case '/': return(2); case '^': return(3); case '(': case '#': return(0); } }
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
10MCA27-DATA STRUCTURES LAB USING C
3.Write a C program to evaluate a valid suffix/postfix expression using stack. Assume that the suffix/postfix expression is read as a single line consisting of non-negative single digit operands & binary arithmetic operators. The arithmetic operators are +(add), -(subtract), *(multiply), &/(divide). #include<stdio.h> #include<conio.h> #include<ctype.h> #include<stdlib.h> #include<math.h> #define MAXSIZE 25 void push(int); int pop(); int stk[MAXSIZE],top=-1; void main() { char postfix[MAXSIZE],ch; int i,op1,op2,res; clrscr(); printf("\n\t\t Program to evaluate postfix expression\n"); printf("\t\t-----------------------------------------\n"); printf("\t\t Enter the postfix expression\n\t\t"); scanf("%s",&postfix); for(i=0;i<strlen(postfix);i++) { ch=postfix[i]; if(isdigit(ch)) push(ch-'0'); else { op2=pop(); op1=pop(); switch(ch) { case '+' : res=op1+op2; break; case '-' : res=op1-op2; break; case '*' : res=op1*op2; break; case '/' : res=op1/op2; break; case '^' : res=pow(op1,op2); break; M.S.E.C Dept Of MCA Page 7
10MCA27-DATA STRUCTURES LAB USING C
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
10MCA27-DATA STRUCTURES LAB USING C
4. Write a C program using recursive function for the following: a. To calculate GCD & LCM of 2 integer numbers. b. To solve Towers of Hanoi problem. c. To search an element in a list using binary search. a. To calculate GCD & LCM of 2 integer numbers. #include<stdio.h> #include<conio.h> void main() { int m,n; clrscr(); printf("Enter two numbers\n"); scanf("%d%d",&m,&n); printf("The GCD(%d,%d)=%d LCM(%d,%d)=%d\n",m,n,gcd(m,n),m,n,(m*n)/gcd(m,n)); getch(); } int gcd(int m,int n) { if(n==0) return m; else if(n>m) return(gcd(n,m)); else return(gcd(n,m%n)); } /* Enter two numbers 10 5 The GCD(10,5)=5 LCM(10,5)=10 * /
M.S.E.C
Dept Of MCA
Page 9
10MCA27-DATA STRUCTURES LAB USING C
b. To solve Towers of Hanoi problem. #include<stdio.h> #include<conio.h> void toh(int,char,char,char); void main() { char source='S',temp='T',dest='D'; int n; clrscr(); printf("Enter the number of disks\n"); scanf("%d",&n); printf("The sequence of movement of disks from source to destination\n"); toh(n,source,temp,dest); getch(); } void toh(int n,char source,char temp,char dest) { if(n>0) { toh(n-1,source,dest,temp); printf("Move disk %d from %c --> %c\n",n,source,dest); toh(n-1,temp,source,dest); } } *****************OUTPUT********************** Enter the number of disks 3 The sequence of movement of disks from source to destination Move disk 1 from S --> D Move disk 2 from S --> T Move disk 1 from D --> T Move disk 3 from S --> D Move disk 1 from T --> S Move disk 2 from T --> D Move disk 1 from S --> D
M.S.E.C
Dept Of MCA
Page 10
10MCA27-DATA STRUCTURES LAB USING C
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
10MCA27-DATA STRUCTURES LAB USING C
if(result==-1) printf("%d is not found in the given set of elements\n"); else printf("%d is found at %d location",key,result+1); getch(); }
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; } }
/* ******************OUTPUT******************* Enter the total no of elements
M.S.E.C
Dept Of MCA
Page 12
10MCA27-DATA STRUCTURES LAB USING C
5 Enter the elements one by one 14 21 29 35 76 Enter search element 35 35 is found at 4 location */ 5. Write a C program to simulate the working of a queue of integers using an array. Provide the following operations: a. Insert b. Delete c. Display
#include<stdio.h> #include<conio.h> #define MAXSIZE 4 void qinsert(); void qdelete(); void display(); int queue[MAXSIZE],front=0,rear=-1,item;
void main() { char ch; M.S.E.C Dept Of MCA Page 13
10MCA27-DATA STRUCTURES LAB USING C
int choice; clrscr(); do { printf("\n*************** Queue Operations **************\n"); printf("\t\t 1-> QInsert\n"); printf("\t\t 2-> QDelete\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: qinsert(); break; case 2: qdelete(); break; case 3: display(); break; case 4: exit(0); break; default: printf("\t\tInvalid choice\n"); break; } printf("\t\tDo you want to continue(type y or n):"); fflush(stdin);
M.S.E.C
Dept Of MCA
Page 14
10MCA27-DATA STRUCTURES LAB USING C
scanf("%c",&ch);
} 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
10MCA27-DATA STRUCTURES LAB USING C
printf("\t\t This is the last element in the queue\n"); printf("\t\t The deleted element is %d:\n",queue[front]); front=0; rear=-1; } else { printf("\t\t The deleted element is %d\n",queue[front]); front++; } }
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
10MCA27-DATA STRUCTURES LAB USING C
*/--------------------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
10MCA27-DATA STRUCTURES LAB USING C
Enter your choice:3 The elements in Queue are 4 45
Do you want to continue(type y or n):y
*************** 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
10MCA27-DATA STRUCTURES LAB USING C
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
10MCA27-DATA STRUCTURES LAB USING C
clrscr(); do { printf("\n*************** Circular Queue Operations **************\n"); printf("\t\t 1-> CQInsert\n"); printf("\t\t 2-> CQDelete\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: cqinsert(); break; case 2: cqdelete(); break; case 3: display(); break; case 4: exit(0); break; default: printf("\t\tInvalid choice\n"); break; } printf("\t\tDo you want to continue(type y or n):"); fflush(stdin); scanf("%c",&ch);
M.S.E.C
Dept Of MCA
Page 20
10MCA27-DATA STRUCTURES LAB USING C
} 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
10MCA27-DATA STRUCTURES LAB USING C
else { printf("\t\t The deleted element is %d:\n",cqueue[front]); cqueue[front]=NULL; if(front==rear) front=rear=-1; else front=(front+1)%N; } }
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
10MCA27-DATA STRUCTURES LAB USING C
{ for(i=front;i<=N-1;i++) printf("\t%d",cqueue[i]); for(i=0;i<=rear;i++) printf("\t%d",cqueue[i]); } } printf("\n"); }
*/*************** 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
10MCA27-DATA STRUCTURES LAB USING C
Do you want to continue(type y or n):y
*************** 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
10MCA27-DATA STRUCTURES LAB USING C
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
10MCA27-DATA STRUCTURES LAB USING C
int choice; clrscr(); do { printf("\n*************** Queue Operations **************\n"); printf("\t\t 1-> PQInsert\n"); printf("\t\t 2-> PQDelete\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: printf("\t\t Enter the priority:"); scanf("%d",&pr); if(pr>0 && pr<4) pqinsert(pr-1); else printf("\t\t Invalid priority\n"); break; case 2: pqdelete(); break; case 3: display(); break; case 4: exit(0); break;
M.S.E.C
Dept Of MCA
Page 26
10MCA27-DATA STRUCTURES LAB USING C
default: printf("\t\tInvalid choice\n"); break; } printf("\t\tDo you want to continue(type y or n):"); fflush(stdin); scanf("%c",&ch);
} 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
10MCA27-DATA STRUCTURES LAB USING C
static int i=0 ; if(rear[i]==front[i]-1) { printf("\t\t Queue %d Underflows\n",i+1); i++; } else { printf("\t\t The deleted element is %d of queue %d:\n",pqueue[i][front[i]],i+1); front[i]++; } } }
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
10MCA27-DATA STRUCTURES LAB USING C
printf("\t%d",pqueue[i][j]); printf("\n"); } } }
/* 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
*************** Queue Operations ************** 1-> PQInsert
M.S.E.C
Dept Of MCA
Page 29
10MCA27-DATA STRUCTURES LAB USING C
2-> PQDelete 3-> Display 4-> Exit Enter your choice:1 Enter the priority:3 Enter the element:25 Do you want to continue(type y or n):y
*************** 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
10MCA27-DATA STRUCTURES LAB USING C
15 87
The elements in Queue 3 are 25 Do you want to continue(type y or n):y */
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)
#include<stdio.h> #include<conio.h> #include<alloc.h> struct node {
M.S.E.C
Dept Of MCA
Page 31
10MCA27-DATA STRUCTURES LAB USING C
int id; int sem; char name[20]; struct node *link; }; typedef struct node NODE; NODE *start; void create_list(NODE *); void disp_list(NODE*); NODE *insert_beg(NODE*); void insert_end(NODE*); void insert_node(); void insert_pos(NODE*); void delete_node(); void search_list(); void main() {
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
10MCA27-DATA STRUCTURES LAB USING C
printf("\t\t 3-> Deletion\n"); printf("\t\t 4-> Search\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_list(start); break; case 2: insert_node(); break; case 3: delete_node(); break; case 4: search_list(); break; case 5: disp_list(start); break; case 6: exit(0); break; default: printf("\t\tInvalid choice\n"); break; } /* printf("\t\tDo you want to continue(type y or n):"); fflush(stdin); scanf("%c",&ch); */
M.S.E.C
Dept Of MCA
Page 33
10MCA27-DATA STRUCTURES LAB USING C
} 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
10MCA27-DATA STRUCTURES LAB USING C
case 4: return; default: printf("\t\t Invalid choice\n"); } } }
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
10MCA27-DATA STRUCTURES LAB USING C
if(temp->id==item) { found=1; currptr->link=temp->link; } currptr=currptr->link; } } if(found==1) printf("\t\t Item found and deleted\n"); else printf("\t\t Item not found\n"); }
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
10MCA27-DATA STRUCTURES LAB USING C
} temp=temp->link; count++; } if(found==1) printf("\t\t Item found at %d position\n",count); else printf("\t\t item not found in the list\n");
} 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
10MCA27-DATA STRUCTURES LAB USING C
{ newnode=(NODE*)malloc(sizeof(NODE)); currptr->link=newnode; currptr=newnode; } else { currptr->link=NULL; break; } } } void disp_list(NODE *currptr) { printf("\t\t The elements of list are as follows\n"); printf("\t\t Id\t Name\t Sem\n"); while(currptr!=NULL) { printf("\t\t %d\t %s\t %d ->\n",currptr->id,currptr->name,currptr->sem); currptr=currptr->link; } printf("\t\t NULL"); } NODE *insert_beg(NODE *currptr) { NODE *newnode;
M.S.E.C
Dept Of MCA
Page 38
10MCA27-DATA STRUCTURES LAB USING C
newnode=(NODE*)malloc(sizeof(NODE)); printf("\t\t Enter 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); newnode->link=currptr; currptr=newnode; return(currptr); }
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
10MCA27-DATA STRUCTURES LAB USING C
currptr->link=newnode; newnode->link=NULL; }
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
10MCA27-DATA STRUCTURES LAB USING C
*/-------------------------------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
10MCA27-DATA STRUCTURES LAB USING C
Enter your choice:5 The elements of list are as follows Id 1 2 Name Sem FA DA 2 -> 2 ->
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
10MCA27-DATA STRUCTURES LAB USING C
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
10MCA27-DATA STRUCTURES LAB USING C
{
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
10MCA27-DATA STRUCTURES LAB USING C
default: printf("\t\tInvalid choice\n"); break; } /* printf("\t\tDo you want to continue(type y or n):"); fflush(stdin); scanf("%c",&ch); */
} 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
10MCA27-DATA STRUCTURES LAB USING C
}
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
10MCA27-DATA STRUCTURES LAB USING C
*************** 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
10MCA27-DATA STRUCTURES LAB USING C
Enter element:6
*************** Stack Operations ************** 1-> PUSH 2-> POP 3-> DISPLAY 4-> EXIT Enter your choice:3
The elements of stack are as follows 6 23 12 -> -> ->
NULL *************** Stack Operations ************** 1-> PUSH 2-> POP 3-> DISPLAY 4-> EXIT Enter your choice:4 ---------------------------------------------------------------------------------------------*/
M.S.E.C
Dept Of MCA
Page 48
10MCA27-DATA STRUCTURES LAB USING C
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
10MCA27-DATA STRUCTURES LAB USING C
void insert_end(); void delete_beg(); void main() {
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
10MCA27-DATA STRUCTURES LAB USING C
default: printf("\t\tInvalid choice\n"); break; } /* printf("\t\tDo you want to continue(type y or n):"); fflush(stdin); scanf("%c",&ch); */
} 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
10MCA27-DATA STRUCTURES LAB USING C
} }
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
10MCA27-DATA STRUCTURES LAB USING C
{ NODE *currptr; if(front==NULL) printf("\t\t Queue is empty\n"); printf("\t\t Status of Queue before deletion\n"); disp_list(); if(front==rear) { front=rear=NULL; printf("\t\t After deletion\n"); printf("\t\t NULL\n"); } else { front=front->link; printf("\t\t After deletion\n"); disp_list(); printf("\t\t Front of the queue is %d\n",front->info); printf("\t\t Rear of the queue is %d\n",rear->info); } }
*/--------------------------------output-----------------------------------
*************** Queue Operations **************
M.S.E.C
Dept Of MCA
Page 53
10MCA27-DATA STRUCTURES LAB USING C
1-> Insert 2-> Delete 3-> Display 4-> exit Enter your choice:2 Status of Queue before deletion
The elements of queue are as follows 4 -> 5 -> NULL
After deletion
The elements of queue are as follows 5 -> NULL
Front of the queue is 5 Rear of the queue is 5
*************** Queue Operations ************** 1-> Insert 2-> Delete 3-> Display 4-> exit Enter your choice:2 Status of Queue before deletion
The elements of queue are as follows 5 -> NULL
M.S.E.C
Dept Of MCA
Page 54
10MCA27-DATA STRUCTURES LAB USING C
After deletion NULL
*************** 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
10MCA27-DATA STRUCTURES LAB USING C
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
10MCA27-DATA STRUCTURES LAB USING C
typedef struct node NODE; NODE *start=NULL,*currptr,*newnode,*temp,*save; NODE *create(NODE*); void insert_node(); void delete_node(); void disp_list(NODE*); void main() {
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
10MCA27-DATA STRUCTURES LAB USING C
break; case 3: delete_node(); break; case 4: disp_list(start); break; case 5: exit(0); break; default: printf("\t\tInvalid choice\n"); break; } } while(choice!=6); getch(); }
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
10MCA27-DATA STRUCTURES LAB USING C
{ found=1; if(item==start->info) if(start->forw==NULL) { save=start; free(save); start=NULL; } else { if(start->back==NULL) { save=start; start->back->forw=NULL; start=start->forw; free(save); } } else { save=temp; temp->back->forw=temp->forw; temp->forw->back=temp->back; free(save); }
M.S.E.C
Dept Of MCA
Page 59
10MCA27-DATA STRUCTURES LAB USING C
} temp=temp->forw; }
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
10MCA27-DATA STRUCTURES LAB USING C
newnode->back=NULL; currptr->back=newnode; currptr=newnode; return(currptr); } }
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; } } }
void insert_node() { int key,item; newnode=(NODE*)malloc(sizeof(NODE));
M.S.E.C
Dept Of MCA
Page 61
10MCA27-DATA STRUCTURES LAB USING C
printf("\t\t Enter the key item:"); scanf("%d",&key); printf("\t\t Enter the data for newnode:"); scanf("%d",&newnode->info); if(key==start->info) { newnode->back=NULL; newnode->forw=start; start->back=newnode; start=newnode; } else { currptr=start; while(currptr->info!=key && currptr!=NULL) { currptr=currptr->forw; } if(currptr==NULL) printf("\t\t Key element is not found in the given list\n"); else { newnode->forw=currptr; newnode->back=currptr->back; currptr->back->forw=newnode; currptr->back=newnode;
M.S.E.C
Dept Of MCA
Page 62
10MCA27-DATA STRUCTURES LAB USING C
} } }
/* ----------------------------------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
10MCA27-DATA STRUCTURES LAB USING C
*************** Doubly Linked List Operations ************** 1-> Insert at front 2-> Insert Left 3-> Delete 4-> Display 5-> Exit Enter your choice:2 Enter the key item:22 Enter the data for newnode:33
*************** 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
10MCA27-DATA STRUCTURES LAB USING C
-----------------------------------------------------------------------------------*/
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
10MCA27-DATA STRUCTURES LAB USING C
void create_bst(); //void insert_node(); void display_tree(NODE*,int); void preorder(NODE*); void postorder(NODE*); void inorder(NODE*); void main() {
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
10MCA27-DATA STRUCTURES LAB USING C
case 2: inorder(root); break; case 3: preorder(root); break; case 4: postorder(root); break; case 5: display_tree(root,1); break; case 6: exit(0); default: printf("\t\tInvalid choice\n"); break; } } while(choice!=6); getch(); }
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
10MCA27-DATA STRUCTURES LAB USING C
newnode->left=NULL; newnode->right=NULL; if(root==NULL) { root=newnode; } else { currptr=root; while(currptr!=NULL) { temp=currptr; currptr=(newnode->info>currptr->info)?currptr->right:currptr->left; } if(newnode->info>temp->info) temp->right=newnode; else temp->left=newnode; } printf("\t\t Do you want to add another node(type y):"); fflush(stdin); ch=getchar(); }while(toupper(ch)=='Y');
M.S.E.C
Dept Of MCA
Page 68
10MCA27-DATA STRUCTURES LAB USING C
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); } }
void preorder(NODE *ptr) { if(ptr!=NULL) { printf("%2d",ptr->info); preorder(ptr->left); preorder(ptr->right); } }
void inorder(NODE *ptr) {
M.S.E.C
Dept Of MCA
Page 69
10MCA27-DATA STRUCTURES LAB USING C
if(ptr) { inorder(ptr->left); printf("%2d",ptr->info); inorder(ptr->right); } }
void postorder(NODE *ptr) { if(ptr) { postorder(ptr->left); postorder(ptr->right); printf("%2d",ptr->info); } }
/*
*************** BST and Traversal Operations ************** 1-> Create BST 2-> Inorder 3-> Preorder 4-> Postorder
M.S.E.C
Dept Of MCA
Page 70
10MCA27-DATA STRUCTURES LAB USING C
5-> Display 6-> Exit Enter your choice:1 Read the information for the newnode:12 Do you want to add another node(type y):y Read the information for the newnode:13 Do you want to add another node(type y):n
*************** 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
10MCA27-DATA STRUCTURES LAB USING C
*************** BST and Traversal Operations ************** 1-> Create BST 2-> Inorder 3-> Preorder 4-> Postorder 5-> Display 6-> Exit Enter your choice:2 1213 *************** BST and Traversal Operations ************** 1-> Create BST 2-> In order 3-> Preorder 4-> Post order 5-> Display 6-> Exit Enter your choice:6
M.S.E.C
Dept Of MCA
Page 72
10MCA27-DATA STRUCTURES LAB USING C
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
10MCA27-DATA STRUCTURES LAB USING C
for(i=0;i<n;i++) scanf("%d",&a[i]); while(1) { printf("\t 1->Linear Search\n"); printf("\t 2->Binary Search\n"); printf("\t 3->Exit\n"); printf("\t Enter your choice:"); scanf("%d",&ch); switch(ch) { case 1:l_search(a,n); break; case 2:b_search(a,n); break; case 3:exit(0); default: printf("Invalid choice\n"); } } }
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
10MCA27-DATA STRUCTURES LAB USING C
for(i=0;i<n;i++) if(a[i]==key) { printf("The element %d is found at %d position\n",key,i+1); flag=1; break; } if(flag==0) printf("The element %d is not found\n",key); }
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
10MCA27-DATA STRUCTURES LAB USING C
else last=mid-1;
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
10MCA27-DATA STRUCTURES LAB USING C
/* ***************OUTPUT******************** Enter the total no of elements:5 Enter the elements one by one: 13 25 9 59 37 1->Linear Search 2->Binary Search 3->Exit Enter your choice:2 The elements in sorted order 9 13 25 37 59 Enter the key element:37 37 is found at 4 location 1->Linear Search 2->Binary Search 3->Exit Enter your choice:2 The elements in sorted order 9
M.S.E.C
Dept Of MCA
Page 77
10MCA27-DATA STRUCTURES LAB USING C
13 25 37 59 Enter the key element:45 45 is not found in the given list 1->Linear Search 2->Binary Search 3->Exit Enter your choice:1 Enter the key element:29 The element 29 is not found 1->Linear Search 2->Binary Search 3->Exit Enter your choice:1 Enter the key element:13 The element 13 is found at 2 position 1->Linear Search 2->Binary Search 3->Exit Enter your choice:3
M.S.E.C
Dept Of MCA
Page 78
10MCA27-DATA STRUCTURES LAB USING C
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
10MCA27-DATA STRUCTURES LAB USING C
float f; clrscr(); printf("\n 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]); quick_sort(a,0,n-1); printf("The sorted elements are\n"); for(i=0;i<n;i++) printf("%d\n",a[i]); getch(); }
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
10MCA27-DATA STRUCTURES LAB USING C
int i,j,temp,key; key=a[low]; i=low+1; j=high; while(1) { while(i<high && key >= a[i]) i++; while(key<a[j]) j--; if(i<j) { temp=a[i]; a[i]=a[j]; a[j]=temp; } else { temp=a[low]; a[low]=a[j]; a[j]=temp; return j; } } }
M.S.E.C
Dept Of MCA
Page 81
10MCA27-DATA STRUCTURES LAB USING C
/*------------------------------output--------------------------
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
10MCA27-DATA STRUCTURES LAB USING C
if(a[v][i]&&!visited[i]) q[++r]=i; if(f<=r) { visited[q[f]]=1; bfsearch(q[f++]); } }
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
10MCA27-DATA STRUCTURES LAB USING C
printf("\n Enter the starting vertex: "); scanf("%d",&v); bfsearch(v); printf("\n The vertex visite at nodes: \n"); for(i=1;i<=n;i++) if(visited[i]) printf("%d\t",i); getch(); } ********************OUTPUT*************** Enter the vertexs:3
Enter the graphical order moment: The sdjecent of 1 b/w 1 is: 0
The sdjecent of 1 b/w 2 is: 1
The sdjecent of 1 b/w 3 is: 1
The sdjecent of 2 b/w 1 is: 1
The sdjecent of 2 b/w 2 is: 0
The sdjecent of 2 b/w 3 is: 1
The sdjecent of 3 b/w 1 is: 1
M.S.E.C
Dept Of MCA
Page 84
10MCA27-DATA STRUCTURES LAB USING C
The sdjecent of 3 b/w 2 is: 1
The sdjecent of 3 b/w 3 is: 0
Enter the starting vertex: 1
The vertex visite at nodes: 1 2 3
M.S.E.C
Dept Of MCA
Page 85
10MCA27-DATA STRUCTURES LAB USING C
M.S.E.C
Dept Of MCA
Page 86