You are on page 1of 81

SRI VENKATESWARA COLLEGE OF ENGINEERING

SRIPERUMBUDUR.

LAB MANUAL
CS16361 PROGRAMMING AND DATA STRUCTURES
LABORATORY
YEAR : II Yr B.E (Common to EC and EE)
SEMESTER : 3

Prepared by Approved by
Signature
Name Ms S.Poorani Dr R.Anitha
Designation Assistant Professor /CS HOD/CS
D
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EXNO :1.a Conditional and Control Statement - Perfect number

AIM:

To check whether the given number is perfect number or not.

ALGORITHM:

Start.
Accept the number, n
Find the sum of factors of n and store in Sum
If sum equals to n then n is perfect number else n is not perfect number.
Stop

SAMPLE INPUT OUTPUT:

Enter the number 3


3 is not perfect number.

Enter the number 6


6 is perfect number

RESULT:
Thus a C program is implemented for perfect number verification
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EXNO:1b Area of four geometric shapes using functions

AIM:

To find area of four geometric shapes using functions

ALGORITHM:

Start.
Read radius of circle r, side of square a, length l and breadth b of rectangle, height h and base
bas of triangle.
Call the function cir(r), squ(a), rec(l,b), tri(h,bas).
Print the areas.
Stop.

Function cir(r)
Start
Return 3.14*r*r.
Stop.
Function squ(a)
Start
Return a*a.
Stop.
Function rec(l,b)
Start
Return l*b.
Stop.
Function tri(h,bas)
Start
Return o.5*h*bas.
Stop.

SAMPLE INPUT OUTPUT:

Enter the radius if circle, side of square, length and breadth of rectangle , height and base of
triangle respectively 123434
Area of circle=3.140000
Area of square=4
Area of rectangle=12
Area of triangle=6.00000

RESULT:
Hence area of four geometric shape is found using functions.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EXNO:1c Simple calculator using switch statement

AIM:
To design a calculator using switch statement

ALGORITHM:

Start.
Get two operands and the operator character from the user.
If the operator is ‘+‘ then add the operands.
If the operator is ‘-‘ then subtract one operand from the other operand.
If the operator is ‘*‘ then multiply the operands
If the operator is ‘/‘ then divide one operand by the other.
If the entered character is not matching any of these above four operators then print the
default statement ―Enter the correct operator‖
Stop.

SAMPLE INPUT OUTPUT:


Enter two numbers 2 3
Enter the operator /
2/3=0.666666

Enter two numbers 2 3


Enter the operator &
Operator is invalid.

RESULT:
Hence a calculator is designed using switch statement.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EXNO:1d Numbers divisible by 5

AIM:
To find numbers divisible number by 5

ALGORITHM:
Start
Read the value of n.
Initialize variable i to 1
Repeat loop n times
if i%5=0 print i , else print “ ”
Stop

SAMPLE INPUT OUTPUT:

enter the limit 27


5 10 15 20 25

RESULT:

Hence numbers divisible by ‘5’ is found


FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EXNO :1e Sum of digits , reverse of digits and largest of digits.

AIM:

To Find Sum of digits, reverse of number and largest of digits.

ALGORITHM:
Start.
Accept the number, N
Assign max to 0.
Extract digit by digit and sum the digits in a variable sum.
Find the reverse of the number N by extracting the last digit, multiplying by 10 and reducing
the number and store the reverse number in a variable rev
Extract digit by digit and compare it with max and store the largest digit in max.
Print sum, rev, max.
Stop

SAMPLE INPUT OUTPUT:


Enter the number 123
Sum of digits 6
Reverse of digits 321
Largest of digits 3

RESULT:
Hence sum of digits, reverse of number and largest of digits is found.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EXNO :1f Armstrong numbers from 1 to 1000

AIM:
To find Armstrong number from 1 to 1000

ALGORITHM:

Start
Make a loop Declare variable i=1 Increment i by 1 up to 1000
Find the sum of the cube of the individual digits of the number i and store in SUM1.
If SUM1 equals to i then print I else print “ ”
Stop

SAMPLE INPUT OUTPUT:

153 370 371 407

RESULT:

Hence Armstrong number between 1 and 1000 is found.


FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EXNO :1g Prime numbers

AIM:
To find ‘n’ prime numbers

ALGORITHM:

Start
Read the value of n.
To print the value 2 on screen.
Make a loop Declare variable i=3 Increment i by 1 upto n
Using another loop Declare variable j=2 From j=2 to j<i; divide i by j If i % j = = 0 then
print the number else print (― ―)
Stop

SAMPLE INPUT OUTPUT:


enter the limit- 100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

RESULT:

Hence ‘n’ prime numbers found.


FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EXNO :2a Arrays.


Largest element in an array.

AIM:
To find largest element in a array.

ALGORITHM:
1.Start
2.Read the value of n,a[n].
3.Call the function lar(a[n],n).
4.Print max.
5.Stop.

Function lar(a[10],n)
1.Start.
2. Make a loop Declare variable i=0 Increment i by 1 upto n.
3.if max is less than a[i],store a[i] in max.
4.Return max.
5.Stop.

SAMPLE INPUT OUTPUT:

Enter the limit- 5


Enter the elements- 12 5 9 10 2
largest element is 12

RESULT:

Hence largest element in an array is found.


FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EXNO :2b MATRIX ADDITION

AIM:
To perform matrix addition.

ALGORITHM:

1.Start.
2. Accept the number of rows and columns (m,n , p,q) for the 2 matrices a & b.
3. If m!=p && n!=q then write “Matrix addition is not possible.” goto Step .
4. Accept the elements of the 2 matrices.
5.call the function add(a,b,m,n)
6. Stop
Function add( a[10][10],b[10][10],m,n)
1.Start
2. Repeat Steps 5 to 8 for i = 1 to m.
3. Repeat Steps 6 to 7 for j = 1 to q.
4. c[i][j]=a[i][j]+b[i][j]
5.Continue looping for j.
6.Continue looping for i.
7. Display “ The resultant matrix is”
8. Display the matrix c.
9.stop.

SAMPLE INPUT OUTPUT:


Enter the order of matrix 1- 3 2
Enter the order of matrix 2- 3 2
Enter the elements of array 1-
2
4
5 6
Enter the elements of array 2-
3 5
2 1
3 2
sum of matrix is
4 7
5 5
8 8
Enter the order of matrix 1- 3 2
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Enter the order of matrix 2- 2 2


addion cannot be performed.

RESULT:
Hence sum of matrix is found.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EXNO :2c MATRIX multiplication

AIM:
To perform matrix multiplication.

ALGORITHM:
1.Start.
2.Accept the number of rows and columns (m,n , p,q) for the 2 matrices a & b.
3.If n<>p then write “Matrix multiplication is not possible.” And goto Step 6.
4..Accept the elements of the 2 matrices.
5.call te function mul(a,b,n,m1,n1)
6.Stop.
Function mul
1.start
2.Repeat Steps 6 to 11 for i = 1 to m.
3.Repeat Steps 7 to 10 for j = 1 to q.
4.Repeat Steps 8 & 9 for k = 1 to n.
5.c[i][j]=c[i][j]+a[i][k]* b[k][j].
6.Continue looping for k.
7.Continue looping for j.
8.Continue looping for i.
9.Display “ The resultant matrix is”
10.Display the matrix c.

SAMPLE INPUT OUTPUT:

Enter the order of matrix 1- 3 2


Enter the order of matrix 2- 2 3
Enter the elements of array 1-
2
4
5 6
Enter the elements of array 2-
3 2 3
5 2 1
product of matrix is
13 6 5
29 14 13
45 22 21
Enter the order of matrix 1- 3 2
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Enter the order of matrix 2- 3 2


Matrix multiplication cannot be performed.

RESULT:

Hence product of matrix is found.


FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EXNO :2d MATRIX transpose

AIM:

To perform matrix transpose.

ALGORITHM:

1.Start.
2. Accept the number of rows and columns (m,n ) of matrices a .
3. Accept the elements of the 2 matrices.
4.call the function tro(a,m,n)
6. Stop
Function tra( a[10][10],m,n)
1.Start
2. Repeat Steps 5 to 8 for i = 1 to m.
3. Repeat Steps 6 to 7 for j = 1 to q.
4. c[i][j]=a[i][j]
5.Continue looping for j.
6.Continue looping for i.
7. Display “ The resultant matrix is”
8. Display the matrix c.
9.stop
SAMPLE INPUT OUTPUT:

Enter the order of matrix-2 3


Enter the elements of array-
3 4 5
6 7 8
Transpose of matrix is
3 6
4 7
5 8

RESULT:
Hence matrix transpose is found.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

STRINGS

EX NO: 3a Determination of number of digits, vowels, consonants and white spaces

AIM:
To find number of digits, vowels, consonants and white spaces in a given line.

ALGORITHM:
Start.
Read a line ‘str’.
Initialise the variables d,c,v,s to 0.
Call the function len(str) and store the value in l.
for i=0 to l-1 repeat step 6 to 8.
Store str[i] to ch.
Call the function non(ch).
If k=0 increment s by one. else if k=1 increment d by 1.
else if k=2 increment v by 1 else increment c by 1.
Print s,d,v,c.
Stop.

function len
Start.
Assign 0 to len,I to 0.
Assign first character to ch .
Repeat step 5 and step 6 while ch not equal to '\0'.
Increment len by 1 and I by 1.
Assign ch to str[i] character.
Return len.
Stop.

function non
Start.
2: if ch is “ “ , return 0;;else if ch is a digit return 1;E
Else if ch is a vovel return 2.
Stop
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

SAMPLE INPUT OUTPUT:


santhosh is good boy
number of vovels=6
number of consonents=11
number of digits=0
number white spases=3

RESULT:
Hence number of digits, consonants, vowels and white spaces in line is determined.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EX NO.:3b Palindrome of a string

AIM:
To verify whether the given string is a palindrome or not.

ALGORITHM:
Start.
Read string ‘str’.
Call the function len.
for i=0 to len-1 do
Swap the ith and (n-1-i)th character and store it on a another string ‘str1’.
if the function cmp returns 1 else go to step 8.
Print it is palindrome.
Print it is not palindrome.
Stop

function len
Start.
Assign 0 to len,I to 0.
Assign first character to ch .
Repeat step 5 and step 6 while ch not equal to '\0'.
Increment len by 1 and I by 1.
Assign ch to str[i] character.
Return len.
Stop.

function cmp

Start.
Assign first character of str and str1 to ch and ch1 respectively.
Assign flag to 1
for i=0 to l-1 repeat step 5 to 7
if not equal to ch1
Set flag to 0
Assign ch and ch1 to str[i] and str1 character respectively.
Return flag.
Stop
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

SAMPLE INPUT OUTPUT:


enter a string-
anna
anna is palindrome.

enter a string-
good
good is not palindrome

RESULT:
Hence the given string is verified as a palindrome or not.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EX NO.:3c Reverse of a string

AIM:
To perform string reverse without using library functions of C.

ALGORITHM:
Start.
Read string str
Call the function len
for i=0 to len-1 do
Swap the ith and (n-1-i)th character
Print reversed string str
Stop

function len
Start.
Assign 0 to len,I to 0.
Assign first character to ch .
Repeat step 5 and step 6 while ch not equal to '\0'.
Increment len by 1 and I by 1.
Assign ch to str[i] character.
Return len.
Stop

SAMPLE INPUT OUTPUT:


enter a string-
santhosh
reverse of santhosh is hsohtnas

RESULT:
Hence string reverse without using library functions of C is performed.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EX NO.:3d Determination of a substring in a string

AIM:
To find a substring in a given string.

ALGORITHM:
Start
Read string ‘str’ and the sub sring’str1’
Call the function len(str) and store the value in l
Call the function len(str1) and store the value in l1
for i=0 to len-1 repeat
Assign k=0
for j=0 to l1-1 repeat
Store the l1 consecutive characters in str1 ftom the kth position of str
Increment k by 1.
if the called function cmp return 1
Print sub string is present; else print sub string is not present.
Stop.

function len
Start.
Assign 0 to len,I to 0.
Assign first character to ch .
Repeat step 5 and step 6 while ch not equal to '\0'.
Increment len by 1 and I by 1.
Assign ch to str[i] character.
Return len.
Stop.

function cmp

Start.
Assign first character of str and str1 to ch and ch1 respectively.
Assign flag to 1
for i=0 to l-1 repeat step 5 to 7
if not equal to ch1
Set flag to 0
Assign ch and ch1 to str[i] and str1 character respectively.
Return flag.
Stop
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

SAMPLE INPUT OUTPUT:

enter the string-


god is grate
enter the substring -
ate

ate is present in the -god is grate

enter the string-


god is grate
enter the substring -
dog
dog is not present in the -god is grate

RESULT:
Hence a substring in a string is found.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EX NO.:3e Upper case of a string


AIM:
To convert a given string to its upper case without using library functions of C.

ALGORITHM:
Start
Read string str
Call the function len
for i=0 to len-1 do
Print the called function up(str[i])
Stop

function len
Start.
Assign 0 to len,I to 0.
Assign first character to ch .
Repeat step 5 and step 6 while ch not equal to '\0'.
Increment len by 1 and I by 1.
Assign ch to str[i] character.
Return len.
Stop.

function up
Start
if ch>=65 and ch<=91 return ch; else return ch-32.
Stop.

SAMPLE INPUT OUTPUT:


enter a string
santhosh
uppare case of' santhosh 'is SANTHOSH

RESULT:
Hence the given string is converted to its upper case without using library functions of C.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EX NO.:3f Sorting of strings

AIM:

To sort n strings alphabetically.

ALGORITHM:
Start
Accept the number of names, n, in the array.
for i = 0 to n-1 do step 4
Read a(i)
for i = 0 to n-1 do steps 6 to 10
for j = 0 to n-i-1 steps 7 to 10
if (strcmp(a(j),a(j+1)) >0) do steps 8 to 10
strcpy(temp, a(j))
strcpy(a(j) , a(j+1))
strcpy(a(j+1) , temp)
for i = 0 to n-1 do step 12
Write a(i)
Stop

SAMPLE INPUT OUTPUT:


enter the number of strings- 3

enter the strings


delhi
mumbai
bangaolre

sorted strings are-

bangaolre
delhi
mumbai

RESULT:
Hence the n strings are sorted alphabetically.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

FUNCTIONS AND POINTERS

EX NO.:4a Fibonacci series

AIM:
To print the Fibonacci series using functions and pointers.

ALGORITHM:
Start.
Accept the limit ,n
Call the function by the reference of n as , fib(&n)
Print the series
Stop

function fib(*n)
Start
if *n==0 return 0
else if n==1 return 1
else do
store the value of *n-1 and *n-2 in i and k
f(i)=fib(&i)+fib(&k)
5. return f(i)
6. Stop

SAMPLE INPUT OUTPUT:


Enter the limit-8

Fibonacci series
0 1 1 2 3 5 8 13

RESULT:
Hence Fibonacci series using functions and pointers is printed.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EX NO.:4b Factorial of the given number using recursion

AIM:
To compute factorial of the given number recursively using functions and pointers.

ALGORITHM:
Start
Enter the number,n for which the factorial has to be calculated
Call the function by passing the references of n, fact(&n)
Print the factorial of the number
Stop

function fact(*n)
Start
if *n==1 return 1
else do step 4,5
Store the value of *n-1 in ‘i’
Compute f=(i+1)*fact(&i)
Return f
Stop

SAMPLE INPUT OUTPUT:

enter a number : 4
factorial of number 4 is 24

RESULT:
Hence factorial of the given number is computed recursively using functions and pointers.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EX NO.:4c Arithmetic operations using pointers.

AIM:
To perform arithmetic operations using functions and pointers.

ALGORITHM:

Start.
Read two numbers.
Call the function by the reference of a and b as , add(&a, &b) , sub(&a, &b) , mul(&a, &b),
div(&a, &b).
Print the values.
Stop.

function add(*a, *b)


Start
store the value of *a+*b in c
Return c
Stop.

function sub(*a, *b)


Start
store the value of *a-*b in c
Return c
Stop.

function mul(*a, *b)


Start
store the value of (*a )* (*b) in c
Return c
Stop.

function div(*a, *b)


Start
store the value of *a / *b in c
Return c
Stop.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

SAMPLE INPUT OUTPUT:


enter two numbers- 2 3
2+3=5
2-3=-1
2*3=6
2/3=0.666667

RESULT:
Hence the arithmetic operations is performed using functions and pointers.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EX NO.:4d Matrix transpose

AIM:
To print the transpose of a matrix using dynamic memory allocation.

ALGORITHM:
1.Start.
2. Accept the number of rows and columns (man ) of matrices a .
3.create the dynamic memory of the matrix a and b where b is the transpose of a
4. Accept the elements of the matrices a.
5.Repeat Steps 6 to 7 for i = 1 to n.
6. Repeat Steps 7 for j = 1 to m.
7. assign the *(a+j*n+i) to *(b+i*m+j)
8.print the elements of matrix b
9.Stop

SAMPLE INPUT OUTPUT:


enter the order of matrix-
2 4
enter the elements.
2 3 4 5
4 5 6 7
transpose of the given matrix is.
2 4
3 5
4 6
5 7

RESULT:
Hence the transpose of a matrix is printed using dynamic memory allocation.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EXNO :5 (a) Implementation of Singly Linked List with its operations.

AIM:

To implement a singly linked list with its operations.

ALGORITHM:

Declare the structure node.

Declare the header node L

Declare the functions create, insert, delete, display, islast, find prev.

Read the number of terms

Call the function create()

Print “enter the choice”

Print “1.INSERT 2.DELETE 3.SEARCH 4.DISPLAY 5.EXIT”

Read the choice

If choice equal to 1 then read the number and position. Call insert ()

If choice equal to 2 then read the number and call delete()

If choice equal to 3 then read the number and call search()

If choice equal to 4 then call display()

If choice equal to 5 then exit else goto step 6

ROUTINE FOR CREATION

void create (integer n)

Assign NULL to next of header node

Read n numbers and call insert function

ROUTINE FOR INSERTION


void insert ( integer no, position po)
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Declare the pointers p and tmp to the structure node

Assign p = l-> next

Traverse the pointer p through the list till the previous position

Allocate memory for tmp using malloc function

Assign the number no to data of tmp node

Assign the address in the p-> next to tmp-> next

Assign p-> next = tmp

ROUTINE FOR SEARCH

void search (integer no)

Declare the pointer p to the structure node

Assign p = l-> next

Traverse through the list using pointer p till the last node and check for the number no

If the number is found print the position else print “element not found ”

ROUTINE FOR ISLAST

Integer islast (integer no)

Traverse through the list and find whether the given number is last or not

If the number is last return 0 else return 1

ROUTINE FOR FIND PREVIOUS

Position findprev (integer no)

Assign the pointer p to the structure node

Assign p= l-> next

Traverse through the list using pointer p till the previous node to the number no

Return the position p


ROUTINE FOR DELETION
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Void delete(integer no)

Assign the pointers p and temp to the structure node

Assign p = l->next

If the number is in first position then assign l = p->next and free p and return

Call findprev() and store the return position in p

Call islast() function and if return is 0 then goto 6

Assign temp=p->next

Assign p->next = temp->next

Free temp and return

Print “element not in the list”

ROUTINE FOR DISPLAY

void print ()

Assign the pointer p to the structure node

Assign p = l->next

Traverse through the list using the pointer and print the list

SAMPLE INPUT OUTPUT:

LINKED LIST IMPLEMENTATION

creation

enter the number of terms :5

enter 5 numbers :35

12

43

56
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

09

select the operation

1.INSERT

2.DELETE

3.SEARCH

4.DISPLAY

5.EXIT

enter the choice :1

INSERTION

enter the number :100

enter the position :3

select the operation

1.INSERT

2.DELETE

3.SEARCH

4.DISPLAY

5.EXIT

enter the choice :2


DELETION

enter the number :09

select the operation


FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

1.INSERT

2.DELETE

3.SEARCH

4.DISPLAY

5.EXIT

enter the choice :4

DISPLAY

35 12 100 43 56

select the operation

1.INSERT

2.DELETE

3.SEARCH

4.DISPLAY

5.EXIT

enter the choice :3

SEARCH

enter the element :43

element found!!

position is 4

select the operation

1.INSERT
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

DELETE

3.SEARCH

4.DISPLAY

5.EXIT

enter the choice :5

bye!!

RESULT:

Thus the C program to implement singly linked list is written and executed successfully.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EXNO :6 (a) Implementation of Doubly Linked List with its operations.

AIM:

To implement a doubly linked list with its operations.

ALGORITHM:

Declare the structure node.

Declare the header node L

Declare the functions create, insert, delete, display, islast

Read the number of terms

Call the function create()

Print “enter the choice”

Print “1.INSERT 2.DELETE 3.SEARCH 4.DISPLAY 5.EXIT”

Read the choice

If choice equal to 1 then read the number and position and call insert ()

If choice equal to 2 then read the number and call delete()

If choice equal to 3 then read the number and call search()

If choice equal to 4 then call display()

If choice equal to 5 then exit else go to step 6

ROUTINE FOR CREATION

void create (integer n)

Assign NULL to next and previous of header node

Read n numbers and call insert function


FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

ROUTINE FOR INSERTION

void insert(integer number, position po)

Declare the pointers p and tmp to the structure

Assign p = l

Traverse the pointer p through the list until previous position

Allocate memory for tmp using malloc function

Assign tmp->data = no

Assign tmp->next = p->next

Assign p->next- >previous = tmp

Assign p->next = tmp

Assign tmp->prev = p

ROUTINE FOR SEARCH

void search (integer no)

Declare the pointer p to the structure node

Assign p = l-> next

Traverse through the list using pointer p till the last node and check for the number no

If the number is found print the position else print “element not found ”

ROUTINE FOR DELETE

void delete(integer number)

Assign the pointer p and tmp to the list

Assign p = l->next
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Traverse through the list till last, using pointer p and if p->data = no then goto 4 else

continue traversing

Assign p->prev- >next = p->next

Assign p->next- >prev = p->prev

Free the node p and return

Print “element not found”

ROUTINE FOR DISPLAY

void print ()

Assign the pointer p to the structure node

Assign p = l->next

Traverse through the list using the pointer and print the list

SAMPLE INPUT OUTPUT:

DOUBLY LINKED LIST IMPLEMENTATION

creation

enter the number of terms :6

enter 5 numbers :12

34

55

67

33

10
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

select the operation

1.INSERT

2.DELETE

3.SEARCH

4.DISPLAY

5.EXIT

enter the choice :1

INSERTION

enter the number :11

enter the position :4

select the operation

1.INSERT

2.DELETE

3.SEARCH

4.DISPLAY

5.EXIT

enter the choice :2

DELETION

enter the number :33

select the operation

1.INSERT
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

2.DELETE

SEARCH

4.DISPLAY

5.EXIT

enter the choice :4

DISPLAY

12 34 55 11 67 10

select the operation

1.INSERT

2.DELETE

3.SEARCH

4.DISPLAY

5.EXIT

enter the choice :3


SEARCH

enter the element :11

element found!!

position is 4

select the operation

1.INSERT

2.DELETE

3.SEARCH
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

4.DISPLAY

5.EXIT

enter the choice :5

bye!!

RESULT:

Thus the C program to implement a doubly linked list is written and executed

successfully.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EXNO :6 (b) Implementation of Circular Linked List with its operations.

AIM:

To implement a circular linked list with its operations.

ALGORITHM:

Declare the structure node.

Declare the header node L

Declare the functions create, insert, delete, display, islast

Read the number of terms

Call the function create()

Print “enter the choice”

Print “1.INSERT 2.DELETE 3.SEARCH 4.DISPLAY 5.EXIT”

Read the choice

If choice equal to 1 then read the number and position and call insert ()

If choice equal to 2 then read the number and call delete()

If choice equal to 3 then read the number and call search()

If choice equal to 4 then call display()

If choice equal to 5 then exit else go to step 6

ROUTINE FOR CREATION

