p&ds lab

113
BHARATHIDASAN ENGINEERING COLLEGE NATTRAMPALLI – 635 854 DEPARTMENT OF INFORMATION TECHNOLOGY NAME : ............................................................. ................................... SUBJECT : ....................................................... ........................................ DEPARTMENT : ......................................................... ........................................ SEMESTER : ......................................................... ....................................... YEAR : ......................................................... .......................................

Upload: raji-sharmi

Post on 28-Jan-2016

245 views

Category:

Documents


0 download

DESCRIPTION

P&DS LAB

TRANSCRIPT

Page 1: P&DS LAB

BHARATHIDASAN ENGINEERING COLLEGE

NATTRAMPALLI – 635 854

DEPARTMENT OF INFORMATION TECHNOLOGY

NAME : ................................................................................................

SUBJECT : ...............................................................................................

DEPARTMENT : .................................................................................................

SEMESTER : ................................................................................................

YEAR : ................................................................................................

APRIL-2014

DEPARTMENT OF INFORMATION TECHNOLOGY

Page 2: P&DS LAB

BHARATHIDASAN ENGINEERING COLLEGE

NATTRAMPALLI – 635 854

DEPARTMENT OF INFORMATION TECHNOLOGY

Certified with this is a bonafied record of the practical work done by _ _ _ _ _

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 2nd Semester Information Technology in

Programming And Data Structures Lab-1 (IT6212) during the period January 2014 to April 2014.

Staff In-Charge Head of the Department

University Register Number:

Submitted for the Practical examination held on……….....…………….

Internal Examiner External

Page 3: P&DS LAB

Examiner

Date Experiment Name Signature

1Programs using Conditional Statements

2 Programs using Looping Statements

3Program using Arrays

4Program using Strings

5Program using Pointers

6Program using Functions

7File Handling – Sequential File

8File Handling – Random File

9Infix to Postfix Conversion

10Simple Expression Evaluation using Stacks

11Linked List Implementation of Stack

12Linked List Implementation of Queue

13Sorting Algorithm

14Implementation of Linear and Binary Search

Page 4: P&DS LAB

SCIENTIFIC PROBLEM SOLVING USING CONDITIONAL STATEMENTS

ODD OR EVEN

PROGRAM:

#include<stdio.h> main(){

int a,rem;printf("Enter a Number\n"); scanf("%d",&a);rem=a%2; if(rem==0)

printf("The Given Number is Even");else

}printf("The Given Number is Odd");

Page 5: P&DS LAB

OUTPUT:

Enter a Number 13The Given Number is Odd

Page 6: P&DS LAB

SCIENTIFIC PROBLEM SOLVING USING CONDITIONAL STATEMENTS

BIGGEST OF 3 NUMBERS

PROGRAM:

#include<stdio.h> main(){

int a,b,c;printf("Enter 3 Numbers\n"); scanf("%d%d%d",&a,&b,&c); if(a>b){

if(a>c){

printf("The First Number %d(a) is Biggest\n",a);}

}else if(b>c)

Page 7: P&DS LAB

printf("The Second Number %d(b) is Biggest\n",b);

} else

printf("The Third Number %d(c) is Biggest\n",c);

}

OUTPUT:

Enter 3 Numbers 592The Second Number 9(b) is Biggest

Page 8: P&DS LAB

SCIENTIFIC PROBLEM SOLVING USING CONDITIONAL STATEMENTS

ARITHMETIC CALCULATOR

PROGRAM:

#include<stdio.h> main(){

int a,b,ch,c;printf("\nEnter the Number 1:\n"); scanf("%d",&a);printf("\nEnter the Number 2:\n"); scanf("%d",&b); printf("\n1.Add\n2.Subtract\

Page 9: P&DS LAB

n3.Multiply\n4.Divide\n"); printf("\nEnter the Choice:\n");scanf("%d",&ch); switch(ch){

case 1:

Page 10: P&DS LAB

c=a+b;printf("\n %d + %d = %d\n",a,b,c); break;

case 2:c=a-b;printf("\n %d - %d = %d\n",a,b,c); break;

case 3: c=a*b;

printf("\n %d * %d = %d\n",a,b,c); break;

case 4: c=a/b;

printf("\n %d / %d = %d\n",a,b,c); break;

}

}

OUTPUT:

Enter the Number 1: 15

Enter the

Page 11: P&DS LAB

Number 2: 561.Add 2.Subtract 3.Multiply 4.DivideEnter the Choice: 215 - 56 = -41

Page 12: P&DS LAB

SCIENTIFIC PROBLEM SOLVING USING LOOPING

SUM OF ‘N’ NATURAL NUMBERS

PROGRAM:

#include<stdio.h> main(){

int i,n,sum=0;printf("Enter the range\n"); scanf("%d",&n);i=1; w

Page 13: P&DS LAB

hile(i<=n){

sum=sum+i; i++;

}printf("\nThe sum of first %d numbers is %d\n",n,sum);

}

}

OUTPUT:

Enter the range 16

Page 14: P&DS LAB

The sum of first 16 numbers is 136

Page 15: P&DS LAB
Page 16: P&DS LAB

SCIENTIFIC PROBLEM SOLVING USING LOOPING

