1. Trang chủ
  2. » Công Nghệ Thông Tin

C and data structures p s deshpande

930 116 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 930
Dung lượng 4,46 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

This thorough text provides a comprehensive guide to all the data types in C with internal implementation, while providing examples to demonstrate their behavior... Divided into three se

Trang 1

This thorough text provides a comprehensive guide to all the data types in C with internal implementation, while providing examples to demonstrate their behavior.

CD Content

Trang 3

Divided into three separate sections, C & Data

Structures covers C programming, as well as the

implementation of data structures and an analysis of advanced data structure problems Beginning with the basic concepts of the C language (including the

operators, control structures, and functions), the book progresses to show these concepts through practical application with data structures such as linked lists and trees, and concludes with the integration of C

programs and advanced data structure problem-solving The book covers a vast range of data

structures and programming issues, such as syntactic and semantic aspects of C, all control statements in C, concepts of function, macro, files and pointers with

examples, graphs, arrays, searching and sorting

techniques, stacks and queues, files, and

preprocessing C & Data Structures provides a

comprehensive guide to all the data types in C with internal implementation, while providing examples to demonstrate their behavior.

Covers data structures such as linked lists, trees, graphs, arrays, and commonly used algorithms, as

Trang 4

well as advanced data structures such as B- trees and B+ trees

About the Authors

P.S Deshpande is a faculty member in the Department

of Computer Science at Visvesvarya National Institute

of Technology He has acted as a consultant to various government and private organizations in the field of database management, software engineering, data

warehousing, WAP, and J2EE design patterns, and has published a number of papers on Oracle, data

Trang 6

Requests for replacement of a defective CD-ROM must be accompanied

by the original disc, your mailing address, telephone number, date ofpurchase and purchase price Please state the nature of the problem,and send the information to CHARLES RIVER MEDIA, INC., 10 DownerAvenue, Hingham, Massachusetts 02043 CRM's sole obligation to thepurchaser is to replace the disc, based on defective materials or faultyworkmanship, but not on the operation or functionality of the product

Acknowledgments

Writing any book is not an easy task We spent about one year designingthe contents, implementing the programs and testing the programs Our

12 years of teaching experience has helped us to explain the issues inthe language and complex problems in data structures

We are thankful to our student Rupesh Nasre who, after receiving an

Trang 7

implementing advanced problems in data structures We are also

thankful to our students Ms Usha Agrawal and Mr Ramchandra Vibhutefor helping us in writing programs for Parts I and II

Trang 8

The book is written for both undergraduate and graduate students of corecomputer science areas The book would be very useful for other

students to learn programming for the purpose of making a career incomputer science It covers all those topics that generally appear in

aptitude tests and interviews It not only gives the language syntax butalso discusses its behavior by showing the internal implementation Wehave covered almost the entire range of data structures and

programming such as non-recursive implementation of tree traversals, A*algorithm in Artificial Intelligence, 8-queens problems, etc We also havesupplied a CD-ROM which contains all the source material that appears

in the book We welcome comments from our readers

Trang 10

Chapter 1: Introduction to the C Language

Trang 13

Points to Remember

1 The variables are defined at the begining of the block

2 The data type is defined at the begining of declaration andfollowed by a list of variables

3 It is the data type that assigns a property to a variable

Trang 14

Introduction

In C, the input from a standard input device, such as a keyboard, is taken

by using the function scanf In scanf, you have to specify both thevariables in which you can take input, and the format specifier, such as

2 In scanf, you have to specify the address of the variable, such

as &i The address is the memory location where a variable isstored The reason you must specify the address will be

discussed later

3 The number of format specifiers must be the same as the

Trang 15

Point to Remember

In scanf, you have to specify the address of a variable, such as &i, &j,and a list of format specifiers

Trang 16

1 Statement A indicates the if statement The general form of

the if statement is if(expr)

{

Trang 17

3 In C, the value nonzero is true while zero is taken as false.

4 If you want to execute only one statement, opening and closingbraces are not required, which is indicated by C and D in thecurrent program

5 The else part is optional If the if condition is true then thepart that is enclosed after the if is executed (Part X) If the ifcondition is false then the else part is executed (Part Y)

6 Without the else statement (in the first if statement), if thecondition is true then Part Z is executed

Points to Remember

1 if and if else are used for conditional execution Afterthe if statement the control is moved to the next statement

2 If the if condition is satisfied, then the "then" part is executed;otherwise the else part is executed

3 You can include any operators such as <, >, <=, >=, = = (forequality) Note that when you want to test two expressions forequality, use = = instead of =

Trang 18

Introduction

When you want to execute certain statements repeatedly you can useiteration statements C has provided three types of iteration statements:the for loop, while loop, and do while loop Generally, the

statements in the loop are executed until the specified condition is true orfalse

Trang 19

7 When expr2 is evaluated false, the loop is terminated and therepeat section is not executed.

8 After execution of the repeat section, expr3 is executed

Generally, this is the expression that is used to ensure that theloop will be terminated after certain iterations

Points to Remember

1 The for loop is used for repeating the execution of certainstatements

2 The statements that you want to repeat should be written in therepeat section

3 Generally, you have to specify any three expressions in the forloop

4 While writing expressions, ensure that expr2 is evaluated to befalse after certain iterations; otherwise your loop will never beterminated, resulting in infinite iterations

Trang 21

Introduction

The do while loop is similar to the while loop, but it checks theconditional expression only after the repetition part is executed When theexpression is evaluated to be false, the repetition part is not executed.Thus it is guaranteed that the repetition part is executed at least once.Program

Trang 22

3 The loop is terminated when the condition is evaluated to befalse

Point to Remember

The do while loop is used when you want to make at least one

iteration The condition should be checked after each iteration

Trang 24

3 If the value is equal, the corresponding statements (S1 and S2)are executed If break is not written, then S3 and S4 are

executed If break is used, only S1 and S2 are executed andcontrol moves out of the switch statement

4 If the value of switch_expr does not match that of

constant_expr1, then it is compared with the next

constant_expr If no values match, the statements in defaultare executed

5 In the program, statement A is the switch expression Theexpression i%2 calculates the remainder of the division For 2,

4, 6 etc., the remainder is 0 while for 1, 3, 5 the remainder is 1.Thus i%2 produces either 0 or 1

6 When the expression evaluates to 0, it matches that of

Trang 25

7 The clause ‘default’ is optional

Point to Remember

The switch statement is more easily read than multiple if elsestatements and it is used when you want to selectively execute oneaction among multiple actions

Trang 26

Chapter 2: Data Types

Trang 28

printf ("the value of integer after overflow is %d\n",i);}

2 The variables should be declared by specifying the data type

3 The data type determines the number of bytes to be allocated tothe variable and the valid operations that can be performed on

the variable

Trang 29

Introduction

C supports various data types for processing information There is afamily of integer data types and floating-point data types Characters arestored internally as integers, and they are interpreted according to thecharacter set The most commonly used character set is ASCII In theASCII character set, A is represented by the number 65

2 Data type determines the permissible operations on variables

Trang 31

Introduction

Integer data types are used for storing whole numbers and characters.The integers are internally stored in binary form

Program/Example

Here is an example that shows how integers are stored in the binaryform

1111 1111) This is the case when the data type is unsigned char

However, with signed char, the leftmost bit is used to represent the sign

of the number If the sign bit is 0, the number is positive, but if it is 1, thenumber is negative

Binary representation of the following numbers in signed char is asfollows:

Number = 127 Binary representation = 0111 1111 (leftmost bit is 0,

indicating positive.)

Number = −128 Binary representation = 1000 0000 (leftmost bit is 1,indicating negative.)

Trang 33

char 1 byte 0 to 28−1 (0 to 255)

short 2 bytes −215 to 215 −1 (−32768 to

32767)Unsigned

short 2 bytes 0 to 216 −1 (0 to 65535)

long int4 bytes 231 to 231−1 (2,147,483,648

to 2,147,483,647)

int 2 or 4 bytes depending on

implementation

Range for 2 or 4 bytes asgiven above

Explanation

1 In C, the range of the number depends on the number of bytesallocated and whether the number is signed

2 If the data type is unsigned the lower value is 0 and the upperdepends on the number of bytes allocated

3 If the data type is signed then the leftmost bit is used as a signbit

4 The negative number is stored in 2's complement form

5 The overflow behavior is determined by the binary presentationand its interpretation, that is, whether or not the number is

Trang 34

Points to Remember

1 The behavior of a data type can be analyzed according to itsbinary representation

2 In the case of binary representation, you have to determinewhether the number is positive or negative

Trang 36

3 The variable j stores the previous value of i as given in

statement B

4 The program determines the maximum value as 127 The valueafter overflow is -128

5 The initial value of i is 1 and it is incremented by 1 in the

while loop After i reaches 127, the next value is -128 and theloop is terminated

Points to Remember

1 In the case of signed char, if you continue adding 1 then youwill get the maximum value, and if you add 1 to the maximumvalue then you will get the most negative value

2 You can try this program for short and int, but be carefulwhen you are using int If the implementation is 4 bytes it willtake too much time to terminate the while loop

3 You can try this program for unsigned char Here you will getthe maximum value, 255 The value after overflow is 0

Trang 37

Introduction

Alpha characters are stored internally as integers Since each charactercan have 8 bits, you can have 256 different character values (0–255).Each integer is associated with a character using a character set Themost commonly used character set is ASCII In ASCII, "A" is represented

sequences Though the escape sequences look like two characters, eachrepresents only a single character

The complete selection of escape sequences is shown here

\a alert (bell) character \\ backslash

\b backspace \? question mark

Trang 38

2 The most common character set is ASCII.

3 You can give directive to functions such as printf by usingescape sequence characters

Trang 39

Introduction

You can represent a number by using the octal number system; that is,base 8 For example, if the number is 10, it can be represented in theoctal as 12, that is, 1*81 + 2*80

Explanation

When octal numbers are printed they are preceeded by "%0"

Trang 40

Introduction

Hexadecimal numbers use base 16 The characters used in hexadecimalnumbers are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F For example,

if the decimal number is 22, it is represented as 16 in the hexadecimalrepresentation: 1*161 + 6*160

Explanation

You can print numbers in hexadecimal form by using the format "0x"

Trang 41

NUMBERS

Introduction

Floating-point numbers represent two components: one is an exponentand the other is fraction For example, the number 200.07 can be

represented as 0.20007*103, where 0.2007 is the fraction and 3 is theexponent In a binary form, they are represented similarly There are twotypes of representation: short or single- precision floating-point numberand long or double-precision floating-point number short occupies 4bytes or 32 bits while long occupies 8 bytes or 64 bits

Program/Example

In C, short or single-precision floating point is represented by the datatype float and appears as:

float f ;

A single-precision floating-point number is represented as follows:

Here the fractional part occupies 23 bits from 0 to 22 The exponent partoccupies 8 bits from 23 to 30 (bias exponent, that is, exponent +

Trang 42

3 In a binary representation, exponent 6 means the number 110.Now add the bias, 0111 1111, to get the exponent: 1000 0101Since the number is positive, the sign bit is 0 The significant, or

fractional, part is:

1001 0001 0000 0000 0000 000

Note that up until the fractional part, only those bits that are on the rightside of the decimal point are present The 0s are added to the right side

to make the fractional part take up 23 bits

Special rules are applied for some numbers:

1 The number 0 is stored as all 0s, but the sign bit is 1

2 Positive infinity is represented as all 1s in the exponent and all0s in the fractional part with the sign bit 0

3 Negative infinity is represented as all 1s in the exponent and all0s in fractional part with the sign bit 1

4 A NAN (not a number) is an invalid floating number in which allthe exponent bits are 1, and in the fractional part you may have1s or 0s

The range of the float data type is 10−38 to 1038 for positive values and

−1038 to −10−38 for negative values

The values are accurate to 6 or 7 significant digits depending on theactual implementation

Trang 43

c Significant or fractional part: 1001 0010 0000 0000 0000 000Since the exponent is bias, find out the unbiased exponent.

d 100 = 1000 0011 – 0111 1111 (number 4)

Represent the number as 1.1001001*24

Represent the number without the exponent as 11001.001Convert the binary number to decimal: −25.125

For double precision, you can declare the variable as double d; it isrepresented as

Here the fractional part occupies 52 bits from 0 to 51 The exponent partoccupies 11 bits from 52 to 62 (the bias exponent is the exponent plus

Trang 44

2 The numerical part can be of following form:

d.d, d., d, d, where d is a set of digits

3 If the exponent part is present, it should be represented by ‘e’ or

‘E’, which is followed by a positive or negative integer It shouldnot have a decimal point and there should be at least 1 digitafter ‘E’

4 All floating numbers have decimal points or ‘e’ (or both)

5 When ‘e’ or ‘E’ is used, it is called scientific notation

6 When you write a constant, such as 50, it is interpreted as aninteger To interpret it as floating point you have to write it as50.0 or 50, or 50e0

You can use the format %f for printing floating numbers For example,printf("%f\n", f);

%f prints output with 6 decimal places If you want to print output with 8columns and 3 decimal places, you can use the format %8.3f For

printing double you can use %lf

Floating-point computation may give incorrect results in the followingsituations:

1 If the calculated value has a precision that exceeds the

Ngày đăng: 19/04/2019, 11:11

TỪ KHÓA LIÊN QUAN