Sie sind auf Seite 1von 52

CS6212-PROGRAMMING & DATA STRUCTURES LAB

Syllabus:
1. C Programs using Conditional and Control Statements
2. C Programs using Arrays, Strings and Pointers and Functions
3. Representation of records using Structures in C Creation of Linked List
Manipulation of records in a Linked List
4. File Handling in C Sequential access Random Access
5. Operations on a Stack and Queue infix to postfix simple expression
evaluation using stacks - Linked Stack Implementation Linked Queue
Implementation
6. Implementation of Sorting algorithms
7. Implementation of Linear search and Binary Search.

Page 1

CS6212-PROGRAMMING & DATA STRUCTURES LAB

1. C PROGRAMS USING CONDITIONAL AND CONTROL STATEMENTS


Program to illutrate if-else - Program to find odd or even number
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("enter the num\n");
scanf("%d",&n);
if(n%2==0)
printf("the num is even\n");
else
printf("the num is odd");
getch();
}

OUTPUT:

Page 2

CS6212-PROGRAMMING & DATA STRUCTURES LAB

Program to Illustrate Conditional Operator- find greatest of two numbers


PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("enter the two numbers");
scanf("%d %d",&a,&b);
if((a>b)?printf("%d is greater"):printf("%d is greater"))
getch();
}
OUTPUT

Page 3

CS6212-PROGRAMMING & DATA STRUCTURES LAB

Program to illustrate nested if-else-find greatest of threee numbers


PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("enter the three numbers");
scanf("%d %d %d",&a,&b,&c);
if((a>b)&&(a>c))
printf("%d is greater",a);
else if((b>a)&&(b>c))
printf("%d is greater",b);
else
printf("%d is greater",c);
getch();
}

OUTPUT:

Page 4

CS6212-PROGRAMMING & DATA STRUCTURES LAB

Program to illustrate use of switch case


PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{int choice,r,a,b;
clrscr();
printf("enter the two num");
scanf("%d %d",&a,&b);
ch: printf("enter your choice\n");
printf("\n1)add\n2)sub\n3)mul\n4)div");
scanf("%d",&choice);
switch(choice)
{
case 1:(r=a+b);
printf("%d is sum",r);
break;
case 2:(r=a-b);
printf("%d is diff",r);
break;
case 3:(r=a*b);
printf("%d is product",r);
break;
case 4:r=a/b;
printf("%d is ratio",r);
break;
default:printf("plz enter correct value");
goto ch;
} getch();}
OUTPUT

Page 5

CS6212-PROGRAMMING & DATA STRUCTURES LAB

Program to illutrate for loop- find the factorial of given number


PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,fact=1,i;
clrscr();
printf("enter the num");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("%d is the factorial",fact);
getch();
}

OUTPUT

Page 6

CS6212-PROGRAMMING & DATA STRUCTURES LAB

Program to illutrate do-while loop-find the fibonacci series


PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,fib,i=1,n;
clrscr();
printf("enter the range");
scanf("%d",&n);
do
{
fib=a+b;
a=b;
b=fib;
i++;
printf("%d\t",fib);
}
while(i<=n);
getch();
}

OUTPUT:

Page 7

CS6212-PROGRAMMING & DATA STRUCTURES LAB

Program to illustrate while loop-factorial of a number


PROGRAM
#include <stdio.h>
int main()
{
int number,factorial;
printf("Enter a number.\n");
scanf("%d",&number);
factorial=1;
while (number>0)
{
factorial=factorial*number;
--number;
}
printf("Factorial=%d",factorial);
return 0;
}
OUTPUT

Page 8

CS6212-PROGRAMMING & DATA STRUCTURES LAB

2. C PROGRAMS USING ARRAYS, STRINGS AND POINTERS AND


FUNCTIONS
(i)C PROGRAMS USING ARRAYS
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],b[100],sum[100],diff[100],i,n;
clrscr();
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the value of a:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the value of b:");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
{
sum[i]=a[i]+b[i];
printf("%d\t",sum[i]);
}
printf("\n");
for(i=0;i<n;i++)
{
diff[i]=a[i]-b[i];
printf("%d\t",diff[i]);
}
getch();
}

Page 9

CS6212-PROGRAMMING & DATA STRUCTURES LAB

OUTPUT:

Page 10

CS6212-PROGRAMMING & DATA STRUCTURES LAB

Finding Minimum and Maximum


PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
intn,i,a[100],max,min;
clrscr();
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
max=a[0];
for(i=1;i<n;i++)
{
if(max<a[i])
{
max=a[i];
}
}
printf("Maximum is %d\n",max);
min=a[0];
for(i=1;i<n;i++)
{
if(min>a[i])
{
min=a[i];
}
}
printf("Minimum is %d",min);
getch();
}

Page 11

CS6212-PROGRAMMING & DATA STRUCTURES LAB

OUTPUT:

Page 12

CS6212-PROGRAMMING & DATA STRUCTURES LAB

ARRAY TWO-DIMENSIONAL
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
inti,j,a[10][10],b[10][10],r,c;
clrscr();
printf("Enter the row and column value:");
scanf("%d %d",&r,&c);
printf("Enter the values for first matrix");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}printf(\nTranspose of Matrix\n);
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
printf("%d\t",a[j][i]);
}
printf("\n");
}
getch();
}

Output:

Page 13

CS6212-PROGRAMMING & DATA STRUCTURES LAB

(ii)POINTERS
Program to find sum of Array elements using pointers
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],n,*ptr,sum=0,i;
clrscr();
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the value of a:");
for(i=0;i<n;++i)
{
scanf("%d",&a[i]);
}
ptr=&a[0];
for(i=0;i<n;++i)
{
sum=sum+*ptr;
ptr++;
}
printf("Sum is %d",sum);
getch();
}
OUTPUT:

Page 14

CS6212-PROGRAMMING & DATA STRUCTURES LAB

(iii)STRINGS AND POINTERS


ACCESSING STRING ARRAY WITH POINTERS
PROGRAM
#include <stdio.h>
#include<string.h>
int main()
{
char input_str[20];
char *output_str;
strcpy(input_str, "Hello");
printf("input_str: %s\n", input_str);
output_str = strcpy(input_str, "World");
printf("input_str: %s\n", input_str);
printf("output_str: %s\n", output_str);
return 0;
}

OUTPUT

Page 15

CS6212-PROGRAMMING & DATA STRUCTURES LAB

ACCESSING DATA ARRAY WITH POINTERS


PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10];
int i,sum=0;
int *ptr;
printf("Enter 10 elements:n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
ptr = a;
/* a=&a[0] */
for(i=0;i<10;i++)
{
sum = sum + *ptr;
ptr++;
}
printf("The sum of array elements is %d",sum);
}
OUTPUT

Page 16

CS6212-PROGRAMMING & DATA STRUCTURES LAB

(iv)FUNCTIONS
CALLING FUNCTION WITHOUT ARGUMENTS AND WITH RETURN
VALUE
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
float sum;
float total();
clrscr();
sum = total();
printf(" Sum = %f\n" , sum);
}
float total()
{
float a, b;
a = 5.0 ;
b = 15.0 ;
return(a+b);
}

Page 17

CS6212-PROGRAMMING & DATA STRUCTURES LAB

CALLING FUNCTION WITH ARGUMENTS AND WITH RETURN VALUE


#include<stdio.h>
float calculate_area(int);
int main()
{
int radius;
float area;
printf("\nEnter the radius of the circle : ");
scanf("%d",&radius);
area = calculate_area(radius);
printf("\nArea of Circle : ",area);
return(0);
}
float calculate_area(int radius)
{
float areaOfCircle;
areaOfCircle = 3.14 * radius * radius;
return(areaOfCircle);
}
OUTPUT

Page 18

CS6212-PROGRAMMING & DATA STRUCTURES LAB

CALLING FUNCTION WITH ARGUMENTS AND WITHOUT RETURN


VALUE
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
void fact(int num);
clrscr();
printf("nEnter the number to find Factorial");
scanf("%f",&n);
fact(n);
getch();
}
void fact(int num)
{
int factorial=1,i;
for(i=1;i<=num;i++)
factorial=factorial*i;
printf("Factorial of the given number is = %d",factorial);
}
OUTPUT

Page 19

CS6212-PROGRAMMING & DATA STRUCTURES LAB

CALLING FUNCTION WITHOUT ARGUMENTS AND WITHOUT RETURN


VALUE
#include<stdio.h>
#include<conio.h>
void fib();
void main()
{
fib();
getch();
}
void fib()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n",n);
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}
}
OUTPUT

Page 20

CS6212-PROGRAMMING & DATA STRUCTURES LAB