SUM OF DIGITS OF A NUMBER

PROGRAM:

#include<stdio.h> main(){

int n,i,sum=0;printf("Enter a Number\n"); scanf("%d",&n);do{

}

i=n%10; sum=sum+i; n=n/10;

}while(n>0);printf("The Sum of digit is %d\n",sum);

Page 17: P&DS LAB

OUTPUT:

Enter a Number

5891 The Sum of digit is 23

Page 18: P&DS LAB

SCIENTIFIC PROBLEM SOLVING USING LOOPING EVALUATION OF SINE SERIES

Page 19: P&DS LAB

PROGRAM:

#include<stdio.h>#include<math.h> int factorial(int n){

int i,sum=1; for(i=1;i<=n;i++)

sum=sum*i; return sum;

}main(){

int i,n,j,dr;float res=0.0,x,nr;printf("\nEnter the Value of x\n"); scanf("%f",&x);printf("\nEnter the total no of terms\n"); scanf("%d",&n);j=1; for(i=1;i<n*2;i+=2){

nr=pow(x,i)*j; dr=factorial(i); res+=(nr/dr);j=-j;

}printf("The Result of sine series is : %f\n",res);

}

Page 20: P&DS LAB

OUTPUT:

Enter the Value of x 0.21Enter the total no of terms 5The Result of sine series is : 0.208460

Page 21: P&DS LAB

SCIENTIFIC PROBLEM SOLVING USING LOOPING

NUMBER CHECKING

Page 22: P&DS LAB

PROGRAM:#include<stdio.h>#include<math.h> main(){

int a,i,sum=0,n,ch,m; printf("\nEnter a Number\n"); scanf("%d",&a);printf("\n1.Palindrome\n2.Armstrong\n3.Prime\n"); printf("\nEnter the Choice:\n");scanf("%d",&ch); switch(ch){

case 1: n=a; while(a>0)

{i=a%10; sum=(sum*10)+i; a=a/10;

}if(n==sum)

printf("Given Number is Palindrome\n"); else printf("Given Number is Not Palindrome\n");

Page 23: P&DS LAB

n=a; while(a>0)

case 2: n=a; do

{i=a%10; sum=sum+(i*i*i); a=a/10;

}while(a>0); if(n==sum)

printf("Given Number is Armstrong\n"); e lse printf("Given Number is Not Armstrong\n");

break; case 3:

m=5; n=sqrt(a); for(i=2;i<=n;i++){

if(a%i==0){

m=0; break;

}}if(m==0)

printf("Given Number is Prime\n");else printf(“Given Number is Not Prime\n”);break;}}

Page 24: P&DS LAB
Page 25: P&DS LAB

OUTPUT:

Enter a Number 121 1.Palindrome 2.Armstrong 3.Prime

Enter the Choice:

1 Given Number is Palindrome

Page 26: P&DS LAB

SIMPLE PROGRAMMING FOR ONE DIMENSIONAL AND TWO DIMENSIONAL ARRAYS

SUM OF ARRAY ELEMENTS

PROGRAM:

#include<stdio.h> main(){

int i,n,a[10],sum=0;printf("Enter total no. of Elements\n"); scanf("%d",&n);

printf("Enter Array elements one by one\n"); for(i=0;i<n;i++)

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

sum=sum+a[i];printf("The Sum of Array Elements is %d\n",sum);}

Page 27: P&DS LAB
Page 28: P&DS LAB

OUTPUT :

Enter total no. of Elements 8 Enter Array elements one by one 15 69 32 10 45 66 32 11 The Sum of Array Elements is 280

Page 29: P&DS LAB

SIMPLE PROGRAMMING FOR ONE DIMENSIONAL AND TWO

DIMENSIONAL ARRAYS

DISPLAY EVEN NUMBERS OF AN ARRAY

PROGRAM:

#include<stdio.h> main(){

int i,n,a[10];printf("Enter total no. of Elements\n"); scanf("%d",&n);printf("Enter Array elements one by one\n");

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

printf("The even numbers of given array:\n"); for(i=0;i<n;i++){

if(a[i]%2==0) printf("%d\n",a[i]);

}

}

Page 30: P&DS LAB

OUTPUT:

Enter total no. of Elements 6Enter Array elements one by one 981135612214The even numbers of given array: 982214

Page 31: P&DS LAB

SIMPLE PROGRAMMING FOR ONE DIMENSIONAL AND TWO DIMENSIONAL ARRAYS

MULTIPLICATION OF 2*2 MATRIXES

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

{

int arr1[10][10], arr2[10][10];

int arr3[][3]={

{0,0,0},

{0,0,0},

{0,0,0}

};

int i, j;

clrscr();

printf("Enter 3x3 array 1:\n");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

printf("Enter element %d x %d:",i,j);

scanf("%d",&arr1[i][j]);

}

}

printf("Enter 3x3 array 2:\n");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++){

printf("Enter element %d x %d:",i,j);

scanf("%d",&arr2[i][j]);

}

}

for(i=0;i<3;i++)

{

Page 32: P&DS LAB

for(j=0;j<3;j++)

{

arr3[i][j]=arr1[i][j] + arr2[i][j];

}

}

printf("Addition of Array 1 and Array 2 is: \n");

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

