CS6212 PDS Lab Manual CSE 2013 Regulations
CS6212 PDS Lab Manual CSE 2013 Regulations
CS6212 PDS Lab Manual CSE 2013 Regulations
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
OUTPUT:
Page 2
Page 3
OUTPUT:
Page 4
Page 5
OUTPUT
Page 6
OUTPUT:
Page 7
Page 8
Page 9
OUTPUT:
Page 10
Page 11
OUTPUT:
Page 12
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
(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
OUTPUT
Page 15
Page 16
(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
Page 18
Page 19
Page 20
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
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
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
}
}
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
}
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
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
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
}
while(n!=4);
}
OUTPUT:
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
}
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
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
{
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
OUTPUT:
}
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
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
OUTPUT:
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
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
OUTPUT:
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:
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
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
OUTPUT:
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
i++;
}
}
else{
for(k=l;k<=mid;k++){
temp[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++){
arr[k]=temp[k];
}
}
OUTPUT:
{
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
Page 51
OUTPUT:
Page 52