void create (integer n)

Assign NULL to next and previous of header node


FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Read n numbers and call insert function

ROUTINE FOR INSERTION

void insert(integer number, position po)

Declare the pointers p and tmp to the structure

Assign p = l

Traverse the pointer p through the list until previous position

Allocate memory for tmp using malloc function

Assign tmp->data = no

Assign tmp->next = p->next

Assign p->next- >previous = tmp

Assign p->next = tmp

Assign tmp->prev = p

ROUTINE FOR SEARCH

void search (integer no)

Declare the pointer p to the structure node

Assign p = l-> next

Traverse through the list using pointer p till the last node and check for the number no

If the number is found print the position else print “element not found ”

ROUTINE FOR DELETE

void delete(integer number)

Assign the pointer p and tmp to the list


FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Assign p = l->next

Traverse through the list till last, using pointer p and if p->data = no then goto 4 else

continue traversing

Assign p->prev- >next = p->next

Assign p->next- >prev = p->prev

Free the node p and return

Print “element not found”

ROUTINE FOR DISPLAY

void print ()

Assign the pointer p to the structure node

Assign p = l->next

Traverse through the list using the pointer and print the list

SAMPLE INPUT OUTPUT:

CIRCULAR LINKED LIST IMPLEMENTATION

creation

enter the number of terms :6

enter 5 numbers :12

34

55

67

33
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

10
select the operation

1.INSERT

2.DELETE

3.SEARCH

4.DISPLAY

5.EXIT

enter the choice :1

INSERTION

enter the number :11

enter the position :4

select the operation

INSERT

2.DELETE

3.SEARCH

4.DISPLAY

5.EXIT

enter the choice :2

DELETION

enter the number :33

select the operation


FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

1.INSERT

2.DELETE

3.SEARCH

4.DISPLAY

5.EXIT

enter the choice :4

DISPLAY

12 34 55 11 67 10

select the operation

1.INSERT

2.DELETE

3.SEARCH

4.DISPLAY

5.EXIT

enter the choice :3


SEARCH

enter the element :11

element found!!
position is 4

select the operation

1.INSERT

2.DELETE
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

3.SEARCH

4.DISPLAY

5.EXIT

enter the choice :5

bye!!

RESULT:
Thus the C program to implement a circular linked list is written and executed successfully.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EXNO :7 (a) FILE HANDLING

Sequential access

AIM:

To write bank customer details into a file and to sequentially access the customer details

from the file.

ALGORITHM:

To write the customer details into the file

Open the file ‘fn’ in write mode.

Read the number of customers ‘n’.

Get the customer details like account number, name and balance from the user.

Write the account number, name and balance of each customer into the file.

Close the file.

ii. To access the customer details from the file

Open the file ‘fn’ in read mode.

Get any one of the following option from the user.

List accounts with zero balances

List accounts with credit balances

List accounts with debit balances

Exit

Read the customer details account number, name and balance from the file.

Check whether the balance is zero, credit or debit balance based on the option for each
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

customer and the print the corresponding customer details.

Close the file.

SAMPLE INPUT AND OUTPUT:

To write the customer details into the file

Enter the number of customers: 5

Enter the account number, name and balance of customer 1 : 123 AAA 0

Enter the account number, name and balance of customer 2 : 456 BBB -80

Enter the account number, name and balance of customer 3 : 789 CCC 100

Enter the account number, name and balance of customer 4 : 147 DDD -60

Enter the account number, name and balance of customer 5 : 258 EEE 1000

ii. To access the customer details from the file

Enter the option

List accounts with zero balances

List accounts with credit balances

List accounts with debit balances

Exit

Accounts with zero balances:

123 AAA 0

Enter the option

List accounts with zero balances


FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

List accounts with credit balances

List accounts with debit balances

Exit

Accounts with credit balances:

456 BBB -80

147 DDD -60

Enter the option

List accounts with zero balances

List accounts with credit balances

List accounts with debit balances

Exit

Accounts with credit balances:

789 CCC 100

258 EEE 1000

Enter the option

List accounts with zero balances

List accounts with credit balances

List accounts with debit balances

Exit
4
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Bye!

RESULT:

Thus the C program to write bank customer details into a file and to sequentially access

the customer details from the file is written and executed successfully.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EXNO :7 (b) FILE HANDLING

Random access

AIM:

To write bank customer details into a file and to randomly access the customer details

from the file.

ALGORITHM:

To write the customer details into the file

Define the structure with customer details.

Open the file ‘fn’ in write mode.

Read the number of customers ‘n’.

Get the customer details like name and balance from the user and the account number

should be assigned sequentially from 1 to n for each customer.

Write the account number, name and balance of each customer into the file.

Close the file.

ii. To access the customer details from the file

Open the file ‘fn’ in read mode.

Get the account number from the user.

Read the customer detail name and balance from the file based on the account number

using fseek().

4. Print the corresponding customer details.

Close the file.


FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

SAMPLE INPUT AND OUTPUT:

To write the customer details into the file

Enter the number of customers: 5

Enter the name and balance of customer 1 : AAA 0

Enter the name and balance of customer 2 : BBB -80

Enter the name and balance of customer 3 : CCC 100

Enter the name and balance of customer 4 : DDD -60

Enter the name and balance of customer 5 : EEE 1000

ii. To access the customer details from the file

Enter the account number: 3

3 CCC 100

Enter the account number: 6

Account number does not exist!!!

RESULT:

Thus the C program to write bank customer details into a file and to randomly access

the customer details from the file is written and executed successfully
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Ex.No:8a IMPLEMENTATION OF STACK USING ARRAYS


AIM:
To write a c program to implement stack using arrays.
ALGORITHM:
Start.
Display the menu(push,pop,display).
Read the choice.
Call respective functions based on the choice using switch case.
Repeat as many times required.
Function push():
Start.
check if stack is full.
If full display that stack is full or overflow.
If not full then increment the top value by one and save the data in top index.
Stop.
Function pop():
Start .
Check if stack is empty.
If empty display underflow.
If not full display the element in top value and decrement top by one.
Stop.
Function display():
Start.
Start loop from top and display elements in the position and decrement the position till it
reaches 0.
Stop.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