for(j=0;j<3;j++){

printf("\t%d",arr3[i][j]);

}

printf("\n");

}

getch();

}

OUTPUT: Enter 3x3 array 1:

Page 33: P&DS LAB

Enter element 0 x 0:1

Enter element 0 x 1:1

Enter element 0 x 2:1

Enter element 1 x 0:1

Enter element 1 x 1:1

Enter element 1 x 2:1

Enter element 2 x 0:1

Enter element 2 x 1:1

Enter element 2 x 2:1

Enter 3x3 array 2:

Enter element 0 x 0:1

Enter element 0 x 1:1

Enter element 0 x 2:1

Enter element 1 x 0:1

Enter element 1 x 1:1

Enter element 1 x 2:1

Enter element 2 x 0:1

Enter element 2 x 1:1

Enter element 2 x 2:1

Addition of Array 1 and Array 2 is:

2 2 2

2 2 2

2 2 2

Page 34: P&DS LAB

SOLVING PROBLEMS USING STRING FUNCTIONS

PROGRAM:

#include<stdio.h>#include<string.h> main(){

char s[20],s1[20]; printf("Enter a String\n"); scanf("%s",s); strcpy(s1,s); if(strcmp(s,s1)==0)

printf("The Given String is Palindrome\n");else

}printf("The Given String is Not Palindrome\n");

Page 35: P&DS LAB

OUTPUT:

Enter a String madamThe Given String is Palindrome

Page 36: P&DS LAB

SOLVING PROBLEMS USING STRING FUNCTIONS

STRING CONCATENATION

PROGRAM:

#include<stdio.h>#include<string.h> main(){

char s[20],s1[20]; printf("Enter a String1\n"); scanf("%s",s);printf("Enter a String2\n"); scanf("%s",s1);strcat(s,s1);printf("The Concatenated String is %s\n",s);

}

Page 37: P&DS LAB

OUTPUT:

Enter a String1 hai

Enter a String2 helloThe Concatenated String is haihello

Page 38: P&DS LAB

PROGRAM USING STRUCTURES

PROGRAM:

#include <stdio.h>

int main ()

Page 39: P&DS LAB

{ int foo = 42; int bar = -1; int * foo_ptr;

foo_ptr = & foo;

printf ("Get the existing values of foo, bar, foo_ptr, and * foo_ptr:\n");

printf ("foo = %d\n", foo); printf ("bar = %d\n", bar); printf ("foo_ptr = %p\n", foo_ptr); printf ("* foo_ptr = %d\n", * foo_ptr);

printf ("Change the value of * foo_ptr:\n");

* foo_ptr = 99;

printf ("foo = %d\n", foo); printf ("bar = %d\n", bar); printf ("foo_ptr = %p\n", foo_ptr); printf ("* foo_ptr = %d\n", * foo_ptr);

printf ("Change the value of foo_ptr to & bar:\n");

foo_ptr = & bar;

printf ("foo = %d\n", foo); printf ("bar = %d\n", bar); printf ("foo_ptr = %p\n", foo_ptr); printf ("* foo_ptr = %d\n", * foo_ptr);

return 0; }

OUTPUT:

Getting the existing values of foo,foo_bar etc

Page 40: P&DS LAB

PROGRAMS WITH USER DEFINED FUNCTIONS

FUNCTIONS WITHOUT ARGUMENTS & RETURN TYPE

PROGRAM: #include<std

io.h> void isleap(){

Page 41: P&DS LAB

int yr;printf("Enter a Year\n"); scanf("%d",&yr); if(yr%4==0)

printf("Given Year is Leap year");else

}printf("Given Year is Not a Leap year");

main(){isleap();}

Page 42: P&DS LAB

OUTPUT: OUTPUT:

Page 43: P&DS LAB

Enter a year1965Given year is Not a leap year

PROGRAMS WITH USER DEFINED FUNCTIONS

FUNCTIONS WITHOUT ARGUMENTS & WITH RETURN TYPE

PROGRAM: #include<stdio.h>

#include<math.h> float area(){

int a,b,c;

Page 44: P&DS LAB

float s,ar;printf("Enter 3 Sides\n"); scanf("%d%d%d",&a,&b,&c); s=(a+b+c)/2;ar=sqrt(s*(s-a)*(s-b)*(s-c)); return ar;

}

main(){

float a; a=area();printf("The Area of Triangle is %f\n",a);

}

Page 45: P&DS LAB

OUTPUT:

Enter 3 Sides 1287The Area of Triangle is 19.748418

Page 46: P&DS LAB
Page 47: P&DS LAB

FUNCTIONS WITH ARGUMENTS & WITHOUT RETURN TYPE

PROGRAM:

{

#include<stdio.h>void sorting(int a[],int n)

int i,j,t;for(i=0;i<n

-1;i++)

{for(j=i+1;j<n;j++){

if(a[i]>a[j]){

t=a[i]; a[i]=a[j]; a[j]=t;

}}

}printf("Array Elemets before sorting\n"); for(i=0;i<n;i++)

Page 48: P&DS LAB

printf("%d\t",a[i]);

}main(){

}

:

int i,a[10],n;printf("Enter total no. of elements\n"); scanf("%d",&n);printf("Enter Array Elements one by one\n"); for(i=0;i<n;i++)

