C and Data Structure_101 13.pdf - .emaN e1.sruIncrease oC eht fothe ezifont s tnosize f ehof t esthe aerCourse cnI.1 Name.egaP revoC e2.ht nuse i rethe

C and Data Structure_101 13.pdf - .emaN e1.sruIncrease oC...

This preview shows page 1 out of 314 pages.

Unformatted text preview: .emaN e1.sruIncrease oC eht fothe ezifont s tnosize f ehof t esthe aerCourse cnI .1 Name. .egaP revoC e2.ht nuse i rethe daefollowing h a sa gniwasolaloheader f eht esin u the .2 Cover Page. B.C.A. 101 13 C AND DATA STRUCTURE YTISREVINUALAGAPPA APPAGALAUNIVERSITY elcyC drihT eht ni )46.3:APGC( CA[Accredited AN yb edarG with ’+A’’A+’ htiw Grade detidby ercNAAC cA[ (CGPA:3.64) in the Third CycleCGU-DRHM yb ytisrevinU I–yrogeand taC Graded sa dedaras G Category–I dna University by MHRD-UGC] 300 036 – IDUKIARA KARAIKUDI K – 630 003 NOITACUDE ECNATSIDDIRECTORATE FO ETAROTCEOF RIDDISTANCE EDUCATION I - Semester C AND DATA STRUCTURE B.C.A. 101 13 itnem sYou a egaare p reinstructed voc eht etatodpupdate u ot dethe tcurcover tsni erpage a uoYas mentioned below: .emaN e1.sruIncrease oC eht fothe ezifont s tnosize f ehof t esthe aerCourse cnI .1 Name. aP revoC e2.ht nuse i rethe daefollowing h a sa gniwasolaloheader f eht esin u the .2 Cover Page. ISREVINUALAGAPPA APPAGALAUNIVERSITY rihT eht ni )46.3:APGC( CA[Accredited AN yb edarG with ’+A’’A+’ htiw Grade detidby ercNAAC cA[ (CGPA:3.64) in the Third CycleCGU-DRHM yb ytisrevinU I–yrogeand taC Graded sa dedaras G Category–I dna University by MHRD-UGC] 300 036 – IDUKIARA KARAIKUDI K – 630 003 B.C.A. TACUDE ECNATSIDDIRECTORATE FO ETAROTCEOF RIDDISTANCE EDUCATION C AND DATA STRUCTURE I - Semester ALAGAPPA UNIVERSITY [Accredited with ‘A+’ Grade by NAAC (CGPA:3.64) in the Third Cycle and Graded as Category–I University by MHRD-UGC] (A State University Established by the Government of Tamil Nadu) KARAIKUDI – 630 003 Directorate of Distance Education B.C.A. I - Semester 101 13 C AND DATA STRUCTURE Authors Dr Subburaj Ramasamy, Former Senior Director, Department of Information Technology, Government of India Units (1-8, 10) Rohit Khurana, CEO, ITL Education Solutions Ltd. Units (9, 11-14) "The copyright shall be vested with Alagappa University" All rights reserved. No part of this publication which is material protected by this copyright notice may be reproduced or transmitted or utilized or stored in any form or by any means now known or hereinafter invented, electronic, digital or mechanical, including photocopying, scanning, recording or by any information storage or retrieval system, without prior written permission from the Alagappa University, Karaikudi, Tamil Nadu. Information contained in this book has been published by VIKAS® Publishing House Pvt. Ltd. and has been obtained by its Authors from sources believed to be reliable and are correct to the best of their knowledge. However, the Alagappa University, Publisher and its Authors shall in no event be liable for any errors, omissions or damages arising out of use of this information and specifically disclaim any implied warranties or merchantability or fitness for any particular use. Vikas® is the registered trademark of Vikas® Publishing House Pvt. Ltd. VIKAS® PUBLISHING HOUSE PVT. LTD. E-28, Sector-8, Noida - 201301 (UP) Phone: 0120-4078900 Fax: 0120-4078999 Regd. Office: 7361, Ravindra Mansion, Ram Nagar, New Delhi 110 055 Website: Email: [email protected] Work Order No. AU/DDE/DE1-238/Preparation and Printing of Course Materials/2018 Dated 30.08.2018 Copies - 500 SYLLABI-BOOK MAPPING TABLE C and Data Structure BLOCK 1: INTRODUCTION TO C UNIT - 1: Program Development Styles and Basics of C. Introduction to C - Character Set - Identifiers and Keywords - Data Types - Constants - Variables - Declarations - Declaring Variables, - Rules for Defining Variables. Initializing Variables - Type Conversion. Operator and Expressions UNIT - 2: Data Input, Output and Preliminaries - Single Character Input and Output - Entering Input Data - Writing Output Data - Gets and Puts Functions UNIT - 3: Control Statements: Branching and Looping - Nested Control Structures - Switch - Break - Continue and Goto. BLOCK 2: FUNCTIONS, ARRAYS AND POINTERS UNIT - 4: Function: Defining a Function - Accessing a Function - Passing Arguments to a Function - Recursion - Library Function - Macros - C Preprocessor - Program Structure: Storage Classes - Automatic Variables Global Variables - Static Variables- Multiple Programming - Bitwise Operation. UNIT - 5: Arrays - Array Initialization, Definition of Array, Characteristic of Array, One Dimensional Array, Two Dimensional Array, Multidimensional Arrays, Character Array and Strings - String Handling Functions. UNIT - 6: Pointers - Features of Pointers, Pointer Declaration, Arithmetic Operation with Pointers, Pointers and Arrays, Pointers and Two Dimensional Arrays, Array of Pointers, Pointers to Pointers, Pointers and strings. Unit 1: Program Development Styles and Basics of C (Pages 1-33); Unit 2: Data Input, Output and Preliminaries (Pages 34-41); Unit 3: Control Statements (Pages 42-71) Unit 4: Function (Pages 72-106); Unit 5: Arrays (Pages 107-120); Unit 6: Pointers (Pages 121-142) BLOCK 3: STRUCTURE UNION AND FILES UNIT - 7: Structures and Unions: Defining a Structure - Processing a Structure - Structures and Pointers - Passing Structures to Functions - Self Referential Structures - Bit Fields - Unions - Enumerations. UNIT - 8: Data File: Opening and Closing a Data File - Creating a Data File - Processing a Data File - Unformatted Data File - Command Line Parameters. Unit 7: Structures and Union (Pages 143-166); Unit 8: Data File (Pages 167-187) BLOCK 4: LINEAR DATA STRUCTURE UNIT - 9: Introduction to Data Structure, Stack, Stack Related Terms, Operation on a Stack, Representation of Stack, Implementation of a Stack Polish Notation. UNIT - 10: Queues, Various Positions of Queue, Circular Queues. Operations on Queue, Representation of Queues. Applications of Queue. UNIT - 11: List, Merging Lists, Linked List, Single Linked List, Double Linked List, Header Linked List, Insertion and Deletion of Linked List, Traversing a Linked List.Representation of Linked List Unit 9: Introduction to Data Structure (Pages 188-209); Unit 10: Queues (Pages 210-225); Unit 11: List (Pages 226-262) BLOCK 5: NON-LINEAR DATA STRUCTURE UNIT - 12: Introduction - Trees, Binary Trees, Types of Binary Trees, UNIT - 13: Binary Tree Representation, Traversing Binary Trees, UNIT - 14: Binary Search Tree, Insertion and Deletion Operations, Trees and their Applications Hashing Techniques. Unit 12: Introduction to Trees (Pages 263-267); Unit 13: Binary Tree Representation (Pages 268-275); Unit 14: Binary Search Tree (Pages 276-304) CONTENTS INTRODUCTION BLOCK I: INTRODUCTION TO C UNIT 1 PROGRAM DEVELOPMENT STYLES AND BASICS OF C 1-33 1.0 Introduction 1.1 Objectives 1.2 Introduction to Programming 1.2.1 Tokens, Identifiers and Keywords; 1.2.2 The C Character Set 1.2.3 Elementary Data Types; 1.2.4 Constants; 1.2.5 Variable 1.3 1.4 1.5 1.6 1.7 1.8 Operators and Expressions Answers to Check Your Progress Questions Summary Key Words Self Assessment Questions and Exercises Further Readings UNIT 2 DATA INPUT, OUTPUT AND PRELIMINARIES 34-41 2.0 Introduction 2.1 Objectives 2.2 I/O Functions: Entering Input Data and Writing Output Data 2.2.1 Single Character Input/Output; 2.3 2.4 2.5 2.6 2.7 2.2.2 Strings—gets() and puts() Answers to Check Your Progress Questions Summary Key Words Self Assessment Questions and Exercises Further Readings UNIT 3 CONTROL STATEMENTS 42-71 3.0 Introduction 3.1 Objectives 3.2 Decision and Control Structures 3.3 3.4 3.5 3.6 3.7 3.2.1 Logical Operators and Branching; 3.2.2 Loops and Control Constructs 3.2.3 switch Statement; 3.2.4 break, continue, return and goto Answers to Check Your Progress Questions Summary Key Words Self Assessment Questions and Exercises Further Readings BLOCK II: FUNCTIONS, ARRAYS AND POINTERS UNIT 4 FUNCTION 4.0 Introduction 4.1 Objectives 72-106 4.2 Function 4.2.1 Defining and Accessing a Function; 4.2.2 Function Arguments 4.2.3 Arrays and Functions; 4.2.4 Recursive Function 4.3 Storage Classes 4.3.1 Automatic Variables; 4.3.2 Register Variables 4.3.3 External (Global) Variables; 4.3.4 Static Variables 4.3.5 External (Global) Static Variable; 4.3.6 Multi-file Program 4.4 4.5 4.6 4.7 4.8 4.9 4.10 Macros Preprocessor Directives Answers to Check Your Progress Questions Summary Key Words Self Assessment Questions and Exercises Further Readings UNIT 5 ARRAYS 107-120 5.0 Introduction 5.1 Objectives 5.2 One Dimensional Array 5.2.1 Two-Dimensional Arrays 5.3 Strings and Characters Array 5.3.1 String Manipulation Using Library Functions 5.4 5.5 5.6 5.7 5.8 Answers to Check Your Progress Questions Summary Key Words Self Assessment Questions and Exercises Further Readings UNIT 6 POINTERS 121-142 6.0 Introduction 6.1 Objectives 6.2 Concept of Pointers 6.2.1 Pointer Arithmetic; 6.3 6.4 6.5 6.6 6.7 6.2.2 Passing Pointer to a Function; 6.2.3 Pointers and Strings Answers to Check Your Progress Questions Summary Key Words Self Assessment Questions and Exercises Further Readings BLOCK III: STRUCTURE UNION AND FILES UNIT 7 STRUCTURES AND UNION 143-166 7.0 Introduction 7.1 Objectives 7.2 Structures 7.2.1 Processing a Structure; 7.2.2 Array of Structures 7.2.3 Structure Elements Passing to Functions; 7.2.4 Structure passing to Functions 7.2.5 Structure Within Structure; 7.2.6 Structure Containing Arrays; 7.2.7 Pointers to Structures 7.3 Union Definition 7.4 Enumerated Data Types 7.5 7.6 7.7 7.8 7.9 Answers to Check Your Progress Questions Summary Key Words Self Assessment Questions and Exercises Further Readings UNIT 8 DATA FILE 167-187 8.0 Introduction 8.1 Objectives 8.2 Data File 8.2.1 Opening and Closing a Data File; 8.2.2 Concept of Binary Files 8.2.3 Formatted I/O Operations with Files; 8.2.4 Writing and Reading a Data File 8.2.5 Unformatted Data Files; 8.2.6 Processing a Data File; 8.2.7 Use of the Command Line Argument 8.3 8.4 8.5 8.6 8.7 Answers to Check Your Progress Questions Summary Key Words Self Assessment Questions and Exercises Further Readings BLOCK IV: LINEAR DATA STRUCTURE UNIT 9 INTRODUCTION TO DATA STRUCTURE 9.0 9.1 9.2 9.3 Introduction Objectives Stack Related Terms and Operations on Stack Application and Implementation of Stack 9.4 9.5 9.6 9.7 9.8 Answers to Check Your Progress Questions Summary Key Words Self Assessment Questions and Exercises Further Readings 188-209 9.3.1 Converting Infix Notation to Postfix and Prefix or Polish Notations UNIT 10 10.0 10.1 10.2 10.3 10.4 10.5 10.6 10.7 10.8 10.9 10.10 QUEUES 210-225 Introduction Objectives Queues Representation of Queues Circular Queue Applications of Queues Answers to Check Your Progress Questions Summary Key Words Self Assessment Questions and Exercises Further Readings UNIT 11 LIST 11.0 Introduction 11.1 Objectives 226-262 11.2 Merging List and Linked List 11.3 Singly-Linked Lists 11.3.1 Traversing; 11.3.2 Insertion; 11.3.3 Deletion 11.4 Doubly-Linked Lists 11.4.1 Insertion; 11.4.2 Deletion 11.5 11.6 11.7 11.8 11.9 11.10 11.11 Header List Representation of Linked List Answers to Check Your Progress Questions Summary Key Words Self Assessment Questions and Exercises Further Readings BLOCK V: NON-LINEAR DATA STRUCTURE UNIT 12 INTRODUCTION TO TREES 263-267 12.0 Introduction 12.1 Objectives 12.2 Trees 12.2.1 Forms of Binary Tree 12.3 12.4 12.5 12.6 12.7 Answers to Check Your Progress Questions Summary Key Words Self Assessment Questions and Exercises Further Readings UNIT 13 BINARY TREE REPRESENTATION 268-275 13.0 Introduction 13.1 Objectives 13.2 Binary Tree 13.2.1 Array Representation; 13.3 13.4 13.5 13.6 13.7 13.8 13.2.2 Linked Representation Binary Tree Traversals Answers to Check Your Progress Questions Summary Key Words Self Assessment Questions and Exercises Further Readings UNIT 14 BINARY SEARCH TREE 14.0 Introduction 14.1 Objectives 14.2 Binary Search Tree 14.2.1 Inserting a Node; 14.3 14.4 14.5 14.6 14.7 14.8 14.9 14.2.2 Deleting a Node Applications of Trees Hashing Techniques Answers to Check Your Progress Questions Summary Key Words Self Assessment Questions and Exercises Further Readings 276-304 Introduction NOTES INTRODUCTION C is a programming language and is substantially different from C++ and C#. Many operating systems are written using C, UNIX being the first. Later, Microsoft Windows, Mac OS X and GNU/Linux were written in C. Not only is C the language of operating systems, it is the precursor and inspiration for almost all the popular high-level languages available today. Perl, PHP, Python and Ruby are also written in C. In fact, one of the strengths of C is its universality and portability across various computer architectures. Therefore, C can be used for the development of different types of applications that include real-time systems and expert systems. C also provides flexibility to users for introducing new types of features in their programs, depending upon the requirement and definition of userdefined functions. The various features of C—algorithms, flow charts, decisionmaking statements, functions, arrays, linked lists, stacks and trees, structures and pointers—are useful for program developers. The goal of this book is to introduce you to C—a programming language that is ideally suited to modern computers and modern programming. The book, C and Data Structure, follows the self-instructional mode wherein each unit begins with an Introduction to the topic followed by an outline of the Objectives. The detailed content is then presented in a simple and structured manner interspersed with Check Your Progress questions. A list of Key Words, a Summary and a set of Self Assessment Questions and Exercises is also provided at the end of each unit for effective recapitulation. 8 Self-Instructional Material BLOCK - I INTRODUCTION TO C UNIT 1 PROGRAM DEVELOPMENT STYLES AND BASICS OF C Program Development Styles and Basics of C NOTES Structure 1.0 Introduction 1.1 Objectives 1.2 Introduction to Programming 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3 1.4 1.5 1.6 1.7 1.8 1.0 Tokens, Identifiers and Keywords The C Character Set Elementary Data Types Constants Variable Operators and Expressions Answers to Check Your Progress Questions Summary Key Words Self Assessment Questions and Exercises Further Readings INTRODUCTION In this unit, you will learn about the program development life cycle, building blocks of program, operators and expressions. There are various stages through which a program passes in order to provide the output. Compiler is a program that processes statements written in a particular programming language and turns them into machine language or “code” that a computer’s processor uses. Tokens are atomic elements or building blocks of a program. There are six classes of tokens which are explained in this unit. Further you will learn about the evaluation of expression and operators. 1.1 OBJECTIVES After going through this unit, you will be able to: Discuss the concept of program development Explain the building blocks of program Explain the various data types Define and initialize the variables Explain the various types of operators Self-Instructional Material 1 Program Development Styles and Basics of C NOTES 1.2 INTRODUCTION TO PROGRAMMING A computer program or software is a sequence of instructions coded in a programming language, such as FORTRAN, Pascal, BASIC, C, C++, Java, C # (pronounced C sharp), etc. Software development is considered to be an art. Now, it is an engineering discipline having a set of well-developed methodologies for software development and maintenance. Software Development Life Cycle (SDLC) When programming languages became available in the 1960s, software development emerged as an independent profession. Till then, only computer specialists who were knowledgeable about computer hardware could make the computer perform intended operations and use them through machine languages. The emergence of the various high-level languages and progressive reduction of cost of computer systems gave rise to a number of software developers or programmers. The software development projects undertaken in the early days were essentially developing the program statements or the codes for the applications straight away. This resulted in a software crisis due to which projects where always getting delayed and exceeding the original cost by two to three times or even more. The software did not perform the intended functions on delivery and contained many defects. Therefore, a software engineering conference was held in the early 970s to overcome the crisis. Software engineering is essentially the application of engineering principles to the development of software. One of the recommendations was to divide the software development activity into a number of phases. This led to the formulation of a software development life cycle model. The stages of the SDLC model are shown in Figure 1.1. Planning Stage (Resource allocation and team assignments) Requirement Stage (Simple description about given problem, use-case diagram) Development Stage (Test scripts are developed and integration) Testing Stage (Data Validation and integration is tested) Release Stage (checklist and users-guide implementation) Fig. 1.1 Stages of SDLC C is a procedural systems implementation language. Despite its low-level capabilities, the language was designed to encourage cross-platform programming. C has abilities for structured programming and supports lexical variable scope and recursion. In a 2 Self-Instructional Material C program, all executable codes are contained within functions and the function parameters are always passed by value. Pass-by-reference is simulated in C by explicitly passing pointer values. C program uses the semicolon as a statement terminator. Comments are written between the delimiters /* and */. C source files contain declarations and definitions of function. Function definitions contain declarations and statements. Declarations either define new types using keywords, such as struct, union and enum or assign types to new variables by writing the type followed by the variable name. Keywords, such as char and int specify built-in types. The programming codes are enclosed in curly braces ({ }) for limiting the scope of declarations and to act as a single statement for control structures. C statements denote specific actions. The most common statement is an expression statement which contains an expression to be evaluated followed by a semicolon (;). To modify the normal sequential execution of statements, C supports various control flow statements using the C reserved keywords. If and if…else are used for conditional execution whereas do…while, while and for are used for iterative execution or looping. break and continue keywords are used to leave the innermost enclosing loop statement. The goto statement directly goes to the specified label within the function. switch selects a case to be executed which is based on the value of an integer expression. Expressions can use a variety of built-in operators and may contain function calls. A program in C is run by selecting Run option from the main menu to get the result. The following steps are required to run an executable file: Execute or run the .exe file. The result is obtained if no error is present. The program is debugged if any error occurs. If an error occurs, the compilation step is repeated for the execution step. Program Development Styles and Basics of C NOTES After writing the program it must be saved as .c extension name. If the .c file is compiled successfully, it produces the desired result. The following sequence is required to run a C program: Creating a C program Compiling the program Linking the program from C library Executing the C program Get the output Figure 1.2 shows the flow chart of the development stage of a simple C program in which you find that Calculate.c file contains the predefined header file; for example <stdio.h>, <stdlib.h>, which is then sent to the compiler. After successful compilation, Calculate.obj file is prepared. Linker is used to link the program. Linking is the process of combining various pieces of code and data together...
View Full Document

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture