DATA STRUCTURES LAB · Subject Name : SRI CHANDRASEKHARENDRA SARASWATHI VISWA MAHAVIDYALAYA...

Preview:

Citation preview

SRI CHANDRASEKHARENDRA SARASWATHI VISWA MAHAVIDYALAYA

(UNIVERSITY ESTABLISHED UNDER SECTION 3 OF UGC ACT 1956) ENATHUR - KANCHIPURAM – 631 561

DATA STRUCTURES LAB

LABORATORY RECORD

Name : _______________________________

Register. No : ____________________________ ___

Class : II Year –B.E (CSE) – S2 SECTI ON

Subject Code :

Subject Name :

SRI CHANDRASEKHARENDRA SARASWATHI VISWA MAHAVIDYALAYA

(UNIVERSITY ESTABLISHED UNDER SECTION 3 OF UGC ACT 1956)

BONAFIDE CERTIFICATE

This is to Certify that this is the bonafide record of work done by

Mr/Ms._____________________________________________________________, with

Reg.No ___________________ of II Year/III semester, B.E (CSE) in the Data Structures

Laboratory during the year 2018-2019.

Staff-in-charge Head of the Department

Submitted for the Practical Examination held on _____________

Internal Examiner External Examiner

INDEX

PROGRAM NO

DATE

PROGRAM NAME

SIGNATURE

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.

15.

16.

EXP.NO:01 ARRAY OPERATIONS DATE: AIM ALGORITHM PROGRAM #include <stdio.h> main() {

intLA[] = {1,3,5,7,8}; int item = 10, k = 3, n = 5; inti = 0, j = n;

printf("The original array elements are :\n"); for(i = 0; i<n; i++) {

printf("LA[%d] = %d \n", i, LA[i]); }

n = n + 1; while( j>= k) {

LA[j+1] = LA[j]; j = j - 1; }

LA[k] = item; printf("The array elements after insertion :\n");

for(i = 0; i<n; i++) {

printf("LA[%d] = %d \n", i, LA[i]); }

}

OUTPUT: The original array elements are : LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 7 LA[4] = 8 The array elements after insertion : LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 10 LA[4] = 7 LA[5] = 8 RESULT

1 B) DELETION OPERATION ALGORITHM: \ SOURCE CODE #include <stdio.h> main() {

intLA[] = {1,3,5,7,8}; int k = 3, n = 5; inti, j;

printf("The original array elements are :\n"); for(i = 0; i<n; i++) {

printf("LA[%d] = %d \n", i, LA[i]); } j = k; while( j< n) {

LA[j-1] = LA[j]; j = j + 1; } n = n -1; printf("The array elements after deletion :\n"); for(i = 0; i<n; i++) {

printf("LA[%d] = %d \n", i, LA[i]); }

}

OUTPUT: The original array elements are : LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 7 LA[4] = 8 The array elements after deletion : LA[0] = 1 LA[1] = 3 LA[2] = 7 LA[3] = 8 RESULT

1 C) SEARCH OPERATION ALGORITHM: SOURCE CODE #include <stdio.h> main() { intLA[] = {1,3,5,7,8}; int item = 5, n = 5; inti = 0, j = 0; printf("The original array elements are :\n"); for(i = 0; i<n; i++) { printf("LA[%d] = %d \n", i, LA[i]); } while( j< n){ if( LA[j] == item ) { break; } j = j + 1; } printf("Found element %d at position %d\n", item, j+1); }

OUTPUT: The original array elements are : LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 7 LA[4] = 8 Found element 5 at position 3 RESULT

1 D) UPDATE OPERATION ALGORITHM: SOURCE CODE #include <stdio.h> main() { intLA[] = {1,3,5,7,8}; int k = 3, n = 5, item = 10; inti, j; printf("The original array elements are :\n"); for(i = 0; i<n; i++) { printf("LA[%d] = %d \n", i, LA[i]); } LA[k-1] = item; printf("The array elements after updation :\n"); for(i = 0; i<n; i++) { printf("LA[%d] = %d \n", i, LA[i]); } }

OUTPUT: The original array elements are : LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 7 LA[4] = 8 The array elements after updation : LA[0] = 1 LA[1] = 3 LA[2] = 10 LA[3] = 7 LA[4] = 8 RESULT

EXP.NO:02 STRUCTURES IN C DATE: 2 A) PROGRAM FOR C STRUCTURE AIM: ALGORITHM: SOURCE CODE: #include <stdio.h> #include <string.h> struct student { int id; char name[20]; float percentage; }; intmain() { struct student record = {0}; //Initializing to null record.id=1; strcpy(record.name, "Raju"); record.percentage = 86.5; printf(" Id is: %d \n", record.id); printf(" Name is: %s \n", record.name); printf(" Percentage is: %f \n", record.percentage); return 0; }