CALL BY VALUE
PROGRAM
#include<stdio.h>
#include<conio.h>
void swap(int x, int y)
{
int z;
z = x;
x = y;
y = z;
printf("Swapped values in SWAP function are a = %d and b = %d", x, y);
}
void main()
{
int a ,b;
clrscr();
printf(Enter a value);
scanf(%d,&a);
printf(Enter b value);
scanf(%d,&b);
printf("Original values are a = %d and b = %d", a, b);
swap(a, b);
printf("The values after swap in MAIN function are a = %d and b = %d", a, b);
getch();
}
OUTPUT

Page 21

CS6212-PROGRAMMING & DATA STRUCTURES LAB

CALL BY REFERENCE
PROGRAM
#include<stdio.h>
#include<conio.h>
void swap(int *x, int *y)
{
int *z;
*z = *x;
*x = *y;
*y = *z;
printf("Swapped values in SWAP function are a = %d and b = %d",* x, *y);
}
void main()
{
int a ,b;
clrscr();
printf(Enter a value);
scanf(%d,&a);
printf(Enter b value);
scanf(%d,&b);
printf("Original values are a = %d and b = %d", a, b);
swap(&a, &b);
printf("The values after swap in MAIN function are a = %d and b = %d", a, b);
getch();
}
OUTPUT

Page 22

CS6212-PROGRAMMING & DATA STRUCTURES LAB

3. Representation of records using Structures in C Creation of Linked


List Manipulation of records in a Linked List
List implementation and Manipulation of records
PROGRAM:
#include<conio.h>
#include<stdio.h>
struct node
{
int data;
struct node *link;
}*p,*pre;
struct node *head=NULL;
int pos,i;
void create()
{
int no;
printf("enter the no of nodes:");
scanf("%d",&no);
if(no>=1)
{
p=(struct node*)malloc(sizeof(struct node));
printf("enter the data1:");
scanf("%d",&p->data);
head=p;
for(i=1;i<no;i++)
{
pre=p;
p=(struct node*)malloc(sizeof(struct node));;
pre->link=p;
printf("Enter the data %d :",i+1);
scanf("%d",&p->data);
}
p->link=NULL;
printf("List Created...");
}
Page 23

CS6212-PROGRAMMING & DATA STRUCTURES LAB

else
printf("Cannot create");
}
void insert()
{
if(head==0)
{
printf("* Cannot insert *");
}
else
{
printf("Enter the position:");
scanf("%d",&pos);
pre=head;
for(i=1;i<pos-1;i++)
{
pre=pre->link;
if(pre->link==NULL)
break;
}
if(pos>i+2||pos<1)
{
printf("*Position out of range");
}
else
{
p=(struct node*)malloc(sizeof(struct node));
printf("Enter the data:");
scanf("%d",&p->data);
if(pos==1)
{
p->link=head;
head=p;
}
else
{
p->link=pre->link;
pre->link=p;
}
Page 24

CS6212-PROGRAMMING & DATA STRUCTURES LAB

printf("Data has been inserted..");


}
}
}
void del()
{
if(head==0)
{
printf("* Cannot delete * list is empty*");
}
else
{
printf("Enter the position:");
scanf("%d",&pos);
pre=head;
for(i=1;i<pos-1;i++)
{
pre=pre->link;
if(pre->link==NULL)
break;
}
p=pre->link;
if(pos>i+1||pos<1)
{
printf("\nPosition out of range");
}
else
{
if(pos==1)
{
head=head->link;
printf("Data has been deleted...");
}
else
{
pre->link=p->link;
printf("data has been deleted...");
}
}
Page 25

CS6212-PROGRAMMING & DATA STRUCTURES LAB

}
}
void display()
{
printf("SINGLY LINKED LIST->");
p=head;
printf("%d",p->data);
while((p->link!=NULL)&&(head!=NULL))
{
printf("->");
p=p->link;
printf("%d",p->data);
}
}
void main()
{
int ch=0;
clrscr();
while(ch<5)
{
printf("\nSINGLY LINKED LIST MENU");
printf("\n1.Create\n2.Insert\n3.Delete\n4.Display\n5.Exit");
printf("\nEnter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: insert();
break;
case 3: del();
break;
case 4: display();
break;
default:printf("* Exit ****");
}
}
getch();
Page 26

CS6212-PROGRAMMING & DATA STRUCTURES LAB

}
OUTPUT:
SINGLY LINKED LIST MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter the choice:1
enter the no of nodes:3
enter the data1:10
Enter the data 2 :20
Enter the data 3 :30
List Created...
SINGLY LINKED LIST MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter the choice:2
Enter the position:2
Enter the data:60
Data has been inserted..
SINGLY LINKED LIST MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter the choice:4
SINGLY LINKED LIST->10->60->20->30
SINGLY LINKED LIST MENU
1.Create
2.Insert
Page 27