scanf("%d",&a[i]);printf("Array Elemets before sorting\n"); for(i=0;i<n;i++)

printf("%d\t",a[i]); printf("\n");

sorting(a,n);

OUTPUT:

Enter total no. of elements6Enter Array Elements one by one 2129453011Array Elemets before sorting 21 29 45 30 11Array Elemets before sorting 29 11 21 30 45

Page 49: P&DS LAB

PROGRAMS WITH USER DEFINED FUNCTIONS

FUNCTIONS WITH ARGUMENTS & RETURN TYPE

PROGRAM: #include<stdio.h>

int small(int a[],int n){

int s,i; s=a[0]; for(i=0;i<

Page 50: P&DS LAB

n;i++)

{if(a[i

]<s) s=a[i];

} return s;

Page 51: P&DS LAB

main(){

int i,a[10],n,s;printf("Enter total no. of elements\n"); scanf("%d",&n);printf("Enter Array Elements one by one\n"); for(i=0;i<n;i++)

scanf("%d",&a[i]); printf("Array Elemets:\n"); for(i=0;i<n;i++)

printf("%d\t",a[i]); printf("\n"); s=small(a,n);printf("The Smallest element of given array is %d",s);

}

OUTPUT:

Page 52: P&DS LAB

Enter total no. of elements 5Enter Array

Elements one by one 1982660

Array Elemets: 1 98 26 6 0The Smallest element of given array is 0

Page 53: P&DS LAB

PROGRAM USING RECURSIVE FUNCTION

FACTORIAL OF A NUMBER

PROGRAM:#include<stdio.h> int factorial(int n){

if(n==0 || n==1) return 1;

else

}return n*factorial(n-1);

main(){

int n;printf("\nEnter a Number\n"); scanf("%d",&n);printf("\nThe factorial of %d is %d\n",n,factorial(n));

}

Page 54: P&DS LAB

OUTPUT:

Enter a Number 6 The factorial of 6 is 720

Page 55: P&DS LAB

SEQUENTIAL FILE ACCESS

PROGRAM:

#include<stdio.h> #include<stdlib.h>

int c,i,id; char name[20]; FILE *fp; int n; int search(FILE *fp,int id);void display(FILE *fp);

typedef struct details { int id; char name[20]; }details;

details d;

void main(){ printf("\nHow many records you would like to insert ? : "); scanf("%d",&n); fp=fopen("one.txt","a"); for(i=0;i<n;i++) { printf("\nEnter id and name"); scanf("%d%s",&d.id,d.name); fwrite(&d,sizeof(d),1,fp);

Page 56: P&DS LAB

} fclose(fp);

while(1) { printf("\nWhat would you like to do now ? : \n"); printf("\n1.Display \t2.Search \t3.Modify \t4.Delete \t5.Exit"); scanf("%d",&c); switch(c) { case 1: fp=fopen("one.txt","r+"); display(fp); fclose(fp); break; case 2: fp=fopen("one.txt","r+"); printf("\nEnter ID to search : "); scanf("%d",&id); if(search(fp,id)) { printf("\nThe record is as follows : "); printf("\n%d\t%s",d.id,d.name); } else printf("\nRecord not found"); fclose(fp); break; case 3: fp=fopen("one.txt","r+"); printf("\nEnter ID to modify d record : "); scanf("%d",&id);

if(search(fp,id)) { printf("\nEnter new name"); scanf("%s",d.name); fwrite(&d,sizeof(d),1,fp); } else printf("\nSpecified record not found "); fclose(fp); break; } }}

int search(FILE *fp,int id){ rewind(fp); while(fread(&d,sizeof(d),1,fp))

Page 57: P&DS LAB

{ if(id==d.id) return 1; } return 0;}

void display(FILE *fp){ rewind(fp); while(fread(&d,sizeof(d),1,fp)) { printf("\n%d\t%s",d.id,d.name); }}

OUTPUT: How many records you would like to insert ?: 4 Enter id and name 1 anu Enter id and name 2 akash Enter id and name 3 adhavan Enter id and name 4 jai

What would you like to do now?: 1.Display 2.Search 3.Modify 4.Delete 5. Exit 1 1 anu 2 akash 3 adhavan 4 jai

What would you like to do now?: 1.Display 2.Search 3.Modify 4.Delete 5. Exit 2Enter id to search:2 2 akash What would you like to do now?: 1.Display 2.Search 3.Modify 4.Delete 5. Exit 3Enter id to modify d record: 4Enter new name 4 jaijai What would you like to do now?: 1.Display 2.Search 3.Modify 4.Delete 5. Exit5

Page 58: P&DS LAB

RANDOM FILE ACCESS

PROGRAM

#include <stdio.h>

struct hardwareData {

int recordNum;char toolname[20];int quantity;double cost;

};

//functions int enterChoice( void ); void printList( FILE *readPtr ); void updateRecord( FILE *fPtr ); void newRecord( FILE *fPtr ); void deleteRecord( FILE *fPtr );