OUTPUT: Id is: 1 Name is: Raju Percentage is: 86.500000 RESULT

2 B) PROGRAM – ANOTHER WAY OF DECLARING C STRUCTU RE: ALGORITHM: SOURCE CODE: #include <stdio.h> #include <string.h> struct student { int id; char name[20]; float percentage; } record; intmain() { record.id=1; strcpy(record.name, "Raju"); record.percentage = 86.5; printf(" Id is: %d \n", record.id); printf(" Name is: %s \n", record.name); printf(" Percentage is: %f \n", record.percentage); return 0; }

OUTPUT: Id is: 1 Name is: Raju Percentage is: 86.500000 RESULT

2 C) EXAMPLE PROGRAM FOR ARRAY OF STRUCTURES IN C : ALGORITHM: SOURCE CODE: #include <stdio.h> #include <string.h> struct student { int id; char name[30]; float percentage; }; intmain() { inti; struct student record[2]; // 1st student's record record[0].id=1; strcpy(record[0].name, "Raju"); record[0].percentage = 86.5; // 2nd student's record record[1].id=2; strcpy(record[1].name, "Surendren"); record[1].percentage = 90.5; // 3rd student's record record[2].id=3; strcpy(record[2].name, "Thiyagu"); record[2].percentage = 81.5; for(i=0; i<3; i++) { printf(" Records of STUDENT : %d \n", i+1); printf(" Id is: %d \n", record[i].id); printf(" Name is: %s \n", record[i].name); printf(" Percentage is: %f\n\n",record[i].percentage); } return 0; }

OUTPUT: Records of STUDENT: 1 Id is: 1 Name is: Raju Percentage is: 86.500000 Records of STUDENT : 2 Id is: 2 Name is: Surendren Percentage is: 90.500000 Records of STUDENT : 3 Id is: 3 Name is: Thiyagu Percentage is: 81.500000 RESULT

2 D) PROGRAM – PASSING STRUCTURE TO FUNCTION IN C BY VALUE: ALGORITHM: SOURCE CODE: #include <stdio.h> #include <string.h> struct student { int id; char name[20]; float percentage; }; void func(struct student record); intmain() { struct student record; record.id=1; strcpy(record.name, "Raju"); record.percentage = 86.5; func(record); } void func(struct student record) { printf(" Id is: %d \n", record.id); printf(" Name is: %s \n", record.name); printf(" Percentage is: %f \n", record.percentage); }

OUTPUT: Id is: 1 Name is: Raju Percentage is: 86.500000 RESULT

2 E) PROGRAM – PASSING STRUCTURE TO FUNCTION IN C BY ADDRESS: ALGORITHM: SOURCE CODE: #include <stdio.h> #include <string.h> struct student { int id; char name[20]; float percentage; }; void func(struct student *record); intmain() { struct student record; record.id=1; strcpy(record.name, "Raju"); record.percentage = 86.5; func(&record); return 0; } void func(struct student *record) { printf(" Id is: %d \n", record->id); printf(" Name is: %s \n", record->name); printf(" Percentage is: %f \n", record->percentage); return 0; }

OUTPUT: Id is: 1 Name is: Raju Percentage is: 86.500000 RESULT

2 F) PROGRAM TO STRUCTURES USING POINTERS ALGORITHM: SOURCE CODE: #include <stdio.h> #include <string.h> struct student { int id; char name[30]; float percentage; }; intmain() { inti; struct student record1 = {1, "Raju", 90.5}; struct student *ptr; ptr = &record1; printf("Records of STUDENT1: \n"); printf(" Id is: %d \n", ptr->id); printf(" Name is: %s \n", ptr->name); printf(" Percentage is: %f \n\n", ptr->percentage); return 0; }

OUTPUT: Records of STUDENT1: Id is: 1 Name is: Raju Percentage is: 90.500000 RESULT

EXP.NO:03 UNION IN C DATE: 3 A) PROGRAM FOR C UNION AIM: ALGORITHM: SOURCE CODE: #ijnclude<stdio.h> #include<conio.h> #include<string.h> union student { char name[20]; char subject[20]; float percentage; }; intmain() { union student record 1; union student record 2; strcpy(record 1.name,”raju”); strcpy(record 1. Subject,”maths”); record 1.percentage=86.50; printf(“union record 1 values example\n”); printf(“name:%s\n”,record 1.name); printf(“subject:%s\n”,record 1. Subject); printf(“percentage:%f\n\n”,record 1.percentage); printf(“union record 2 values example\n”); strcpy(record 2.name,”mani”); printf(“name:%s\n”,record 2.name); srtcpy(record 2.subject,”physics”); printf(“subject:%s\n”,record 2.subject); record 2.percentage=99.50; printf(“percentage:%f\n”,record 2.percentage); getch(); return 0: }

