DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya...

65
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 SECTION Subject Code : EC3P7 Subject Name : Data Structures Lab

Transcript of DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya...

Page 1: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

SRI CHANDRASEKHARENDRA SARASWATHIVISWA 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 SECTION

Subject Code : EC3P7

Subject Name : Data Structures Lab

Page 2: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

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

Page 3: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

INDEX

PROGRAMNO

DATE PROGRAM NAME SIGNATURE

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.

15.

Page 4: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

EXP.NO:01 ARRAY OPERATIONS

1 A) INSERTION OPERATION

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]);}

}

Page 5: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

OUTPUT:

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

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

RESULT

Page 6: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

1 B) DELETION OPERATION

AIM

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]);}

}

Page 7: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

OUTPUT:

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

The array elements after deletion :LA[0] = 1LA[1] = 3LA[2] = 7LA[3] = 8

RESULT

Page 8: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

1 C) SEARCH OPERATION

AIM

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);}

Page 9: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

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

RESULT

Page 10: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

1 D) UPDATE OPERATION

AIM

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]);}

}

Page 11: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

OUTPUT:

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

The array elements after updation :LA[0] = 1LA[1] = 3LA[2] = 10LA[3] = 7LA[4] = 8

RESULT

Page 12: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

EXP.NO:02 STRUCTURES IN C

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;

}

Page 13: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

OUTPUT:

Id is: 1Name is: RajuPercentage is: 86.500000

RESULT

Page 14: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

2 B) EXAMPLE PROGRAM FOR ARRAY OF STRUCTURES IN C:

AIM:

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 recordrecord[0].id=1;strcpy(record[0].name, "Raju");record[0].percentage = 86.5;

// 2nd student's recordrecord[1].id=2;strcpy(record[1].name, "Surendren");record[1].percentage = 90.5;

// 3rd student's recordrecord[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;

}

Page 15: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

OUTPUT:

Records of STUDENT: 1Id is: 1Name is: RajuPercentage is: 86.500000Records of STUDENT : 2Id is: 2Name is: SurendrenPercentage is: 90.500000Records of STUDENT : 3Id is: 3Name is: ThiyaguPercentage is: 81.500000

RESULT

Page 16: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

2 C) PROGRAMS – PASSING STRUCTURE TO FUNCTION IN C BY VALUE:

AIM:

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);}

Page 17: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

OUTPUT:

Id is: 1Name is: RajuPercentage is: 86.500000

RESULT

Page 18: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

2 D) PROGRAMS – PASSING STRUCTURE TO FUNCTION IN C BY ADDRESS:

AIM:

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;

}

Page 19: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

OUTPUT:

Id is: 1Name is: RajuPercentage is: 86.500000

RESULT

Page 20: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

EXP.NO:03 UNION IN C

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:}

Page 21: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

OUTPUT:

Union record 1 values exampleName:Subject:Percentage:86.500000;Union 2 values exampleName:maniSubject:physicsPercentage:99.500000

RESULT

Page 22: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

3 B) PROGRAMS – ANOTHER WAY OF DECLARING C UNION

AIM:

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;

}

OUTPUTName :Subject :Percentage : 86.500000

RESULT

Page 23: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

EXP.NO: 04 POINTERS IN C

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: 2686784Value of c: 22Address of pointer pc: 2686784Content of pointer pc: 22Address of pointer pc: 2686784Content of pointer pc: 11Address of c: 2686784Value of c: 2

RESULT

Page 24: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

EXP.NO:05 STACK ADT

AIM:

ALGORITHM:

Page 25: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

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 5int 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){

Page 26: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

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.exitenter your choice: 1

enter the element :2enter your choice: 1enter the element :4enter your choice: 3elements of stack are : 4 2enter your choice: 2

poped element is 4enter your choice: 2

poped element is 2enter your choice: 2

stack is underflowenter your choice: 3

stack is underflowenter your choice: 4

RESULT

Page 27: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

EXP.NO: 06 QUEUE ADT

AIM:

ALGORITHM:

Page 28: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

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 10intqueue[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];

Page 29: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

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]);}}}

Page 30: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

OUTPUT:

1.enqueue2.dequeue3.displayenter yoyr choice 1enter the number 2the insert number is 21.enqueue2.dequeue3.displayenter yoyr choice 1enter the number 7the insert number is 71.enqueue2.dequeue3.displayenter yoyr choice 3271.enqueue2.dequeue3.displayenter yoyr choice 21.enqueue2.dequeue3.displayenter yoyr choice 371.enqueue2.dequeue3.displayenter yoyr choice 4

RESULT

Page 31: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

EXP.NO:07 INFIX TO POSTFIX CONVERSION

AIM:

ALGORITHM:

Page 32: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

SOURCE CODE:

/* Conversion of infix to postfix expression */include<stdio.h>#include<conio.h>#define max 100void 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)

Page 33: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

{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;}

Page 34: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

OUTPUT:

Enter the infix expression…..d– b + cc is pushed into stackd is inserted into postfix-Is pushed into stackB is insertedinto postfix-poped outNow postfix – is inserted+ is pushed into stackC is inserted into postfixThe postfix expression for given infix is:d b – c +

poped out.

RESULT

Page 35: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

EXP.NO: 08 EVALUATION OF POSTFIX EXPRESSION

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);

Page 36: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

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 expressionab+cd-*enter the value of a10enter the value of b20enter the value of c30enter the value of d40the output is:-300

RESULT

Page 37: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

EXP.NO: 09 SINGLE LINKED LISTS

AIM:

ALGORITHM:

Page 38: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561
Page 39: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561
Page 40: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561
Page 41: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561
Page 42: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

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 atlast\n4.Insert at middle\n5.Delete at first\n6.Delete at last\n7.Delete atmiddle\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);

Page 43: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

}}

}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;}

Page 44: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

}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 bemade\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;

Page 45: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

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");

Page 46: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

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 list2.Insert at first3.Insert at last4.Insert at middle5.Delete at first6.Delete at last7.Delete at middle8.Display?1CREATIONEnter the idno2Do you want to cont...?yEnter the idno4Do you want to cont...?yEnter the idno6Do you want to cont...?nEnter the choice:1.Create a list2.Insert at first3.Insert at last4.Insert at middle5.Delete at first6.Delete at last7.Delete at middle8.Display?8DISPLAYING-->2-->4-->6 Count = 3Enter the choice:

Page 47: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

?2INSERT AT FIRSTEnter the idno :1Enter the choice:?3INSERT AT LASTEnter the idno7Enter the choice:?4INSERT AT MIDDLEEnter the idno3Enter the data of node after which insertion to be made2Enter the choice:?5DELETING AT FIRSTDeleted data is 1Enter the choice:?6DELETING AT LASTDeleted data is 7Enter the choice:?7DELETING AT MIDDLEEnter the data of node in list for deleting on3Deleted data is 3Enter the choice:?8DISPLAYING-->2-->4-->6 Count = 3

RESULT

Page 48: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

EXP.NO: 10 TREE TRAVERSAL

AIM:

ALGORITHM:

Page 49: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

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{

Page 50: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

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);elset->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);}}

Page 51: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

OUTPUT

Enter number of nodes to create tree:4Enter the node value124321435Tree is createdBinary tree traversal1.Inorder2.Preorder3.Postorder4.ExitEnter the choice 1

In order traversal of the tree 12 14 35 432Enter the choice 2

Preorder traversal of the tree 12 432 14 35Enter the choice3

Postorder traversal of the tree 35 14 432 12Enter choice 0

RESULT

Page 52: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

EXP.NO:11 BUBBLE SORT

AIM:

ALGORITHM:

Page 53: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

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 :82016

Sorted elements are: 0 1 2 6 8

RESULT

Page 54: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

EXP.NO:12 SELECTION SORT

AIM:

ALGORITHM:

Page 55: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

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: 51. Enter element: 122. Enter element: 13. Enter element: 234. Enter element: 25. Enter element: 0In ascending order: 0 1 2 12 23

RESULT

Page 56: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

EXP.NO:13 LINEAR SEARCH

AIM:

ALGORITHM:

Page 57: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

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 Array6Enter 6 numbers7 2 9 4 1 6Enter a number to serach in Array4Number 4 found at index 3

RESULT

Page 58: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

EXP.NO:14 BINARY SEARCH

AIM:

ALGORITHM:

SOURCE CODE

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

void main(){

int first, last, middle, size, i, sElement, list[100];clrscr();

printf("Enter the size of the list: ");scanf("%d",&size);

printf("Enter %d integer values in Assending order\n", size);

for (i = 0; i < size; i++)scanf("%d",&list[i]);

printf("Enter value to be search: ");scanf("%d", &sElement);

first = 0;last = size - 1;middle = (first+last)/2;

while (first <= last) {if (list[middle] < sElement)

first = middle + 1;

Page 59: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

else if (list[middle] == sElement) {printf("Element found at index %d.\n",middle);break;

}else

last = middle - 1;

middle = (first + last)/2;}if (first > last)

printf("Element Not found in the list.");getch();

}

OUTPUT

Enter Number of Elements in Array6Enter 6 numbers12 25 33 44 55 66Enter a number to serach in Array44Number 44 found at index 4

RESULT

Page 60: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

EXP.NO:15 BFS AND DFSDATE:

AIM:

ALGORITHM:

Page 61: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

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;}

Page 62: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

}if(result==1)/*result stores the output of graph searching i.e. successful or unsuccessful*/printf("nSearchsuccessful,target node is found.n");elseprintf("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 wantsto 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 isempty*/{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 alreadyvisited*/status[j]=visited;/*State of all successors become visited*/}}}}

Page 63: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

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;elsereturn 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;

Page 64: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

}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;elsereturn False;}

Page 65: DATA STRUCTURES LABOct 02, 2018  · sri chandrasekharendra saraswathi viswa mahavidyalaya (university established under section 3 of ugc act 1956) enathur -kanchipuram – 631 561

OUTPUT

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

RESULT