0% found this document useful (0 votes)
33 views56 pages

SYBSC SEMIII Labbook 2020 21 Final Version

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views56 pages

SYBSC SEMIII Labbook 2020 21 Final Version

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Page 1 of 57

Savitribai Phule Pune University

WORKBOOK

Data Structures and Algorithms I


&
Software Engineering Mini Project
S. Y. B. Sc. (Computer Science)
CS -233
SEMESTER III

Student Name:

College:

Roll No: Exam Seat No:

Year: Division:
Page 3 of 57

Table of Contents

Sr No Contents Page Number

1 Introduction 4

2 Assignment Completion Sheet 7

Section I - Data Structure and Algorithms I


3 Searching Algorithms 9

4 Sorting Algorithms – Bubble, Insertion, Selection 13

5 Sorting Algorithms – Counting,Merge, Quick 18

6 Singly Linked List – Dynamic Implementation 22

7 Doubly Linked List - Dynamic Implementation 25

8 Linked List Applications 27

9 Stack 30

10 Applications on Stack 32

11 Linear Queue and Circular Queue 35

12 Priority Queue and Doubly Ended Queue (Dqueue) 39

Section II - Software Engineering Mini Project

13 Assignment Completion Sheet 42


Page 4 of 57

Introduction

About the workbook

This workbook is intended to be used by S. Y. B. Sc (Computer Science) students for the


Data structures and Algorithms using C Lab course and Software Engineering Mini Project in
Semester III.

Workbook is divided in two sections.

1. First section contains assignments on Data Structure and Algorithms.


2. Second section contains assignments on Software Engineering Mini Project.

Data structures and Algorithm is an important core subject of computer science curriculum,
and hands-on laboratory experience is critical to the understanding of theoretical concepts
studied as part of this course. Study of any programming language is incomplete without
hands on experience of implementing solutions using programming paradigms and verifying
them in the lab. This workbook provides rich set of problems covering the basic algorithms as
well as numerous computing problems demonstrating the applicability and importance of
various data structures and related algorithms.

The objectives of this book are

• Defining clearly the scope of the course

• Bringing uniformity in the way the course is conducted across different colleges

• Continuous assessment of the course

• Bring variation and variety in experiments carried out by different students in a batch

• Providing ready reference for students while working in the lab

• Catering to the need of slow paced as well as fast paced learners

How to use this workbook

The Data Structures and Algorithms practical syllabus is divided into five assignments. Each
assignment has problems divided into three sets A, B and C.

• Set A is used for implementing the basic algorithms or implementing data structure
along with its basic operations. Set A is mandatory.
Page 5 of 57

• Set B is used to demonstrate small variations on the implementations carried out in set
A to improve its applicability. Depending on the time availability the students should
be encouraged to complete set B.

• Set C prepares the students for the viva in the subject. Students should spend
additional time either at home or in the Lab and solve these problems so that they get
a deeper understanding of the subject.

Instructions to the students

Please read the following instructions carefully and follow them.

• Students are expected to carry workbook during every practical.

• Students should prepare oneself before hand for the Assignment by reading the
relevant material.

• Instructor will specify which problems to solve in the lab during the allotted slot and
student should complete them and get verified by the instructor. However student
should spend additional hours in Lab and at home to cover as many problems as
possible given in this work book.

• Students will be assessed for each exercise on a scale from 0 to 5


➢ Not done 0
➢ Incomplete 1
➢ Late Complete 2
➢ Needs improvement 3
➢ Complete 4
➢ Well Done 5

Instruction to the Practical In-Charge

• Explain the assignment and related concepts in around ten minutes using white board
if required or by demonstrating the software.

• Choose appropriate problems to be solved by students. Set A is mandatory. Choose


problems from set B depending on time availability. Discuss set C with students and
encourage them to solve the problems by spending additional time in lab or at home.

• Make sure that students follow the instruction as given above.

• You should evaluate each assignment carried out by a student on a scale of 5 as


specified above by ticking appropriate box.
Page 6 of 57

• The value should also be entered on assignment completion page of the respective
Lab course.

Instructions to the Lab administrator and Exam guidelines

• You have to ensure appropriate hardware and software is made available to each
student.

• Do not provide Internet facility in Computer Lab while examination

• Do not provide pen drive facility in Computer Lab while examination.

The operating system and software requirements are as given below:

• Operating system: Linux


• Editor: Any linux based editor like vi, gedit etc.
• Compiler: cc or gcc
Page 7 of 57

Assignment Completion Sheet

Sr. No Assignment Name Marks Signature


(Out of 5)
1 Searching Algorithms

2 Sorting Algorithms – Bubble, Insertion, Selection

3 Sorting Algorithms – Counting ,Merge, Quick

4 Singly Linked List

5 Doubly Linked List

6 Linked List Applications

7 Stack

8 Application on Stack

9 Linear Queue and circular queue

10 Priority Queue and Doubly Ended Queue (Dqueue)

Total out of 50

a. Total out of 5 (DS Assignment marks converted to 5)

b. Total out of 10 (Software Engineering mini project)

Total (Out of 15) (a+b)

This is to certify that Mr/Ms _________________________________________________

University Exam Seat Number _________ has successfully completed the course work

for Lab Course I and has scored _________ Marks out of 15.

Instructor Head

Internal Examiner External Examiner


Page 8 of 57

Section I

Data Structure
and
Algorithms I
Page 9 of 57

Assignment 1: Searching Algorithms

Searching is the process of finding a value in a list of values.The searching algorithms you
are to use in this assignment are linear, sentinel and binary search.

In Linear search, we search an element or value in a given array by traversing the array from
the starting, till the desired element or value is found.

Algorithm:

Step 1: Start
Step 2: Accept n numbers in an array num and a number to be searched
Step 3: se ti=0 and flag=0
Step 4: if i<n then goto next step else goto step 7
Step 5: Compare num[i] and number
If equal then
set flag=1 and goto step 7
Step 6: i=i+1 and goto step 4
Step 7: if (flag=1) then
Print “Required number is found at location i+1”