int main() { FILE *cfPtr; int choice;

if ((cfPtr = fopen( "hardware.dat", "ab+" )) == NULL ) {

printf( "File could not be opened.\n" );}else {

Page 59: P&DS LAB

while ( choice != 4 ) {

switch ( choice = enterChoice() ) { //adds a new record, or upates one

case 1:newRecord ( cfPtr );break;

//deletes a record case 2:

deleteRecord ( cfPtr );break;

//prints listcase 3:

printList ( cfPtr ); break;

//display message if user does not enter valid choice default:

printf( "Incorrect choice\n" );break;

}

} fclose( cfPtr );

} //system("pause");return 0;

}

void printList( FILE *fPtr )//print function { struct hardwareData hardware;

fseek(fPtr, 0, SEEK_SET);printf("%-10s%-16s%-10s%10s\n", "Number", "Name", "Quantity", "cost");

while(fread(&hardware, sizeof( struct hardwareData), 1, fPtr)) {if(hardware.recordNum != 0){

printf("%-10d%-16s%-10d%-10.2lf\n", hardware.recordNum, hardware.toolname, hardware.quantity, hardware.cost);

}}

}

void deleteRecord( FILE *fPtr ) {

struct hardwareData hardware; struct hardwareData blankRecord = { 0, "", 0,0.00 };int recordNum;

Page 60: P&DS LAB

printf( "Enter record number to delete (1-100): " );scanf( "%d", &recordNum );

fseek( fPtr, ( recordNum - 1 ) * sizeof( struct hardwareData ), SEEK_SET );

fread( &hardware, sizeof( struct hardwareData ), 1, fPtr );

if ( hardware.recordNum==0 ) {

printf ( "Record %d does not exist.\n", recordNum );} else {

fseek( fPtr, ( recordNum - 1 ) * sizeof( struct hardwareData ), SEEK_SET );fwrite( &blankRecord, sizeof( struct hardwareData ), 1, fPtr );printf("\ndeleted\n");

} }

void newRecord( FILE *fPtr ) {

struct hardwareData hardware = { 0, "", 0, 0.0 };int piece;

printf( "Enter record number to create (1-100) : " );scanf( "%d", &piece);

fseek( fPtr, (piece - 1) * sizeof( struct hardwareData ), SEEK_SET );

fread( &hardware, sizeof( struct hardwareData ), 1, fPtr );

printf( "Enter tool name, quantity, cost\n?" );scanf("%s%d%lf", &hardware.toolname, &hardware.quantity, &hardware.cost);

hardware.recordNum = piece;

fseek(fPtr, (hardware.recordNum-1) * sizeof( struct hardwareData), SEEK_SET);

fwrite( &hardware, sizeof( struct hardwareData), 1, fPtr);} int enterChoice( void ) {

int menuChoice;printf( "\nEnter your choice\n"

"1 - Add a new tool, or update an existing tool\n""2 - Delete a tool\n""3 - Print the list\n""4 - End program\n?\n");

scanf( "%d", &menuChoice );

Page 61: P&DS LAB

getchar();return menuChoice;

}

OUTPUT:

Enter your choice1- Add a new tool,or update an existing tool2- Delete a tool3- Print the list4- End program

?1 Enter record number to create(1-100):55 Enter tool name,quantity,cost RAM,2gb,3000

Enter your choice1-Add a new tool,or update an existing tool2-Delete a tool

3-Print the list4-End program

?1

Enter record number to create(1-100):68 Enter tool name,quantity,cost ROM,1gb,2000

Enter your choice1-Add a new tool,or update an existing tool2-Delete a tool

3-Print the list4-End program

?1 Enter record number to create(1-100):23 Enter tool name,quantity,cost

Page 62: P&DS LAB

Joystick,2mb,5000

Enter your choice1-Add a new tool,or update an existing tool2-Delete a tool

3-Print the list4-End program

?1

Enter record number to create(1-100):20 Enter tool name,quantity,cost Jai,1mb,3000 Enter your choice

1-Add a new tool,or update an existing tool2-Delete a tool

3-Print the list 4-End program ?3 Number Name Quantity Cost 55 Ram,2gb,3000 12 0.00 68 Rom,1gb,2000 0 0.00 23 Joystick,2mb,5000 2 3.00 20 jai,1mb,3000 0 0.00

Enter your choice1-Add a new tool,or update an existing tool2-Delete a tool

3-Print the list4-End program

?4

Page 63: P&DS LAB

PROGRAM USING STRUCTURES

STUDENT RECORD

PROGRAM:

#include<stdio.h> struct student{

int rno,m1,m2,m3; float avg;char name[20],dept[10];

};main(){

struct student s;printf("Enter the Student Details:\n"); printf("Enter the Stuent roll no:\n"); scanf("%d",&s.rno);printf("Enter the Stuent Name:\n"); scanf("%s",&s.name);printf("Enter the Stuent Dept:\n"); scanf("%s",&s.dept);printf("Enter the 3 marks:\n"); scanf("%d%d%d",&s.m1,&s.m2,&s.m3); s.avg=(s.m1+s.m2+s.m3)/3;printf("The Student Average is :%f\n",s.avg);

}

Page 64: P&DS LAB

OUTPUT:

Enter the Student Details: Enter the Stuent roll no: 12Enter the Stuent Name: KumarEnter the Stuent Dept: CSEEnter the Stuent marks: 401890The Student Average is :49.0000

