DD
DD
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
III SEMESTER
Computer Science & Engineering
Department
Laboratory
Manual
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
CERTIFICATE
Head Of Department:...........................................
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
TABLE OF CONTENT
Page No Date of Mark
Sr Date of
Experiment Title Completi Sign s (out
. Start
From To on of 10)
N
o
Introduction to pointers. (a) Call
by Value (write a function that
1. return max of two passed value)
(b) Call by reference.(write a
function to
swap value of two variable).
Introduction to Dynamic
Memory Allocation. DMA
functions malloc(), calloc(),
2. free() etc. (a) W.A.P. to create
dynamic int array using
malloc() and free() (b) W.A.P. to
create dynamic char array
using calloc() and free().
3. Write a program to implement
structure in c.
Write a program to implement
4. (a) linear Search (b) Binary
Search.
Implement a program for
stack that performs following
5. operations using array.
(a)PUSH (b) POP (c)
PEEP (d) CHANGE (e)
DISPLAY.
Implement a program to convert
6. infix notation to postfix
notation using stack.
7. Implement a program to
evaluate postfix notation.
Write a program to implement
QUEUE using arrays that
8. performs following operations
(a)INSERT (b) DELETE (c)
DISPLAY.
Write a menu driven program
to implement following
operations on the singly linked
9. list. (a) Insert a node at the
front of the linked list.
(b) Insert a node at the end
of the linked list.
Write a program to
10. implement (a) Bubble Sort
(b) Insertion Sort (c)
Selection Sort.
Practical 1
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
Aim :- Introduction to pointers. (a) Call by Value (write a function that return max of two passed
value) (b) Call by reference.(write a function to swap value of two variable).
Call by reference
INPUT
#include <stdio.h>
void swap(int*, int*);
int main()
{
int x, y;
printf("Enter the value of x\n");
scanf("%d",&x);
printf("Enter the value of y\n");
scanf("%d",&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
swap(&x, &y);
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
void swap(int *a, int *b)
{
int temp;
temp = *b;
*b = *a;
*a = temp;
}
Output -
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
INPUTs
Call by value
#include <stdio.h> void
swap(int, int); int main()
{
int x, y;
printf("Enter the value of x and y\n"); scanf("%d
%d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
swap(x, y);
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
void swap(int a, int b)
{
int temp;
temp = b;
b = a;
a = temp;
printf("Values of a and b is %d %d\n",a,b);
}
Output -
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
INPUT
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
Practical-2
Calloc
Aim:- Introduction to Dynamic Memory Allocation. DMA functions malloc(), calloc(), free() etc. (a) W.A.P.
to create dynamic int array using malloc() and free() (b) W.A.P. to create dynamic char array using calloc()
and free().
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n , count, *ptr,sum=0;
printf("enter no of elements");
scanf("%d",&count);
ptr=(int*)calloc(count,sizeof(int));
if(ptr==0)
{
printf("memory is not allocated"); exit
(0);
}
for(n = 0;n<count;n++)
{
printf("Enter element %d : ",(n+1));
scanf("%d",ptr+n);
sum+= *(ptr+n);
}
printf("sum is %d \n",sum);
free(ptr);
return 0;.
Output –
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
Malloc
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n , count, *ptr,sum=0;
printf("enter no of elements");
scanf("%d",&count);
ptr=(int*)malloc(count*sizeof(int));
if(ptr==0)
{
printf("mempory is not allocated"); exit
(0);
}
for(n = 0;n<count;n++)
{
printf("Enter element %d : ",(n+1));
scanf("%d",ptr+n);
sum += *(ptr+n);
}
printf("sum is %d \n",sum);
free(ptr);
return 0;
}
Output –
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
INPUT
Practical 3
Aim:- Write a program to implement structure in c.
#include <stdio.h>
struct Distance
{
int feet;
float inch;
}
dist1, dist2, sum;
int main()
{
printf("1st distance\n");
printf("Enter feet: ");
scanf("%d", &dist1.feet);
printf("Enter inch: ");
scanf("%f", &dist1.inch);
printf("2nd distance\n");
printf("Enter feet: ");
scanf("%d", &dist2.feet);
printf("Enter inch: ");
scanf("%f", &dist2.inch);
sum.feet = dist1.feet + dist2.feet;
sum.inch = dist1.inch + dist2.inch;
while (sum.inch >= 1.
++sum.feet;
sum.inch = sum.inch - 12;
}
printf("Sum of distances = %d\'-%.1f\"", sum.feet, sum.inch); return
0;
}
Output –
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
Practical 4
INPUT
Aim:- Write a program to implement (a) linear Search (b) Binary Search.
#include <stdio.h>
#include<stdlib.h>
void main() {
int arr[5],p,q,i;
p=0;
for(i=0; i<5; i++)
{
printf("enter %dth element = ",i);
scanf("%d",&arr[i]);
}
printf("Enter your number for comparison - ");
scanf("%d",&q);
for(i=0; i<5; i++)
{
if(arr[i]==q)
{
printf("the element is in the array at %d position",i+1);
p++;
}
}
if(p==0)
{
printf("no result found\n");
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
}
}
Output –
Binary search
INPUT
#include <stdio.h>
void main() {
int arr[10],flag,q,low,high,mid,i;
flag=0;
low=0;
high=9;
for(i=0; i<10; i++)
{
printf("enter %dth element = ",i);
scanf("%d",&arr[i]);
}
printf("enter your value for comparison - ");
scanf("%d",&q);
while(low<=high)
{
mid=low+high/2;
if(arr[mid]==q)
{
flag++;
break;
}
else if (arr[mid]<q)
low = mid++;
else
high=mid--;
}
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
if(flag==1)
{
printf("SUCCESS\n");
}
else
printf("Not success");
}Output –
Practical 5
Aim:- Implement a program for stack that performs following operations using array.
(a)PUSH (b) POP (c) PEEP (d) CHANGE (e) DISPLAY.
INPUT
#include<stdio.h>
#define M 5
int top=-1,i,d;
int stack[M];
void push (int b);
int pop();
int display();
int peep();
int main()
{
int a,b,c=1;
while(c ==1)
{
printf("LISTED MENU\n");
printf("1 - Push Operation\n");
printf("2 - Pop Operation\n");
printf("3 - Display Operation\n");
printf("4-peep\n");
printf("5 - For exiting\n");
printf("Enter your choice - \n");
scanf("%d",&a);
switch(a)
{
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
}
return 0;
}
void push(int b)
{
if(top>=M-1)
{
printf("stack is full");
}
else
{
top=top+1;
stack[top]=b;
}
}
int pop()
{
if(top == -1)
{
printf("stack is underflow , insert the element");
}
else
{
d=stack[top];
top= top - 1;
printf("last inserted number deleted is %d",d);
}
}
int display()
{
for(i=top;i>0;i--)
printf("\t%d",stack[i]);
}
int peep()
{
printf("\n\t top = %d ",top); printf("\n\
t value = %d ",stack[top]);
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
}
Output –
Practical 6
Aim:- Implement a program to convert infix notation to postfix notation using stack.
INPUT
#include<stdio.h>
#define max 40
typedef struct stack{
int data[max];
int top;
}stack;
else if(scanned=='(')
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
push(&s,'(');
else{
if(scanned==')'){
while((x=pop(&s))!='(')
postfix[j++]=x;
}
else{
while(precedence(scanned)<=precedence(top(&s
))&& !empty(&s)){
x=pop(&s);
postfix[j++]=x;
}
push(&s, scanned);
}
}
}
while(!empty(&s)){
x=pop(&s);
postfix[j++]=x;
}
postfix[j]='\0';
}
int main(){
char infix[30],postfix[30];
printf("Enter the infix expression:");
gets(infix);
infixtopostfix(infix, postfix);
printf("postfix expression %s", postfix);
}
Output –
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
Practical -7
Aim:- Implement a program to evaluate postfix notation.
INPUT
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
// Stack type
struct Stack
{
int top;
array;
};
// Stack Operations
stack->top = -1;
stack->capacity = capacity;
return stack;
}
return stack->top == -1 ;
}
return stack->array[stack->top];
}
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
if (!isEmpty(stack))
'$';
}
stack->array[++stack->top] = op;
}
int i;
(isdigit(exp[i]))
switch (exp[i])
return pop(stack);
}
int main()
{
Practical -8
Aim:- Write a program to implement QUEUE using arrays that performs following operations
(a)INSERT (b) DELETE (c) DISPLAY.
INPUT
#include <stdio.h>
#define SIZE 5
void enQueue(int);
void deQueue(); void
display();
int main() {
//deQueue is not possible on empty queue deQueue();
//enQueue 5 elements
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
display();
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
return 0;
}
void deQueue() {
if (front == -1)
printf("\nQueue is Empty!!");
else {
printf("\nDeleted : %d", items[front]);
front++;
if (front > rear)
front = rear = -1;
}
}
PRACTICAL -9
Aim:-Write a menu driven program to implement following operations on the singly linked
list. (a) Insert a node at the front of the linked list. (b) Insert a node at the end of the linked
list.
INPUT
#include <stdio.h>
struct node {
int info;
struct node* link;
};
struct node* start = NULL;
void traverse()
{
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
else {
temp = start;
while (temp != NULL) {
printf("Data = %d\n",
temp->info);
temp = temp->link;
}
}
}
void insertAtFront()
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
{
int data;
struct node* temp;
temp = malloc(sizeof(struct node));
printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);
temp->info = data;
temp->link = start;
start = temp;
}
void insertAtEnd()
{
int data;
struct node *temp, *head;
temp = malloc(sizeof(struct node));
printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);
temp->link = 0;
temp->info = data;
head = start;
while (head->link != NULL) {
head = head->link;
}
head->link = temp;
}
void insertAtPosition()
{
struct node *temp, *newnode;
int pos, data, i = 1;
newnode = malloc(sizeof(struct node));
printf("\nEnter position and data :");
scanf("%d %d", &pos, &data);
temp = start; newnode-
>info = data; newnode-
>link = 0; while (i <
pos - 1) {
temp = temp->link;
i++;
}
newnode->link = temp->link;
temp->link = newnode;
}
void deleteFirst()
{
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
else {
temp = start; start
= start->link;
free(temp);
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
}
}
void deleteEnd()
{
struct node *temp, *prevnode; if
(start == NULL)
printf("\nList is Empty\n");
else {
temp = start;
while (temp->link != 0) {
prevnode = temp; temp
= temp->link;
}
free(temp);
prevnode->link = 0;
}
}
void deletePosition()
{
struct node *temp, *position;
int i = 1, pos;
if (start == NULL) printf("\
nList is empty\n");
else {
printf("\nEnter index : ");
scanf("%d", &pos);
position = malloc(sizeof(struct node)); temp
= start;
while (i < pos - 1) {
temp = temp->link;
i++;
}
position = temp->link; temp-
>link = position->link;
free(position);
}
}
void maximum()
{
int a[10];
int i;
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
else {
temp = start;
int max = temp->info;
while (temp != NULL) {
if (max < temp->info)
max = temp->info;
temp = temp->link;
}
printf("\nMaximum number " "is
: %d ",
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
max);
}
}
void mean()
{
int a[10];
int i;
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
else {
temp = start;
int sum = 0, count = 0;
float m;
while (temp != NULL) { sum
= sum + temp->info; temp
= temp->link; count++;
}
m = sum / count; printf("\
nMean is %f ", m);
}
}
void sort()
{
struct node* current = start;
struct node* index = NULL;
int temp;
if (start == NULL)
{ return;
}
else {
while (current != NULL) {
index = current->link;
while (index != NULL)
{
if (current->info > index->info) {
temp = current->info;
current->info = index->info;
index->info = temp;
}
index = index->link;
}
current = current->link;
}
}
}
void reverseLL()
{
struct node *t1, *t2, *temp;
t1 = t2 = NULL;
if (start == NULL)
printf("List is empty\n");
else {
while (start != NULL)
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
{ t2 = start->link;
start->link = t1;
t1 = start;
start = t2;
}
start = t1;
temp = start;
printf("Reversed linked " "list
is : ");
while (temp != NULL) {
printf("%d ", temp->info);
temp = temp->link;
}
}
}
int main()
{
int choice; while (1) {
printf("\n\t1 To see list\n");
printf("\t2 For insertion at"
" starting\n"); printf("\t3
For insertion at"
" end\n");
printf("\t4 For insertion at "
"any position\n");
printf("\t5 For deletion of "
"first element\n"); printf("\
t6 For deletion of " "last
element\n"); printf("\t7 For
deletion of "
"element at any position\n");
printf("\t8 To find maximum among"
" the elements\n");
printf("\t9 To find mean of "
"the elements\n"); printf("\t10
To sort element\n"); printf("\t11
To reverse the "
"linked list\n"); printf("\
t12 To exit\n"); printf("\
nEnter Choice :\n");
scanf("%d", &choice); switch
(choice) {
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAtPosition();
break;
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR
case 5:
deleteFirst();
break;
case 6:
deleteEnd();
break;
case 7:
deletePosition();
break;
case 8:
maximum();
break;
case 9:
mean();
break;
case 10:
sort();
break; case
11:
reverseLL();
break;
case 12:
exit(1);
break;
default:
printf("Incorrect Choice\n");
}
}
return 0;
}
OUTPUT
COMPUTER SCIENCE AND
ENGINEERING FACULTY OF
ENGINEERING & TECHNOLOGY DSA
(203105206) B. Tech. 2nd YEAR