You are on page 1of 44

Data Structures Jan 2012

Assignment Sheet for Data Structures Lab (Jan 2012)

Jaypee Institute of Information Technology, NOIDA 1

Data Structures Jan 2012

NOTE
All Data structures students are required to maintain a lab record (Hard Copy). This record would primarily consist of following: 1. Lab Assignment questions 2. 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).

Jaypee Institute of Information Technology, NOIDA 2

Data Structures Jan 2012

Assignment (Lab A): Week 1 (9th Jan to 14th Jan)

Ex 1. Declare a structure for complex numbers (one real and one imaginary part). Write a 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 with function 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 as return type. structnodeadd(structnode*,structnode*); c. Pass the pointers to node variable a and b into a function and take pointer to node as return type. structnode*add(structnode*,structnode*); d. Pass the node variable into a function, but using array of structures structnodes[3]; structnodeadd(structnode,structnode); 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 user b. Write these integers onto a file c. 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 file b. Read this list of names from the file and sort them alphabetically c. Write this sorted list onto another file

Jaypee Institute of Information Technology, NOIDA 3

Data Structures Jan 2012

Assignment (Lab B): Week 1 (9th Jan to 14th Jan)

Here is a snapshot of a file which stores students details: S No 1 2 3 4 Branch CS EC BT CS Name Rohan Ajai Juhi Shyam

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 and display 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.

Jaypee Institute of Information Technology, NOIDA 4

Data Structures Jan 2012

Assignment (Lab A): Week 2 (16th Jan to 21st Jan)

Ex 1. Write a program to declare a structure (member elements: string, integer and float). 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 follows
structcall_details { charmobile_number[11]; intcall_duration; };

Assume call cost per second is Rs.0.5. Input call_details for n users and write a program using pointers to structures
structcall_detailsCall_List[20];

(i) (ii)

To input n such records (input n from user).


voidinputList(structcall_detailsList[])

To generate a bill for a particular mobile number, as requested by the user(input number from the user) as input:
voidgenerateBill(char*m_number);

(iii)

Sort the call details as per the mobile number and display the sorted list.
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 is recorded as follows in file customer.txt. S.no Customer Age 1 Archie 22 2 Jughead 21 3 Betty 23 4 Veronica 18 5 Joey 24 6 Chandler 25 Read the records from file and create a array of customers that stores the customers data. Display all customers who are older than 20 years.

Jaypee Institute of Information Technology, NOIDA 5

Data Structures Jan 2012

Assignment (Lab B): Week 2 (16th Jan to 21st Jan)

Quiz scheduled

Jaypee Institute of Information Technology, NOIDA 6

Data Structures Jan 2012

Assignment (Lab A): Week 3 (23rd Jan to 28th Jan) (Evaluation 10 marks)

Use the structure structnode{ intinfo; structnode*next; }; Ex 1. a. b. c. d. e. f. g. h. Write a menu driven program (use switch case) to do the following : Create a Linked List Insert n elements in a Linked List Determine the length of Linked List Insert at a given position in the Linked List Insert at beginning Insert at End Insert after a given value 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. insertion functions) 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.

Jaypee Institute of Information Technology, NOIDA 7

Data Structures Jan 2012

b. Code to insert at a position p intp,i=0; structnode*temp; printf(\n\tEntertheposition); scanf(%d,&p); curr=start; while(i<p){ curr=curr>next; 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 elements in the list(e.g. p = 7, and there are 5 elements in the list?)

Jaypee Institute of Information Technology, NOIDA 8

Data Structures Jan 2012

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 the following : a. Create a Linked List b. Insert an element in a Linked List c. Delete an element from the Linked List d. 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 the functionality 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

Jaypee Institute of Information Technology, NOIDA 9

Data Structures Jan 2012

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.

Jaypee Institute of Information Technology, NOIDA 10

Data Structures Jan 2012

Assignment (Lab A): Week 4 (30th Jan to 4th Feb) (In a team of two students- Marks 10)

NOTE All Data structures students are required to maintain a lab record (Hard Copy). This record would primarily consist of following: 1. Lab Assignment questions 2. 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).

Ex 1. Let's say you're on top of a cliff, which is vertically 150 m above to the ocean below. 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 by the 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 and subsequently 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 to represent a chemical equation in txt file). Write a program, which a. 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 file otherwise append that equation into the unbalcd.txt file. You have to process the following types of queries(Using Menu):
Jaypee Institute of Information Technology, NOIDA 11

Data Structures Jan 2012

a. Given a compound display the equations, which have the compound as reactant from balanced.txt file. b. Given a compound, display the equations, which have the compound as product from balanced.txt file. c. Given a compound, display the equations, which have the compound acting as catalyst from balanced.txt file.

Jaypee Institute of Information Technology, NOIDA 12

Data Structures Jan 2012

Assignment (Lab B): Week 4 (30th Jan to 4th Feb)


NOTE All Data structures students are required to maintain a lab record (Hard Copy).. This record would primarily consist of following: 1. Lab Assignment questions 2. 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).

Ex 1. The shape of an irregular polygon is given. Write a Menu driven program to a. 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 the polygon. d. Calculate the perimeter and the area of polygon formed by joining the midpoints of sides of the polygon. e. Write a function that calculates nth level of inner polygons address, given are two levels of inner polygons.

Levels of inner polygon Irregular polygon

Jaypee Institute of Information Technology, NOIDA 13

Data Structures Jan 2012

Assignment (Lab A): Week 5 (6th Feb to 11th Feb)


NOTE All Data structures students are required to maintain a lab record (Hard Copy).. This record would primarily consist of following: 1. Lab Assignment questions 2. 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).

Ex 1. Suppose we wish to manipulate polynomials of the form p(x) = c1xe1 + c2xe2 + . . . + cnxen, where e1 > e2 > . . . > en0. Such a polynomial can be represented by a linked list in which each cell has three fields: one for the coefficient 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 addition
structterm*add(structterm*poly1,structterm*poly2);

b. Multiply : Takes two polynomials as input and return their multiplication


structterm*multiply(structterm*poly1,structterm*poly2);

c. Subtract: Takes two polynomials(p1, p2) as input and return their subtraction(p1-p2)
structterm*subtract(structterm*poly1,structterm*poly2);

Note: Make the above code menu driven and reuse the code of Ex 1.

Jaypee Institute of Information Technology, NOIDA 14

Data Structures Jan 2012

Assignment (Lab B): Week 5 (6th Feb to 11th Feb) Marks:10


NOTE All Data structures students are required to maintain a lab record (Hard Copy).. This record would primarily consist of following: 1. Lab Assignment questions 2. 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).

Suppose we declare cells by structcelltype{ intbit;//possiblevalues0or1only structcelltype*next; }; Ex 1. A binary number b1b2 . . . bn, where each bi is 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. For example for a binary number (110011)2 the list would be bnumber

NULL

Write a menu driven program which: a. Inputs a binary number b. Outputs the entered binary number c. Calculate the decimal equivalent of the binary number

Jaypee Institute of Information Technology, NOIDA 15

Data Structures Jan 2012

Ex 2. For the above binary number a. Write a procedure compare(bnumber, bnumber1) that determines which one of the binary numbers is greater(let the function return 1 if bnumber is greater and 2 if bnumber1 is greater and 0 if both are equal). b. Implement left shift operation on the binary number (left shift means shifting all digits to the left) Note: Append these functionalities to the Ex 1 functions

Jaypee Institute of Information Technology, NOIDA 16

Data Structures Jan 2012

Assignment (Lab A): Week 6 (13th Feb to 18th Feb)

Quiz Scheduled Course: Singly Linked List

Jaypee Institute of Information Technology, NOIDA 17

Data Structures Jan 2012

Assignment (Lab B): Week 6 (13th Feb to 18th Feb)


NOTE All Data structures students are required to maintain a lab record (Hard Copy).. This record would primarily consist of following: 1. Lab Assignment questions 2. 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).

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 that person from the circle. Continuing from the person who has just been removed, count k times again and that person is removed. This process is repeated until one person remains. 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 of the 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 a menu 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 pointers one location.

Jaypee Institute of Information Technology, NOIDA 18

Data Structures Jan 2012

2. Keeping the pointer in 1stlist at the same node, move the pointer of the 2nd list over the entire list. Multiply the coefficients and add the exponents at every stage. Insert the coeff, exp pair in a new node in the 3rd list. 3. After the entire 2nd list has been traversed once, move the pointer of the 1st list to the next node and position pointer of the 2nd list back to the 1stnode and continue the above process. 4. Before inserting the new coeff, exp pair in the third list, traverse the entire list and 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 is complete. Connect the pointer of the last node of the 3rd list 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: Polynomial2: x2+x+1=0 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 DLL c. Delete an element from nth place in the DLL d. Search for a given element in the DLL and display its position and data. e. Display DLL f. Display reversed DLL
Jaypee Institute of Information Technology, NOIDA 19

Data Structures Jan 2012

Assignment (Lab A): Week 7 (20th Feb to 25th Feb)


NOTE All Data structures students are required to maintain a lab record (Hard Copy).. This record would primarily consist of following: 1. Lab Assignment questions 2. 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).

Ex 1. A restaurants Menu is divided into following sub-headings : 1) Starters 2) Soups 3) Main Course 4) Rice 5) Breads 6) Beverages 7) 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 category d. Edit an item in the category(Delete or change data) e. Insert a new category in the menu

Items in list
Category1 Category2 Item Item cost cost
rec

Item

cost

rec

rec

All Categories of Items

Jaypee Institute of Information Technology, NOIDA 20

Data Structures Jan 2012

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 provide minimum value selections available in the menu. c. Ask user to input the quantity of a category she wants to order and then provide a list of recommended dishes in the category.(recommended dishes can be indicated by a flag(rec) in the item structure)

Jaypee Institute of Information Technology, NOIDA 21

Data Structures Jan 2012

Assignment (Lab B): Week 7 (20th Feb to 25th Feb)


NOTE All Data structures students are required to maintain a lab record (Hard Copy).. This record would primarily consist of following: 1. Lab Assignment questions 2. 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).

Ex 1. Write a C program to demonstrate the working of a stack using arrays and singly linked list. The elements of the stack may be assumed to be of type integer. The operations to be supported are: i. Push ii. Pop iii. Display The program should print appropriate messages for stack overflow, stack underflow, and stack empty.

Ex 2. Suppose we declare cells by structcelltype{ intbit;//possiblevalues0or1only structcelltype*next; }; A binary number b1b2 . . . bn, where each bi is 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 procedure increment(bnumber) that adds one to the binary number pointed to by bnumber.

Jaypee Institute of Information Technology, NOIDA 22

Data Structures Jan 2012

For example for a binary number (110011)2 the list would be bnumber 1 1 0 0 1 1 NULL

And after adding one the list would be bnumber 1 1 0 1 0 0

CHANGE

NULL

Use a stack operation to add the carry forward.

Jaypee Institute of Information Technology, NOIDA 23

Data Structures Jan 2012

NOTICE
Syllabus for T-1 a. b. c. d. Singly Linked List Circular Linked List Doubly Linked List Multi-List

And all the concepts done before the above

Jaypee Institute of Information Technology, NOIDA 24

Data Structures Jan 2012

Assignment (Lab A): Week 8 (12th Mar to 18th Mar) (Practice)


NOTE All Data structures students are required to maintain a lab record (Hard Copy).. This record would primarily consist of following: 1. Lab Assignment questions 2. 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).

Ex 1. Write a program to create a sparse matrix(single linked list), the elements are 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 matrices c. Subtract two sparse matrices d. Delete an element from the matrix, by index and by value e. Search an element in the matrix and display its position

Jaypee Institute of Information Technology, NOIDA 25

Data Structures Jan 2012

Assignment (Lab B): Week 8 (12th Mar to 18th Mar) (Practice)


NOTE All Data structures students are required to maintain a lab record (Hard Copy).. This record would primarily consist of following: 1. Lab Assignment questions 2. 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).

Ex 1. Write a program to create a sparse matrix (multi list format). This data structure should have a pointer for every row and every column. For example following matrix: 23 0 0 0 Gets stored as:
Column List 4, 4

0 0 15 24

0 3 0 0

1 42 0 0

0 0 1 2 3 23

3 1

NULL NULL NULL NULL NULL

R o w L i s t

3 15 24

42

NULL

NULL

NULL

NULL

NULL

Jaypee Institute of Information Technology, NOIDA 26

Data Structures Jan 2012

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 index c. Delete an element from a given index d. Display the M X N matrix (with zeros at corresponding positions)

Jaypee Institute of Information Technology, NOIDA 27

Data Structures Jan 2012

Assignment (Lab A): Week 9 (19th Mar to 25th Mar) (Practice- to be done in team of two)
NOTE All Data structures students are required to maintain a lab record (Hard Copy).. This record would primarily consist of following: 1. Lab Assignment questions 2. 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).

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 two for 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 west 2. 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.

Jaypee Institute of Information Technology, NOIDA 28

Data Structures Jan 2012

Assignment (Lab B): Week 9 (19th Mar to 25th Mar) Lab Test-1 Scheduled Syllabus: Multi List Marks: 15 Duration: 1 hour

Jaypee Institute of Information Technology, NOIDA 29

Data Structures Jan 2012

Assignment (Lab A): Week 10 (26th Mar to 1st Apr) (Evaluative: 15 Marks)
NOTE All Data structures students are required to maintain a lab record (Hard Copy).. This record would primarily consist of following: 1. Lab Assignment questions 2. 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).

Ex 1. Write a Menu Driven Program to implement following polish notation conversions: 1. Convert an infix expression to postfix expression 2. Convert an infix expression to prefix expression 3. Evaluate postfix expression There can be multiple digits in a number, let the numbers be separated by space 4. Evaluate prefix expression There can be multiple digits in a number, let the numbers be separated by space 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 Sort b. Insertion Sort c. Selection Sort Ex 3. Present a comparison for the above sorting algorithms (two at a time) in terms of Best, 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 of large 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. CompareBubblesortandSelectionsort ii. CompareSelectionsortandInsertionsort iii. CompareInsertionsortandBubblesort

Jaypee Institute of Information Technology, NOIDA 30

Data Structures Jan 2012

Expected Output:

Best Case

Jaypee Institute of Information Technology, NOIDA 31

Data Structures Jan 2012

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 connect all 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 four squares, 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 four equal 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

Jaypee Institute of Information Technology, NOIDA 32

Data Structures Jan 2012

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 the plygon using a. Recursive flood fill b. Non recursive flood fill Ex 2. Implement following problems using recursion: a. Calculate factorial of n b. Calculate summation of n c. 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 List b. Display reversed Linked List c. Delete an element from the linked list d. Given an integer linked list, calculate the sum of all its elements

Jaypee Institute of Information Technology, NOIDA 33

Data Structures Jan 2012

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 for n 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 <= 6) version. Usage of different colors at each stage will be appreciated.

Ex 3. Show steps of Ackermans Function for a given m and n

Jaypee Institute of Information Technology, NOIDA 34

Data Structures Jan 2012

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 middle element 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 function ExtremeSwap, 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 8 After 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 1 And , finally function will stop after fourth call, and the values will be 8 7 6 5 4 3 2 1

Jaypee Institute of Information Technology, NOIDA 35

Data Structures Jan 2012

Week 13 Lab A (16th Apr to 21st Apr, 2012) Practice Q 1. Write a program to implement following variations of Quicksort a. First element is the pivot element b. Middle element is the pivot element c. Select any random element to be the pivot element For a given array display the number of iterations for each of above variation of QuickSort. Write a program to implement a. Recursive Mergesort b. Non recursive Mergesort Does the Merger Sort behave differently for sorted and unsorted arrays?

Q 2.

Q 3. Write a program to implement Radix Sort. Display the list after each iteration of radix sort. Q 4. Given a linked list of numbers which is the best sorting algorithm to sort it. Implement the algorithm to sort a given a. Singly Linked List b. Doubly Linked List

Jaypee Institute of Information Technology, NOIDA 36

Data Structures Jan 2012

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 a program to find top three scoring students in each course. There can be various courses 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 by name or Roll No depending on users choice. Q 3. Given a sorted list of Items (Item Name, Item ID, Cost ), which is sorted by Item Name, find a particular Item by its name.

Jaypee Institute of Information Technology, NOIDA 37

Data Structures Jan 2012

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 if p = NIL then p = Getnode() info(p) e left(p) right(p) NIL else if e = info(p) then Write "Duplicate element. Insertion can't be done" else if e < info(p) then left(p) CreateTree(left(p), e) else right(p) CreateTree(right(p), e) return p end CreateTree. Algorithm Preorder(p) // p pointer to the root if p NIL then Write "Node visited: ", info(p) Preorder(left(p)) Preorder(right(p)) end Preorder.

Jaypee Institute of Information Technology, NOIDA 38

Data Structures Jan 2012

Algorithm Inorder(p) // p pointer to the root if p NIL then Inorder(left(p)) Write "Node visited: ", info(p) Inorder(right(p)) end Inorder. Algorithm Postorder(p) // p pointer to the root if p NIL then Postorder(left(p)) Postorder(right(p)) Write "Node visited: ", info(p) end Postorder.

Q 2. Count nodes Write 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 the address of root node as an argument Q 3. Determine Node Write 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 search tree and do the following : a. If the node is a leaf then print it b. If the node is the root then print the entire tree c. If the node is an internal node (non-leaf) node then print its immediate children

Jaypee Institute of Information Technology, NOIDA 39

Data Structures Jan 2012

NOTICE Lab Test-2 schedule is as follows: Syllabus Date Marks Duration : Trees : 30th April to 5th May (Lab A) : 15 : 1 hour

Quiz-3 schedule is as follows: Syllabus Date Marks Duration : Graphs : 7th May to 12th May (Lab B) : 10 : 45 minutes

Jaypee Institute of Information Technology, NOIDA 40

Data Structures Jan 2012

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:30 delete: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 auxiliary data structure to store intermediate nodes for traversing back, use following slides for hints)

Jaypee Institute of Information Technology, NOIDA 41

Data Structures Jan 2012

Lab A: Week 15(1st May to 8th May) Lab Test 2

Jaypee Institute of Information Technology, NOIDA 42

Data Structures Jan 2012

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 and DFS 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. InputNewGraph 2. Addavertextothegraph 3. Addanedgetothegraph 4. DisplayBreadthFirstTraversalofGraph 5. DisplayDepthFirstTraversalofGraph 6. Exit Enteryourchoice: 1 Enterthenumberofverticesofthegraph: 6 Enterthenumberofedgesinthegraph: 5 Entertheedge1: startvertex : 4 endvertex : 6 Entertheedge2: startvertex : 1 endvertex : 4 Entertheedge3: 1 startvertex : endvertex : 5 Entertheedge4: startvertex : 5 endvertex : 3 Entertheedge5: startvertex : 6 endvertex : 2 GraphEnteredCorrectly.....

Jaypee Institute of Information Technology, NOIDA 43

Data Structures Jan 2012

Menu 1. InputNewGraph 2. Addavertextothegraph 3. Addanedgetothegraph 4. DisplayBreadthFirstTraversalofGraph 5. DisplayDepthFirstTraversalofGraph 6. Exit Enteryourchoice: 4 BFStraversalofthegraphis: 145632 .and so on. Make your program as user friendly as possible, handle all the exceptions and possible wrong inputs from user. Q 2. Do the above program with input from a file, where the file stores the following data: a. Number of vertices b. 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 vertices and edges are enclosed in brackets, with first number is starting vertex and second is the ending vertex. Finally * indicates end of file.

Jaypee Institute of Information Technology, NOIDA 44

You might also like