else
Print “Require data not found”
Step 8: Stop

Time Complexity:

Base Case: O(1)


Worst Case: O(n)
Average Case: O(n)

Sentinel search is a type of linear search where the number of comparisons is reduced as
compared to a traditional linear search. The value to be searched can be appended to the list
at the end as a “sentinel value.

Algorithm:

Step 1: Start
Step 2: Accept n numbers in an array num and a number to be searched
Step 3: set i=0, last=num[n-1], num[n-1]=number
Step 4: Compare num[i] and number
If equal then goto step 6
Step 5: i=i+1 and goto step 4
Page 10 of 57

Step 6: num[n-1]=last
Step 7: if (num[i]=number) then
Print “Required number is found at location i+1”

else
Print “Require data not found”
Step 8: Stop

Time Complexity:

Base Case: O(1)


Worst Case: O(n)
Average Case: O(n)

Binary Search is used with sorted array or list. So a necessary condition for Binary search to
work is that the list/array should be sorted. It works by repeatedly dividing in half the portion
of the list that could contain the item.

Algorithm:

Step 1: Start
Step 2: Accept n numbers in an array num and a number to be searched
Step 3: set low=0, high=n-1 and flag=0
Step 5: if low <= high then
middle=(low+high)/2
else
goto step 8.
Step 6: if (num[middle]=number)
position=middle, flag=1 goto step 8.
else if (number< num[middle])
high=middle-1
else
low=middle+1
Step 7: goto step 5
Step 8: if flag=1
Print “Required number is found at location position+1”
Else
Print “Required number is not found.
Step 9: Stop

Time Complexity:

Base Case: O(1)


Worst Case: O(log n)
Page 11 of 57

Average Case: O(log n)

The data to be searched is in memory usually in an array. It could be an array of integers,


characters, strings or of defined structure type. To test a searching algorithm we require large
data set. Data is generated using random (rand()) function. The array of random integers in
the range 0 to 99 is generated by using following code:

void generate ( int * a , int n)


{
int i;
for (i=0; i<n; i++)
a[i]=rand()%100;
}

Set A

a) Create a random array of n integers. Accept a value x from user and use linear search
algorithm to check whether the number is present in the array or not and output the position if
the number is present.

b) Accept n values in array from user. Accept a value x from user and use sentinel linear
search algorithm to check whether the number is present in the array or not and output the
position if the number is present.

c) Accept n sorted values in array from user. Accept a value x from user and use binary
search algorithm to check whether the number is present in sorted array or not and output the
position if the number is present.

Set B

a) Read the data from file 'cities.txt' containing names of cities and their STD codes. Accept a
name of the city from user and use linear search algorithm to check whether the name is
present in the file and output the STD code, otherwise output “city not in the list”.

b) Read the data from file 'cities.txt' containing names of cities and their STD codes. Accept a
name of the city from user and use sentinel linear search algorithm to check whether the
name is present in the file and output the STD code, otherwise output “city not in the list”.

c) Read the data from file ‘sortedcities.txt’ containing sorted names of cities and their STD
codes. Accept a name of the city from user and use binary search algorithm to check whether
the name is present in the file and output the STD code, otherwise output “city not in the list”.

Set C
a) If the file contains multiple occurrences of a given element, linear search will give the
Page 12 of 57

position of the first occurrence, what modifications are required to get the last occurrence?

b) If the file contains multiple occurrences of a given element, will binary search output the
position of first occurrence or last occurrence?

c) Which is best case search when searching using linear search, sentinel search and when
using binary search?

d) What modifications are required to linear search, sentinel search and binary search
algorithm to count the number of comparisons?

e) What modifications are required to binary search so that it returns the position where x can
be inserted in the sorted array to retain the sorted order?

Assignment Evaluation

0: Not Done 1: Incomplete 2:Late Complete


3: Needs Improvement 4: Complete 5: Well Done

Practical In-charge
Page 13 of 57

Assignment 2: Sorting Algorithms – Bubble Sort, Insertion Sort, Selection


Sort

Arranging in an ordered sequence is called Sorting.The sorting algorithms you are to use in
this assignment are bubble sort, insertion sort and selection sort. These are non-recursive
algorithms.

Bubble Sort
It compares adjacent elements and swaps if required. The pass is repeated until the list is
sorted.

Algorithm:

Step1: Start
Step2: Accept ‘n’ numbers in array ‘A’
Step3: set i=0
Step4: if i<n-1 then goto next step else goto step 9
Step4: set j=0
Step5: if j<n-i-1 then goto next step else goto step 8
Step6: if A[j] > A[j+1] then
interchange A[j] and A[j+1]
Step7: j=j+1 and goto step 5
Step8: i=i+1 and goto step 4
Step9:Stop

Time Complexity:

Base Case: O(n)


Worst Case: O(n2)
Average Case: O(n2)

Insertion Sort

Insertion sort removes one element from list, find proper location in the list and insert it in the
list. This is repeated until no input element remains.

Algorithm:

Step1: Start
Step2: Accept ‘n’ numbers and store all in array ‘A’
Step3: set i=1
Step4: if i<=n-1 then goto next step else goto step 10
Step5: set Temp=A[i] and j=i-1
Page 14 of 57

Step6: if Temp < A[j] && j>=0 then goto next step else goto step 9
Step7: set A[j+1]=A[j]
Step8: set j=j-1
Step9: set A[j+1]=Temp
Step10: Stop

Time Complexity:

Base Case: O(n)


Worst Case: O(n2)
Average Case: O(n2)

Selection Sort:

In this sort the smallest element is selected from the unsorted array and swapped with the
leftmost element.

Algorithm:

Step1: Start
Step2: Accept ‘n’ numbers and store all in array ‘A’
Step3: set i=0
Step4: if i<n-1 then goto next step else goto step 11
Step5: set min=i and j=i+1
Step6: if j<n then goto next step else goto step 9
Step7: if A[j]<A[min] then
min=j
Step8: set j=j+1 and goto step 7
Step9: if(min not equal to i) then
interchange A[i] and A[min]
Step10: i=i+1 and goto step 4
Step11: Stop

Time Complexity:

Base Case: O(n2)


Worst Case: O(n2)
Average Case: O(n2)

In reality data to be sorted is externally stored in files. One need to read data from files and
bring it into memory in an array before sorting and sorted array also need to be written back
to an external file.

Suppose the records to be sorted containing name, age and salary of a set of employees, is in
Page 15 of 57

a text file “employee.txt” as follows:

Rajiv 43 100000
Prakash 34 29000
Vinay 35 20000
..................................

The data is read into memory in an array of structures as follows

Variable declarations & main program

typedef struct
{
char name[30];
int age;
intsalary;
}RECORD;

RECORD emp[100];
main()
{
int n; n=readFile(emp);
sort(emp,n);
writeFile(emp,n);
}

Function for reading from a file

int readFile(RECORD *a)


{
int i=0;
FILE *fp;
if((fp=fopen("emp.txt","r"))!=NULL)
{
while(! feof(fp))
{
fscanf(fp,"%s%d%d", a[i].name, &a[i].age, &a[i].salary);
i++;
}
}
return i; // number of records read
}
Page 16 of 57

Function for writing to a file

void writeFile(RECORD *a, int n)


{
int i=0;
FILE *fp;
if((fp=fopen("sortedemp.txt","w"))!=NULL)
{
for(i=0;i<n; i++)
{
fprintf(fp,"%s\t%d\t%d\n", a[i].name, a[i].age, a[i].salary);
}
}
}

Set A

a) Sort a random array of n integers (accept the value of n from user) in ascending order by
using bubble sort algorithm.

b) Sort a random array of n integers (create a random array of n integers) in ascending order
by using insertion sort algorithm.

c) Sort a random array of n integers (accept the value of n from user) in ascending order by
using selection sort algorithm.

Set B

a) Read the data from the file “employee.txt” and sort on age using bubble sort, insertion sort
and selection sort.

b) Read the data from the file “employee.txt” and sort on names in alphabetical order (use
strcmp) using bubble sort, insertion sort and selection sort.

Set C

a) What modification is required to bubble sort, insertion sort and selection to sort the
integers in descending order?

c)What modifications are required to bubble sort to count the number of swaps?

c)What modifications are required to insertion sort to count the number of key comparisons?
Page 17 of 57

d) What modifications are required to output the array contents after every pass of the sorting
algorithm?

Assignment Evaluation

0: Not Done 1: Incomplete 2:Late Complete


3: Needs Improvement 4: Complete 5: Well Done

Practical In-charge
Page 18 of 57

Assignment 3: Sorting Algorithms –Counting Sort, Merge Sort, Quick Sort

Sorting techniques to be used in this assignment are Counting Sort, Merge sort, Quick sort.
Merge and Quick sort both are recursive and use divide and conquer strategy.

Counting sort is an algorithm for sorting a collection of objects according to keys that are
small integers. So, it is an integer sorting algorithm.

countingSort(array, size)
max <- find largest element in array
initialize count array with all zeros
for j <- 0 to size
find the total count of each unique element and
store the count at jth index in count array
for i<- 1 to max
find the cumulative sum and store it in count array itself
for j <- size down to 1
restore the elements to array
decrease count of each element restored by 1

In Merge sort, data is divided into two parts, each part is sorted by using the same merge sort
technique and the sorted files are then merged using a Merge procedure.

Algorithm:
mergesort(a, N) algorithm:
Step1: Start
Step2: Accept N numbers and store into array a
Step3: Assign low=0 and high=N-1
Step4: if low < high
mid=(low+high)/2
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid,high);

merge(A, low, mid, high) algorithm:


Step1: i=low, j=mid+1, k=0;
Step2: while i<=mid && j<=high
if(a[i]<=a[j])
b[k]=a[i]
k=k+1, i=i+1
else
b[k]=a[j]
k=k+1, j=j+1
Page 19 of 57

Step3: while(i<=mid)
b[k]=a[i]
k=k+1, i=i+1

Step4: while(j<=high)
b[k]=a[j]
k=k+1, j=j+1

Step5: j=low, k=0


Step6: while j<=high
a[j]=b[k]
j=j+1, k=k+1
Step7: stop

In Quick sort, data is partitioned into two parts in such a way that all elements in first part
are less than or equal to elements in second part. Both the parts are then sorted using the same
Quick sort technique.

Algorithm:

QuickSort(A,n)
Step1: Start
Step2: Accept ‘n’ numbers and store all in array ‘A’
Step3: lb=0, ub=n-1
Step3: if (lb<ub)
j=Partition(A,lb,ub)
QuickSort(A,lb,j-1)
QuickSort(A,j+1,ub)

Partition(A, lb, ub)


Step1: down=lb, up=ub
Step2: pivot=A[lb]
Step3: while (A[down]<=pivot && down<up)
down++
Step4: while (A[up]>pivot && up>down)
up--
Step5: if (down < up)
Interchange A[down] and A[up] and goto step 3.
Step6: Interchange A[up] and pivot
Step7: return up
Step8: Stop

Set A
Page 20 of 57

a) Sort a random array of n integers (accept the value of n from user) in ascending order by
using recursive Counting sort algorithm.

b)Sort a random array of n integers (accept the value of n from user) in ascending order by
using a recursive Merge sort algorithm.

c) Sort a random array of n integers (create a random array of n integers) in ascending order
by using recursive Quick sort algorithm.

Set B

a)Read the data from the ‘employee.txt’ file and sort on age using Counting sort, Merge sort,
Quick sort and write the sorted data to another file 'sortedemponage.txt'.

b)Read the data from the file and sort on names in alphabetical order (use strcmp) using
Counting sort, Merge sort, Quick sort and write the sorted data to another file
'sortedemponname.txt'.

Set C

a) What modifications are required to choose the pivot element randomly instead of choosing
the first element as pivot element while partitioning in Quick sort algorithm?

b)Compare the system time taken by Merge sort and bubble sort by using time command on a
random array of integers of size 10000 or more.

c)What modification is required to Merge sort to sort the integers in descending order?

d)In 'employee.txt' there are records with same name but different age and salary values.
What are the relative positions when the data is sorted on name using Merge sort and what
happens in case of quick sort?

e)Sort a random array of integers of large size and store the sorted file. Compare the system
time taken by Quicksort on a random file of large size and the sorted file of same size. Repeat
the same for Merge sort. Does sorted file give best time?

Assignment Evaluation

0: Not Done 1: Incomplete 2:Late Complete


3: Needs Improvement 4: Complete 5: Well Done

Practical In-charge
Page 21 of 57

How to Make Custom Header File / Library file?

What are the header files?

Header files are helping file of C program which holds the definitions of various functions
and their associated variables.These header files are imported into the C program with the
help of pre-processor #include statement.

The basic syntax of using these header files is:

#include<file>/* This syntax searches for file-name in the standard list of system
directories */
OR

#include “file”/* This technique is used to search the file(s) within the directory that
contains the file where this include statement is written. */

The custom header file [.h extension] is a file that contains user defined functions. The
Function should accept input (arguments) and generate the output after processing the
received input.

For example, header file [arithmetic.h] has following functions

int add(int a.int b) • Here, functions only accept value and


{ returns the result after processing the input.
return(a+b); • Functions written inside header file
} should not have any scanf / gets or printf /
int mult(int a,int b) puts statements.
{
return(a*b);
}

Do you remember how the functions already defined in C header file works?
• Let’s try to explore math.h header file.
• This header file contains the function called as sqrt.
• The signature of the sqrt function is
double sqrt(double arg);
• The sqrt function accepts one argument and returns the result. It doesn’t directly print
the result inside sqrt function.

Note:
Function written inside header file will only accept and return value. It will not have any type
of standard input or output statements.
Page 22 of 57

Assignment 4: Singly Linked List – Dynamic Implementation

An abstract data type List is an ordered set of elements where insertions and deletions are
possible from any position. Implementing List statically using an array to store elements is
costly as insertions and deletions require moving of array elements. List is efficiently
implemented dynamically using Linked list.

The linked list is a series of nodes where each node contains the data element and a link to
the node containing the next element. The data element can be integer, character or user
defined type. A list is a single entity which is a pointer to the first node of the linked list. A
dummy node is used as header of the list so that it is not affected by insertions or deletions.

List

Head Next Data Next Data Next Data NULL

Operations on Linked List:

append(L, x) Insert the data element x by creating the node containing the data
element x and inserting it in last position.

insert (L, x, pos) inserts the data element x by creating the node containing the data
element x and inserting it in position pos. The links are appropriately
changed. If pos equals 1, the node is inserted in first position
immediately after the header. If pos is greater than the nodes present in
the list, the node is added at the end of the list.

search (L, x) Searches for the data element x and returns the pointer to the node
containing x if x is present or returns NULL.

delete (L, x) Deletes the node containing the data element x by appropriately
modifying the links.

delete (L, pos) Delete the node at a position given by pos. If the position pos is invalid
then display appropriate message.
Page 23 of 57

display (L) Displays all the data elements in the list

In a singly linked list there is only a link to the next element. For insertion as well as
deletion one need to traverse with two pointers back and current.

List back pos current

Head Next Data Next Data Next Data NULL

Data Next

Set A

a) Implement a list library (singlylist.h) for a singly linked list with the above six operations.
Write a menu driven driver program to call the operations.

b) Implement a list library (singlylist.h) for a singly linked list. Create a linked list, reverse it
and display reversed linked list.

Set B

a) There are lists where insertion should ensure the ordering of data elements. Since the
elements are in ascending order the search can terminate once equal or greater element is
found. Implement a singly linked list of ordered integers(ascending/descending) with insert,
search and display operations.

b) There are lists where new elements are always appended at the end of the list. The list can
be implemented as a circular list with the external pointer pointing to the last element of the
list. Implement singly linked circular list of integers with append and display operations. The
operation append(L, n), appends to the end of the list, n integers either accepted from user or
randomly generated.

Set C

a) How to divide a singly linked list into two almost equal size lists?

b) The union operation of two disjoint sets takes two disjoint sets S1 and S2 and returns a
disjoint set S consisting of all the elements of S1 and S2 and the original sets S1 and s2 are
destroyed by the union operation. How to implement union in O(1) time using a suitable
list data structure for representing a set?
Page 24 of 57

c) What is the method to reverse a singly linked list in just one traversal?

Assignment Evaluation

0: Not Done 1: Incomplete 2:Late Complete


3: Needs Improvement 4: Complete 5: Well Done

Practical In-charge
Page 25 of 57

Assignment 5: Doubly Linked List – Dynamic Implementation

In a Doubly linked list there is link to the next element as well as a link to the previous
element. For insertion one needs only the pointer to current element and same is true for
deletion.

The two links allow traversal of list in both direction i.e. forward and backward. It is easy to
reverse doubly linked list as compared to singly linked list.

Head

NULL Data Next Prev Data Next Prev Data NULL

Set A

a) Implement a list library (doublylist.h) for a doubly linked list with the above four
operations. Write a menu driven driver program to call the operationsappend, insert, delete
specific node, delete at position, search, display.

b) Implement a list library (doublylist.h) for a doubly linked list. Create a linked list, reverse
it and display reversed linked list.

Set B

a)There are lists where insertion should ensure the ordering of data elements. Since the
elements are in ascending order the search can terminate once equal or greater element is
found. Implement a doubly linked list of ordered integers(ascending/descending) with insert,
search and display operations.