OUTPUT: Union record 1 values example Name: Subject: Percentage:86.500000; Union 2 values example Name:mani Subject:physics Percentage:99.500000 RESULT

3 B) PROGRAM – ANOTHER WAY OF DECLARING C UNION ALGORITHM: SOURCE CODE: #include <stdio.h> #include <string.h> union student { char name[20]; char subject[20]; float percentage; }record; intmain() { strcpy(record.name, "Raju"); strcpy(record.subject, "Maths"); record.percentage = 86.50; printf(" Name : %s \n", record.name); printf(" Subject : %s \n", record.subject); printf(" Percentage : %f \n", record.percentage); return 0; } OUTPUT Name : Subject : Percentage : 86.500000 RESULT

EXP.NO: 04 POINTERS IN C DATE: AIM: ALGORITHM: SOURCE CODE: #include <stdio.h> intmain(){ int* pc; int c; c=22; printf("Address of c:%u\n",&c); printf("Value of c:%d\n\n",c); pc=&c; printf("Address of pointer pc:%u\n",pc); printf("Content of pointer pc:%d\n\n",*pc); c=11; printf("Address of pointer pc:%u\n",pc); printf("Content of pointer pc:%d\n\n",*pc); *pc=2; printf("Address of c:%u\n",&c); printf("Value of c:%d\n\n",c); return 0; }

OUTPUT: Address of c: 2686784 Value of c: 22 Address of pointer pc: 2686784 Content of pointer pc: 22 Address of pointer pc: 2686784 Content of pointer pc: 11 Address of c: 2686784 Value of c: 2 RESULT

EXP.NO:05 STACK ADT DATE: AIM: ALGORITHM:

SOURCE CODE: #include<stdio.h> #include<conio.h> #include"stack.c" void main() { int s; clrscr(); printf("\n1.push,\n2.pop,\n3.display,\n4.exit"); while(1) { printf("\n enter your choice: "); scanf("%d",&s); switch(s) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); default: exit(0); } } } //STACK.C #define size 5 int stack[size],top=0,cn,c,res,b; void push(); void pop(); void display(); void push() { if(top>=size) { printf("\nstack is overflow"); return; } else { printf("\nenter the element :"); scanf("%d",&b); top++; stack[top]=b; return; } } void pop() { if(top==0) {

printf("\nstack is underflow"); return; } else { res=stack[top]; top--; printf("\npoped element is %d",res); return; } } void display() { inti; if(top==0) { printf("\nstack is underflow"); return; } else { printf("\nelements of stack are :"); for(i=top;i>0;i--) { printf("\t%d",stack[i]); } }}

OUTPUT: 1.push, 2.pop, 3.display, 4.exit enter your choice: 1 enter the element :2 enter your choice: 1 enter the element :4 enter your choice: 3 elements of stack are : 4 2 enter your choice: 2 poped element is 4 enter your choice: 2 poped element is 2 enter your choice: 2 stack is underflow enter your choice: 3 stack is underflow enter your choice: 4 RESULT

EXP.NO: 06 QUEUE ADT DATE: AIM: ALGORITHM:

SOURCE CODE: #include<stdio.h> #include<conio.h> #include"queue.c" void main() { int s; clrscr(); while(1) { printf("\n1.enqueue,\n2.dequeue,\n3.display,\n4.exit"); printf("\nenter your choice :"); scanf("%d",&s); switch(s) { case 1: enqueqe(); break; case 2: dequeue(); break; case 3: display(); break; case 4: exit(0); default: exit(0); } } } //QUEUE.C #define size 10 intqueue[10],front=0,rear=-1; void enqueqe() { if(rear==size) { printf("\nqueue is full"); return; } else { printf("\nenter element :"); rear=rear+1; scanf("%d",&queue[rear]); } } void dequeue() { int temp; if(rear==-1) { printf("\nqueue is empty"); return; } else if(front==rear) {

temp=queue[front]; front=0; rear=-1; } else { temp=queue[front]; front=front+1; } printf("\ndeleted data is %d",temp); } void display() { if(rear==-1) { printf("\nqueue is empty"); return; } else { inti; i=front; printf("\nelements of queue are :"); for(;i<=rear;i++) { printf("\t%d",queue[i]); } } }

OUTPUT: 1.enqueue 2.dequeue 3.display enter yoyr choice 1 enter the number 2 the insert number is 2 1.enqueue 2.dequeue 3.display enter yoyr choice 1 enter the number 7 the insert number is 7 1.enqueue 2.dequeue 3.display enter yoyr choice 3 2 7 1.enqueue 2.dequeue 3.display enter yoyr choice 2 1.enqueue 2.dequeue 3.display enter yoyr choice 3 7 1.enqueue 2.dequeue 3.display enter yoyr choice 4 RESULT

EXP.NO:07 INFIX TO POSTFIX CONVERSION DATE: AIM: ALGORITHM: SOURCE CODE: /* Conversion of infix to postfix expression */ include<stdio.h> #include<conio.h> #define max 100 void push(char); char pop(); intprec(char); int top=0; char stack[max]; void main()

{ char a,infix[100],post[100],x,ch; inti,j=0; clrscr(); printf("enter the infix expression..:\n"); gets(infix); push('('); for(i=0;(x=infix[i])!='\0';i++) { if((x>='a')&&(x<='z')) { post[j++]=x; printf("\n %c is inserted into postfix", post[j-1]); } else if(x=='(') push(x); else if(x==')') { while(ch!='(') { ch=pop(); post[j++]=ch; printf("\n Now Postfix %c is inserted", post[j-1]) ; } j--; } else { while(prec(x)<=prec(stack[top])) { ch=pop(); post[j++]=ch; printf("\n Now Postfix %c is inserted", post[j-1]); } push(x); } } post[j]='\0'; printf("\n the postfix expression for given infix is:"); puts(post); while(stack[top]!='(') { post[j]=pop(); j++; } getch(); } intprec(char y) { int k; switch(y) { case'+':k=1; break; case'-':k=1; break; case'*':k=2; break; case'/':k=2; break;

case'^':k=3; break; case'(':k=0; break; } return(k); } void push(char item) { if(top==max) { printf("overflow"); } else { top=top+1; stack[top]=item; printf("\n %c is pushed into stack",stack[top]); } return; } char pop() { char item; if(top==1) { printf("underflow"); return(0); } else { item=stack[top]; printf("\n %c popedout",stack[top]); top=top-1; return item; }

OUTPUT: Enter the infix expression….. d– b + c c is pushed into stack d is inserted into postfix -Is pushed into stack B is insertedinto postfix -poped out Now postfix – is inserted + is pushed into stack C is inserted into postfix The postfix expression for given infix is: d b – c + poped out. RESULT

EXP.NO: 08 EVALUATION OF POSTFIX EXPRESSION DATE: AIM: ALGORITHM: SOURCE CODE: #include<stdio.h> #include<conio.h> #include<string.h> void push(int); intpop(); intstack[20]; int top=0; void main () { char exp[20]; intx,a,b,c,l,i; clrscr(); printf("give the expression\n"); scanf("%s",&exp); l=strlen(exp); for(i=0;i<l;i++) { if(exp[i]=='+'||exp[i]=='-'||exp[i]=='*'||exp[i]=='/') { b=pop(); a=pop(); switch(exp[i]) { case '+': c=a+b; push(c); break; case '-':

c=a-b; push(c); break; case '*': c=a*b; push(c); break; case '/': c=a/b; push(c); break; default: printf("invalid choice\n"); break; } } else { printf("enter the value of %c",exp[i]); scanf("%d",&x); push(x); } } printf("the output is:%d",stack[top]); getch(); } void push(int item) { stack[++top]=item; } intpop() { int value; value=stack[top]; top--; return value; }

OUTPUT give the expression ab+cd-* enter the value of a10 enter the value of b20 enter the value of c30 enter the value of d40 the output is:-300 RESULT

EXP.NO: 09 SINGLE LINKED LIST DATE: AIM: ALGORITHM:

Source Code: #include<stdio.h> #include<stdlib.h> #include<conio.h> typedef struct list { intidno; struct list *link; }node; node *newnode,*lastnode,*head,*midnode,*delnode; void create(); void insertfirst(); void insertlast(); void insertmiddle(); void deletefirst(); void deletelast(); void deletemiddle(); void display(); void main() { inti; char ch; clrscr(); head=NULL; while(1) { printf("\nEnter the choice: "); printf("\n1.Create a list\n2.Insert at first\n3.Insert at last\n4.Insert at middle\n5.Delete at first\n6.Delete at last\n7.Delete at middle\n8.Display\n?"); scanf("%d",&i); switch(i) { case 1: create(); break; case 2: insertfirst(); break; case 3: insertlast(); break; case 4: insertmiddle(); break; case 5: deletefirst(); break; case 6: deletelast(); break; case 7: deletemiddle(); break; case 8: display(); break; default:

exit(0); } } } void create() { char ch; printf("\n\nCREATION\n\n"); do { newnode=(node*)malloc(sizeof(node)); printf("\nEnter the idno\n"); scanf("\t%d",&newnode->idno); newnode->link=NULL; if(head==NULL) { head=newnode; } else { lastnode->link=newnode; } lastnode=newnode; printf("\nDo you want to cont...?\n"); ch=getche(); }while((ch=='y')||(ch=='Y')); newnode->link=NULL; } void insertfirst() { printf("\n\nINSERT AT FIRST\n\n"); newnode=(node*)malloc(sizeof(node)); printf("\nEnter the idno :\n"); scanf("%d",&newnode->idno); if(head==NULL) { head=newnode; newnode->link=NULL; } else { newnode->link=head; head=newnode; } } void insertlast() { printf("\nINSERT AT LAST\n"); lastnode=(node*)malloc(sizeof(node)); printf("\nEnter the idno"); scanf("%d",&lastnode->idno); lastnode->link=NULL; if(head==NULL) head=lastnode; else { newnode=head; while(newnode->link!=NULL) newnode=newnode->link; newnode->link=lastnode;

} } void insertmiddle() { int data; printf("\n\nINSERT AT MIDDLE\n\n"); newnode=(node*)malloc(sizeof(node)); printf("\nEnter the idno\n"); scanf("%d",&newnode->idno); printf("\nEnter the data of node after which insertion to be made\n"); scanf("%d",&data); if(head==NULL) head=newnode; else { lastnode=head; while(lastnode!=NULL) { if(lastnode->idno==data) { newnode->link=lastnode->link; lastnode->link=newnode; return; } else { lastnode=lastnode->link; } } printf("\nGiven data is not available\n"); } } void deletefirst() { printf("\n\nDELETING AT FIRST\n"); if(head==NULL) { printf("\nList is empty\n"); return; } delnode=head; head=delnode->link; printf("Deleted data is %d",delnode->idno); free(delnode); } void deletelast() { node *prevnode; printf("\n\nDELETING AT LAST\n"); if(head==NULL) { printf("\nList is empty\n"); return; } else { delnode=head; while(delnode->link!=NULL) {

prevnode=delnode; delnode=delnode->link; } if(head==delnode) { head=NULL; printf("\nDeleted node is %d",delnode->idno); free(delnode); } prevnode->link=NULL; printf("\nDeleted data is %d\n",delnode->idno); free(delnode); } } void deletemiddle() { node *prevnode; int data; printf("\n\nDELETING AT MIDDLE\n"); if(head==NULL) { printf("\nList is empty\n"); return; } delnode=head; printf("\nEnter the data of node in list for deleting on\n"); scanf("%d",&data); if(head->idno==data) { head=head->link; printf("\nDeleted data is %d\n",delnode->idno); free(delnode); return; } else { delnode=head->link; prevnode=head; while(delnode!=NULL) { if(delnode->idno==data) { prevnode->link=delnode->link; printf("\nDeleted data is %d\n", delnode->idno); free(delnode); return; } else { delnode=delnode->link; prevnode=prevnode->link; } } printf("\nData is not found\n"); } } void display() { int count=0;

printf("\n\nDISPLAYING\n\n"); if(head==NULL) { printf("\nList is empty\n"); printf("\n count = %d ",count); return; } else { newnode=head; for(;newnode!=NULL;newnode=newnode->link) { count=count+1; printf("-->%d",newnode->idno); } printf("\t\tCount = %d \n",count); } OUTPUT Enter the choice: 1.Create a list 2.Insert at first 3.Insert at last 4.Insert at middle 5.Delete at first 6.Delete at last 7.Delete at middle 8.Display ?1 CREATION Enter the idno 2 Do you want to cont...? y Enter the idno 4 Do you want to cont...? y Enter the idno 6 Do you want to cont...? n Enter the choice: 1.Create a list 2.Insert at first 3.Insert at last 4.Insert at middle 5.Delete at first 6.Delete at last 7.Delete at middle 8.Display ?8 DISPLAYING -->2-->4-->6 Count = 3

Enter the choice: ?2 INSERT AT FIRST Enter the idno : 1 Enter the choice: ?3 INSERT AT LAST Enter the idno7 Enter the choice: ?4 INSERT AT MIDDLE Enter the idno 3 Enter the data of node after which insertion to be made 2 Enter the choice: ?5 DELETING AT FIRST Deleted data is 1 Enter the choice: ?6 DELETING AT LAST Deleted data is 7 Enter the choice: ?7 DELETING AT MIDDLE Enter the data of node in list for deleting on 3 Deleted data is 3 Enter the choice: ?8 DISPLAYING -->2-->4-->6 Count = 3 RESULT

EXP.NO: 10 TREE TRAVERSAL DATE: AIM: ALGORITHM:

SOURCE CODE #include<stdio.h> #include<conio.h> typedef struct tree *node; node insert(int,node); void inorder(node); void preorder(node); void postorder(node); struct tree { int data; struct tree *right,*left; }*root; void main() { node t=NULL; intnum,i,data,ch; clrscr(); printf("\nEnter number of nodes to create tree:"); scanf("%d",&num); printf("\nEnter the node value\n\n"); for(i=1;i<=num;i++) { scanf("%d",&data); t=insert(data,t); } if(t!=NULL) printf("\n\t\tTree is created"); while(1) { printf("\n\t\t\tBinary tree traversal\n"); printf("\t\t1.inorder2.preorder\n\t\t3.postorder\n\t\t4.exit\n\t\tEnter the choice"); scanf("%d",&ch); switch(ch) { scase1:printf("\n\tInorder traversal of the tree\n\n\t"); inorder(t); break; case 2:printf("\n\tpreorder traversal of the tree\n\n\t"); preorder(t); break; case 3:printf("\n\tpostorder traversal of the tree\n\n\t"); postorder(t); break; case 0:exit(0);break; default:printf("\n\t\tInvalid option\n\n");break; } getch(); clrscr(); } } node insert(intx,node t) { node newnode; newnode=(node)malloc(sizeof(struct tree));

if(newnode==NULL) printf("\n\t\tOut of space\n"); else { if(t==NULL) { newnode->data=x; newnode->left=NULL; newnode->right=NULL; t=newnode; }else {if(x<t->data) t->left=insert(x,t->left); else t->right=insert(x,t->right); } }return t; } void inorder(node t) { if(t!=NULL) { inorder(t->left); printf("%d\t",t->data); inorder(t->right); } } void preorder(node t) { if(t!=NULL) { printf("%d\t",t->data); preorder(t->left); preorder(t->right); } } void postorder(node t) { if(t!=NULL) { postorder(t->left); postorder(t->right); printf("%d\t",t->data); } }

OUTPUT Enter number of nodes to create tree:4 Enter the node value 12 432 14 35 Tree is created Binary tree traversal 1.Inorder 2.Preorder 3.Postorder 4.Exit Enter the choice 1 In order traversal of the tree 12 14 35 432 Enter the choice 2 Preorder traversal of the tree 12 432 14 35 Enter the choice3 Postorder traversal of the tree 35 14 432 12 Enter choice 0 RESULT

EXP.NO:11 BUBBLE SORT DATE: AIM: ALGORITHM: SOURCE CODE #include<stdio.h> #include<conio.h> void bubblesort(int a[],int n); void bubblesort(int a[],int n) { inti,j,temp; for(i=1;i<n;i++) { for(j=i+1;j<=n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } }

} printf("\nsorted elements are :"); for(i=1;i<=n;i++) { printf("%d\t",a[i]); } } void main() { inta[20],n,i; clrscr(); printf("\nenter the number of elements :"); scanf("%d",&n); printf("\nenter the elements :"); for(i=1;i<=n;i++) { scanf("%d",&a[i]); } bubblesort(a,n); getch(); } OUTPUT: enter the number of elements :5 enter the elements :8 2 0 1 6 Sorted elements are: 0 1 2 6 8 RESULT

EXP.NO:12 SELECTION SORT DATE: AIM: ALGORITHM:

SOURCE CODE #include <stdio.h> intmain() { intdata[100],i,n,steps,temp; printf("Enter the number of elements to be sorted: "); scanf("%d",&n); for(i=0;i<n;++i) { printf("%d. Enter element: ",i+1); scanf("%d",&data[i]); } for(steps=0;steps<n;++steps) for(i=steps+1;i<n;++i) { if(data[steps]>data[i]) /* To sort in descending order, change > to <. */ { temp=data[steps]; data[steps]=data[i]; data[i]=temp; } } printf("In ascending order: "); for(i=0;i<n;++i) printf("%d ",data[i]); return 0; }

OUTPUT Enter the number of elements to be sorted: 5 1. Enter element: 12 2. Enter element: 1 3. Enter element: 23 4. Enter element: 2 5. Enter element: 0 In ascending order: 0 1 2 12 23 RESULT

EXP.NO:13 HEAP SORT DATE: AIM: ALGORITHM:

SOURCE CODE #include<stdio.h> void create(int []); void down_adjust(int [],int); void main() { int heap[30],n,i,last,temp; printf("Enter no. of elements:"); scanf("%d",&n); printf("\nEnter elements:"); for(i=1;i<=n;i++) scanf("%d",&heap[i]); //create a heap heap[0]=n; create(heap); //sorting while(heap[0] > 1) { //swap heap[1] and heap[last] last=heap[0]; temp=heap[1]; heap[1]=heap[last]; heap[last]=temp; heap[0]--; down_adjust(heap,1); } //print sorted data printf("\nArray after sorting:\n"); for(i=1;i<=n;i++) printf("%d ",heap[i]); } void create(int heap[]) { int i,n; n=heap[0]; //no. of elements for(i=n/2;i>=1;i--) down_adjust(heap,i); } void down_adjust(int heap[],int i) { int j,temp,n,flag=1; n=heap[0]; while(2*i<=n && flag==1) { j=2*i; //j points to left child if(j+1<=n && heap[j+1] > heap[j]) j=j+1; if(heap[i] > heap[j]) flag=0; else

{ temp=heap[i]; heap[i]=heap[j]; heap[j]=temp; i=j; } } } Output Enter no. of elements:5 Enter elements:12 8 46 23 7 Array after sorting: 7 8 12 23 46 ESULT

