Sie sind auf Seite 1von 23

HINDUSTHAN INSTITUTE OF TECHNOLOGY COIMBATORE CS6202 PROGRAMMING AND DATA STRUCTURES I TWO MARKS UNIT I C PROGRAMMING FUNDAMENTALS- A REVIEW

1. Give any four features of C language. a. C is a general purpose, structured programming language. b. C is powerful, efficient, compact and flexible. c. C is highly portable. d. C is a robust language whose rich set of built in functions and operators can be used to write any complex programs. 2. Define comments? Comments are very helpful in identifying the program features and underlaying logic of the program. he lines begins with !" and ending with "! are #nown as comment lines. hese are not executable, the compiler is ignored any thing in between !" and "! Ex: !! $ample comment line !" program for adding two numbers"! 3. %hat is meant by C to#ens? he to#ens are usually referred as individual text and punctuation in a passage of text. he C language program can contain the individual units call the C to#ens. Ex: operator, string, constant, identifier 4. Define identifiers. &dentifiers are names given to various program elements, such as variables, functions and arrays etc. Ex: basic'pay, num(, sum 5. Define data types. Data type is the type of the data, that are going to access within the program. C supports different data types, each data type may have predefined memory re)uired and storage representation. 6. %hat are the different data types available in C? here are four basic data types available in C. i* int ii* float iii* char iv* double

. %hat are #eywords? +eywords are certain reserved words, that have standard and pre, defined meaning in C. hese #eywords can be used only for their intended purpose. Ex: int, float, static, extern, const !. %hy do we need to use comments in programs? a. o increase readability and understandability. b. o help users in debugging and testing. ". %rite down the syntax for Declaring a variable S#$%&x: Datatype variablenameEx: int a1'. %rite the use of scanf . printf with example? he scanf statement is used to get the input from the standard input device.. he printf statement is used to print the result on the standard output device. Ex&()*+: scanf/01d2, .a*printf/03esult42, a*11. %hat is meant by variables? 5ow and where are the variables declared? 6 variable is an identifier that is used to represent some specified type of information within a designed portion of the program. 7ariables are declared along with its data type at the beginning of the program. 8x4 data'type variable 'name%here data type represents the type of data, variable'name is the valid C identifier. 12. Define constants? 5ow they differ from variables?. he items whose values cannot be changed during the execution of rogram are called constants. Ex4 (9, (9.:, a, 0priya2 he value stored in the variable can be changed during the execution of the program. 13. Define $tring constants. 6 string constant is a se)uence of characters enclosed in double )uotes, the character s may be letters, numbers, special characters and blan# spaces etc. at the end of string ;9 is automatically placed. 8x4 0sangee2, 0(:<2, 22, 0=1>2 14. %hat is an operator and operand? 6n operator is a symbol that specifies an operation to be performed on operands.

Ex: ", ?, ,, ! are called arithmetic operators. he data items that operators act upon are called operands. Ex: a?b- &n this statement a and b are called operands. 15. %hat are the types of operator? 6rithmetic operators 3elational operators @ogical operators 6ssignment operators &ncrement and decrement operators Conditional operator/ ernary operator* Aitwise operators $pecial operators. 16. %hat is ernary operator? ernary operator is a conditional operator with symbols ? and 4. $yntax4 variable B exp( ? exp: 4 exp< &f the exp( is true, variable ta#es the value of exp:. if the exp( is false, variable ta#es the value of exp<. 1 . Define expression. 6n expression represents data item such as variables, constants and are interconnected with operators as per the syntax of the language. 6n expression is evaluated using assignment operator. Ex : c B a?b 1!. %hat is the output of the following? main/* C int xB(99, yB:99Drintf/01d2,/xEy*?x4y*F Gutput4 :99 1". %hat are the types of &!G statements available in C? here are two types of &!G statements available in C Hormatted &!G statements 8x4 scanf/ *, printf/ * Informatted &!G statements. 8x4 getchar/ *, putchar/ *, gets/ *, puts/ * 2'. Define single characters input getchar/* function. he getchar/* function is written in standard &!G library. &t reads a single character from a standard input device. his function do not re)uire any arguments, through a pair of empty parentheses, must follow the statement getchar/*.

21. Define single characters output putchar/* function. he putchar/* function is used to display one character at a time on the standard output device. his function does the reverse operation of the single character input function. 22. %hat is the difference between scanf/* and gets/* function? &n scanf/* when there is a blan# was typed, the scanf/* assumes that it is an end. Gets/* assumes the enter #ey as end. hat is gets/* gets a newline /;n* terminated string of characters from the #eyboard and replaces the ;n with ;9. 23. Define simple if,statement. he if statement is a decision ma#ing statement. &t is used to control the flow of execution of the statements and also used to test logically whether the condition is true or false. 24. %hat is mean by looping? he loop is defined as the bloc# of statements which are repeatedly executed for certain number of times. 25. Define if,else statement. &t is basically two way decision ma#ing statement and always used in conJunction with condition. &t is used to control the flow of execution and also used to carry out the logical test and then pic#up one of the two possible actions depending on the logical test. 26. Define while loop. he while loop is an entry controlled loop statement, means the condition is evaluated first and it is true, then the body of the loop is executed. 6fter executing the body of the loop, the condition is once again evaluated and if it is true, the body is executed once again, the process of repeated execution of the body of the loop continues until the condition finally becomes false and the control is transferred out of the loop. 2 . Define do,while loop. &t is also repetitive control structure and executes the body of the loop once irrespective of the condition, then it chec#s the condition and continues the execution until the condition becomes false. 2!. Define for loop. he for loop is another repetitive control structure, and is used to execute set of instructions repeatedly until the condition becomes false.

2". %hat are the differences between if and while statement? i* &t is statement ii* &f the condition is true, it ii* 8xecutes the statements executes some statements within the while bloc# if the condition is true. iii* &f the condition is false iii* &f the condition is false, the then it stops the execution of control is transferred to the next the statement. statement of the loop. 3'. %hat are the differences between while and doKwhile loop? W./*+ i* his is the top tested loop. ii* he condition is first tested, if the condition is true then the bloc# is executed until the condition becomes false. iii* @oop will not be executed if the condition is false D01-./*+ i* his is the bottom tested loop. ii* &t executes the body once, after it chec#s the condition, if it is true the body is executed until the condition becomes false. iii* @oop is executed atleast once even though the condition is false. statement of the loop. I, -./*+ a conditional i* &t is a loop control statement

31. %hat is meant by switch statement? he switch statement is used to pic#up or execute a particular group of statements from several available group of statements. &t allow us to ma#e a decision from the number of choices. 32. Give the comparison between switch case statement and nested if statement. S-/%2.34 2&5+ i* he switch/* can test only constant values. ii* Lo two case statements have identical constants in the same switch. iii* Character constants are automatically converted to integers. iv* &n switch/* case statement, nested if can be used. N+5%+6 /,5 i* he if can evaluate relational or logical expression. ii* $ame conditions may be repeated for number of times. iii* Character constants are automatically converted to integers. iv* &n nested if statements, switch/* case can be used.

33. %hat is mean by brea# statement? he brea# statement is used to terminate the loop. %hen the #eyword brea# is used inside any C loop, control automatically transferred to the first statement after the loop. 6 brea# is usually associated with an if statement. 34. %hat is mean by continue statement? &n some situations, we want to ta#e the control to the beginning of the loop, bypassing the statements inside the loop which have not yet been executed, for this purpose the continue is used. %hen the statement continue is encountered inside any C loop control automatically passes to the beginning of the loop. 35. Define goto statement. he goto statement to transfer control unconditionally from one place to another place in the program. 36. %hat is type casting? ype casting is the process of converting the value of an expression to a particular datat ype. $yntax 4 variable'name B /type name*expression8x4 c B / float * x ! y 3 . %hat is global variable? he variables which are declared outside of all the functions is called global variables. hese variables are used any where in the program. 8x4 int aBMmain/* 7 )rintf/01d2,a*display/*F display/* C aBa?MF 3!. %hat is local variable? he variables which are declared with in a function is called as local variable. hese variables cannot be accessed outside the function. 8x4 main/* 7 int aBMdisplay/*F display/* C int cF

%here a and c are local variables. 7ariable a cannot be used in the function display/*. 3". &f aBM9, bB(9, and cB:9, evaluate the following complex expression. c ? B /aE9 .. a N B (9 *? ??a 4 a ! b 6nswer B :M 4'. %hat is typedef? &t allows us to create an identifier that represents an existing datype. he identifier is later used to declare variables. 8x 4 typedef int mar#mar# maths, phy,che41. @ist out the various bac#slash character constants. ;n , Lewline ;t , ab ;b , Aac#space ;f , Horm feed ;r , Carriage return ;a , 6lert ; , $ingle )uote ;2 , Double )uote ;; , Aac#slash 42. %hat is meant by operator precedence? Gperator precedence describes the order in which C evaluates different operators in a complex expression. 43. %hat is associativity? %hat is the associativity of unary operators? 6ssociativity can be either left to right or right to left. 6ssociativity is used only when the operators are of same precedence. he associativity of unary operator is right to left. 44. %hat do u meant by left to right associativity and right to left associativity? @eft to right associativity evaluates the expression by starting on the left and moving to the right. 3ight to left associativity evaluates the expression by starting on the right and moving to the left. 45. %hat is the purpose of control string in a scanf function? &t indicates that what type of data is to be read. &t is always preceded with 1 sign

46. %hat is mean by user define functions? he function defined by the users according to their re)uirements are called user,defined functions. he users can modify the function according to their re)uirements.

4 . Give the advantages of user,defined functions. he length of the source program can be reduced by dividing it into the smaller functions. Ay using functions it is very easy to locate and debug an error. he user,defined function can be used in many other source programs whenever necessary. Hunctions avoid coding of repeated programming of the similar instructions. 4!. %hat is mean by actual and formal parameters? /i* 6ctual parameters4 hese are the parameters transferred from the calling program / main program* to the called program /function*. /ii* Hormal parameters4 hese are the parameters, transferred into the calling function /main program* from the called program /function*. 4". Define prototype. &n C while defining user,defined function it is a must to declare its prototype. 6 prototype statement #eep the compiler to chec# the return type and arguments types of the function. 5'. Define call by value. his method copies the values of actual parameters into the formal parameters of the function. 5ere, the changes of the formal parameters cannot affect the actual parameters, because formal arguments are photocopy of actual arguments. 51. Define call by reference. Call by reference is another way of passing parameters o the function. 5ere, the address of arguments are copied into the parameters inside the function, the address is used to access the actual arguments used in the call. 5ence changes made in the arguments are permanent. 52. %hat is meant by recursion? 3ecursion is the process of calling the same function itself again and again until some condition is satisfied. his process is used for repetitive computation in which each action is satisfied in terms of a previous result. 53. %rite the function of toascii/x* functions. %here the argument is a character or integer value. returns the 6$C&& value of the argument. his function

54. Define array. 6n array is a collection of similar data items, that are stored under a common name. a value in an array is identified by index or subscript enclosed in s)uare brac#ets with array name.

55. %rite the classification of arrays. 6rrays can be classified into Gne dimensional arrays. wo dimensional arrays. Oulti,dimensional arrays. 56. Define one,dimensional arrays. he collection of data items can be stored under a one variable name using only one subscript, such a variable is called the one,dimensional array. 5 . %rite the features of arrays. 6n array is a derived data type. &t is used to represent a collection of elements of the same data type. he elements can be accessed with base address /index* and the subscripts define the position of the element. &n array the elements are stored in continuous memory location. he starting memory location is represented by the array name and it is #nown as the base address of the array. &t is easier to refer the array elements by simply incrementing the value of the subscript. 5!. %hat is meant by two,dimensional array? wo dimensional arrays are used in situation where a table of values need to be stored in an array. hese can be defined in the same fashion as in one dimensional arrays, except a separate pair of s)uare brac#ets are re)uired for each subscript. 5". Define multi,dimensional array. @i#e one and two dimensional arrays. C language allows multidimensional arrays. he dimension with three or more called multi dimensional arrays. 6'. Define pointer. 6 pointer is a variable- it may contain the memory address of variable. Dointer can have any number that is legal for other variable. &t is declared in the same manner li#e other variable. &t is always denoted by " operator. 61. Lame the operators /., "* ., 6ddress operators " , Deferencing operator.

62. %hat are the advantages of using pointers? 6 pointer provides the following advantages. /iii* /iv* /v* /vi* Dointers are more compact and efficient code. Dointers can be used to achieve clarity and simplicity. Dointers are used to pass information band and forth between function and its reference and its reference point. Dointers provide a way to return multiple data items from a function using its function arguments.

63. %hat is indirection operator? Gnce a pointer has been assigned the address of a variable, the )uestion remains as to how to access the value of the variable using the pointer. his is done by using another unary operator ", usually #nown as the indirection operator. 64. %hat is the difference between the indirection and address of operators? he indirection operators /"* returns the value of the address stored in a pointer. he address of operator /.* returns the memory address of the variable. 65. %hat is the difference between an array and a pointer? S.N0 (. :. <. P. A88&# &t belongs to static memory allocation Oemory is allocated during compile time. Oemory allocated is fixed Oemory is wasted. 90/$%+8 &t belongs to dynamic memory allocation Oemory is allocated during run time. Oemory allocated can be varied. Oemory is conserved.

66. %hat does null pointer mean? 6 pointer is said to be a null pointer when its right side value is 9. 6 null pointer can never point to a valid date. Hor chec#ing a pointer, if it is assigned to 9, then it is a null pointer and is not valid. 6 . %hat is a character pointer? 6 character pointer can also be used to access character arrays and string constants in the same way as accessing numeric arrays using their respective pointer variable. char "pointer'character6!. Define array of pointers. 6rray notation is a form of relative addressing. he compiler treats the subscripts as a relative offset from the beginning of the array. %hen a

pointer variable is set to the start of an array and is incremented to find the next element, absolute addressing is being implemented. 6". %hat is pointer to pointer? 6 variable that is a pointer to a pointer must be declared using additional indirection operator symbols in front of the name. 8g. int ""g(his declaration tells compiler that g( is a pointer of int type. he pointer g( is not an integer, but rather a pointer to an integer pointer. '. %hat is meant by call by value and call by reference? he process of passing the actual value of variables is #nown as 0call by value2. he process of calling a function using pointers to pass the addresses of variables is #nown as 0call by reference2. 1. %rite any three comparisons between call by value and call by reference. S.N0 C&** :# ;&*<+ C&** :# R+,+8+$2+ (. Different memory locations are $ame memory location occupied by formal and actual occupied by formal and actual arguments. arguments and there is a saving of memory location. :. 6ny alteration in the value of 6ny alteration in the value of the arguments passed is local to the argument passed is accepted the function and is not accepted in the calling program. in the calling program. <. he usual method to call a he address of the variable is function in which only the value passed as an argument. of the variable is passed as an argument. 2. %rite the output of the following C program main/ * C int vB<int "pvpvB.vprintf/0;n "pvB1d vB1d2, "pv,v*"pvBMprintf/0;n "pvB1d vB1d2, "pv,v*F O<%)<%: he output of the program is "pvB< vB< "pvBM vBM 3.Distinguish between /"m*/M* and "m/M*. /"m*/M* B m is a pointer to function which returns an integer. "m/M* B m is a function returning pointer to an integer.

UNIT=II

C PROGRAMMING ADVANCED FEATURES

(. Define structure. $tructure is the mechanism for pac#ing data of different types. 6 structure is a convenient tool for handling a group of logically related data items. 8xample4 &t can be used to represent a set of attributes, such as student, name, roll number and mar#s. :. Give the syntax for structure. struct tag'name C datatype element(datatype element: KKK datatype elementnF8g4 struct boo#'ban# C char titleQ:9Rchar authorQ(MRint pagesfloat priceFtitle, author, pages, price ,E four data fields. boo#'ban# ,E name of the structure or structure tag. <. %hat are the elements to be included while declaring a structure variable? /vii* he #eyword struct /viii* he structure tag name /ix* @ist of variable names separated by commas. /x* 6 terminating semicolon. P. Give example for initialiSation of structure. struct stud C int rnochar nameQ(MRFsBC(9(,2priya2F-

M. Define nested structure. $tructures within a structure means nesting of structures. $yntax4 struct emp C int enochar enameQ(MRstruct C int daint hraint maFallowancesFempT. Define union. Inions are derived data types they are declared li#e structure. he only difference is in terms of storage. &n structure each member has its own storage, in union all members use the same location. U. Give syntax for union. union union,name C union member(union member:KKKKKK.. union memberLFV. Differentiate between array and structure. S.N0 A88&# 6n 6rray is a collection of (. similar data items. 6n 6rray is derived data :. type. &t behaves li#e a built in <. data types. 6n 6rray can be increased P. or decreased. S%8<2%<8+ 6 $tructure is a collection of dissimilar data items. &t is a user defined data type. &t must be declared and defined. 6 structure elements can be added if necessary.

W. Give any three comparison structure and union. $tructure4 8very member has its own memory space. +eyword struct is used. 6ny member can be accessed at any time without the loss of data. Inion4 6ll the members use the same memory space to store the values. +eyword union is used. Different interpretations for the same memory location is possible.

UNIT III

LINEAR DATA STRUCTURES = LIST

1. D+,/$+ D&%& S%8<2%<8+5 Data $tructures is defined as the way of organiSing all data items that consider not only the elements stored but also stores the relationship between the elements. 2. D+,/$+ L/$>+6 L/5%5 @in#ed list consists of a series of structures, which are not necessarily adJacent in memory. 8ach structure contains the element and a pointer to a structure containing its successor. %e call this theLext Dointer. he last cellsLext pointer points to LI@@.

3. S%&%+ %.+ 6/,,+8+$% %#)+5 0, */$>+6 */5%5 he different types of lin#ed list include singly lin#ed list, doubly lin#ed list and circular lin#ed list. 4. L/5% %.+ :&5/2 0)+8&%/0$5 2&88/+6 0<% /$ & */$>+6 */5% he basic operations carried out in a lin#ed list include4 X Creation of a list X &nsertion of a node X Deletion of a node X Oodification of a node X raversal of the list 5. L/5% 0<% %.+ &6?&$%&@+5 0, <5/$@ & */$>+6 */5% X &t is not necessary to specify the number of elements in a lin#ed list during its declaration X @in#ed list can grow and shrin# in siSe depending upon the insertion and deletion that occurs in the list X &nsertions and deletions at any place in a list can be handled easily and efficiently X 6 lin#ed list does not waste any memory space 6. L/5% 0<% %.+ 6/5&6?&$%&@+5 0, <5/$@ & */$>+6 */5% X $earching a particular element in a list is difficult and time consuming X 6 lin#ed list will use more storage space than an array to store the same number of elements . L/5% 0<% %.+ &))*/2&%/0$5 0, & */$>+6 */5% $ome of the important applications of lin#ed lists are manipulation of polynomials, sparse matrices, stac#s and )ueues.

!. S%&%+ %.+ 6/,,+8+$2+ :+%-++$ &88&#5 &$6 */$>+6 */5%5 A88&#5 L/$>+6 L/5%5 $iSe of an array is fixed $iSe of a list is variable &t is necessary to specify the &t is not necessary to specify the number of elements during number of elements during declaration. declaration &nsertions and deletions are &nsertions and deletions are carried somewhat difficult out easily &t occupies less memory than a &t occupies more memory lin#ed list for the same number of elements ". W.# -+ $++6 2<8508 /()*+(+$%&%/0$ 0, */$>+6 */5%5A Oany languages such as A6$&C and HG3 36L do not support pointers. &f lin#ed lists are re)uired and pointers are not available, then an alternative implementation must be used #nown as cursor implementation. 1'. D+,/$+ &$ A:5%8&2% D&%& T#)+ 3ADT4 6n abstract data type is a set of operations. 6D s are mathematical abstractionsnowhere in an 6D s definition is there any mention of how the set of operations is implemented. GbJects such as lists, sets and graphs, along with their operations can be viewed as abstract data types. 11. W.&% &8+ %.+ &6?&$%&@+5 0, (06<*&8/%#A X &t is much easier to debug small routines than large routines X &t is easier for several people to wor# on a modular program simultaneously X 6 well,written modular program places certain dependencies in only one routine, ma#ing changes easier 12. W.&% &8+ %.+ 0:B+2%/?+5 0, 5%<6#/$@ 6&%& 5%8<2%<8+5A X o identify and create useful mathematical entities and operations to determine what classes of problems can be solved using these entities and operations X o determine the representation of these abstract entities and to implement the abstract operations on these concrete representation

UNIT IV

LINEAR DATA STRUCTURES STACKS, QUEUES

(. D+,/$+ & 5%&2> $tac# is an ordered collection of elements in which insertions and deletions are restricted to one end. he end from which elements are added and!or removed is referred to as top of the stac#. $tac#s are also referred as piles, push,down lists and last,in,first, out /@&HG* lists. 2. L/5% 0<% %.+ :&5/2 0)+8&%/0$5 %.&% 2&$ :+ )+8,08(+6 0$ & 5%&2> he basic operations that can be performed on a stac# are X Dush operation X Dop operation X Dee# operation X 8mpty chec# X Hully occupied chec# 3. S%&%+ %.+ 6/,,+8+$% -&#5 0, 8+)8+5+$%/$@ +x)8+55/0$5 he different ways of representing expressions are X &nfix Lotation X Drefix Lotation X Dostfix Lotation 4. S%&%+ %.+ 8<*+5 %0 :+ ,0**0-+6 6<8/$@ /$,/x %0 )05%,/x 20$?+85/0$5 X Hully parenthesiSe the expression starting from left to right. During parenthesiSing, the operators having higher precedence are first parenthesiSed X Oove the operators one by one to their right, such that each operator replaces their corresponding right parenthesis X he part of the expression, which has been converted into postfix is to be treated as single operand 5. S%&%+ %.+ 6/,,+8+$2+ :+%-++$ 5%&2>5 &$6 */$>+6 */5%5 he difference between stac#s and lin#ed lists is that insertions and deletions may occur anywhere in a lin#ed list, but only at the top of the stac# 6. M+$%/0$ %.+ &6?&$%&@+5 0, 8+)8+5+$%/$@ 5%&2>5 <5/$@ */$>+6 */5%5 %.&$ &88&#5 X &t is not necessary to specify the number of elements to be stored in a stac# during its declaration, since memory is allocated dynamically at run time when an element is added to the stac# X &nsertions and deletions can be handled easily and efficiently X @in#ed list representation of stac#s can grow and shrin# in siSe without wasting memory space, depending upon the insertion and deletion that occurs in the list X Oultiple stac#s can be represented efficiently using a chain for each stac# . D+,/$+ & C<+<+ Yueue is an ordered collection of elements in which insertions are restricted to one end called the rear end and deletions are restricted to other end called the front end. Yueues are also referred as Hirst,&n,Hirst,Gut /H&HG* @ists.

!. D+,/$+ & )8/08/%# C<+<+ Driority )ueue is a collection of elements, each containing a #ey referred as the priority for that element. 8lements can be inserted in any order /i.e., of alternating priority*, but are arranged in order of their priority value in the )ueue. he elements are deleted from the )ueue in the order of their priority /i.e., the elements with the highest priority is deleted first*. he elements with the same priority are given e)ual importance and processed accordingly. ". S%&%+ %.+ 6/,,+8+$2+ :+%-++$ C<+<+5 &$6 */$>+6 */5%5 he difference between )ueues and lin#ed lists is that insertions and deletions may occur anywhere in the lin#ed list, but in )ueues insertions can be made only in the rear end and deletions can be made only in the front end. 1'. D+,/$+ & D+C<+ De)ue /Double,8nded Yueue* is another form of a )ueue in which insertions and deletions are made at both the front and rear ends of the )ueue. here are two variations of a de)ue, namely, input restricted de)ue and output restricted de)ue. he input restricted de)ue allows insertion at one end /it can be either front or rear* only. he output restricted de)ue allows deletion at one end /it can be either front or rear* only. 11. W.&% &8+ %.+ %#)+5 0, C<+<+5A X @inear Yueues Z he )ueue has two ends, the front end and the rear end. he rear end is where we insert elements and front end is where we delete elements. %e can traverse in a linear )ueue in only one direction ie* from front to rear. X Circular Yueues Z 6nother form of linear )ueue in which the last position is connected to the first position of the list. he circular )ueue is similar to linear )ueue has two ends, the front end and the rear end. he rear end is where we insert elements and front end is where we delete elements. %e can traverse in a circular )ueue in only one direction ie* from front to rear. X Double,8nded,Yueue Z 6nother form of )ueue in which insertions and deletions are made at both the front and rear ends of the )ueue. 12. L/5% %.+ &))*/2&%/0$5 0, 5%&2>5 X owers of 5anoi X 3eversing a string X Aalanced parenthesis X 3ecursion using stac# X 8valuation of arithmetic expressions 13. L/5% %.+ &))*/2&%/0$5 0, C<+<+5 X [obs submitted to printer X 3eal life line X Calls to large companies X 6ccess to limited resources in Iniversities X 6ccessing files from file server

UNIT V

SORTING, SEARCHING AND HASH TECHNIQUES

1. D+,/$+ H&5./$@. 5ashing is the transformation of string of characters into a usually shorter fixed length value or #ey that represents the original string. 5ashing is used to index and retrieve items in a database because it is faster to find the item using the short hashed #ey than to find it using the original value. 2. W.&% 60 #0< (+&$ :# .&5. %&:*+A he hash table data structure is merely an array of some fixed siSe, containing the #eys. 6 #ey is a string with an associated value. 8ach #ey is mapped into some number in the range 9 to tablesiSe,( and placed in the appropriate cell. 3. W.&% 60 #0< (+&$ :# .&5. ,<$2%/0$A 6 hash function is a #ey to address transformation which acts upon a given #ey to compute the relative position of the #ey in an array. he choice of hash function should be simple and it must distribute the data evenly. 6 simple hash function is hash'#eyB#ey mod tablesiSe. 4. W8/%+ %.+ /()08%&$2+ 0, .&5./$@. X Oaps #ey with the corresponding value using hash function. X 5ash tables support the efficient addition of new entries and the time spent on searching for the re)uired data is independent of the number of items stored. 5. W.&% 60 #0< (+&$ :# 20**/5/0$ /$ .&5./$@A %hen an element is inserted, it hashes to the same value as an already inserted element, and then it produces collision. 6. W.&% &8+ %.+ 20**/5/0$ 8+50*<%/0$ (+%.065A X $eparate chaining or 8xternal hashing X Gpen addressing or Closed hashing . W.&% 60 #0< (+&$ :# 5+)&8&%+ 2.&/$/$@A $eparate chaining is a collision resolution techni)ue to #eep the list of all elements that hash to the same value. his is called separate chaining because each hash table element is a separate chain /lin#ed list*. 8ach lin#ed list contains all the elements whose #eys hash to the same index. !. W8/%+ %.+ &6?&$%&@+ 0, 5+)&8&%+ 2.&/$/$@. X Oore number of elements can be inserted as it uses lin#ed lists. ". W8/%+ %.+ 6/5&6?&$%&@+5 0, 5+)&8&%+ 2.&/$/$@. X he elements are evenly distributed. $ome elements may have more elements and some may not have anything. X &t re)uires pointers. his leads to slow the algorithm down a bit because of the time re)uired to allocate new cells, and also essentially re)uires the implementation of a second data structure. 1'. W.&% 60 #0< (+&$ :# 0)+$ &668+55/$@A Gpen addressing is a collision resolving strategy in which, if collision occurs alternative cells are tried until an empty cell is found. he cells h9/x*, h(/x*, h:/x*,K. are

tried in succession, where hi/x*B/5ash/x*?H/i**mod ablesiSe with H/9*B9. he function H is the collision resolution strategy. 11. W.&% &8+ %.+ %#)+5 0, 20**/5/0$ 8+50*<%/0$ 5%8&%+@/+5 /$ 0)+$ &668+55/$@A X @inear probing X Yuadratic probing X Double hashing 12. W.&% 60 #0< (+&$ :# 980:/$@A Drobing is the process of getting next available hash table array cell. 13. W.&% 60 #0< (+&$ :# */$+&8 )80:/$@A @inear probing is an open addressing collision resolution strategy in which H is a linear function of i, H/i*Bi. his amounts to trying se)uentially in search of an empty cell. &f the table is big enough, a free cell can always be found, but the time to do so can get )uite large. 14. W.&% 60 #0< (+&$ :# )8/(&8# 2*<5%+8/$@A &n linear probing collision resolution strategy, even if the table is relatively empty, bloc#s of occupied cells start forming. his effect is #nown as primary clustering means that any #ey hashes into the cluster will re)uire several attempts to resolve the collision and then it will add to the cluster. 15. W.&% 60 #0< (+&$ :# C<&68&%/2 )80:/$@A Yuadratic probing is an open addressing collision resolution strategy in which H/i*Bi:. here is no guarantee of finding an empty cell once the table gets half full if the table siSe is not prime. his is because at most half of the table can be used as alternative locations to resolve collisions. 16. W.&% 60 #0< (+&$ :# 5+20$6&8# 2*<5%+8/$@A 6lthough )uadratic probing eliminates primary clustering, elements that hash to the same position will probe the same alternative cells. his is #nown as secondary clustering. 1 . W.&% 60 #0< (+&$ :# 60<:*+ .&5./$@A Double hashing is an open addressing collision resolution strategy in which H/i*Bi.hash:/\*. his formula says that we apply a second hash function to \ and probe at a distance hash:/\*, :hash:/\*,K.,and so on. 6 function such as hash:/\*B3, /\mod3*, with 3 a prime smaller than ablesiSe. 1!. W.&% 60 #0< (+&$ :# 8+.&5./$@A &f the table gets too full, the running time for the operations will start ta#ing too long and inserts might fail for open addressing with )uadratic resolution. 6 solution to this is to build another table that is about twice as big with the associated new hash function and scan down the entire original hash table, computing the new hash value for each element and inserting it in the new table. his entire operation is called rehashing. 1". W.&% /5 %.+ $++6 ,08 +x%+$6/:*+ .&5./$@A &f either open addressing hashing or separate chaining hashing is used, the maJor problem is that collisions could cause several bloc#s to be examined during a Hind, even

for a well,distributed hash table. 8xtendible hashing allows a find to be performed in two dis# accesses. &nsertions also re)uire few dis# accesses. 2'. L/5% %.+ */(/%&%/0$5 0, */$+&8 )80:/$@. X ime ta#en for finding the next available cell is large. X &n linear probing, we come across a problem #nown as clustering. 21. M+$%/0$ 0$+ &6?&$%&@+ &$6 6/5&6?&$%&@+ 0, <5/$@ C<&68&%/2 )80:/$@. A6?&$%&@+: he problem of primary clustering is eliminated. D/5&6?&$%&@+4 here is no guarantee of finding an unoccupied cell once the table is nearly half full. ::.%hat is meant by sorting? Grdering the data in an increasing or decreasing fashion according to some relationship among the data item is called sorting. 23. W.&% &8+ %.+ %-0 (&/$ 2*&55/,/2&%/0$5 0, 508%/$@ :&5+6 0$ %.+ 50<82+ 0, 6&%&A a. &nternal sorting b. 8xternal sorting 24. W.&% /5 (+&$% :# +x%+8$&* 508%/$@A 8xternal sorting is a process of sorting in which large bloc#s of data stored in storage devices are moved to the main memory and then sorted. 25. W.&% /5 (+&$% :# /$%+8$&* 508%/$@A &nternal sorting is a process of sorting the data in the main memory. 26. W.&% &8+ %.+ ?&8/0<5 ,&2%085 %0 :+ 20$5/6+8+6 /$ 6+2/6/$@ & 508%/$@ &*@08/%.(A a. Drogramming time b. 8xecution time of the program c. Oemory needed for program environment 2 .W.&% /5 %.+ (&/$ /6+& /$ B<::*+ 508%A he basic idea underlying the bubble sort is to pass through the file se)uentially several times.8ach pass consists of comparing each element in the file with its successor /xQiR and xQi?(R and interchanging the two elements if they are not in proper order. 2!. W.&% /5 %.+ (&/$ /6+& :+./$6 /$5+8%/0$ 508%A he main idea of insertion sort is to insert in the ith pass the ith element in 6 /(* 6 /:*...6 /i* in its rightful place. 2". W.&% /5 %.+ (&/$ /6+& :+./$6 5+*+2%/0$ 508%A he main idea behind the selection sort is to find the smallest element among in 6 /&* 6 /[?(*...6 /n* and then interchange it with a /[*. his process is then repeated for each value of [. 3'. W.&% /5 %.+ :&5/2 /6+& 0, 5.+** 508%A &nstead of sorting the entire array at once, it is first divide the array into smaller segments, which are then separately sorted using the insertion sort. 31. W.&% /5 %.+ 0%.+8 $&(+ ,08 5.+** 508%A Diminishing increment sort.

32. W.&% /5 %.+ )<8)05+ 0, C</2> 508%A he purpose of the )uic# sort is to move a data item in the correct direction, Just enough for to reach its final place in the array. 33. W.&% / %.+ &6?&$%&@+ 0, C</2> 508%A Yuic# sort reduces unnecessary swaps and moves an item to a greater distance, in one move. 34. W.&% /5 %.+ &?+8&@+ +,,/2/+$2# 0, .+&) 508%A he average efficiency of heap sort is 9 /n/log: n** where, n is the number of elements sorted. 35. D+,/$+ 5+@(+$%A %hen large bloc#s of data are to be sorted, only a portion of the bloc# or file is loaded in the main memory of the computer since, it cannot hold the entire bloc#. his small portion of file is called a segment. 36. N&(+ 50(+ 0, %.+ +x%+8$&* 508%/$@ (+%.065A a. Dolyphase merging b. Gscillation sorting c. Oerge sorting 3 . W.+$ /5 & 508%/$@ (+%.06 5&/6 %0 :+ 5%&:*+A 6 sorting method is said to be stable, it two data items of matching values are guaranteed to be not rearranged with respect to each other as the algorithm progresses. 3!. N&(+ 50(+ 5/()*+ &*@08/%.(5 <5+6 /$ +x%+8$&* 508%/$@A a. Oultiway merge b. Dolyphase merge c. 3eplacement selection 3". W.+$ 2&$ -+ <5+ /$5+8%/0$ 508%A &nsertion sort is useful only for small files or very nearly sorted files. 4'. H0- (&$# )&55+5 &8+ 8+C</8+6 ,08>=-&# (+8@/$@A he number of passes re)uired using #,way merging is Qlog # /n!m*R because the L 5 $ get # times as large in each pass. 41. D+,/$+ (&x .+&)A 6 heap in which the parent has a larger #ey than the child]s is called a max heap. 42. D+,/$+ (/$ .+&)A 6 heap in which the parent has a smaller #ey than the child is called a min heap. 43.W.&% /5 %.+ /6+& :+./$6 A668+55 2&*2<*&%/0$ 508% A his sorting is also called 5ashing.&n this method a function f is applied to each #ey. he result of the function determines into which of several subfiles the record is to be placed. he function should have the property that if xNBy ,f/x*NBf/y*.$uch a function is called order preserving.

9ART B UNIT=1 (. :. <. P. M. T. U. V. W. (9. ((. (:. (<. %hat are functions? 8xplain the types of functions in detail with an example program for each type. Define arrays. 8xplain the array types with an example program for each type. %hat are pointers? %hen and why they are used? 8xplain in detail with sample programs. Describe in detail about the Dreprocessors in C. Arief call by value and call by reference in detail. Discuss about function prototypes in detail/or* 8xplain about the different parameter passing methods with examples. 8xplain in detail about C declarations and variables. Discuss about the various data types in C. Describe the various types of operators in C language along with its priority. 8xplain about the various decision ma#ing statements in C language. %rite short notes on the following4 a*for loop ii*while loop iii* do,while loop iv*switch case 8xplain the follwing i* brea# ii* continue iii*goto %hat are the advantages of using pointers? 5ow are pointers declared and c initialiSed? 5ow the value of a variable is accessed using pointers? Give examples. UNIT=2 ( Define $tructures. 8xplain structures in detail. : Define Inion. 8xplain Inion in detail. < 8xplain in detail about file handling methods in detail. P 8xplain in detail about file Oanagement functions in detail. M 8xplain in detail about error handling in file . demonstrate the use of fsee#/* function. T %rite a c program to write record to a dis# file and read it bac#. U %rite a c program to write the integer numbers in the binary file. V Describe in detail about how to wor#ing with files. W 8xplain in detail about processing and updatation in file. (9 8xplain in detail about how to open . close a file with an simple example program. (( 8xplain in detail about use of getc,putc ,fputs,fgets ,rewind function (: 8xplain in detail about use of fprintf,fscanf function in detail.

UNIT=3 (. %hat are the various lin#ed list operations? 8xplain. :.%rite routines to implement addition, subtraction . differentiation of two polynomials. <. 8xplain cursor implementation of list. P. %rite a program in C to return the position of an element \ in a @ist @ M. $tate . explain 6pplication of lin#ed list. T. a*%rite a c program to multiply two polynomials. /V*

/b* %rite a c program to add two polynomials. /V* U. 8xplain the operations and the implementation of list 6D . V. 8xplain the operations and the implementation of singly lin#ed list. W. 8xplain the operations and the implementation of doubly lin#ed list. (9. 8xplain the operations and the implementation of circular lin#ed list. UNIT=4 (.8xplain the array and lin#ed list implementation of $tac#. :. 8xplain the array and lin#ed list implementation of Yueue. <. 8xplain how stac# is applied for evaluating an arithmetic expression. P.%rite a Drogram in C to create an empty stac# and to push an element into it. /V* <. 8xplain how )ueues can be implemented using 6rrays /(T* M. /a* %rite an algorithm to convert infix to postfix expression and explain it with example /V* /b* %rite an algorithm to evaluate a postfix expression and explain it with example /V* T. /a* %rite an algorithm to chec# given expression contains balanced parenthesis or not. /V* /b* %rite an algorithm for insertion and deletion operation in a circular )ueue /V* U.Give a procedure to convert an infix expression a?b"c?/d"e?f*"g to postfix notation UNIT=5 (. Define 5ash function. %rite routines to find and insert an element in separate chaining. :. 8xplain extendible hashing to resolve collision. <. 8xplain open addressing with example. P. $tate . explain the algorithm to perform 5eap sort. 6lso analySe the time complexity of the algorithm /(T* M. %rite a C program to perform Oerge sort and analySe time complexity of the algorithm./(T* T. $tate . explain the algorithm to perform Yuic# sort. 6lso analySe the time complexity of the algorithm. /(T* U. $tate . explain the algorithm to perform $hell sort. 6lso analySe the time complexity of the algorithm. /(T* V. $tate . 8xplain 8xternal sorting 6lgorithm Z wo %ay Oerge, Oultiway Oerge. /(T* W. $ort the se)uence <, (, P,U,M,W,:,T,M using &nsertion sort. (9. 8xplain the operation and implementation of &nsertion sort and shell sort. ((. 8xplain the operation and implementation of merge sort. (:. 8xplain the operation and implementation of external sorting. (<. race the )uic# sort algorithm for the following list of numbers. W9,UU,T9,WW,MM,VV,TT /T* (P.$how how heap sort processes the input (P:,MP<,(:<,TM,PM<,VUW,MU:,P<P,(((,:P: ,V((,(9:.

Das könnte Ihnen auch gefallen