b)There are lists where new elements are always appended at the end of the list. The list can
be implemented as a circular list with the external pointer pointing to the last element of the
list. Implement doubly linked circular list of integers with append and display operations. The
operation append(L, n), appends to the end of the list, n integers either accepted from user or
randomly generated.

Set C

a) Which operation is performed more efficiently in doubly linked list than singly linked list?

b) What is difference between singly circular linked list and doubly circular linked list?
Page 26 of 57

Assignment Evaluation

0: Not Done 1: Incomplete 2:Late Complete


3: Needs Improvement 4: Complete 5: Well Done

Practical In-charge
Page 27 of 57

Assignment 6: Linked List Applications

A linked list is a linear collection of data elements called as nodes in which linear
representation is given by links from one node to another. When we insert a new node in
linked lists, we need to check for OVERFLOW condition, which occurs when no free
memory cell is present in the system. When we delete a node from a linked list, we must first
check for UNDERFLOW condition which occurs when we try to delete a node from a linked
list that is empty. When we delete a node from a linked list, we have to actually free the
memory occupied by that node. The memory is returned back to the free pool so that it can be
used to store other programs and data. Linked lists can be used to implement lists, chains,
single and multi variable polynomials etc.

Merge two sorted linked lists:

Two linked lists, each of which is sorted in increasing order, can be merged together into
third list which is again sorted in increasing order.

For example:

Linked list a is: 8->11->25

Linked list b is: 4->50->88

Merged list c is : 4->8->11->25->50->88.

Algorithm:

• Create a new node for storing result of merged lists, say R


• Traverse both the linked lists at the same time, starting from head node
• Compare the first node values of both the linked lists
• If first linked list’s starting node has smaller value then add this node in the result
node and move head pointer of first linked list and head pointer of R list.
• Else add second lists start node in the result node and move head pointer of second
linked list and head pointer of R list.
• Again compare the node values of first and second lists.
• Keep doing until first or second list gets over
• Copy the rest of the nodes of unfinished list to the result list R

Addition of single variable polynomial

A polynomial is represented by a series of terms, each of which has twointegers. The first
integer represents the coefficient; the second integerrepresents the exponent.
Example: 2x3+ x
Here first term consist of 2x3 , where 2 represent coefficient of x and 3 represent exponent of
x
Page 28 of 57

Two single variable polynomials can be represented by a linked list. Adding two polynomials
means to add the coefficients who have same variable powers.

The rules for the addition of polynomials are as follows:

a. If the powers are equal, the coefficients are algebraically added.


b. If the powers are unequal, the term with the higher power is inserted inthe new polynomial.
c. If the exponent is 0, it represents x0, which is 1. The value of the term istherefore the value
of the coefficient.
d. If the result of adding the coefficients results in 0, term is dropped(0 times anything is 0).

Example:

Input:
1st number = 6x^2 + 4x^1 + 2x^0
2nd number = 5x^1 + 5x^0
Output:
6x^2 + 9x^1 + 7x^0
Input:
1st number = 3x^3 + 8x^2 + 2x^0
2nd number = 5x^1 + 5x^0
Output:
3x^3 + 8x^2 + 5x^1 + 7x^0

Set A

1)Write a program that merges two ordered linked lists into third new list. When two lists are
merged the data in the resulting list are also ordered. The two original lists should be left
unchanged. That is merged list should be new one. Use linked implementation.

2) Write a program that adds two single variablepolynomials. Each polynomial should be
represented as a list with linked list implementation.

Set B

1) Write a program that sorts the elements of linked list using any of sorting technique.

2) Write a program that multiply two single variable polynomials. Each polynomial should be
represented as a list with linked list implementation.

Set C

1) Write a program to find common elements of two linked lists and create third list. Ensure
Page 29 of 57

that the common elements appear only once in the third list.

Assignment Evaluation

0: Not Done 1: Incomplete 2:Late Complete


3: Needs Improvement 4: Complete 5: Well Done

Practical In-charge
Page 30 of 57

Assignment 7: Stack

A stack is a linear data structure in which elements are added and removed only from one
end, which is called the top. Hence, a stack is called a LIFO (Last-In, First-Out) data structure
as the element that is inserted last is the first one to be taken out.

1. Static Implementation:

A stack is implemented statically by using an array of size MAX to hold stack elements and
integer top storing the position of topmost element of the stack. A stack represents a structure
made up of both the array and the top. The stack elements can be integers, characters, strings
or user definedtypes.

The basic operations to be performed on a stack are:

Init(S) Create an empty stack and initializes value of top to -1 indicating the
stack is empty

Push(S,x) Insert element x into stack S

Pop(S) Deletes and returns topmost element from stack S

Peek(S) Returns topmost element from the stack S without deleting element.

isEmpty(S) Checks and returns if the stack S is empty or not. Stack isempty when
top==-1

isFull(S) Checks and returns if the stack S is full or not. Stack isfull when
top==MAX-1

2. DynamicImplementation

A stack is implemented dynamically by using a Linked list where each node in the linked
list has two parts, the data element and the pointer to the next element of the stack. Stack is a
single entity i.e. a pointer pointing to the top node in the linked list. The stack elements can
be integers, characters, strings or user defined types. Dynamic lists can grow without limits.

The operations to be performed on a stack are

Init(S) Create an empty stack and initializing top to NULL indicating the stack
is empty

S=Push(S,x) Adding an element x to the stack S requires creation of node containing


x and putting it in front of the top node pointed by S. This changes the
top node S and the function should return the changed value ofS.
Page 31 of 57

S=Pop(S) Deletes top node from stack and returns. Next element will become top
of stack and function returns the changed value of S.

X=Peek(S) Returns topmost element from the stack S without deleting element.

isEmpty(S) Checks and returns if the stack S is empty or not. Stack isempty when
top==NULL

Set A

a) Implement a stack library (ststack.h) of integers using a static implementationof the stack
and implementing the above six operations. Write a driver program that includes stack library
and calls different stackoperations.