EXP.NO:14 LINEAR SEARCH DATE: AIM: ALGORITHM:

SOURCE CODE # include <stdio.h> # include <conio.h> intmain(){ intinputArray[100], elementCount, counter, num; printf("Enter Number of Elements in Array\n"); scanf("%d", &elementCount); printf("Enter %d numbers \n", elementCount); /* Read array elements */ for(counter = 0; counter <elementCount; counter++){ scanf("%d", &inputArray[counter]); } printf("Enter a number to serach in Array\n"); scanf("%d", &num); /* search num in inputArray from index 0 to elementCount-1 */ for(counter = 0; counter <elementCount; counter++){ if(inputArray[counter] == num){ if(counter == elementCount){ printf("Number %d Not Present in Input Array\n", num); } getch(); return 0; } OUTPUT Enter Number of Elements in Array 6 Enter 6 numbers 7 2 9 4 1 6 Enter a number to serach in Array 4 Number 4 found at index 3 RESULT

EXP.NO:15 BINARY SEARCH DATE: AIM: ALGORITHM : SOURCE CODE #include <stdio.h> intmain() { int c, first, last, middle, n, search, array[100]; printf("Enter number of elements\n"); scanf("%d", & n); printf("Enter %d integers\n", n); for (c = 0; c < n; c++) scanf("%d", & array[c]); printf("Enter value to find\n"); scanf("%d", & search); first = 0; last = n - 1; middle = (first + last) / 2; while (first <= last) { if (array[middle] < search) first = middle + 1; else if (array[middle] == search) { printf("%d found at location %d.\n", search, middle + 1); break; } else last = middle - 1; middle = (first + last) / 2; } if (first > last) printf("Not found! %d is not present in the list.\n", search); return 0;}

OUTPUT Enter the number of vertex : 6 Enter the value of a[0][0] : 0 Enter the value of a[0][1] : 1 Enter the value of a[0][2] : 1 Enter the value of a[0][3] : 0 Enter the value of a[0][4] : 0 Enter the value of a[0][5] : 0 Enter the value of a[1][0] : 0 Enter the value of a[1][1] : 0 Enter the value of a[1][2] : 0 Enter the value of a[1][3] : 1 Enter the value of a[1][4] : 1 Enter the value of a[1][5] : 0 Enter the value of a[2][0] : 0 Enter the value of a[2][1] : 1 Enter the value of a[2][2] : 0 Enter the value of a[2][3] : 0 Enter the value of a[2][4] : 1 Enter the value of a[2][5] : 0 Enter the value of a[3][0] : 0 Enter the value of a[3][1] : 0 Enter the value of a[3][2] : 0 Enter the value of a[3][3] : 0 Enter the value of a[3][4] : 1 Enter the value of a[3][5] : 0 Enter the value of a[4][0] : 0 Enter the value of a[4][1] : 0 Enter the value of a[4][2] : 0 Enter the value of a[4][3] : 0 Enter the value of a[4][4] : 0 Enter the value of a[4][5] : 1 Enter the value of a[5][0] : 0 Enter the value of a[5][1] : 0 Enter the value of a[5][2] : 0 Enter the value of a[5][3] : 0 Enter the value of a[5][4] : 0 Enter the value of a[5][5] : 0 The matrix is :- 0 1 1 0 0 0 0 0 0 1 1 0 0 1 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 RESULT

EXP.NO:16 B.F.S AND D.F.S DATE: AIM: ALGORITHM:

SOURCE CODE #include<stdio.h> #include<stdlib.h> #define max 10/*Defining maximum number of vertices of the input graph*/ enum{ready,visited}; enum{True,False}; main() { int a[max][max],v,i,j,g,result,ch; char chr; intbfs(int[][j],int,int); intdfs(int[][j],int,int); printf("Enter the number of vertex : "); scanf("%d",&v); for(i=0;i<v;i++) { for(j=0;j<v;j++) { printf("Enter the value of a[%d][%d] : ",i,j); scanf("%d",&a[i][j]); } } printf("The matrix is :-n"); for(i=0;i<v;i++) { for(j=0;j<v;j++) { printf("%dt",a[i][j]); } printf("n"); } while(1) { printf("Enter the node you want to search : "); scanf("%d",&g);/*Read target node from user*/ printf("n..........Menu..........n"); printf("n1>.....Breadh First Search.....n2>.....Depth First Search.....n"); printf("nEnter choice : "); scanf("%d",&ch); switch(ch) { case 1: { printf("Traversed path for B.F.S : "); result=bfs(a,v,g); break; } case 2: { printf("Traversed path for D.F.S : "); result=dfs(a,v,g); break; } default: { printf("!!!!!Error!!!!!n!!!!!Invalid choice given!! !!!"); return;

} } if(result==1)/*result stores the output of graph searching i.e. successful or unsuccessful*/ printf("nSearchsuccessful,target node is found.n"); else printf("nSearch unsuccessful, entire graph is traversed but target node is not found.n"); printf("nWant to continue?(give 1 for yes) : ");/*The program will continue until the user wants to exit*/ scanf("%d",&chr); if(chr!=1) { printf("The program will exit now."); return; } } } /*The input graph,number of vertices and the target node are passed as parameters*/ intbfs(int a[max][max],intv,int g) { ints,i,t,j,open[max],status[max],f; void insert(int[],int,int*); int delete(int[],int*); intqueue_empty(int*,int*); f=0;/*If search will be successful then value of f will be changed to 1*/ int front=0; int rear=0; for(i=0;i<v;i++) status[i]=ready;/*Initially all the nodes are in ready state*/ s=0;/*Searchig starts from the first node of the graph*/ if(s==g)/*Check whether starting node is our target*/ { printf("v%d",s); f=1; return f; } insert(open,s,&rear);/*Insert the starting node in the queue*/ while((queue_empty(&front,&rear))==False)/*Searching will be considered until queue is empty*/ { t=delete(open,&front);/*t contains the node just deleted from the queue*/ printf("v%d->",t); status[t]=visited;/*The state of the deleted node becomes visited*/ for(j=0;j<v;j++) { if(a[t][j]==1) { if(j==g)/*Checking whether any successor node of t is our target*/ { printf("v%d",j); f=1; return f; } if(status[j]!=visited) { insert(open,j,&rear);/*Insert only that successors of t which are not already visited*/ status[j]=visited;/*State of all successors become visited*/ } } }

} printf("b bb "); return f; } void insert(int queue[],intdata,int* rear) { if(*rear>=max) { printf("Error overflown"); return; } queue[*rear]=data; (*rear)++; } intdelete(int queue[],int* front) { int data; data=queue[*front]; (*front)++; return data; } intqueue_empty(int* front,int* rear) { if(*front==*rear) return True; else return False; } /*The input graph,number of vertices and the target node are passed as parameters*/ intdfs(int a[max][max],intv,int g) { ints,i,t,j,open[max],status[max],f; void push(int[],int,int*); int pop(int[],int*); intstack_empty(int*); f=0;/*If search will be successful then value of f will be changed to 1*/ int top=0; for(i=0;i<v;i++) status[i]=ready;/*Initially all the nodes are in ready state*/ s=0;/*Searchig starts from the first node of the graph*/ if(s==g)/*Check whether starting node is our target*/ { printf("v%d",s); f=1; return f; } push(open,s,&top);/*Insert the starting node in the stack*/ while(stack_empty(&top)==False)/*Searching will be considered until stack is empty*/ { t=pop(open,&top);/*t contains the node just deleted from the stack*/ printf("v%d->",t); status[t]=visited;/*The state of the deleted node becomes visited*/ for(j=0;j<v;j++) { if(a[t][j]==1) { if(j==g)/*Checking whether any successor node of t is our target*/ { printf("v%d",j); f=1;

return f; } if(status[j]!=visited) { push(open,j,&top);/*Insert only that successors of t which are not already visited*/ status[j]=visited;/*State of all successors become visited*/ } } } } printf("b bb "); return f; } void push(int stack[],intdata,int* top) { if(*top>=max) { printf("Error overflown"); return; } stack[*top]=data; (*top)++; } intpop(int stack[],int *top) { int data; (*top)--; data=stack[*top]; return data; } intstack_empty(int *top) { if(*top==0) return True; else return False; }

OUTPUT Enter the node you want to search : 4 ..........Menu.......... 1>.....Breadh First Search..... 2>.....Depth First Search..... Enter choice : 1 Traversed path for B.F.S : v0->v1->v4 Search successful,target node is found. Want to continue?(give 1 for yes) : 1 Enter the node you want to search : 4 ..........Menu.......... 1>.....Breadh First Search..... 2>.....Depth First Search..... Enter choice : 2 Traversed path for D.F.S : v0->v2->v4 Search successful,target node is found. Want to continue?(give 1 for yes) : 1 Enter the node you want to search : 7 ..........Menu.......... 1>.....Breadh First Search..... 2>.....Depth First Search..... Enter choice : 1 Traversed path for B.F.S : v0->v1->v2->v3->v4->v5 Search unsuccessful, entire graph is traversed but target node is not found. Want to continue?(give 1 for yes) : 1 Enter the node you want to search : 8 ..........Menu.......... 1>.....Breadh First Search..... 2>.....Depth First Search..... Enter choice : 2 Traversed path for D.F.S : v0->v2->v4->v5->v1->v3 Search unsuccessful, entire graph is traversed but target node is not found. Want to continue?(give 1 for yes) : 0 The program will exit now. RESULT

Recommended