Page 65: P&DS LAB

CONVERSION OF INFIX EXPRESSION TO POST

PROGRAM:

#include<stdio.h>#include<conio.h>int stack[20],top=0;char inf[40],post[40];void push(int);void postfix();char pop();void main(void){

clrscr();printf("\t\t\t****INFIX TO POSTFIX****\n\n");printf("Enter the infix expression :: ");scanf("%s",inf);postfix();getch();

}void postfix(){

int i,j=0;for(i=0;inf[i]!=NULL;i++){

switch(inf[i]){

case '+':while(stack[top]>=1)

post[j++]=pop();push(1);break;

case '-':while(stack[top]>=1)

post[j++]=pop();push(2);break;

case '*':while(stack[top]>=3)

post[j++]=pop();push(3);break;

case '/':while(stack[top]>=3)

post[j++]=pop();push(4);break;

case '^':while(stack[top]>=4)

post[j++]=pop();

Page 66: P&DS LAB

push(5);break;

case '(':push(0);break;

case ')':while(stack[top]!=0)

post[j++]=pop();top--;break;

default:post[j++]=inf[i];

}}while(top>0)

post[j++]=pop();printf("\nPostfix Expression is :: %s",post);

}void push(int ele){

top++;stack[top]=ele;

}char pop(){

char e;e=stack[top];top--;switch(e){

case 1:e='+';break;

case 2:e='-';break;

case 3:e='*';break;

case 4:e='/';break;

case 5:e='^';break;

}return(e);

}

Page 67: P&DS LAB

OUTPUT:

Enter the infix expression :: (a+b)/(c*d)

Postfix Expression is :: ab+cd*/

Manual Calculation

