Algorithms & Data Structures

Similar documents
Homework #3 CS2255 Fall 2012

CSC 211 Intermediate Programming. Pointers

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

[0569] p 0318 garbage

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

Pointers. Pointers. Pointers (cont) CS 217

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

FORM 2 (Please put your name and form # on the scantron!!!!)

DECLARAING AND INITIALIZING POINTERS

CS201- Introduction to Programming Current Quizzes

Type Conversion. and. Statements

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Variation of Pointers

MYcsvtu Notes LECTURE 34. POINTERS

Introduction to Programming Using Java (98-388)

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

C Pointers. 6th April 2017 Giulio Picierro

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

First of all, it is a variable, just like other variables you studied

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Introduction to C++ with content from

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Procedural programming with C

Delhi Noida Bhopal Hyderabad Jaipur Lucknow Indore Pune Bhubaneswar Kolkata Patna Web: Ph:

Short Notes of CS201

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

CS201 - Introduction to Programming Glossary By

Reference operator (&)

Incoming Exam. CS 201 Introduction to Pointers. What is a Pointer? Pointers and Addresses. High Speed Memory (RAM) Size of Variable Types.

Arrays, Pointers and Memory Management

Hash Table and Hashing

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

CS2255 HOMEWORK #1 Fall 2012

Pointers. Pointer References

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #07. Topic: Pointer in C Date:

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Dynamic memory allocation (malloc)

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

Chapter 2: Using Data

Exam 3 Chapters 7 & 9

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 28 November 2012

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

CS 231 Data Structures and Algorithms, Fall 2016

Type Checking. Prof. James L. Frankel Harvard University

Programming and Data Structure Solved. MCQs- Part 2

Chapter 4: Arrays, Pointers and Strings

Dynamic Memory Allocation

About this exam review

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

School of Science and Technology

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Darshan Institute of Engineering & Technology for Diploma studies Unit 4

Name :. Roll No. :... Invigilator s Signature :.. CS/B.TECH (NEW)/SEM-2/CS-201/ BASIC COMPUTATION & PRINCIPLES OF COMPUTER PROGRAMMING

CS201 Some Important Definitions

Introduction to C Language (M3-R )

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Pointers. Introduction

Arrays and Pointers (part 1)

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Arrays and Pointers (part 1)

C++ for Java Programmers

Pointers and Arrays 1

COMPUTER APPLICATION

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

Pointers as Arguments

Practice Sheet #07 with Solutions

Type systems. Static typing

3. Java - Language Constructs I

Character Strings. String-copy Example

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary


NCS 301 DATA STRUCTURE USING C

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Object oriented programming C++

C++ Casts and Run-Time Type Identification

An Introduction to MATLAB

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Values (a.k.a. data) representation. Advanced Compiler Construction Michel Schinz

Values (a.k.a. data) representation. The problem. Values representation. The problem. Advanced Compiler Construction Michel Schinz

Chapter 7 C Pointers

Pointers. Reference operator (&) ted = &andy;

Unit 7. Functions. Need of User Defined Functions

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

Introduction to Scientific Computing and Problem Solving

Lecture Set 4: More About Methods and More About Operators

POINTERS. Pointer is a memory variable which can store address of an object of specified data type. For example:

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Lecture 8 Dynamic Memory Allocation

M.EC201 Programming language

Transcription:

GATE- 2016-17 Postal Correspondence 1 Algorithms & Data Structures Computer Science & Information Technology (CS) 20 Rank under AIR 100 Postal Correspondence Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

GATE- 2016-17 Postal Correspondence 2 C O N T E N T: A L G O R I T HM & DATA S T R U C T U R E 1. C POINTERS 03-14 2. ARRAY 15-26 3. HANDLING STRINGS, DYNAMIC MEMORY ALLOCATION 27-36 4. STACK 37-57 5. QUEUE.. 58-78 6. LINKED LIST.. 79-94 7. TREES AND GRAPHS 95-109 8. INTRODUCTION TO ALGORITHM..110-123 9. RECURRENCE RELATION. 124-127 10.DIVIDE AND CONQUER.. 128-147 11.OTHER SORTING ALGORITHMS 148-159 12.DYNAMIC PROGRAMMING. 160-169 13.GREEDY ALGORITHM.. 170-187 14.HASHING.. 188-194 15.AVL TRESS.. 195-200

GATE- 2016-17 Postal Correspondence 3 CHAPTER-1 C POINTERS INTRODUCTION: Pointers are frequently used in C, as they offer number of benefit to the programmer. They include Pointers are more efficient in handling array and data table. Pointer can be used to return multiple values from a function via function argument. Pointer permit reference to function and thereby facilitating passing of functions as argument to other functions. The use of pointer array to character string result in saving of data storage space in memory. Pointer allows C to support dynamic memory management. Pointer provides an efficient tool for manipulating dynamic data structure such as structure linked list queue, stacks and trees. Pointer reduces length and complexity of program. They reduce length and complexity of program. They increase the execution speed and thus reduce the program execution time. Understanding pointers: Whenever we declare a variable then system allocate somewhere in memory, an appropriate location to hold the value of variable. Consider the following statement

GATE- 2016-17 Postal Correspondence 4 intx = 80; x var iable 80 value 5000 address Representation of variable Pointer variable is nothing but a variable that contain an address which is a location of another variable in memory. For example: Variable Value Address x 80 5000 p 5000 5048 Pointer variable Value of variable P is the address of variable X Address of a variable can be determined by & operator in C For example: P & x Would assign 5000 to variable P & operator can be used with simple variable or an array element. The following are the illegal use of address operator & 125 pointing constant int x [10] & x (pointing an array name) & (x + y) (pointing at expression) It x is an array then expression such as & x [0] and & x (i+3) are valid and represent the address

GATE- 2016-17 Postal Correspondence 5 of 0 th and (i+3) th element of int * P The declaration causes the compiler to allocate memory location for the pointer variable. Since memory location has not been assigned any value. These locations may contain some unknown value in them. Therefore they point to some unknown location. P?? Garbage point to unknown Location We can have multiple declaration in same statement * int p, q, r; * int x, p, y; x 10 ; p & x It is also possible to combine the declaration of data variable and declaration of pointer variable and Initialization of pointer variable in one step. * int x, p & x ; We can also define the pointer variable with initial value of null or zero. We can also assign the constant value to pointer variable. Pointer are flexible we can make same pointer to point different data variable in different statement. We can also use different pointer to point the same data variable. * int x, y, ptr ;

GATE- 2016-17 Postal Correspondence 6 ptr & x ; y * ptr //Assign the value pointed by pointer ptr to y * ptr 25; // This statement put the value of 25 at the location whose address is the value of pointer variable; we know the value of pointer variable (ptr) is the address of x. Therefore old value of x is replaced by 25. It is equivalent to assigning value 25 to x. Illustration of Pointer Assignments: Stage Value in storage Address Declaration x y ptr 4104 4108 4106 Address x 10 10 4104 4108 4106 Address ptr & x 10 4104 4104 4108 4106 Address Y * ptr 10 10 4104 4104 4108 4106 Address * ptr 25 25 10 4104 4104 4108 4106 Address Chain of pointers: p p Variable 2 1 address2 Address1 Value

GATE- 2016-17 Postal Correspondence 7 Pointer p2 contain the address of pointer p1 which points to the location that contain the desired value For example int * * p2 ; Arithmetic operation on pointers Multiplication: y * ptr * ptr Multiplication of * p1 and * p2 Division: Z * p2 /* p 1 Space is necessary /* considered as beginning of comment. p 4, p1 5, p 2, p, p all the operation are allowed 1 2 1 2 p p, p p and p! p are allowed 2 1 1 2 1 2 p 1 2 p is also allowed. If p1 and p2 both points to same array then p1 p2 gives the number of element between p1 and p 2. However p / p, p p, p / 3, p 3, p p 1 2 1* 2 1 1* 1 2 Are not allowed

GATE- 2016-17 Postal Correspondence 8 p1 Will cause the pointer p1 to point to next value of its type. suppose p1 is integer. Pointer with initial value 2800 then after operation p 1 value of p1 will be 2802 not 2801 A value cannot be assigned to an arbitrary address i.e. & x 10 is illegal Size of int 2 char 1 float 4 size of point er 2 p 1000 & p 2000 * p 10 Then * & p 1000 * * & p 10 Pointer representation in Array: 1-D: A[3] can be represented by * A 3 Base or starting address of array 2 D: * * a i i a i j In 1-D array to get value stored we need one asterisk (pointer notation) In 2-D array to get value stored in array we need two asterisk. In 3-D array to get value stored in array we need three asterisk.

GATE- 2016-17 Postal Correspondence 9 P: Pointer to the first row P+i: pointer to itn row * p i : Pointer to the first element in its row * p i j : Pointer to j th element in i th row Value stored in cell, * * p i j : i j i.e. i th Row j th column Pointer to one data type can be used to point another data type. This is possible by type casting Pointer to float number can be used as pointer to an integer Some conversion are required Pi int* pf Pointer to integer pointer to float no Difference between * p3 and* p 3. Since precedence of [] is more than * p3 array of 3 pointer while * p 3 declare p as a pointer of an array having 3 elements. declare p as an When an array is passed to a function as an argument, only the address of first element of an array is passed but not the actual value of array element. For example: x int x 10 ; sort Address of x [0] is passed to function sort

GATE- 2016-17 Postal Correspondence 10 Call by reference OR call by address main ( ) int x; x 20; change & x pr int f "%\ d, n x; * p * p 10; Output 30 When function change ( ) is called only the address of x is passed to the pointer P not value. Parameters are passed by value in c, i.e. values being passed are copied into the parameter of called function at the time when function s invoked. Pointer notation and their significance: 1. int * p // p is a pointer to an integer quantity. 2. int * p10 P is a 10 element array of pointer to integer quantities. 3. int * p10 P is a pointer to a 10 element integer array

GATE- 2016-17 Postal Correspondence 11 4. int * pvoid P is a function that returns to an integer quantity 5. int pchar * a P is a function that accepts an argument which is a pointer to character & return an integer quantity 6. int * pchar * a P is a function that accept an argument which is a pointer to a character return a pointer to an integer quantity 7. int * pchar * a P is a pointer to function that accept an argument which is a pointer to a character return an integer quantity 8. int * pchar * a10 P is a function that accept an argument which is a pointer to a character return a pointer to a 10 element integer array 9. int p char * a P is a function that accepts an argument which is a pointer to a character array returns an integer quantity. 10. p char a int * ; P is a function that accepts an argument which is an array of pointer to characters returns an integer quantity 11. p char a int * ; P is a function that accepts an argument which is a character array returns a pointer to an integer quantity

GATE- 2016-17 Postal Correspondence 12 12. p char a int * * ; P is a function that accepts an argument which is a pointer to character array return a pointer to an integer-quantity 13. int * p char * a P is a function that accept an argument which is an array of pointer to character return to an integer quantity 14. p char a int * * ; P is a function that accept an argument which is a pointer to a character array return an integer quantity 15. int * pchar * a P is a pointer to a function that accepts an argument which is a pointer to a character array return a pointer to an integer array. 16. int * * pchar * a P is a ptr to fun! That accepts an argument which is an array of pointer to character return a pointer to an integer quantity. 17. int * p10void P is a 10 element array of a pointer to function, each function return an integer quantity. 18. p char a int * 10 ; P is a 10 element array of pointer to functions. Each function accepts an argument which is a character & returns an integer quantity

GATE- 2016-17 Postal Correspondence 13 19. int * * p10 char a P is a 10 element array of pointer to functions each function accepts an argument which is a character and return a pointer to an integer quantity 20. int * * p10 char * a P is a 10 element array of a pointer to functions; each function accepts an argument which is a pointer to a character and return a pointer to an integer quantity. Questions Question: What is O/P of following C program int f int x, int* py, int** ppz int z, y * * ppz 1 z * * ppz * py 2 y * py x x 3 return x y z Main ( ) int c, * b, ** a c 4, b & c, a & b pr int f "% d ", f c, b, a

GATE- 2016-17 Postal Correspondence 14 Ans: 19 Explanation a b c 2000 1000 4 3000 2000 1000 x p4 ppz 4 1000 2000 6000 5000 4000 ** ppz return 4 and ** ppz 1return 5 So z = 5 * py 2 return 7 So y = 7 As value of x = 4 So x = x+3 return 7 So output is 7+7+5 = 19 Question: What does following program print: # include <studio. h> Void f int* p, int* q

GATE- 2016-17 Postal Correspondence 15 p q * p z int i 0, j 1; int main f & i,& j ; pr int f : % d %\ d ", n, i ; j return 0; Ans: 0,2 Explanation: i j p q 0 1 3000 4000 3000 4000 1000 2000 After p q p q 4000 4000 So * p 2 change value of j and value of i does not effected. New Edition with GATE 2015 Solutions is available. To Buy Postal Correspondence Package call at 0-9990657855