8/2/2019 Lab Assignment Sheet
1/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA1
Assignment Sheet
for
Data Structures Lab(Jan 2012)
8/2/2019 Lab Assignment Sheet
2/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA2
NOTE
All Data structures students are required to maintain a lab record (Hard
Copy). This record would primarily consist of following:
1. Lab Assignment questions2. And the following exercises for any two questions:
a.pseudo code or flowchart of the proposed solution (Not the CCode)
b. Problems faced during the execution of the code (this could bea programming error, logical misunderstanding or any other
difficulty which took a considerable amount of time to resolve).
8/2/2019 Lab Assignment Sheet
3/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA3
Assignment (Lab A): Week 1 (9th
Jan to 14th
Jan)
Ex 1. Declare a structure for complex numbers (one real and one imaginary part). Writea function that adds two such complex numbers and returns the resultant. This can be
done in five different ways, brief description of each is given below along withfunction declaration, your task is to define a function for each:
//structuredefinition
structnode{
inta;intb;
};
a. Pass the node variable a and b into a function and take node as return type.structnodeadd(structnode,structnode);
b. Pass the pointers to node variable a and b into function and take node asreturn type.
structnodeadd(structnode*,structnode*);
c. Pass the pointers to node variable a and b into a function and take pointer tonode as return type.
structnode*add(structnode*,structnode*);
d. Pass the node variable into a function, but using array of structuresstructnodes[3];
structnode
add(struct
node
,struct
node
);
e. Pass the pointer to array of structure to a function.structnodes[3];
voidadd(structnode*);
Ex 2. WAP to do the following:a. To read n integers from userb. Write these integers onto a filec. Read the file and determine largest and smallest elements in the list and
finally write these values at the end of file.
Ex 3. WAP to input a list of names from user and do the following a. Write these names onto a fileb. Read this list of names from the file and sort them alphabeticallyc. Write this sorted list onto another file
8/2/2019 Lab Assignment Sheet
4/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA4
Assignment (Lab B): Week 1 (9th
Jan to 14th
Jan)
Here is a snapshot of a file which stores students details:
Ex 1. Create a file of above type having 10 such records using NOTEPAD application.Then through a C Program, read this file and display all students, branch wise.
Ex 2. Modify above program by including students CGPA too. Now read the file anddisplay the branch wise topper.
Note-1: All students are informed to revise array of structure and pointer to array of
structure. Next week topic: Link List.
S No Branch Name1 CS Rohan2 EC Ajai
3 BT Juhi
4 CS Shyam
8/2/2019 Lab Assignment Sheet
5/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA5
Assignment (Lab A): Week 2 (16th
Jan to 21st
Jan)
Ex 1. Write a program to declare a structure (member elements: string, integer andfloat). Now write a function to interchange the values of two such structures and
display the resultant in main(Use structure pointers).
Ex 2. The details of call made by a Mobile SIM is stored in a structure as followsstructcall_details
{
charmobile_number[11];
intcall_duration;
};
Assume call cost per second is Rs.0.5. Input call_details forn users and write
a program using pointers to structures
structcall_detailsCall_List[20];
(i) To input n such records (input n from user).voidinputList(structcall_detailsList[])
(ii) To generate a bill for a particular mobile number, as requested by theuser(input number from the user) as input:
voidgenerateBill(char*m_number);
(iii) Sort the call details as per the mobile number and display the sortedlist.
voidsortList(structcall_detailsList[]);
//Use the interchanging function written in question one
Ex 3. Write a program to store application in the data report of a store. Customer data isrecorded as follows in file customer.txt.
S.no Customer Age1 Archie 22
2 Jughead 21
3 Betty 23
4
Veronica
185 Joey 24
6 Chandler 25Read the records from file and create a array of customers that stores the customers
data. Display all customers who are older than 20 years.
8/2/2019 Lab Assignment Sheet
6/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA6
Assignment (Lab B): Week 2 (16th
Jan to 21st
Jan)
Quiz scheduled
8/2/2019 Lab Assignment Sheet
7/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA7
Assignment (Lab A): Week 3 (23rd
Jan to 28th
Jan)
(Evaluation 10 marks)
Use the structure
structnode{intinfo;
structnode*next;};
Ex 1. Write a menu driven program (use switch case) to do the following :a. Create a Linked Listb. Insert n elements in a Linked Listc. Determine the length of Linked Listd. Insert at a given position in the Linked Liste. Insert at beginningf. Insert at Endg. Insert after a given valueh. Display the Linked List
Do the above using functions for each. You can reuse functions if the functionality
overlaps (e.g. Insertion at beginning is nothing but Inserting at position 1). Remember
that if the function is changing the value of the start pointer of the list (e.g. insertionfunctions) then that pointer would have to be returned from that function.
Ex 2. What is wrong with following codes:a. Code to insert at the end of a linked list
curr=start;
while(curr!=NULL)
curr=curr>next;printf(\n\tEntertheelementtobeinserted);
scanf(%d,&curr>info);curr>next=NULL;
suggest the changes.
8/2/2019 Lab Assignment Sheet
8/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA8
b. Code to insert at a position pint
p,
i=0;
structnode*temp;
printf(\n\tEntertheposition);
scanf(%d,&p);
curr=start;while(inext;i++;
}
temp=(structnode*)malloc(sizeof(structnode));printf(\n\tEntertheelementyouwanttoinsert);
scanf(%d,&temp
>info);
curr>next=temp;
temp>next=curr>next;
suggest the changes. What will happen in this case if list is empty (start =
NULL). What will happen when p is greater than the number of elementsin the list(e.g. p = 7, and there are 5 elements in the list?)
8/2/2019 Lab Assignment Sheet
9/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA9
Assignment (Lab B): Week 3 (23rd
Jan to 28th
Jan)
Use the structure for following programs
structnode
{intinfo;
structnode*next;
};
Ex 1. Write a menu driven program (use switch case) program to do thefollowing :
a. Create a Linked Listb. Insert an element in a Linked Listc. Delete an element from the Linked Listd. Search for a given element in the Linked List and display its position.e. Display the Linked List
Do the above using functions for each. You can reuse functions if thefunctionality overlaps.
Ex 2. Write a program to create two linked lists with pointers:structnode*start1,*start2;
Write a function that takes these two linked list pointers as arguments and
concatenates the two linked lists and returns the first pointer.
structnode*concatList(structnode*start1,structnode
*start2);
Note: You can use the functions of the above question, only difference is the
argument to the functions would be start1 and start2.
Ex 3. Write a program to display alternate elements of a linked list.If the linked is: 12,35,2,56,23,72,85,166
Output should be: 1222385
8/2/2019 Lab Assignment Sheet
10/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA10
NOTE
CSE/IT students are informed that now onwards, they have to program using
cygwin. ECE and BT students are encouraged to do the same.
8/2/2019 Lab Assignment Sheet
11/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA11
Assignment (Lab A): Week 4 (30th
Jan to 4th
Feb)
(In a team of two students- Marks 10)
Ex 1. Let's say you're on top of a cliff, which is vertically 150 m above to the oceanbelow. You throw a ball with an initial speed of 8.40 m/s at an angle of 20 degrees
above the horizontal.
(a)How long does it take before it hits the water?(b)How far is it from the base of the cliff to the point of impact?Answers
(a)The ball was in the air for 5.83 s.(b)= 46.0 m.
You have to write a program, which shows the projectile path. All the points of the
projectile paths have to be stored in a FILE. You may be asked to find (Not calculate
every time by using formulas, but from the FILE) the maximum height achieved bythe ball (i.e. when velocity became 0 and it started falling down), the maximum
change in curvature, direction of tangent at any time. You may add more queries andsubsequently modify the FILE structure. For storing t (time), X, Y etc. use of Link
List is Compulsory.
Please refer to the Document given Projectile Motion.pdf for understanding the
equations involved.
Ex 2.A given file cheq.txt is equipped with chemical equations (develop a policy torepresent a chemical equation in txt file).
Write a program, whicha. Reads the given file in to a linked list and checks whether the equation is
balanced or not.
b. If equation is balanced append that equation in to the balanced.txt fileotherwise append that equation into the unbalcd.txt file.
You have to process the following types of queries(Using Menu):
NOTE
All Data structures students are required to maintain a lab record (Hard Copy). This record
would primarily consist of following:
1. Lab Assignment questions2. And the following exercises for any two questions:
a. pseudo code or flowchart of the proposed solution(Not the C Code)b. Problems faced during the execution of the code (this could be a programming
error, logical misunderstanding or any other difficulty which took a
considerable amount of time to resolve).
8/2/2019 Lab Assignment Sheet
12/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA12
a. Given a compound display the equations, which have the compound asreactant from balanced.txt file.
b. Given a compound, display the equations, which have the compound asproduct from balanced.txt file.
c.
Given a compound, display the equations, which have the compound acting ascatalyst from balanced.txt file.
8/2/2019 Lab Assignment Sheet
13/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA13
Levels of inner polygon
Assignment (Lab B): Week 4 (30th
Jan to 4th
Feb)
Ex 1. The shape of an irregular polygon is given. Write a Menu driven program toa. Input the coordinates from the user and write the coordinates of this polygon
into a file polygon.txt.b. Compute the Mid point of all the sides of polygon, store the coordinates in a
linked list and write back to the same file (by appending).
c. Generate the polygon automatically by joining the mid points of sides of thepolygon.
d. Calculate the perimeter and the area of polygon formed by joining themidpoints of sides of the polygon.
e. Write a function that calculates nth level of inner polygons address, given aretwo levels of inner polygons.
Irregular polygon
NOTE
All Data structures students are required to maintain a lab record (Hard Copy).. This record
would primarily consist of following:
1. Lab Assignment questions2. And the following exercises for any two questions:
a. pseudo code or flowchart of the proposed solution (Not the C Code)b. Problems faced during the execution of the code (this could be a programming
error, logical misunderstanding or any other difficulty which took a
considerable amount of time to resolve).
8/2/2019 Lab Assignment Sheet
14/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA14
Assignment (Lab A): Week 5 (6th
Feb to 11th
Feb)
Ex 1. Suppose we wish to manipulate polynomials of the form p(x) = c1xe1 +c2x
e2+ . . . + cnx
en, where e1 > e2 > . . . > en0. Such a polynomial can be
represented by a linked list in which each cell has three fields: one for thecoefficient ci, one for the exponent ei, and one for the pointer to the next cell.
Write a program to differentiate polynomials represented in this manner.
Note : the structure can be:
structterm{
intcoeff;intexp;
structterm*next;};
Ex 2. Write individual functions to:a. Add : Takes two polynomials as input and return their additionstruct term*add(structterm*poly1,structterm*poly2);
b. Multiply : Takes two polynomials as input and return their multiplicationstruct term*multiply(structterm*poly1,structterm*poly2);
c. Subtract: Takes two polynomials(p1, p2) as input and return theirsubtraction(p1-p2)
struct term*subtract(structterm*poly1,structterm*poly2);
Note: Make the above code menu driven and reuse the code of Ex 1.
NOTE
All Data structures students are required to maintain a lab record (Hard Copy).. This record
would primarily consist of following:
1. Lab Assignment questions2. And the following exercises for any two questions:
a. pseudo code or flowchart of the proposed solution (Not the C Code)b. Problems faced during the execution of the code (this could be a programming
error, logical misunderstanding or any other difficulty which took a
considerable amount of time to resolve).
8/2/2019 Lab Assignment Sheet
15/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA15
Assignment (Lab B): Week 5 (6th
Feb to 11th
Feb)
Marks:10
Suppose we declare cells by
structcelltype{intbit;//possiblevalues0or1onlystructcelltype*next;
};
Ex 1. A binary numberb1b2 . . . bn, where each biis 0 or 1, has numerical value. Thisnumber can be represented by the list b1, b2 , . . . , bn. That list, in turn, can berepresented as a linked list of cells of type celltype.
For example for a binary number (110011)2 the list would be
Write a menu driven program which:
a. Inputs a binary numberb. Outputs the entered binary numberc. Calculate the decimal equivalent of the binary number
1 1 0 0 1 1
bnumber
NULL
NOTE
All Data structures students are required to maintain a lab record (Hard Copy).. This recordwould primarily consist of following:
1. Lab Assignment questions2. And the following exercises for any two questions:
a. pseudo code or flowchart of the proposed solution (Not the C Code)b. Problems faced during the execution of the code (this could be a programming
error, logical misunderstanding or any other difficulty which took a considerable
amount of time to resolve).
8/2/2019 Lab Assignment Sheet
16/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA16
Ex 2. For the above binary numbera. Write a procedure compare(bnumber, bnumber1) that determines which one
of the binary numbers is greater(let the function return 1 ifbnumberis greater
and 2 ifbnumber1 is greater and 0 if both are equal).b. Implement left shift operation on the binary number (left shift means shiftingall digits to the left)
Note: Append these functionalities to the Ex 1 functions
8/2/2019 Lab Assignment Sheet
17/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA17
Assignment (Lab A): Week 6 (13th
Feb to 18th
Feb)
Quiz Scheduled
Course: Singly Linked List
8/2/2019 Lab Assignment Sheet
18/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA18
Assignment (Lab B): Week 6 (13th
Feb to 18th
Feb)
Ex 1. The Josephus problem is an election method of a group of n people in a circle.Starting from the first person, you count around the circle k times and remove thatperson from the circle. Continuing from the person who has just been removed, countk times again and that person is removed. This process is repeated until one personremains. That person wins the election.
Write a program in C that allows the user to input the number of people in the circle,n and the number of people to go through each iteration, k. Both n and k must be
positive integers. Displays the sequence of people removed and display the winner ofthe election.
For example if n = 7 and k = 3, the output should be:
Numberofpeople: 7
Numberofpeoplegothrough: 3
Sequenceofpeopleremoved:[3,6,2,7,5,1,4]
The4thpersonwins
Ex 2. Write a program to represent the polynomial as a circular linked list and write amenu driven program to create a polynomial, display a polynomial and multiply two
polynomials.
Note:For procedure to multiply you may use the following guidelines and test your
program with the given testing conditions.
Multiplication of 2 polynomials:
1. Begin with 2 pointers initialized to the head of the 2 CLLs. Move the pointersone location.
NOTE
All Data structures students are required to maintain a lab record (Hard Copy).. This record
would primarily consist of following:
1. Lab Assignment questions2. And the following exercises for any two questions:
a. pseudo code or flowchart of the proposed solution (Not the C Code)b. Problems faced during the execution of the code (this could be a
programming error, logical misunderstanding or any other difficulty which
took a considerable amount of time to resolve).
8/2/2019 Lab Assignment Sheet
19/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA19
2. Keeping the pointer in 1stlist at the same node, move the pointer of the 2nd listover the entire list. Multiply the coefficients and add the exponents at everystage. Insert the coeff, exp pair in a new node in the 3
rdlist.
3. After the entire 2nd list has been traversed once, move the pointer of the 1st listto the next node and position pointer of the 2
ndlist back to the 1stnode and
continue the above process.
4. Before inserting the new coeff, exp pair in the third list, traverse the entire listand if any node with same exponent is already present the new coefficient
must be added to the previous one. Also the exponent should be entered in the
list in the sorted fashion (descending order of exponents).
5. When the pointer of the 1stlist reaches the head node, multiplication iscomplete. Connect the pointer of the last node of the 3
rdlist back to the front,
making it circular and terminate.
Testing Conditions
For evaluation:
Take x as input from user and evaluate the entered polynomial and display result.
Forexample,forx2+x+1=0,
ifx=3then
Outputis13
For multiplication:
Input:
Polynomial1: x2+x+1=0
Polynomial2: 2x+2=0
Multiplicationoftheabove2polynomialsis
2x3+4x2+4x+2=0
Ex 3. Write a menu driven program (use switch case) to do the following :a. Create a Doubly linked list (DLL).b. Insert an element at nth place in the DLLc. Delete an element from nth place in the DLLd. Search for a given element in the DLL and display its position and data.e. Display DLLf. Display reversed DLL
8/2/2019 Lab Assignment Sheet
20/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA20
Cate or 1
Item costCate or 2
All Categories of Items
Items in list
rec
Item cost recItem cost rec
Assignment (Lab A): Week 7 (20th
Feb to 25th
Feb)
Ex 1. A restaurants Menu is divided into following sub-headings :1) Starters2)
Soups3) Main Course
4) Rice5) Breads6) Beverages7) Desertsetc.
Write a user friendly menu driven program which has following features:
a. Create a dynamic data structure to store the above list of items and their costs.Input at least 3 values in each category.
b. Output complete menu of the restaurant.c.
Insert an item in a given categoryd. Edit an item in the category(Delete or change data)
e. Insert a new category in the menu
NOTE
All Data structures students are required to maintain a lab record (Hard Copy).. This record
would primarily consist of following:
1. Lab Assignment questions2. And the following exercises for any two questions:
a. pseudo code or flowchart of the proposed solution (Not the C Code)b. Problems faced during the execution of the code (this could be a
programming error, logical misunderstanding or any other difficulty which
took a considerable amount of time to resolve).
8/2/2019 Lab Assignment Sheet
21/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA21
Ex 2. Add following features to above program:a. Write a function which outputs the list of items which constitute the least value
complete meal(that consist of one dish in every category), along with their costs.b. Ask user to input quantity of a category that she wants to order and then provideminimum value selections available in the menu.
c. Ask user to input the quantity of a category she wants to order and then provide alist of recommended dishes in the category.(recommended dishes can be
indicated by a flag(rec) in the item structure)
8/2/2019 Lab Assignment Sheet
22/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA22
Assignment (Lab B): Week 7 (20th
Feb to 25th
Feb)
Ex 1. Write a C program to demonstrate the working of a stack using arrays and singlylinked list. The elements of the stack may be assumed to be of type integer. The
operations to be supported are:
i. Pushii. Pop
iii. DisplayThe program should print appropriate messages for stack overflow, stackunderflow, and stack empty.
Ex 2. Suppose we declare cells bystructcelltype{
intbit;//possiblevalues0or1onlystructcelltype*next;
};
A binary numberb1b2 . . . bn, where each biis 0 or 1, has numerical value. This
number can be represented by the list b1, b2 , . . . , bn. That list, in turn, can be
represented as a linked list of cells of type celltype. Write a procedureincrement(bnumber) that adds one to the binary number pointed to by bnumber.
NOTE
All Data structures students are required to maintain a lab record (Hard Copy).. This record
would primarily consist of following:
1. Lab Assignment questions2. And the following exercises for any two questions:
a. pseudo code or flowchart of the proposed solution (Not the C Code)b. Problems faced during the execution of the code (this could be a
programming error, logical misunderstanding or any other difficulty which
took a considerable amount of time to resolve).
8/2/2019 Lab Assignment Sheet
23/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA23
For example for a binary number (110011)2 the list would be
And after adding one the list would be
Use a stack operation to add the carry forward.
1 1 0 0 1 1
bnumber
NULL
1 1 0 1 0 0 NULL
CHANGE
8/2/2019 Lab Assignment Sheet
24/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA24
NOTICE
Syllabus for T-1
a. Singly Linked Listb. Circular Linked Listc. Doubly Linked Listd. Multi-List
And all the concepts done before the above
8/2/2019 Lab Assignment Sheet
25/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA25
Assignment (Lab A): Week 8 (12th
Mar to 18th
Mar)
(Practice)
Ex 1. Write a program to create a sparse matrix(single linked list), the elementsare stored left to right and top to down order of index. Your program should have
following functionality:
a. Create and initialize the sparse matrix.b. Add two sparse matricesc. Subtract two sparse matricesd. Delete an element from the matrix, by index and by valuee. Search an element in the matrix and display its position
NOTE
All Data structures students are required to maintain a lab record (Hard Copy).. This record
would primarily consist of following:
1. Lab Assignment questions2. And the following exercises for any two questions:
a. pseudo code or flowchart of the proposed solution (Not the C Code)b. Problems faced during the execution of the code (this could be a
programming error, logical misunderstanding or any other difficulty which
took a considerable amount of time to resolve).
8/2/2019 Lab Assignment Sheet
26/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA26
Assignment (Lab B): Week 8 (12th
Mar to 18th
Mar)
(Practice)
Ex 1.
Write a program to create a sparse matrix (multi list format). This datastructure should have a pointer for every row and every column. For example
following matrix:
23 0 0 1
0 0 3 42
0 15 0 00 24 0 0
Gets stored as:
NULL
0 1 2 3
0
1
2
3
23 1
3 42
15
24
NULL
NULL
NULL
NULL
NULL NULL NULL NULLNULL
Column List
Ro
w
L
i
s
t
4, 4
NOTE
All Data structures students are required to maintain a lab record (Hard Copy).. This recordwould primarily consist of following:
1. Lab Assignment questions2. And the following exercises for any two questions:
a. pseudo code or flowchart of the proposed solution (Not the C Code)b. Problems faced during the execution of the code (this could be a
programming error, logical misunderstanding or any other difficulty which
took a considerable amount of time to resolve).
8/2/2019 Lab Assignment Sheet
27/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA27
For the above data-structure write following functions:a. Initialize a sparse matrix of M X N order (create the list of rows and
columns and allocate NULL to all)
b. Add an element at a given indexc.
Delete an element from a given indexd. Display the M X N matrix (with zeros at corresponding positions)
8/2/2019 Lab Assignment Sheet
28/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA28
Assignment (Lab A): Week 9 (19th
Mar to 25th
Mar)
(Practice- to be done in team of two)
You are required to model a vehicle parking lot system. The parking lot has a facility to
park cars and scooters. The parking lot contains four parking lanes-two for cars and twofor scooters.
Each lane can hold ten vehicles. There is an operator with a console at the East end of the
parking lot. The system should be able to handle following scenarios.
Arrival of a vehicle:1. Type of vehicle (car or scooter) and Registration No. of vehicle should be entered
2. Program should display suitable parking slot
3. Vehicles arrive at East end, leave from West end
Departure of Car:1. If not western most, all cars should be moved to the west2. When the car is driven out, rest of the cars should be moved to the left
3. Vehicle data should be updated
Departure of Scooter:1. Scooters should be able to drive out at random
2. Vehicle data should be updated
Note that when desired the operator must be able to obtain information like number of
vehicles, number of scooters or number of cars currently parked in the parking lot. Also,
the system should be able to display all the parking lots (currently occupied) if desired.Use dynamic data structures (such as queues and linked lists) for the above program.
NOTE
All Data structures students are required to maintain a lab record (Hard Copy).. This record
would primarily consist of following:
1. Lab Assignment questions2. And the following exercises for any two questions:
a. pseudo code or flowchart of the proposed solution (Not the C Code)b. Problems faced during the execution of the code (this could be a
programming error, logical misunderstanding or any other difficulty which
took a considerable amount of time to resolve).
8/2/2019 Lab Assignment Sheet
29/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA29
Assignment (Lab B): Week 9 (19th
Mar to 25th
Mar)
Lab Test-1 Scheduled
Syllabus: Multi List
Marks: 15Duration: 1 hour
8/2/2019 Lab Assignment Sheet
30/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA30
Assignment (Lab A): Week 10 (26th
Mar to 1st
Apr)
(Evaluative: 15 Marks)
Ex 1. Write a Menu Driven Program to implement following polish notationconversions:
1. Convert an infix expression to postfix expression2. Convert an infix expression to prefix expression3. Evaluate postfix expression
There can be multiple digits in a number, let the numbers be separated by
space4. Evaluate prefix expression
There can be multiple digits in a number, let the numbers be separated byspace
Note: use a dynamic stack as an auxiliary data structure, you can re-use previous
exercises.
Ex 2. Implement the following sorting algorithms (use individual functions for each)a. Bubble Sortb. Insertion Sortc. Selection Sort
Ex 3. Present a comparison for the above sorting algorithms (two at a time) in terms ofBest, Worst and Average case. Plot a graph of number of elements vs. time taken(you can know the time by using clock function-consult help). You can take arrays oflarge sizes (1000, 2000, 3000 etc.). A help file has been provided to help understand
the concept of calculating time (time.cpp).
Menu of the program is:i. CompareBubblesortandSelectionsortii. CompareSelectionsortandInsertionsortiii. CompareInsertionsortandBubblesort
NOTE
All Data structures students are required to maintain a lab record (Hard Copy).. This record
would primarily consist of following:
1. Lab Assignment questions2. And the following exercises for any two questions:
a. pseudo code or flowchart of the proposed solution (Not the C Code)b. Problems faced during the execution of the code (this could be a
programming error, logical misunderstanding or any other difficulty which
took a considerable amount of time to resolve).
8/2/2019 Lab Assignment Sheet
31/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA31
Expected Output:
Best Case
8/2/2019 Lab Assignment Sheet
32/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA32
Assignment (Lab B): Week 10 (26th
Mar to 1st
Apr)
(Practice- to be done in teams)
Ex 1. Consider a square of length x units. Divide each side into three parts and connectall the points such that you get nine squares from the original square. Now eliminate the
middle square from every side. This will generate the first stage of fractal with foursquares, each of side x/3 units. Repeat the process with every square. Write a program to
generate fractal of nth stage using recursion.
Ex 2.The Koch curve is generated recursively, as shown in the Figure below. Write a
program that, at each iteration, will generate the points shown as black circles. (The white
circles correspond to points of the previous iteration.)
Write a program that generates points on the Koch curve, all up to n iterations.
Figure for Ex 2
Ex 3. Consider an equilateral triangle of specified length. Break the triangle into fourequal triangles & shade one of them with black color. Break the un-shaded triangles
again & again into four to form a fractal given in the following figure
Level 0 Level 1 Level 4
Reference :Data structures & program Design in C page 110 Robert Kruse 2nd
edition
8/2/2019 Lab Assignment Sheet
33/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA33
Assignment (Lab A): Week 11 (2nd
Apr to 7th
Apr)
(Practice Exercise)
Ex 1.
Draw a Random Polygon of n sides(input n vertices from the user). Color theplygon using
a. Recursive flood fillb. Non recursive flood fill
Ex 2. Implement following problems using recursion:a. Calculate factorial of nb. Calculate summation of nc. Calculate nth element in Fibonacci series
Ex 3.
Write a recursive function for each of following operations on a linked list:a. Display a Linked Listb. Display reversed Linked Listc. Delete an element from the linked listd. Given an integer linked list, calculate the sum of all its elements
8/2/2019 Lab Assignment Sheet
34/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA34
Assignment (Lab B): Week 11 (2nd
Apr to 7th
Apr)
(Evaluative-15 marks)
Ex 1. Implement and show steps in the problem of Tower of Hanoi forn rings.Ex 2. Apply the following transformation rule on a square of 200 pixel length
placed centrally to generate its nth
stage (n: user specified and
8/2/2019 Lab Assignment Sheet
35/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA35
Assignment (Lab B): Week 12 (9th
Apr to 14th
Apr)
(Practice)
Solve the following questions from Test-2 Exam
Ex 1. In Median-last Quick sort, when the input string is of odd length, the middleelement is chosen as pivot element otherwise the last element is chosen as pivot.Apply the said version of quick sort on the following input array to sort it in
ascending order:
12,7,34,0,-1,45,23,8,9,11,18,16,13,20.
Ex 2. Following is the simple transformation rule to generate a pattern:
N=1 N=2 N=3 N=4 N=5
Write a recursive code to generate this pattern for user supplied level N.
Ex 3. Given a doubly linked list of any number of nodes, write a recursive functionExtremeSwap, which will swap vales of the node at extreme pairs. For eg., if the
node values of a doubly linked list are:
1 2 3 4 5 6 7 8After first call of recursive function, the values will be
8 2 3 4 5 6 7 1
After second call of recursive function, the values will be
8 7 3 4 5 6 2 1And , finally function will stop after fourth call, and the values will be
8 7 6 5 4 3 2 1
8/2/2019 Lab Assignment Sheet
36/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA36
Week 13 Lab A (16th Apr to 21st Apr, 2012)
Practice
Q 1. Write a program to implement following variations of Quicksorta. First element is the pivot elementb. Middle element is the pivot elementc. Select any random element to be the pivot element
For a given array display the number of iterations for each of above variation ofQuickSort.
Q 2. Write a program to implementa. Recursive Mergesortb. Non recursive Mergesort
Does the Merger Sort behave differently for sorted and unsorted arrays?
Q 3. Write a program to implement Radix Sort. Display the list after eachiteration of radix sort.
Q 4. Given a linked list of numbers which is the best sorting algorithm to sortit. Implement the algorithm to sort a given
a. Singly Linked Listb. Doubly Linked List
8/2/2019 Lab Assignment Sheet
37/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA37
Week 13- Lab B (16th
Apr to 21st
Apr, 2012)
Practice
Q 1. There is a list of students (Name, Roll No, Course, Marks). Write aprogram to find top three scoring students in each course. There can be variouscourses like CSE, ECE, BI etc. (Apply Median Search Algorithm)
Q 2. In the above list of students, write a program to find a Student either byname or Roll No depending on users choice.
Q 3. Given a sorted list of Items (Item Name, Item ID, Cost ), which is sortedby Item Name, find a particular Item by its name.
8/2/2019 Lab Assignment Sheet
38/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA38
Assignment (Lab A): Week 14 (23rd
April to 29th
April)
Q 1. Write a C program(a)To construct a binary search tree of integers.(b)To traverse the tree using all the methods, i.e. inorder, preorder, and
postorder.(c)To display the elements in the tree.
Following are the algorithm of above programs:
Algorithm CreateTree(p, e)//p pointer to the root
// e element to be inserted
ifp = NILthen
p = Getnode()info(p) e
left(p) right(p) NIL
else
ife = info(p)
thenWrite "Duplicate element. Insertion can't be done"
else
ife < info(p)then
left(p) CreateTree(left(p), e)
else right(p) CreateTree(right(p), e)
returnp
end CreateTree.
Algorithm Preorder(p)//p pointer to the root
ifp NILthen
Write "Node visited: ", info(p)
Preorder(left(p))
Preorder(right(p))end Preorder.
8/2/2019 Lab Assignment Sheet
39/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA39
Algorithm Inorder(p)//p pointer to the root
ifp NIL
then
Inorder(left(p))Write "Node visited: ", info(p)
Inorder(right(p))
end Inorder.
Algorithm Postorder(p)//p pointer to the root
ifp NILthen
Postorder(left(p))
Postorder(right(p))
Write "Node visited: ", info(p)end Postorder.
Q 2. Count nodesWrite a simple recursive function to count the number of nodes in a Tree. Write a
countNode() function that prints the number of node in a tree and takes theaddress of root node as an argument
Q 3. Determine NodeWrite a simple recursive funtion to determine the type of node (leaf, root, non-leaf
node). Input a value from user, search the node with the value in a binary searchtree and do the following :a. If the node is a leaf then print itb. If the node is the root then print the entire treec. If the node is an internal node (non-leaf) node then print its immediate
children
8/2/2019 Lab Assignment Sheet
40/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA40
NOTICE
Lab Test-2 schedule is as follows:
Syllabus : Trees
Date : 30th
April to 5th
May (Lab A)Marks : 15
Duration : 1 hour
Quiz-3 schedule is as follows:
Syllabus : Graphs
Date : 7th
May to 12th
May (Lab B)
Marks : 10
Duration : 45 minutes
8/2/2019 Lab Assignment Sheet
41/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA41
Assignment (Lab B): Week 14 (23rd
April to 29th
April)
Evaluative: 20 Marks
Q 1. Write a recursive function to delete a node from the binary search tree.Display the tree before and after deletion. Test your program for following input:
treeinput:40,20,60,10,25,70,50,65,63,61,30,35,37
delete:10
delete:30delete:40
Q 2. Write a function to display level order traversal of a binary search tree.The nodes at level 0 to n are displayed in increasing order of their level. Also
nodes at same level are displayed from left to right. (Note: you can use auxiliarydata structure to store intermediate nodes for traversing back, use following slides
for hints)
8/2/2019 Lab Assignment Sheet
42/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA42
Lab A: Week 15(1st
May to 8th
May)
Lab Test 2
8/2/2019 Lab Assignment Sheet
43/44
Data Structures Jan 2012
Jaypee Institute of Information Technology, NOIDA43
Assignment(Lab B): Week 15(1st
May to 8th
May)
Practice
Q 1. Write a program to input a graph from the user and display the BFS andDFS traversal of the graph.
Note: You can number the vertices from one to n where n is the number of vertex.Every time user wants to enter a new vertex, just create a vertex with the number
n+1. Let this be a directed graph where every edges weight is one.
Example Output can be:
Menu
1.InputNewGraph2.Addavertextothegraph3.Addanedgetothegraph4.DisplayBreadthFirstTraversalofGraph5.DisplayDepthFirstTraversalofGraph6.Exit
Enteryourchoice: 1
Enterthenumberofverticesofthegraph:
6
Enterthenumberofedgesinthegraph:
5
Entertheedge1:
startvertex : 4
endvertex
:
6Entertheedge2:
startvertex : 1
endvertex : 4
Entertheedge3:startvertex : 1
endvertex : 5
Entertheedge4:
startvertex : 5
endvertex : 3
Entertheedge5:
startvertex
:
6
endvertex : 2
GraphEnteredCorrectly.....
8/2/2019 Lab Assignment Sheet
44/44
Data Structures Jan 2012
Menu
1.InputNewGraph2.Addavertextothegraph3.Addanedgetothegraph4.DisplayBreadthFirstTraversalofGraph5.DisplayDepthFirstTraversalofGraph6.ExitEnteryourchoice: 4
BFStraversalofthegraphis:
145632
.and so on. Make your program as user friendly as possible, handle all the exceptionsand possible wrong inputs from user.
Q 2. Do the above program with input from a file, where the file stores the followingdata:
a. Number of verticesb. Edges
Following is the file format:
graph.txt
#6
(1,4)
(1,5)(4,6)
(5,3)
(6,2)*
# indicates that it is number of verticesand edges are enclosed in brackets, with first number is starting vertex and second
is the ending vertex. Finally * indicates end of file.