b) Implement a stack library (dystack.h) of integers using a dynamic (linked list)


implementation of the stack and implementing the above five operations. Write a driver
program that includes stack library and calls different stackoperations.

Set B
a) Write a program to check whether the contents of two stacks are identical. Use stack
library to perform basic stack operations. Neither stack should be changed.

b) Write a program that copies the contents of one stack into another. Use stack library to
perform basic stack operations. The order of two stacks must be identical.(Hint: Use a
temporary stack to preserve the order).

Set C

a) In dynamic implementation of stack, How to modify pop operation so that it also returns
the popped element as an argument of the pop function?

Assignment Evaluation

0: Not Done 1: Incomplete 2:Late Complete


3: Needs Improvement 4: Complete 5: Well Done

Practical In-charge
Page 32 of 57

Assignment 8: Applications of Stack

Stack can be used in many applications like expression conversion, evaluation, parenthesis
checking, checking string is palindrome or not etc. Infix, prefix, and postfix notations are
three different but equivalent notations of writing algebraic expressions.Reversing string of
characters, converting the infix notation into postfix notation and evaluating the postfix
notation make extensive use of stacks as the primary tool. Following are few applications
which extensively uses stack for their implementation.

1) String reverse and checking palindrome string:

A string of characters can be reversed by reading each character from a string starting from
the first index and pushing it on a stack. Once all the characters have been read, the
characters can be popped one at a time from the stack and then stored in the another string
starting from the first index.

Algorithm to reverse the string:


1. Read the string character by character.
2. Push every character into the stack of characters.
3. When string becomes empty pop every character from stack and attach to the new
string.

2) Infix to Postfix conversion:

Infix, prefix, and postfix notations are three different but equivalent notations of writing
algebraic expressions. In postfix notation, operators are placed after the operands, whereas in
prefix notation, operators are placed before the operands. While expression conversion
precedence of operators is considered.

The precedence of operators can be given as follows:

Highest priority: ^ or $ (Exponential operator)


Higher priority: *, /, %
Lower priority: +, –

The algorithm accepts an infix expression that may contain operators. The algorithm uses a
stack to temporarily hold operators. The postfix expression is obtained from left-to-right
using the operands from the infix expression and the operators which are removed from the
stack.
Page 33 of 57

Algorithm for infix to postfix expression conversion:

1. Scan the infix expression from left to right.


2. If the scanned character is an operand, output it.
3. Else, If the precedence of the scanned operator is greater than the precedence of the
operator in the stack(or the stack is empty or the stack contains a ‘(‘ ), push it.
4. Else, Pop all the operators from the stack which are greater than or equal to in precedence
than that of the scanned operator. After doing that Push the scanned operator to the stack. (If
you encounter parenthesis while popping then stop there and push the scanned operator in the
stack.)
5. If the scanned character is an ‘(‘, push it to the stack.
6. If the scanned character is an ‘)’, pop the stack and and output it until a ‘(‘ is encountered,
and discard both the parenthesis.
7. Repeat steps 2-7 until infix expression is scanned.
8. Print the output
9. Pop and output from the stack until it is not empty.

3) Evaluation of postfix string:

Postfix notations are evaluated using stacks. Every character of the postfix expression is
scanned from left to right. If the character is an operand, it is pushed onto the stack. Else, if it
is an operator, then the top two values are popped from the stack and the operator is applied
on these values. The result is then pushed onto the stack.

Algorithm for evaluation of postfix expressions:

1) Create a stack to store operands (or values).


2) Scan the given expression and do following for every scanned element.
2.1) If the element is a variable then , push it’s value into the stack
2.2) If the element is a operator, pop operands for the operator from stack. Evaluate
the operator and push the result back to the stack
3) When the expression is ended, pop element from stack which is the final answer.

Set A

a)Write a program that reverses a string of characters. The function should use a stack library
(cststack.h) of stack of characters using a static implementation of the stack.

b)Write a program to convert an infix expressionof the form (a*(b+c)*((d-a)/b)) intoits


equivalent postfix notation. Consider usual precedence’s of operators. Use stack library of
stack of characters using static implementation.

Set B
Page 34 of 57

a) A postfix expression of the formab+cd-*ab/ is to be evaluated after accepting the values of


a, b, c and d. The value should be accepted only once and the same value is to be used for
repeated occurrence of same symbol in the expression. Formulate the problem and write a C
program to solve the problem by using stack

b) Write a program that checks whether a string of characters is palindrome or not. The
function should use a stack library (cststack.h) of stack of characters using a static
implementation of thestack.

Set C

a) Write a program that checks the validity of parentheses in any algebraic expression. The
function should use a stack library (cststack.h) of stack of characters using a static
implementation of the stack.
b) Write a program to find all solutions to the four queens problem. Your program will need
to be able to handle a search for a configuration that has no solution.

Assignment Evaluation

0: Not Done 1: Incomplete 2:Late Complete


3: Needs Improvement 4: Complete 5: Well Done

Practical In-charge
Page 35 of 57

Assignment 9: Linear Queue and circular queue

Queue:

A queue is a linear data structure which follows a particular order in which the operations are
performed. The order is First in First out (FIFO) or Last in Last out (LILO).

In queue elements are added from one end called rear and removed from other end called
front. Linear queue can be implemented statically using arrays and dynamically using linked
lists.

1) Static Implementation of linear queue:

A Queue is implemented statically by using an array of size MAX to hold elements and two
integers called front and rear.

A queue is a single entity that is a structure made up of the array, rear and front. Elements are
added from rear end of the queue and can be deleted from front end of the queue. The ‘front’
stores the position of the current front element and ‘rear’ stores the position of the current
rear element of the queue. The Queue elements can be integers, characters, strings or user
defined types.

The basic operations to be performed on a Queue are:

Create an empty queue by initializing bothfront and rear to -1


init (Q)
indicating the queueisempty.

add (Q, x) adds an element x to the rear end of the queue Q