SAMPLE INPUT AND OUTPUT:


1.Push.
2.Pop.
3.Display.
4.Exit.
Enter the choice:1
Enter the item:1
Want to continue:(Y/N):y
1.Push.
2.Pop.
3.Display.
4.Exit.
Enter the choice:1
Enter the item:2
Want to continue:(Y/N):y
1.Push.
2.Pop.
3.Display.
4.Exit.
Enter the choice:1
Enter the item:3
Want to continue:(Y/N):y
1.Push.
2.Pop.
3.Display.
4.Exit.
Enter the choice:3
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

3
2
1
Want to continue:(Y/N):y
1.Push.
2.Pop.
3.Display.
4.Exit.
Enter the choice:2
The item poped out is 3
Want to continue:(Y/N):n

RESULT:
Thus the program to implement stack using arrays has been implemented and the output is
verified.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Ex.No:8b INFIX TO POSTFIX CONVERSION


AIM:
To write a c program that converts an infix expression to postfix.
ALGORITHM:
Start.
Read the infix expressions.
Read character by character and print if operand or push if an operator by calling the
function.
Stop.
Function push():
Start.
Read the infix expressions.
Check the priority and pop the elements of higher priority before pushing the operator.
Stop.
Function priority(char p):
Start
If p is + or p is – then return 1
Else If p is * or / then return 2
Else if p is ( then return 0
Stop
Function pop():
Start
If top >= 0 then print stack[top] and top - -.
Else print Stack is empty
Stop
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

SAMPLE INPUT AND OUTPUT:


Enter an infix expression: A+B*C-D/E
ABC*DE/-+
RESULT:
The Infix to Postfix conversion program has been implemented and the output is verified.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Ex no: 9a EVALUATION OF POSTFIX EXPRESSION


AIM:
To Write a C program to evaluate the given Postfix Expression.
ALGORITHM:
Step 1: Start
Step 2: i=0
Step 3: Read the postfix expression
Step 4: while( (ch=pofx[i++]) != '\0'), do steps 5-6
Step 5: if(isdigit(ch)), do step 5.1
5.1: push(ch-'0')
Step 6: else do steps 6.1-6.5
6.1: Pop two operands op1, op2
6.2: if(ch==+), do step 6.2.1
6.2.1: push(op1+op2)
6.3: if(ch==-), do step 6.3.1
6.3.1: push(op1-op2)
6.4: if(ch==*), do step 6.4.1
6.4.1: push(op1*op2)
6.5: if(ch==/), do step 6.5.1
6.5.1: push(op1/op2)
Step 7: Print the postfix expression and evaluated result
Step 8: Stop
SAMPLE OUTPUT:
Read the Postfix Expression ? 58+213-*-
Given Postfix Expn: 58+213-*-
Result after Evaluation: 17
RESULT: Hence program to evaluate a postfix expression has been executed and its output
has been verified
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Ex.No :9b IMPLEMENTATION OF STACK USING LINKED LIST


AIM:
To write a c program that implements stack using linked list.
ALGORITHM:
Start.
Declare a structure.
Display a menu.
Read the choice.
Call the respective function based on the choice using switch case.
Stop.
Function push():
Start.
Allocate the memory for the new node.
Read the data for the new node.
If stack is Empty the save the node as top.
If not then insert new node before the top and save top value into new node and make the
new node as top.
Stop.
Function pop():
Start.
If stack is empty display error.
If not then delete the first node in the stack and make second node as top.
Stop.
Function display():
Start.
If Stack is empty then display it is empty.
If not then run the loop from head till next pointer is NULL and display the data of each
node.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Stop.
Sample output:
1.Push.
2.Pop.
3.Display.
4.Exit.
Enter the choice:1
Enter the item:1
Want to continue:(Y/N):y
1.Push.
2.Pop.
3.Display.
4.Exit.
Enter the choice:1
Enter the item:2
Want to continue:(Y/N):y
1.Push.
2.Pop.
3.Display.
4.Exit.
Enter the choice:1
Enter the item:3
Want to continue:(Y/N):y
1.Push.
2.Pop.
3.Display.
4.Exit.
Enter the choice:3
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

3
2
1
Want to continue:(Y/N):y
1.Push.
2.Pop.
3.Display.
4.Exit.
Enter the choice:2
Popped Element:3
Want to continue:(Y/N):y
1.Push.
2.Pop.
3.Display.
4.Exit.
Enter the choice:3
2
1Want to continue:(Y/N):n

RESULT:
Thus the program to implement stack using linked list has been implemented and the output
is verified.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Ex no 10 a CIRCULAR QUEUE
AIM:
To write a C Program to implement a Circular Queue and its Various Operations.
Algorithm:
Step 1: Start
Step 2: Insertion, Deletion and Display functions are performed as per user's commands
Step 3: Stop

void insert()
Step 1: Start
Step 2: if((front==0&&rear==max-1)||(front>0&&rear==front-1)), do step 2.1
2.1: Print QUEUE OVERFLOW
Step 3: else do steps 3.1-3.3
3.1: Input an element
3.2: if(rear==max-1&&front>0), do steps 3.2.1-3.2.2
3.2.1: rear=0
3.2.2: q[rear]=x
3.3: else do step 3.3.1
3.3.1: if((front==0&&rear==-1)||(rear!=front-1)), do step 3.3.1.1
3.3.1.1: q[++rear]=x;
Step 4: Stop

void delet()
Step 1: Start
Step 2: if((front==0)&&(rear==-1)), do steps 2.1-2.3
2.1: Print QUEUE UNDERFLOW
2.2: getch()
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

2.3: exit()
Step 3: if(front==rear), do steps 3.1-3.2
3.1: a=q[front]
3.2: rear=-1, front=0
Step 4: else do steps 4.1-4.3
4.1: if(front==max-1), do steps 4.1.1-4.1.2
4.1.1: a=q[front]
4.1.2: front=0
4.2: else a=q[front++]
4.3: Print the deleted element
Step 5: Stop

void display()
Step 1: Start
Step 2: if((front==0)&&(rear==-1)), do steps 2.1-2.3
2.1: Print QUEUE UNDERFLOW
2.2: getch()
2.3: exit()
Step 3: if(front>rear), do steps 3.1-3.4
3.1: i=0
3.2: while(i<=rear), do step 3.2.1-3.2.2
3.2.1: Print q[i]
3.2.2: i++
3.3: j=front
3.4: while(j<=max), do steps 3.4.1-3.4.2
3.4.1: Print q[j]
3.4.2: j++
3.4: Print the locations of rear and front
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Step 4: else do steps 4.1-4.4


4.1: i=front
4.2: while(i<=rear), do steps 4.2.1-4.2.2
4.2.1: Print q[i]
4.2.2: i++
4.3: Print locations of front and rear
4.4: Print a new line
Step 5: Stop

SAMPLE INPUT AND OUTPUT:


Circular Queue operations
1.insert
2.delete
3.display
4.exit
Enter your choice:1
Enter element to be insert:5
Enter your choice:1
Enter element to be insert:3
Enter your choice:1
Enter element to be insert:2
Enter your choice:3
5 3 2
rear is at 2
front is at 5
Enter your choice:2
Deleted element is:5
Enter your choice:3
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

3 2
rear is at 2
front is at 3

RESULT:
Program to implement a circular queue has been executed and the output has been verified.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Ex.No :10b IMPLEMENTATION OF QUEUE USING LINKED LIST


AIM:
To write a c program that implements queue using linked list.
ALGORITHM:
Start.
Declare a structure.
Display a menu.
Read the choice.
Call the respective function based on the choice using switch case.
Stop.
Function Enqueue():
Start.
Allocate the memory for the new node.
Read the data for the new node.
If queue is Empty the save the node as rear
If not then insert new node before the rear and save rear value into new node and make the
new node as rear.
Stop.
Function Dequeue():
Start.
If Queue is empty display error.
If not then delete the front node in the queue and make second node as front.
Stop.
Function display():
Start.
If Queue is empty then display it is empty.
If not then run the loop from head till next pointer is NULL and display the data of each
node.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Stop.
Sample output:
1.Enqueue.
2.Dequeue
3.Display.
4.Exit.
Enter the choice:1
Enter the item:1
Want to continue:(Y/N):y
1.Enqueue.
2.Dequeue
3.Display.
4.Exit.
Enter the choice:1
Enter the item:2
Want to continue:(Y/N):y
1.Enqueue.
2.Dequeue
3.Display.
4.Exit.
Enter the choice:1
Enter the item:3
Want to continue:(Y/N):y
1.Enqueue.
2.Dequeue
3.Display.
4.Exit.
Enter the choice:3
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

3
2
1
Want to continue:(Y/N):y
1.Enqueue.
2.Dequeue
3.Display.
4.Exit.
Enter the choice:2
Deleted Element:1
Want to continue:(Y/N):y
|1.Enqueue.
2.Dequeue
3.Display.
4.Exit.
Enter the choice:3
3
2
Want to continue:(Y/N):n

RESULT:
Thus the program to implement queue using linked list has been implemented and the output
is verified.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Ex: No: 10 c Implementation of Double Ended Queue using Linked List


AIM:
To implement a double ended queue in which insertion and deletion is possible at both
the ends.

ALGORITHM:

1. Create a list with header and footer


2. Print “1.traverse from left 2.traverse from right 3.exit”
3. If ‘traverse from left’ perform enqueue at right and dequeue at left
4. If ‘traverse from right’ perform enqueue at left and dequeue at right
5. If ‘exit’ exit the program

SAMPLE INPUT AND OUTPUT:


1.traverse from left
2.traverse from right
3.exit
1

1.Enqueue 2.Dequeue 3.Display 4.Exit

Enter the choice : 1


Enter the number :3

1.Enqueue 2.Dequeue 3.Display 4.Exit


Enter the choice : 1
Enter the number :5

1.Enqueue 2.Dequeue 3.Display 4.Exit


Enter the choice : 1
Enter the number :7

1.Enqueue 2.Dequeue 3.Display 4.Exit


Enter the choice : 3
3 5 7

1.Enqueue 2.Dequeue 3.Display 4.Exit


Enter the choice : 4

1.traverse from left


2.traverse from right
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

3.exit
2

1.Enqueue 2.Dequeue 3.Display 4.Exit


Enter the choice : 2

1.Enqueue 2.Dequeue 3.Display 4.Exit


Enter the choice : 3
3 5

1.Enqueue 2.Dequeue 3.Display 4.Exit


Enter the choice : 4

1.traverse from left


2.traverse from right
3.exit
3
Bye!!

RESULT:

Thus the C program to implement a double ended queue in which insertion and deletion
is possible in both the ends is written and executed successfully.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Ex: 11a BUBBLE SORT

AIM: To implement bubble sort in an unsorted array.


ALGORITHM:
Start
Read in no of elements,n.
Read in array of size n.
Repeat steps 5- 8as long as c<(n-1) where c=0 initially.
Repeat steps 6- 7 as long as d<(n-c-1) where d=0 initially.
If current element is greater than the next element then swap a[d] and a[d+1],i.e
Swap = array[d]
array[d] = array[d+1]
array[d+1]= swap
d++
c++
Print sorted array
Stop

SAMPLE INPUT: 20 46 15 10 76

SAMPLE OUTPUT: 10 15 20 46 76

RESULT: Thus bubble sort was implemented in C using an array.


FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Ex:11b INSERTION SORT


AIM: To implement insertion sort in an unsorted array.
ALGORITHM:
Start
Read in array arr of size num
Repeat steps 4- as long as i<num where i=1 initially.
Temp=arr[i]
J=i-1
Repeat steps 7- as long as temp<arr[j] and j>=0
Arr[j+1]=arr[j]
j- -
arr[j+1]=temp
i++
Print sorted array
Stop
SAMPLE INPUT:
Arr= { 10,30,20,5,40}

SAMPLE OUTPUT:
Arr= 5 10 20 30 40

RESULT: Thus a program was written in C to implement insertion sort.


FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Ex no:12a SELECTION SORT


AIM: To implement selection sort in an array of given size
ALGORITHM:
Start
Read in array of size n
Repeat steps 4- as long as c=0 and c<n-1 and c++ every iteration
Position=c
Repeat steps 6- as long as d=c+1 and d<n and d++ every iteration
If array[position]>array[d] then position=d
If position is not equal to c then swap array[pos] and array[c]
Print the sorted array
Stop
SAMPLE INPUT: 72,50,98,19,92

SAMPLE OUTPUT: 19,50,72,92,98

RESULT: Thus the program was written and implemented.


FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

EXNO 12.b SHELL SORT


AIM: To implement shell sort in an array
ALGORITHM:
Start
Read in array of size num
Repeat steps 4-10 as long as i>0 where i=num/2 initially
Repeat steps 5- 9 as long as j<num where j=i initially
Repeat steps 6-8 as long as k>=0 as long as k=j-I initially
If ( arr[k+i]>=arr[k]) then break
Else swap arr[k] and arr[k+i]
K=k-i
j++
i=i/2
Print sorted array
Stop
SAMPLE INPUT: 72 50 98 12 16

SAMPLE OUTPUT: 12 16 50 72 98

RESULT: Hence a program was written to implement shell sort.


FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Ex:13 a QUICK SORT


AIM: To sort an array using quick sort
ALGORITHM:
Algorithm for quicksort(int x[],int,int):
Start
Declare pivot,j,temp and i.
If first <last then execute steps 4-15
pivot=first;
i=first;
j=last;
Repeat steps 8- 12 as long as i<j
Repeat step 9 as long as x[i]<=x[pivot] and i<last
I++
Repeat step 11 as long as x[j]>x[pivot]
J- -
If i<j then swap x[i] and x[j]
Swap x[pivot] and x[j]
quicksort(x,first,j-1);
quicksort(x,j+1,last);
Stop

Algorithm for main():


Start
Read in array and its size
Call quicksort(x,0,size-1)
Print the sorted elements
Stop
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

SAMPLE INPUT: 12 15 67 98 50 72

SAMPLE OUTPUT: 12 15 50 67 72 98

RESULT: Hence a program was written in C and executed to implement quick sort.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Ex no:13 b MERGE SORT

AIM: To implement merge sort in array


ALGORITHM:
Algorithm for mergesort(int arr[],int low,int mid,int high):
Start
L=low
I=low
M=mid+1
Repeat steps 6-12 as long as (l<=mid)&&(m<=high))
if(arr[l]<=arr[m]) then execute steps 7 and 8
temp[i]=arr[l];
l++
Else steps 10 and 11
temp[i]=arr[m];
m++;
i++
if(l>mid) then execute 14-16
Repeat steps 15-16 as long as k<=high and k=m initially and k++
Temp=arr[k]
I++
Else steps 18-20
Repeat steps 19-20 as long as k<mid where k=l initially and k++
Temp[i]=arr[k]
I++
Else repeat step 22 as long as k<=high where k=low initially and k++
Arr[k]=temp[k]
Stop
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Algorithm for partition(int arr[],int low,int high):


Start
If low<high then execute 3-6
mid=low+high/2
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
Stop

Algorithm for main():


Start
Read in elements to be sorted
partition(merge,0,n-1);
Print sorted array
Stop
SAMPLE INPUT: 10 70 60 34 56

SAMPLE OUTPUT: 10 34 56 60 70

RESULT: Hence merge sort was implemented in C.


FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Ex no:13 c RADIX SORT

AIM: To implement radix sort in an array


ALGORITHM:
Start
Read in array of size count
Copy contents of array into array1
Repeat steps 5- as long as k<3 where k=0 initially and k++ every iteration
Repeat steps 6- as long i<count where i=0 initially and i++ every iteration
Min=array[i]%10
T=i
Repeat steps 9- as long j<count where j=i+! initially and j++ every iteration
if (min > (array[j] % 10)) then execute 10-11
Min=array[j]%10
T=j
Swap array[i] and array1[t]
Swap array[t] and array[i]
Print sorted array
Stop

SAMPLE INPUT: 10 50 90 40 30

SAMPLE OUTPUT: 10 30 40 50 90

RESULT: Thus radix sort was implemented in C


FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Ex. No: 14a LINEAR SEARCH

AIM:

To search an element in a array using linear search.


ALGORITHM:

1. Read the number, n and r.