SE EXPRESSION STACK RESULT FIELD( (A A+ +,( AB +,( AB) ),( AB+/ / AB+( (,/C (,/ AB+C- -,(,/ AB+CD -,(,/ AB+CD+ +,(,/ AB+CD-

E +,(,/ AB+CD-E) ),+,(,/ AB+CD-E++ +,/ AB+CD-E+/F + AB+CD-E/F- - AB+CD-E/F+G - AB+CD-E/F+G

AB+CD-E/F+G-

Page 68: P&DS LAB

EVALUATING OF POSTFIX EXPRESSION

PROGRAM

#include<stdio.h>

#include<conio.h>

#include<string.h>

#define MAX 50

int stack[MAX];

char post[MAX];

int top = -1;

void pushstack(int tmp);

void calculator(char c);

void main()

{

int i;

clrscr();

printf("Insert a postfix notation :: ");

gets(post);

for (i = 0; i < strlen(post); i++)

{

if (post[i] >= '0' && post[i] <= '9')

{

pushstack(i);

}

if (post[i] == '+' || post[i] == '-' || post[i] == '*' || post[i] == '/' || post[i] == '^')

{

calculator(post[i]);

}

}

printf("\n\nResult :: %d", stack[top]);

getch();

Page 69: P&DS LAB

}

void pushstack(int tmp)

{

top++;

stack[top] = (int)(post[tmp] - 48);

}

void calculator(char c)

{

int a, b, ans;

a = stack[top];

stack[top] = '\0';

top--;

b = stack[top];

stack[top] = '\0';

top--;

switch (c)

{

case '+':

ans = b + a;

break;

case '-':

ans = b - a;

break;

case '*':

ans = b*a;

break;

case '/':

ans = b / a;

break;

case '^':

ans = b^a;

break;

default:

Page 70: P&DS LAB

ans = 0;

}

top++;

stack[top] = ans;

}

Page 71: P&DS LAB

OUTPUT:

Insert a postfix notation: 1453*+8- Result:11

Page 72: P&DS LAB

LINKED LIST IMPLEMENTATION OF STACK

PROGRAM:

#include<stdio.h>#include<conio.h>#include<stdlib.h>push();void pop();void display();struct node{

int data;struct node *next;

}*top=NULL;void main(){

int ch;clrscr();printf("\n\n1.Push\n\n2.Pop\n\n3.Display");do{

printf("\n\nEnter your Choice :: ");scanf("%d",&ch);switch(ch){

case 1:push();break;

case 2:pop();break;

case 3:printf("\n\nContents of stack :: \t");display();break;

default:printf("\n\nInvalid Choice......");getch();exit(0);

}}while(ch<4);getch();

}push(){

int x;struct node *newnode;newnode=malloc(sizeof(struct node));

Page 73: P&DS LAB

printf("\n\nEnter the number to be pushed into the stack :: ");scanf("%d",&x);

newnode->data=x;if(top==NULL){

newnode->next=top;top=newnode;

}else{

newnode->next=top;top=newnode;

}printf("\n\nNumber pushed is %d",x);return(x);}

void pop(){

struct node *t;if(top==NULL)

printf("\n\nStack Underflow");else{

t=top;top=top->next;printf("\nDeleted element is %d",t->data);free(t);

}getch();

}void display(){

struct node*i;for(i=top;i!=NULL;i=i->next)

printf("%d , ",i->data);if(top==NULL)

printf("Stack is empty");getch();

}

Page 74: P&DS LAB

OUTPUT:

1.Push2.Pop3.Display

Enter your Choice :: 1

Enter the number to be pushed into the stack :: 5

Number pushed is 5

Enter your Choice :: 1

Enter the number to be pushed into the stack :: 10

Number pushed is 10

Enter your Choice :: 3

Contents of stack :: 10 , 5 ,

Enter your Choice :: 2

Deleted element is 10

Enter your Choice :: 3

Contents of stack :: 5 ,

Enter your Choice :: 5

Invalid Choice......

Page 75: P&DS LAB

LINKED LIST IMPLEMENTATION OF QUEUE

PROGRAM:

#include <stdlib.h>

typedef struct node

{

 int data;

 struct node *link;

} NODE;

 

void Insert(int);

int Delete();

void Display();

NODE *front, *rear; /* Global Declarations */

 

main()

{

 /* Main Program */

 int opn, elem;

 front = rear = NULL;

 do {

  clrscr();

  printf("\n ### Linked List Implementation of QUEUE Operations ### \n\n");

  printf("\n Press 1-Insert, 2-Delete, 3-Display,4-Exit\n");

  printf("\n Your option ? ");

  scanf("%d", &opn);

  switch (opn) {

  case 1:

   printf("\n\nRead the Element to be Inserted ?");

   scanf("%d", &elem);

   Insert(elem);

   break;

Page 76: P&DS LAB

  case 2:

   elem = Delete();

   if (elem != -1)

    printf(" Deleted Node(From Front)with the Data: %d\n", elem);

   break;

  case 3:

   printf("Linked List Implementation of Queue: Status:\n");

   Display();

   break;

  case 4:

   printf("\n\n Terminating \n\n");

   break;

  default:

   printf("\n\nInvalid Option !!! Try Again !! \n\n");

   break;

  }

  printf("\n\n\n\n  Press a Key to Continue . . . ");

  getch();

 } while (opn != 4);

}

 

void Insert(int info) {

 NODE *temp;

 temp = (NODE *) malloc(sizeof(NODE));

 if (temp == NULL)

  printf(" Out of Memory !! Overflow !!!");

 else {

  temp->data = info;

  temp->link = NULL;

  if (front == NULL) {

   front = rear = temp;

  } /* First Node? */

  else {

   rear->link = temp;

   rear = temp;

Page 77: P&DS LAB

  } /* Insert End */

  printf(" Node has been inserted at End Successfully !!");

 }

}

 int Delete() {

 int info;

 NODE *t;

 if (front == NULL) {

  printf(" Underflow!!!");

  return -1;

 } else {

  t = front;

  info = front->data;

  if (front == rear)

   rear = NULL;

  front = front->link;

  t->link = NULL;

  free(t);

  return (info);

 }

}

 void Display() {

 NODE *t;

 if (front == NULL)

  printf("Empty Queue\n");

 else {

  t = front;

  printf("Front->");

  while (t) {

   printf("[%d]->", t->data);

   t = t->link;

  }

  printf("Rear\n");

 }

}

Page 78: P&DS LAB

OUTPUT:

### Linked List Implementation of Queue operation ###

Press 1-Insert, 2-Delete, 3-Display, 4-Exit Your option ? 1 Read the element to be inserted ? 10 Node has been inserted at end successfully !! Press a Key to continue …….

Your option ? 1 Read the element to be inserted ? 20 Node has been inserted at end successfully !! Press a Key to continue ……. Your option ? 1 Read the element to be inserted ? 30 Node has been inserted at end successfully !! Press a Key to continue …….

Your option ? 1 Read the element to be inserted ? 40 Node has been inserted at end successfully !! Press a Key to continue ……. Your option ? 3 Linked List Implementation of Queue:Status: Front->[10]->[20]->[30]->[40]->Rear Press a Key to continue ……. Your option ? 4

Terminating Press a Key to continue …….

Page 79: P&DS LAB

SORTING ALGORITHMS

BUBBLE SORT

PROGRAM :

#include<stdio.h>

#include<conio.h> void

bubble(int [],int); void

main()

{

int a[20],i,n;

clrscr();

printf("Enter the number of items in the array");

scanf("%d",&n);

printf("Enter the data in the array");

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

{

scanf("%d",&a[i]);

} bubble(a,n);

getch();

}

void bubble(int a[],int n)

{

int i,temp,j,p;

for(i=1;i<n;i++)

{

for(p=0;p<n-i;p++)

{

if(a[p]>a[p+1])

{

Page 80: P&DS LAB

temp=a[p];

a[p]=a[p+1];

a[p+1]=temp;

}

}

}

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

printf("\n%d",a[i]);

}

Page 81: P&DS LAB

OUTPUT: Enter the number of items in the array 5 35 25 15 65 5 Enter the data in the array 5 15 25 35 65

Page 82: P&DS LAB

SELECTION SORT

PROGRAM :

#include<stdio.h>

#include<conio.h> void

select(int [],int); void

bubble(int [],int); int

min(int [],int,int);

void main()

{

int a[20],i,n;

clrscr();

printf("Enter the number of items in the array");

scanf("%d",&n);

printf("Enter the data in the array");

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

{

scanf("%d",&a[i]);

}

bubble(a,n);

select(a,n);

getch();

}

void bubble(int a[],int n)

{

int i,temp,p;

for(i=1;i<n;i++)

{

for(p=0;p<n-i;p++)

{

if(a[p]>a[p+1])

Page 83: P&DS LAB

{

temp=a[p];

a[p]=a[p+1];

a[p+1]=temp;

}

}

}

printf("\nData After Bubble Sort");

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

printf("\n%d",a[i]);

}

void select(int a[],int n){

int i,loc,temp; loc=0; temp=0;for(i=0;i<n;i++){

loc=min(a,i,n); temp=a[loc];

a[loc]=a[i]; a[i]=temp;

}printf("\nData After Selection Sort"); for(i=0;i<n;i++)printf("\n%d",a[i]);

}

int min(int a[],int lb,int ub){

int m=lb; while(lb<ub){if(a[lb]<a[m]){

m=lb;}

lb++;}

return m; }

Page 84: P&DS LAB

OUTPUT: Enter the number of items in the array 4 Enter the data in the array

10 23 89 45 Data After Bubble sort 10 23 45 89

Data After Selection sort 10 23 45 89

Page 85: P&DS LAB

INSERTION SORT

PROGRAM :

#include<stdio.h>

int main(){

 int i,j,s,temp,a[20];

  printf("Enter total elements: ");

  scanf("%d",&s);

  printf("Enter %d elements: ",s);

  for(i=0;i<s;i++)

      scanf("%d",&a[i]);

  for(i=1;i<s;i++){

      temp=a[i];

      j=i-1;

      while((temp<a[j])&&(j>=0)){

       a[j+1]=a[j];

          j=j-1;

      }

      a[j+1]=temp;

  }

  printf("After sorting: ");

  for(i=0;i<s;i++)

      printf(" %d",a[i]);

  return 0;

}

Page 86: P&DS LAB

OUTPUT:

Enter total elements: 5

Enter 5 elements: 3 7 9 0 2

After sorting:  0 2 3 7 9

Page 87: P&DS LAB

QUICK SORT

PROGRAM :

#include<stdio.h>

void quicksort(int [10],int,int);

int main(){

  int x[20],size,i;

  printf("Enter size of the array: ");

  scanf("%d",&size);

  printf("Enter %d elements: ",size);

  for(i=0;i<size;i++)

    scanf("%d",&x[i]);

  quicksort(x,0,size-1);

  printf("Sorted elements: ");

  for(i=0;i<size;i++)

    printf(" %d",x[i]);

  return 0;

}

void quicksort(int x[10],int first,int last){

    int pivot,j,temp,i;

     if(first<last){

         pivot=first;

         i=first;

         j=last;

         while(i<j){

             while(x[i]<=x[pivot]&&i<last)

                 i++;

             while(x[j]>x[pivot])

                 j--;

             if(i<j){

                 temp=x[i];

                  x[i]=x[j];

Page 88: P&DS LAB

                  x[j]=temp;

             }

         }

         temp=x[pivot];

         x[pivot]=x[j];

         x[j]=temp;

         quicksort(x,first,j-1);

         quicksort(x,j+1,last);

    }

}

Page 89: P&DS LAB

OUTPUT: Enter size of the array: 5

Enter 5 elements: 3 8 0 1 2

Sorted elements: 0 1 2 3 8

Page 90: P&DS LAB

LINEAR SEARCH

PROGRAM :

#include<stdio.h>

#include<conio.h>

void main()

{

int

a[10],i,no,item,flag=0;

char c=’y’,ch;

clrscr();

while(c==’y’||’Y’)

{

printf(“\nEnter the sizeof sorting”);

scanf(%d’,&no);

printf("Enter the data in the array");

for(i=0;i<10;i++)

{

scanf("%d",&a[i]);

}

printf("Enter the element to be searched");

scanf("%d",&item);

for(i=0;i<10;i++)

{

if(item==a[i])

{

flag=1;

break;

}

Page 91: P&DS LAB

}

if(flag==0)

printf("Element Not Found");

else

printf("Element Found at Position =%d",i);

getch();

}

Page 92: P&DS LAB

OUTPUT:

Enter the size of sorting 5 Enter the elements of the array 3 2 1 6 5 Enter the element to be searched 4 Element 4 is Not Found Enter the element to be searched 6 Element 6 is in the position 4

BINARY SEARCH

Page 93: P&DS LAB

PROGRAM :

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

void main(){

int a[20],n,mid,beg,i,end,item,loc=-1;

clrscr();printf("Enter the number of elements to be entered\n");scanf("%d",&n);printf("Enter the elements in ascending order");

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

scanf("%d",&a[i]);}

printf("Enter the element to be searched");

scanf("%d",&item);beg=0; end=n-1; while(beg<=end)

{mid=(beg+end)/2; if(item==a[mid]){ loc=mid; break;}

else if(a[mid]<item) beg=mid+1;else end=mid-1;}if(loc==-1)printf("Element Not Present"); elseprintf("Element found at =%d",loc); getch();}

Page 94: P&DS LAB

OUTPUT:

Enter the number of elements to be entered 5 Enter the elements in ascending order 1 3 4 5 6 Enter the elements to be searched 3 Element 3 found at position 2 Enter the elements to be searched 2 Element Not Present