X=delete (Q) Deletes an element x from front end of the queue Q.


Without deleting ,returns the front element from the queue Q
X=peek (Q)

isEmpty (Q) Checks whether the queue is empty. Queue becomes empty when rear
equals to front.

isFull(Q) Checks whether the queue is full. Queue becomes full when front
reaches to MAX -1.

2. DynamicImplementation of linear queue:

A Queue is implemented dynamically by using a Linked list. Each node in the linked list has
two parts, the data element and the pointer to the next element of the queue. Since Queue
should be a single entity, we need to use only one external pointer while here we need two
Page 36 of 57

one for rear and one to the front. To avoid this we use a circular linked list and Queue pointer
is pointing to the rear of the queue. Front can be easily accessed as it is next to rear. The
Queue elements can be integers, characters, strings or user defined types. There is no
restriction on how big the Queue can grow.

The operations to be performed on a Queue:

Create an empty queue as a circular linked list by initializing S


init (Q)
to NULL indicating that the queue is empty

add (Q, x) Adding an element x tso the queue Q requires creation of node
containing x and putting it next to the rear and rear points to the
newly added element. This changes the rear pointer Q and the
function should return the changed value of Q. The function call
will be asfollows

X=delete (Q) Deletes the front node from the queue Q which is actually next
element to the rear pointer Q. However if queue contains only
one element, (Q->next == Q) then deleting this single element
can be achieved by making empty Q (Q =NULL). Since the rear
pointer Q is changed in this case, function should return the
changed value of Q. The function call will be asfollows

Q=delete(Q);
returns the data element in the front (Q->next) node of the
X=peek (Q)
Queue Q

isEmpty (Q) Check if the Queue is empty which is equivalent to checking if


Q==NULL

isFull(Q) Checks whether the queue is full. Queue becomes full when front
reaches to MAX -1.

1) Circular queue:

In a normal Queue, we can insert elements until queue becomes full. But once queue
becomes full, we cannot insert the next element even if there is a space in front of queue.
Even though there is space available, the overflow condition still exist because the condition
REAR = MAX – 1 still holds true. This is a major drawback of a linear queue.

Circular Queue is a linear data structure in which the operations are performed based on
FIFO (First In First Out) principle and the last position is connected back to the first position
to make a circle, hence making the queue behave like a circular data structure or Ring Buffer.

In case of a circular queue, head pointer will always point to the front of the queue, and tail
pointer will always point to the end of the queue.
Page 37 of 57

Initially, the head and the tail pointers will be pointing to the same location, this would mean
that the queue is empty.New data is always added to the location pointed by the tail pointer,
and once the data is added, tail pointer is incremented to point to the next available location.
In a circular queue, data is not actually removed from the queue. Only the head pointer is
incremented by one position when delete_queue() is executed. As the queue data is only the
data between head and tail, hence the data left outside is not a part of the queue anymore,
hence removed. The head and the tail pointer will get reinitialized to 0 every time they reach
the end of the queue. Also, the head and the tail pointers can cross each other. In other words,
head pointer can be greater than the tail. Sounds odd? This will happen when we
delete_queue the queue a couple of times and the tail pointer gets reinitialized upon reaching
the end of the queue.

Create an empty queue as a circular linked list by


init (Q)
initializing S to NULL indicating that the queue is empty
This function is used to insert an element into the circular queue. In
AddQueue(Q, x)
a circular queue, the new element is always inserted at Rear
position.
Steps:
1. Check whether queue is Full – Check ((rear == SIZE-1
&& front == 0) || (rear == front-1)).
2. If it is full then display Queue is full. If queue is not full
then, check if (rear == SIZE – 1 &&front != 0) if it is
true then set rear=0 and insert element.

This function is used to delete an element from the circular queue.


X=DeleteQueue(Q)
In a circular queue, the element is always deleted from front
position.
Steps:
1. Check whether queue is Empty means check
(front==-1).
2. If it is empty then display Queue is empty. If queue is not
empty then step 3
3. Check if (front==rear) if it is true then set front=rear= -1
else check if (front==size-1), if it is true then set front=0
and return the element.

returns the data element in the front (Q->next) node of the


X=peek (Q)
Queue Q

isEmpty (Q) Check if the Queue is empty which is equivalent to checking if


Q==NULL
Page 38 of 57

Set A

a) Implement a linear queue library (st_queue.h) of integers using a static implementation


of the queue and implementing the above six operations. Write a program that includes queue
library and calls different queueoperations

b) Implement a circular queue library (cir_queue.h) of integers using a dynamic (circular


linked list) implementation of the queue and implementing the above five operations. Write a
menu driven program that includes queue library and calls different queue operations.

Set B

a) Implement a linear queue library (dyqueue.h) of integers using a dynamic (circular linked
list) implementation of the queue and implementing the above five operations. Write a driver
program that includes queue library and calls different queue operations.

b)Write a program to reverse the elements of a queue using queue library.

Set C

a) Implement queue library using array or linked list. Use this queue library to simulate
waiting list operations of railway reservation system.

Assignment Evaluation

0: Not Done 1: Incomplete 2:Late Complete


3: Needs Improvement 4: Complete 5: Well Done

Practical In-charge
Page 39 of 57

Assignment 10:Priority Queue and Doubly Ended Queue (Dqueue)

1) Priority queue:

A priority queue is a data structure in which each element is assigned a priority. The priority
of the element will be used to determine the order in which the elements will be processed.

The general rules of processing the elements of a priority queue are:

1. An element with higher priority is processed before an element with a lower priority.
2. Two elements with the same priority are processed on a first-come-first-served (FCFS)
basis.

A Priority Queue is different from a normal queue, because instead of being a “first-in-first-
out”, values come out in order by priority, the highest-priority is retrieved first.Thepriority
of the element can be set based on various factors. Priority queues are widely
used in operating systems to execute the highest priority process first.

2) Doubly Ended Queue (Dqueue):