2. Read the limit ,n
3. Read all the n elements in the array, a.
4. Read the element, s, to search.
5. call the function, seq(a,s,n) for sequential search
6. Display the position of the element s in the array a.

Function seq(a,s,n)
1. Initialize variables big and small to a[0] and posn to –1 respectively.
2. Execute a loop for i = 1 to n do step 3
3. If (s equal to a(i)) then set posn as i+1
4. Return posn

SAMPLE INPUT AND OUTPUT:

Enter the number of elements: 3


Enter the array elements: 2 6 9
Enter the element to search: 6
The element 6 is found at position: 2

RESULT:

Thus the C program to search an element in an array using linear search is written and
executed successfully.
FT/GN/68A/02/30.06.16
SRI VENKATESWARA COLLEGE OF ENGINEERING

CS16361 PROGRAMMING AND DATA STRUCTURES LABORATORY

LAB MANUAL

Ex. No: 14b Binary Search

AIM:

To search an element in a array using binary search.


ALGORITHM:

1. Read the number, n and r.


2. Read the limit, n
3. Read all the n elements in the array, a.
4. Read the element, s, to search.
5. call the function, bin(a,s,n) for sequential search
6. Display the position of the element s in the array a.

Function bin(a,s,n)
1. Initialize left = 0 right = n.
2. Check whether left is less than or equal to right, then goto step 3.
3. Compute mid = floor((right-left)/2)+left.
4. Check whether if value is equal to a[mid], then set left as mid +1.
5. Check whether if value is less than a[mid], then set right as mid-1.
6. If element is found return mid.
7. If element is not found return -1.

SAMPLE INPUT AND OUTPUT:

Enter the number of elements: 3


Enter the array elements: 2 6 9
Enter the element to search: 6
The element 6 is found at position: 2

RESULT:

Thus the C program to search an element in an array using binary search is written and
executed successfully.

You might also like