CS6212-PROGRAMMING & DATA STRUCTURES LAB

3.Delete
4.Display
5.Exit
Enter the choice:3
Enter the position:3
data has been deleted...
SINGLY LINKED LIST MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter the choice:4
SINGLY LINKED LIST->10->60->30
SINGLY LINKED LIST MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit
Enter the choice:5

5. Operations on a Stack and Queue infix to postfix simple expression


evaluation using stacks - Linked Stack Implementation Linked Queue
Implementation
(i)Operations on a Stack
STACK ARRAY IMPLEMNTATION
PROGRAM:
#include<stdio.h>
main()
{
int a[10]={0},i,top=-1,max=10,n,x;
printf("\n\tMENU\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n");
Page 28

CS6212-PROGRAMMING & DATA STRUCTURES LAB

do
{
printf("\nEnter your choice\n");
scanf("%d",&n);
switch(n)
{
case 1:
if(top==max-1)
printf("Overflow\n");
else
{
printf("Enter the element\n");
scanf("%d",&x);
a[++top]=x;
}
break;
case 2:
if(top<0)
printf("Underflow\n");
else
printf("The deleted item =%d",a[top--]);
break;
case 3:
if(top<0)
printf("The stack is empty\n");
else
{
printf("The elements of the stack are :");
for(i=0;i<=top;i++)
printf("%d\n",a[i]);
}
break;
case 4:
break;
default:
printf("Invalid choice\n");
break;
}
Page 29

CS6212-PROGRAMMING & DATA STRUCTURES LAB

}
while(n!=4);
}
OUTPUT:

STACK LINKED LIST IMPLEMENTATION


PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int Data;
struct Node *next;
}*top;
void popStack()
{
struct Node *temp, *var=top;
if(var==top)
{
top = top->next;
free(var);
}
else
Page 30

CS6212-PROGRAMMING & DATA STRUCTURES LAB

printf("\nStack Empty");
}
void push(int value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (top == NULL)
{
top=temp;
top->next=NULL;
}
else
{
temp->next=top;
top=temp;
}
}
void display()
{
struct Node *var=top;
if(var!=NULL)
{
printf("\nElements are as:\n");
while(var!=NULL)
{
printf("\t%d\n",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nStack is Empty");
}
int main(int argc, char *argv[])
{
int i=0;
clrscr();
top=NULL;
Page 31

CS6212-PROGRAMMING & DATA STRUCTURES LAB

printf(" \n1. Push to stack");


printf(" \n2. Pop from Stack");
printf(" \n3. Display data of Stack");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
{
int value;
printf("\nEnter a value to push into Stack: ");
scanf("%d",&value);
push(value);
display();
break;
}
case 2:
{
popStack();
display();
break;
}
case 3:
{
display();
break;
}
case 4:
{
struct Node *temp;
while(top!=NULL)
{
temp = top->next;
free(top);
top=temp;
}
exit(0);
Page 32

CS6212-PROGRAMMING & DATA STRUCTURES LAB

}
default:
{
printf("\nwrong choice for operation");
}
}
}
}
OUTPUT:

(ii)Operations on a Queue
QUEUE -ARRAY IMPLEMENTATION
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAX 10
void insert(int);
int del();
int queue[MAX], rear=0, front=0;
void display();
int main()
{
char ch , a='y';
int choice, token;
Page 33

CS6212-PROGRAMMING & DATA STRUCTURES LAB

printf("1.Insert");
printf("\n2.Delete");
printf("\n3.show or display");
do
{
printf("\nEnter your choice for the operation: ");
scanf("%d",&choice);
switch(choice)
{
case 1: insert(token);
display();
break;
case 2:
token=del();
printf("\nThe token deleted is %d",token);
display();
break;
case 3:
display();
break;
default:
printf("Wrong choice");
break;
}
printf("\nDo you want to continue(y/n):");
ch=getch();
}
while(ch=='y'||ch=='Y');
getch();
}
void display()
{
int i;
printf("\nThe queue elements are:");
for(i=rear;i<front;i++)
{
printf("%d ",queue[i]);
}
}
void insert(int token)
Page 34

CS6212-PROGRAMMING & DATA STRUCTURES LAB

{
char a;
if(rear==MAX)
{
printf("\nQueue full");
return;
}
do
{
printf("\nEnter the token to be inserted:");
scanf("%d",&token);
queue[front]=token;
front=front+1;
printf("do you want to continue insertion Y/N");
a=getch();
}
while(a=='y');
}
int del()
{
int t;
if(front==rear)
{
printf("\nQueue empty");
return 0;
}
rear=rear+1;
t=queue[rear-1];
return t;
}

Page 35

CS6212-PROGRAMMING & DATA STRUCTURES LAB

OUTPUT:

QUEUE-LINKED LIST IMPLEMENTATION


PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{
int Data;
struct Node* next;
}*rear, *front;
void delQueue()
{
struct Node *temp, *var=rear;
if(var==rear)
{
rear = rear->next;
free(var);
}
else
printf("\nQueue Empty");
Page 36

CS6212-PROGRAMMING & DATA STRUCTURES LAB

}
void push(int value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (front == NULL)
{
front=temp;
front->next=NULL;
rear=front;
}
else
{
front->next=temp;
front=temp;
front->next=NULL;
}
}
void display()
{
struct Node *var=rear;
if(var!=NULL)
{
printf("\nElements are as: ");
while(var!=NULL)
{
printf("\t%d",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nQueue is Empty");
}
int main()
{
int i=0;
clrscr();
Page 37

CS6212-PROGRAMMING & DATA STRUCTURES LAB

front=NULL;
printf(" \n1. Enqueue to Queue");
printf(" \n2. Dequeue from Queue");
printf(" \n3. Display Data of Queue");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
{
int value;
printf("\nEnter a value to push into Queue: ");
scanf("%d",&value);
push(value);
display();
break;
}
case 2:
{
delQueue();
display();
break;
}
case 3:
{
display();
break;
}
case 4:
{
exit(0);
}
default:
{
printf("\nwrong choice for operation");
}
}}}
Page 38

CS6212-PROGRAMMING & DATA STRUCTURES LAB

OUTPUT:

(iii)infix to postfix simple expression evaluation using stacks


INFIX TO POST FIX USING STACK:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 50
char stack[MAX],ch;
char in[50],post[50];
int top=-1;
void push(char ch)
{
if(top==MAX-1)
return;
stack[++top]=ch;
}
char pop()
{
if(top==-1)
return'\0';
else
Page 39

CS6212-PROGRAMMING & DATA STRUCTURES LAB

return stack[top--];
}
int prece(char ch)
{
if(ch=='*'||ch=='/')
return 2;
else if(ch=='+'||ch=='-')
return 1;
else
return 0;
}
void postfix()
{
int i=0,j=0;
push('(');
while(in[i]!='\0')
{
if(isdigit(in[i]))
post[j++]=in[i];
else if (in[i]=='(')
push('(');
else if(in[i]==')')
{
while((ch=pop())!='(')
post[j++]=ch;
}
else if(in[i]!=0)
{
while(prece(ch=pop())>=prece(in[i]))
post[j++]=ch;
push(ch);
push(in[i]);
}
i++;
}
post[j]='\0';
printf("postfix notation is %s\n",post);
}
void evalpost()
{
Page 40

CS6212-PROGRAMMING & DATA STRUCTURES LAB

int i=0,op1,op2;
char c;
while( (ch=post[i++]) != '\0')
{
if(isdigit(ch))
{
push(ch-'0'); /* Push the operand */
}
else
{
/* Operator,pop two operands */
op2=pop();
op1=pop();
switch(ch)
{
case '+':push(op1+op2);break;
case '-':push(op1-op2);break;
case '*':push(op1*op2);break;
case '/':push(op1/op2);break;
}
}
}
//printf("\nGiven Postfix Expn: %s\n",post);
printf("\nResult after Evaluation: %d\n",stack[top]);
}
void main()
{
int len;
clrscr();
printf("\nEnter expression in INFIX notation\n");
gets(in);
len=strlen(in);
in[len]=')';
in[len+1]='\0';
postfix();
evalpost();
getch();
}

Page 41

CS6212-PROGRAMMING & DATA STRUCTURES LAB

OUTPUT:

6. Implementation of Sorting algorithms


INSERTION SORTING
PROGRAM :
#include<stdio.h>
#include<conio.h>
void main(){
int i,j,s,temp,a[20];
clrscr();
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;
}
Page 42

CS6212-PROGRAMMING & DATA STRUCTURES LAB

printf("After sorting: ");


for(i=0;i<s;i++)
printf(" %d",a[i]);
getch();
}
OUTPUT:

SELECTION SORT IMPLEMENTATION USING ARRAY


PROGRAM:
#include<stdio.h>
#include<conio.h>
void main(){
int s,i,j,temp,a[20];
clrscr();
printf("Enter total elements: ");
scanf("%d",&s);
printf("Enter %d elements: ",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=0;i<s;i++){
for(j=i+1;j<s;j++){
Page 43

CS6212-PROGRAMMING & DATA STRUCTURES LAB

if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("After sorting is: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
getch();
}
OUTPUT:

BUBBLE SORT IMPLEMENTATION USING ARRAY


PROGRAM:
#include<stdio.h>
#include<conio.h>
void main(){
int s,temp,i,j,a[20];
clrscr();
printf("Enter total numbers of elements: ");
Page 44

CS6212-PROGRAMMING & DATA STRUCTURES LAB

scanf("%d",&s);
printf("Enter %d elements: ",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
//Bubble sorting algorithm
for(i=s-2;i>=0;i--){
for(j=0;j<=i;j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("After sorting: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
getch();
}

OUTPUT:

Page 45

CS6212-PROGRAMMING & DATA STRUCTURES LAB

QUICK SORT IMPLEMENTATION USING ARRAY :


PROGRAM :
#include<stdio.h>
#include<conio.h>
void quicksort(int [10],int,int);
void main(){
int x[20],size,i;
clrscr();
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]);
getch();
}
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];
x[j]=temp;
}
}
Page 46

CS6212-PROGRAMMING & DATA STRUCTURES LAB

temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
OUTPUT:

MERGE SORT IMPLEMENTATION USING ARRAY


PROGRAM :
#include<stdio.h>
#include<conio.h>
#define MAX 50
void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);
void main(){
int merge[MAX],i,n;
clrscr();
printf("Enter the total number of elements: ");
Page 47

CS6212-PROGRAMMING & DATA STRUCTURES LAB

scanf("%d",&n);
printf("Enter the elements which to be sort: ");
for(i=0;i<n;i++){
scanf("%d",&merge[i]);
}
partition(merge,0,n-1);
printf("After merge sorting elements are: ");
for(i=0;i<n;i++){
printf("%d ",merge[i]);
}
getch();
}
void partition(int arr[],int low,int high){
int mid;
if(low<high){
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}
void mergeSort(int arr[],int low,int mid,int high){
int i,m,k,l,temp[MAX];
l=low;
i=low;
m=mid+1;
while((l<=mid)&&(m<=high)){
if(arr[l]<=arr[m]){
temp[i]=arr[l];
l++;
}
else{
temp[i]=arr[m];
m++;
}
i++;
}
if(l>mid){
for(k=m;k<=high;k++){
temp[i]=arr[k];
Page 48

CS6212-PROGRAMMING & DATA STRUCTURES LAB

i++;
}
}
else{
for(k=l;k<=mid;k++){
temp[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++){
arr[k]=temp[k];
}
}
OUTPUT:

7. Implementation of Linear search and Binary Search.


Implementation of Linear search
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
Page 49

CS6212-PROGRAMMING & DATA STRUCTURES LAB

{
int arr[20];
int i,size,sech;
clrscr();
printf("\n\t-- Linear Search --\n\n");
printf("Enter total no. of elements : ");
scanf("%d",&size);
for(i=0; i<size; i++)
{
printf("Enter %d element : ",i+1);
scanf("%d",&arr[i]);
}
printf("Enter the element to be searched: ");
scanf("%d",&sech);
for(i=0; i<size; i++)
{
if(sech==arr[i])
{
printf("Element exits in the list at position : %d",i+1);
break;
}
}
getch();
}
OUTPUT:

Page 50

CS6212-PROGRAMMING & DATA STRUCTURES LAB

Implementation Binary Search.


BINARY SEARCH :
PROGRAM :
#include<stdio.h>
#include<conio.h>
void main(){
int a[10],i,n,m,c=0,l,u,mid;
clrscr();
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements in ascending order: ");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&m);
l=0,u=n-1;
while(l<=u){
mid=(l+u)/2;
if(m==a[mid]){
c=1;
break;
}
else if(m<a[mid]){
u=mid-1;
}
else
l=mid+1;
}
if(c==0)
printf("The number is not found.");
else
printf("The number is found.");
getch();
}

Page 51

CS6212-PROGRAMMING & DATA STRUCTURES LAB

OUTPUT:

Page 52

Das könnte Ihnen auch gefallen