Doubly Ended Queue is a Queue data structure in which the insert and delete operations can
be performed at both the ends (front and rear) of the queue. That means, we can insert at
both front and rear positions and can delete from both front and rear positions.

AddFront(): Adds an item at the front of Dequeue.


AddRear(): Adds an item at the rear of Dequeue.
RemoveFront(): Deletes an item from front of Dequeue.
deleteRear(): Deletes an item from rear of Dequeue.
getFront(): Gets the front element from queueue.
getRear(): Gets the last element from queueue.
isEmpty(): Checks whether Dequeue is empty or not.
isFull(): Checks whether Dequeue is full or not.

Set A

a) Implement a priority queue library (PriorityQ.h) of integers using a static implementation


of the queue and implementing the below two operations. Write a driver program that
includes queue library and calls different queue operations.
1) Add an element with its priorityinto the queue.
2) Deleteanelement from queue according to its priority.
Page 40 of 57

b) A doubly ended queue allows additions and deletions from both the ends that is front and
rear. Initially additions from the front will not be possible. To avoid this situation, the array
can be treated as if it were circular. Implement a queue library (dstqueue.h) of integers using
a static implementation of the circular queue and implementing the nine operations :
1)init(Q), 2) isempty(Q) 3) isFull(Q) 4)getFront(Q), 5)getRear(Q), 6)addFront(Q,x),
7)deleteFront(Q) 8) addRear(Q,x) 9)deleteRear(Q) .

Assignment Evaluation

0: Not Done 1: Incomplete 2:Late Complete


3: Needs Improvement 4: Complete 5: Well Done

Practical In-charge
Page 41 of 57

Section II

Software Engineering
Mini Project
Page 42 of 57

MiniProjectusingSoftware EngineeringTechniques

Marks
Sr. (out of 5 for
Assignment Name Signature
No each
diagram)

Structural Model :

➢ Problem definition, scope of


proposed system

1. ➢ Requirement Specification

➢ ER Diagram

➢ Use Case Diagram

➢ Class Diagrams
Behavioral Model :

➢ Sequence Diagram

2. ➢ Collaboration Diagram

➢ Activity Diagram

➢ State Chart Diagram

Architectural Model :

➢ Component Diagram
3.
➢ Deployment Diagram

➢ Package Diagram

Total ( out of 60 )

Total Converted to 10

This is to certify that Mr/Ms ______________________________________ has


successfully completed the course work Mini Project and has scored ___
Marks out of 10.

Instructor Head, Dept. Of Comp. Sc.

Internal Examiner External Examiner


Page 43 of 57

Assignment 1.1 : Problem Definition &Scope


1) Problem Definition:
___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________
Page 44 of 57

2) Scope of the proposed system:


___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

Assignment Evaluation

0: Not Done 1: Incomplete 2:Late Complete


3: Needs Improvement 4: Complete 5: Well Done

Practical In-charge
Page 45 of 57

Assignment 1.2: Requirement Specification:

Purpose of the System: Intended use of the system :

Users: Those who will primarily benefit from the new system and those who will be affected
by the new system

Functional Requirements
• Describe what the system actually does
• Describes intended interactions between users and the system, independent of
implementation

Assumptions
List any assumptions that affect the requirements, for example, equipment availability, user
expertise, etc. For example, a specific operating system is assumed to be available; if the
operating system is not available, the Requirements Specification would then have to change
accordingly.

Constraints (if any)

Assignment Evaluation

0: Not Done 1: Incomplete 2:Late Complete


3: Needs Improvement 4: Complete 5: Well Done

Practical In-charge
Page 46 of 57

Assignment 1.3: ER Diagram

Assignment Evaluation

0: Not Done 1: Incomplete 2:Late Complete


3: Needs Improvement 4: Complete 5: Well Done

Practical In-charge
Page 47 of 57

Assignment 1.4: Use case Diagram


Identifying the Actors of the system:

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

______________________________________________________________________

Use Cases:
Page 48 of 57

Assignment Evaluation

0: Not Done 1: Incomplete 2:Late Complete


3: Needs Improvement 4: Complete 5: Well Done

Practical In-charge
Page 49 of 57

Assignment 1.5: Class Diagram


Classes Identified:

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

Class Diagram:
Page 50 of 57

Assignment Evaluation

0: Not Done 1: Incomplete 2:Late Complete


3: Needs Improvement 4: Complete 5: Well Done

Practical In-charge
Page 51 of 57

Assignment 2.1: Sequence Diagram

Assignment Evaluation

0: Not Done 1: Incomplete 2:Late Complete


3: Needs Improvement 4: Complete 5: Well Done

Practical In-charge
Page 52 of 57

Assignment 2.2: Collaboration Diagram

Assignment Evaluation

0: Not Done 1: Incomplete 2:Late Complete


3: Needs Improvement 4: Complete 5: Well Done

Practical In-charge
Page 53 of 57

Assignment 2.3: Activity Diagram

Assignment Evaluation

0: Not Done 1: Incomplete 2:Late Complete


3: Needs Improvement 4: Complete 5: Well Done

Practical In-charge
Page 54 of 57

Assignment 2.4: State Chart Diagram

Assignment Evaluation

0: Not Done 1: Incomplete 2:Late Complete


3: Needs Improvement 4: Complete 5: Well Done

Practical In-charge
Page 55 of 57

Assignment 3.1: Component Diagram

Assignment Evaluation

0: Not Done 1: Incomplete 2:Late Complete


3: Needs Improvement 4: Complete 5: Well Done

Practical In-charge
Page 56 of 57

Assignment 3.2: Deployment Diagram

Assignment Evaluation

0: Not Done 1: Incomplete 2:Late Complete


3: Needs Improvement 4: Complete 5: Well Done

Practical In-charge
Page 57 of 57

Assignment 3.3: Package Diagram

Assignment Evaluation

0: Not Done 1: Incomplete 2:Late Complete


3: Needs Improvement 4: Complete 5: Well Done

Practical In-charge

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy