Solution for Data Structure

Similar documents
S.E. Sem. III [CMPN] Data Structures. Primitive Linear Non Linear

Ashish Gupta, Data JUET, Guna

ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees. Introduction to Linked Lists

Frequently asked Data Structures Interview Questions

S.E. Sem. III [INFT] Data Structures & Analysis. Primitive Linear Non Linear

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

Linear Data Structure

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

Lecture 7: Data Structures. EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Module III. Linear Data Structures and their Linked Storage Representation

DC104 DATA STRUCTURE JUNE Q.2 a. If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Data Structure & Algorithms Laboratory Manual (CS 392)

Binary Search Tree 1.0. Generated by Doxygen Mon Jun :12:39

What is recursion. WAP to find sum of n natural numbers using recursion (5)

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra

Write a program that creates in the main function two linked lists of characters and fills them with the following values:

Assignment 6. Q1. Create a database of students using structures, where in each entry of the database will have the following fields:


Linked List. April 2, 2007 Programming and Data Structure 1

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

Model Solution for QP CODE : ( 3 Hours )

Dynamic Data Structures

MTH 307/417/515 Final Exam Solutions

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) WINTER 16 EXAMINATION Model Answer Subject Code:

MODULE V: POINTERS & PREPROCESSORS

Downloaded from : Algorithm: Implementation QUESTION 1 :

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

Computer Systems and Networks

CP2 Revision. theme: dynamic datatypes & data structures

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

An Introduction to Trees

Computer Science Foundation Exam

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

COP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME

CSCE 2014 Final Exam Spring Version A

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING B.E SECOND SEMESTER CS 6202 PROGRAMMING AND DATA STRUCTURES I TWO MARKS UNIT I- 2 MARKS

Bachelor Level/ First Year/ Second Semester/ Science Full Marks: 60 Computer Science and Information Technology (CSc. 154) Pass Marks: 24

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

MAHARASHTRASTATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

#06 More Structures LIFO FIFO. Contents. Queue vs. Stack 3. Stack Operations. Pop. Push. Stack Queue Hash table

Cpt S 122 Data Structures. Data Structures

UNIT-2 Stack & Queue

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

Linked Lists in C and C++

Comp Sci 1MD3 Mid-Term II 2004 Dr. Jacques Carette

Duration: 110 minutes

C Data Structures Stacks. Stack. push Adds a new node to the top of the stack

About this exam review

Stack & Queue on Self-Referencing Structures

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

File: /media/young/d1 (180713)/Work tive_tree_search/search_defs.h Page 1 of 1

(Section : Computer Science)

CS PROGRAMMING & ATA STRUCTURES I. UNIT I Part - A

V.S.B ENGINEERING COLLEGE DEPARTMENT OF INFORMATION TECHNOLOGY I IT-II Semester. Sl.No Subject Name Page No. 1 Programming & Data Structures-I 2

Fall, 2015 Prof. Jungkeun Park

Answers. 1. (A) Attempt any five : 20 Marks

CS 241 Data Organization Binary Trees

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

Archivo: /home/young/work/cordic/binary_tree_search/search_defs.h Página 1 de 1

Stack & Queue on Self-Referencing Structures

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

S.Y. B.Sc. (IT) : Sem. III. Data Structures

Elementary Data Structures: Part 1: Arrays, Lists. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

BBM 201 DATA STRUCTURES

Data Structures and Algorithms Notes

COMP1917 Computing 1 Written Exam Sample Questions

CS32 Discussion Week 3

S.Y. B.Sc. (IT) : Sem. III. Data Structures

Reg. No. : Question Paper Code : 27157

Stacks and Queues. Stack - Abstract Data Type. Stack Applications. Stack - Abstract Data Type - C Interface

Computer Systems and Networks

Language comparison. C has pointers. Java has references. C++ has pointers and references

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

Introduction to Data Structures. Systems Programming

Table of Contents. Chapter 1: Introduction to Data Structures... 1

PROGRAMMAZIONE I A.A. 2017/2018

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

COP 3502 (Computer Science I) Test #3: Data Structures Date: 11/1/2013 VERSION A

Tribhuvan University Institute of Science and Technology Computer Science and Information Technology (CSC. 154) Section A Attempt any Two questions:

Procedural programming with C

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Comp Sci 1MD3 Mid-Term II 2003 answers

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

ALGORITHM 2-1 Solution for Exercise 4

15. Stacks and Queues

Answer all questions. Write your answers only in the space provided. Full marks = 50

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Stacks. CONTENTS 3.1 Introduction 1. Stack as an abstract data type 2. Representation of a Stack as an array 3.2Applications of Stack.

Chapter 9 STACK, QUEUE

/* Polynomial Expressions Using Linked List*/ #include<stdio.h> #include<conio.h> #include <malloc.h> struct node. int num; int coeff;

Transcription:

Solution for Data Structure May 2016 INDEX Q1 a 2-3 b 4 c. 4-6 d 7 Q2- a 8-12 b 12-14 Q3 a 15-18 b 18-22 Q4- a 22-35 B..N.A Q5 a 36-38 b N.A Q6- a 39-42 b 43 1 www.brainheaters.in

Q1) Ans: (a) Define ADT with an example. 2 www.brainheaters.in

3 www.brainheaters.in

(b) What are the advantages of using linked list over arrays? Ans: (c) Describe Expression tree with an example. Ans: 4 www.brainheaters.in

The first three symbol are operands, so we create one-node tree push pointers to them onto a stack. Next, 4 * is read,so two pointers to tree are popped,a new tree is formed and a pointer to it is pushed onto the stack. Next, a + is read so two pointers to tree are popped a new tree is formed and a pointer to it is pushed onto the stack 5 www.brainheaters.in

6 www.brainheaters.in

(d) Write a program in C to implement insertion sort. Ans: 7 www.brainheaters.in

Q2) Ans: (a) Discuss file I/O in C language with different library function. 8 www.brainheaters.in

9 www.brainheaters.in

Compare Text file and Binsry file Basic file operation 10 www.brainheaters.in

Sample program for creating a text file using putc() : #include<stdio.h> void main() File *fp;char file[20]; char x; printf( \n Enter file name: ); 11 www.brainheaters.in

(b) Explain recursion as an application of stack with example. Ans: Any recursive function can be converted to non-recursive function through use of stack. A recursive call is similar to a call to another function. 12 www.brainheaters.in

Tail Recursion: 13 www.brainheaters.in

14 www.brainheaters.in

Q3) Ans: (a) Write a menu driven program in C to implement Queue ADT.The program should perform the following operation. (i) Inserting an element in the queue. (ii) Deleting an element from the queue (iii) Displaying the queue. (iv) Exiting the program. 15 www.brainheaters.in

16 www.brainheaters.in

return(0); 17 www.brainheaters.in

Output Enter 5 elements : 5 4 3 2 1 5 4 3 2 1 3 2 1 Ans: (b) Write a function to implement indexed sequential search.explain with an example. 18 www.brainheaters.in

19 www.brainheaters.in

20 www.brainheaters.in

21 www.brainheaters.in

Q4) (a) Write a C program to implement a Doubly linked list which perform the following operations: (i) Inserting element in the beginning. (ii) Inserting element in the end (iii) Inserting after an element (iv) Deleting a particular element. (v) Displaying the list. Ans: C Program to Implement a Doubly Linked List & provide Insertion, Deletion & Display Operations */ #include <stdio.h> #include <stdlib.h> struct node struct node *prev; int n; struct node *next; *h,*temp,*temp1,*temp2,*temp4; 22 www.brainheaters.in

void insert1(); void insert2(); void insert3(); void traversebeg(); void traverseend(int); void sort(); void search(); void update(); void delete(); int count = 0; void main() int ch; h = NULL; temp = temp1 = NULL; printf("\n 1 - Insert at beginning"); printf("\n 2 - Insert at end"); printf("\n 3 - Insert at position i"); printf("\n 4 - Delete at i"); printf("\n 5 - Display from beginning"); printf("\n 6 - Display from end"); printf("\n 7 - Search for element"); printf("\n 8 - Sort the list"); 23 www.brainheaters.in

printf("\n 9 - Update an element"); printf("\n 10 - Exit"); while (1) printf("\n Enter choice : "); scanf("%d", &ch); switch (ch) case 1: insert1(); break; case 2: insert2(); break; case 3: insert3(); break; case 4: delete(); break; case 5: traversebeg(); break; case 6: temp2 = h; if (temp2 == NULL) 24 www.brainheaters.in

printf("\n Error : List empty to display "); else printf("\n Reverse order of linked list is : "); traverseend(temp2->n); break; case 7: search(); break; case 8: sort(); break; case 9: update(); break; case 10: exit(0); default: printf("\n Wrong choice menu"); /* TO create an empty node */ void create() 25 www.brainheaters.in

int data; temp =(struct node *)malloc(1*sizeof(struct node)); temp->prev = NULL; temp->next = NULL; printf("\n Enter value to node : "); scanf("%d", &data); temp->n = data; count++; /* TO insert at beginning */ void insert1() if (h == NULL) create(); h = temp; temp1 = h; else create(); temp->next = h; h->prev = temp; h = temp; 26 www.brainheaters.in

/* To insert at end */ void insert2() if (h == NULL) create(); h = temp; temp1 = h; else create(); temp1->next = temp; temp->prev = temp1; temp1 = temp; /* To insert at any position */ void insert3() int pos, i = 2; printf("\n Enter position to be inserted : "); scanf("%d", &pos); temp2 = h; 27 www.brainheaters.in

if ((pos < 1) (pos >= count + 1)) printf("\n Position out of range to insert"); return; if ((h == NULL) && (pos!= 1)) printf("\n Empty list cannot insert other than 1st position"); return; if ((h == NULL) && (pos == 1)) create(); h = temp; temp1 = h; return; else while (i < pos) temp2 = temp2->next; i++; create(); temp->prev = temp2; 28 www.brainheaters.in

temp->next = temp2->next; temp2->next->prev = temp; temp2->next = temp; /* To delete an element */ void delete() int i = 1, pos; printf("\n Enter position to be deleted : "); scanf("%d", &pos); temp2 = h; if ((pos < 1) (pos >= count + 1)) printf("\n Error : Position out of range to delete"); return; if (h == NULL) printf("\n Error : Empty list no elements to delete"); return; else 29 www.brainheaters.in

while (i < pos) temp2 = temp2->next; i++; if (i == 1) if (temp2->next == NULL) printf("node deleted from list"); free(temp2); temp2 = h = NULL; return; if (temp2->next == NULL) temp2->prev->next = NULL; free(temp2); printf("node deleted from list"); return; temp2->next->prev = temp2->prev; if (i!= 1) temp2->prev->next = temp2->next; /* Might not need this statement if i == 1 check */ if (i == 1) h = temp2->next; 30 www.brainheaters.in

printf("\n Node deleted"); free(temp2); count--; /* Traverse from beginning */ void traversebeg() temp2 = h; if (temp2 == NULL) printf("list empty to display \n"); return; printf("\n Linked list elements from begining : "); while (temp2->next!= NULL) printf(" %d ", temp2->n); temp2 = temp2->next; printf(" %d ", temp2->n); /* To traverse from end recursively */ 31 www.brainheaters.in

void traverseend(int i) if (temp2!= NULL) i = temp2->n; temp2 = temp2->next; traverseend(i); printf(" %d ", i); /* To search for an element in the list */ void search() int data, count = 0; temp2 = h; if (temp2 == NULL) printf("\n Error : List empty to search for data"); return; printf("\n Enter value to search : "); scanf("%d", &data); while (temp2!= NULL) if (temp2->n == data) 32 www.brainheaters.in

printf("\n Data found in %d position",count + 1); return; else temp2 = temp2->next; count++; printf("\n Error : %d not found in list", data); /* To update a node value in the list */ void update() int data, data1; printf("\n Enter node data to be updated : "); scanf("%d", &data); printf("\n Enter new data : "); scanf("%d", &data1); temp2 = h; if (temp2 == NULL) printf("\n Error : List empty no node to update"); return; while (temp2!= NULL) 33 www.brainheaters.in

if (temp2->n == data) temp2->n = data1; traversebeg(); return; else temp2 = temp2->next; printf("\n Error : %d not found in list to update", data); /* To sort the linked list */ void sort() int i, j, x; temp2 = h; temp4 = h; if (temp2 == NULL) printf("\n List empty to sort"); return; 34 www.brainheaters.in

for (temp2 = h; temp2!= NULL; temp2 = temp2->next) for (temp4 = temp2->next; temp4!= NULL; temp4 = temp4->next) if (temp2->n > temp4->n) x = temp2->n; temp2->n = temp4->n; temp4->n = x; traversebeg(); Ans: N.A (b) Apply Huffman coding for the word MALYALAM give the Huffman code for each symbol. 35 www.brainheaters.in

Q5 Ans: (a) Explain any one application linked list with an example. 36 www.brainheaters.in

return(head); 37 www.brainheaters.in

Ans: N.A (b) Write a program in C to delete a node from a binary search tree.the program should consider all the possible cases. 38 www.brainheaters.in

Q6) Ans: (a) Write a program in C to implement the BFS traversal of a graph Explain the code with an example. Void BFS(int v) 39 www.brainheaters.in

40 www.brainheaters.in

41 www.brainheaters.in

42 www.brainheaters.in

(b) Hash the following in a table of size 11.Use any two collision resolution techniques 23,55,10,71,67,32,100,18,10,90,44 Ans: note: similar 43 www.brainheaters.in