DATA STRUCTURE USING C++

Similar documents
C & Data Structures syllabus

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Contents. Preface. Introduction. Introduction to C Programming

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:


Practical C++ Programming

The Waite Group's. New. Primer Plus. Second Edition. Mitchell Waite and Stephen Prata SAMS

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING UNIT-1

KLiC C. About. Tools: Turbo C++ Syllabus:

Sample Copy. Not For Distribution.

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

KLiC C Programming. (KLiC Certificate in C Programming)

VALLIAMMAI ENGINEERING COLLEGE SRM NAGAR, KATTANGULATHUR

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

The Foundation of C++: The C Subset An Overview of C p. 3 The Origins and History of C p. 4 C Is a Middle-Level Language p. 5 C Is a Structured

Long Questions. 7. How does union help in storing the values? How it differs from structure?

Model Viva Questions for Programming in C lab

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

Code No: R Set No. 1

Practical C Programming. Steve Oualline

M1-R4: Programing and Problem Solving using C (JULY 2018)

C-LANGUAGE CURRICULAM

Computer Programming C++ (wg) CCOs

UNIT I : OVERVIEW OF COMPUTERS AND C-PROGRAMMING


Course Title: C Programming Full Marks: Course no: CSC110 Pass Marks: Nature of course: Theory + Lab Credit hours: 3

CIS 1.5 Course Objectives. a. Understand the concept of a program (i.e., a computer following a series of instructions)

DR. A.P.J. ABDUL KALAM TECHNICAL UNIVERSITY LUCKNOW. Evaluation Scheme & Syllabus. For. B.Tech. First Year (Programming for Problem Solving)

SAURASHTRA UNIVERSITY

Appendices E through H are PDF documents posted online at the book s Companion Website (located at

(i) Describe in detail about the classification of computers with their features and limitations(10)

PESIT-BSC Department of Science & Humanities

C mini reference. 5 Binary numbers 12

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

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

Syllabus for Computer Science General Part I

CSCI 171 Chapter Outlines

Week Day Topic Sub Topic Type Hours Pre-Evaluation Experience Collection & Demographics Online 2 OOPS concepts 1

Texas Instruments Technical Questions and Answers

BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE, Pilani Pilani Campus Instruction Division. SECOND SEMESTER Course Handout Part II

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

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

Contents. 1 Introduction to Computers, the Internet and the World Wide Web 1. 2 Introduction to C Programming 26

CNG 140 C Programming. Syllabus. Course Info Fall Semester. Catalog Description

Quiz 0 Review Session. October 13th, 2014

Important Questions for Viva CPU

ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27. Midterm #2 Review

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p.


CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CS Programming In C

Come and join us at WebLyceum

MPATE-GE 2618: C Programming for Music Technology. Syllabus

Reg. No. : Question Paper Code : 27157

OUTCOMES BASED LEARNING MATRIX

Introduction to Programming Using Java (98-388)

1 Chapter Plan...1 Exercise - Simple Program...2

Multiple Choice Questions ( 1 mark)

UNIT IV-2. The I/O library functions can be classified into two broad categories:

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

[CHAPTER] 1 INTRODUCTION 1

Software Development & Education Center C Programming

FUNDAMENTALS OF COMPUTER PROGRAMMING AND IT

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

GARDEN CITY UNIVERSITY. Bachelor of Computer Applications SEMESTER- I. Course: CONCEPTS OF PROGRAMMING USING C LANGUAGE CODE: 05ABCAR17111 CREDITS: 04

A flow chart is a graphical or symbolic representation of a process.

BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE, Pilani Pilani Campus Instruction Division. SECOND SEMESTER Course Handout Part II

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

Questions Bank. 14) State any four advantages of using flow-chart

Syllabus For F. Y. B. Sc. Semester 1 Core Paper No.: 102 Core Paper Title: Programming in C I 1. Algorithm & Flowcharting 2. Programming Languages & S

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Model Viva Questions for Programming in C lab

BLM2031 Structured Programming. Zeyneb KURT

C - Basics, Bitwise Operator. Zhaoguo Wang

Lesson Plan. Week Theory Practical. Lecture Day Topic(including assignment Test) Practical Day

Student Name: (in Capital Letters) CSE Introduction to Programming for Engineers and Scientists. Final Exam

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

STRUCTURE AND SYLLABUS APPROVED IN THE BOARD OF STUDIES MEETING ON 2001 TO BE EFFECTIVE FROM THE ACADEMIC YEAR

Appendix A Developing a C Program on the UNIX system

A Fast Review of C Essentials Part I

Kadi Sarva Vishwavidyalaya, Gandhinagar

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

SUBJECT COMPUTER SCIENCE PAGE 1

Library Functions. General Questions

Name: Prof. Dr. Iftikhar Hussain. Designation: Vice Chancellor. Department: Industrial Engineering. Specialization: Flexible Manufacturing Systems

Review of the C Programming Language for Principles of Operating Systems

Absolute C++ Walter Savitch

C for Electronic Engineering

DHANALAKSHMI SRINIVASAN INSTITUTE OF RESEARCH AND TECHNOLOGY SIRUVACHUR, PERAMBALUR DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Tentative Teaching Plan Department of Software Engineering Name of Teacher Dr. Naeem Ahmed Mahoto Course Name Computer Programming

Programming Fundamentals - A Modular Structured Approach using C++ By: Kenneth Leroy Busbee

Standard C Library Functions

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

Borland 105, 278, 361, 1135 Bounded array Branch instruction 7 break statement 170 BTree 873 Building a project 117 Built in data types 126

EMBEDDED Systems. Functions. MODULE- 1 C programming with data Structure Introduction to C. Array and String. Control Flow Statements In C

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

CERTIFICATE IN WEB PROGRAMMING

TEXT FILE I/O. printf("this data will be written to the screen.\n"); printf("x = %d, y = %f\n", x, y);

Transcription:

DATA STRUCTURE USING C++ Ghuam Gilanie Janjua Muhammad Attique Goheer Adnan Hameed Qureshi Dr. Hafeez Ullah Janjua Innovative New Al-Badar Kitab Ghar Publisher and Distributor University Chowk, Bahawalpur

Data Structure using C++ 2012 Innovative All rights reserved. No part of this book may be reproduced, stored in a retrieval system or transmitted in any form or by any means, without the prior written permission of the publisher and or writers, except in the case of brief quotations embodied in critical articles or reviews. The authors and publisher have made every effort in the preparation of this book to ensure the accuracy of the information. However, the information in this book is sold without warranty, either express or implied. C++ source code used in this book is available on http://www. Innovative.org

Credits Authors Ghulam Gilanie Janjua Muhammad Attique Goheer Adnan Hameed Qureshi Dr. Hafeez Ullah Janjua Technical Reviewers Dr. Shahid Naweed Shazia Hassan Kashif Hameed Muhammad Saifullah Shahid Nawaz Mirza Adnan Baig Zafar Saeed Technical Editor Sara Naheed Managing Editor Dr. Hafeez Ullah Janjua Qaiser Hassan Graphic Designers Muhammad Faisal Chughtai Muhammad Zubair

About Authors Ghulam Gilanie Janjua Ghulam Gilanie Janjua is M.Phil in Computer Science. He has diversified experience in programming field. He has vast experience of development at different software houses. He is currently working as Computer Programmer in NH &MP. He has already published a book on programming with ANSI C. He is currently working on bio-computing and diagnostic images. He has published his research work in well reputed international journals. His research interests are Medical Image Processing and Pattern Recognition. Muhammad Attique Goheer Adnan Hameed Qureshi Dr. Hafeez Ullah Janjua

Designed by: Muhammad Faisal Chughtai (+92 334 687 3612) Graphic Designer, Innovative Research Centre, Bahawalpur Stockiest: New Al-Badar Kitab Ghar University Chowk, Bahawalpur +92 62 2283138, +92 332 680 2128 Price: Rs. 250/-

Acknowledgement We are nothing without the support and blessings of Almighty Allah, my faith in the pious and peaceful religion Islam and faith in the last messenger of Allah, Hazrat Muhammad (P.B.U.H) are strengthen us to carried out each and every bit of my life and so as in this work. Exceptional thanks for Prof. Dr. Muhammad Shahid Naweed providing us valuable connotation regarding innovative ideas. He is probably the best described as fountains of enthusiasm and ideas. It made possible the book with his continued mentorship. Throughout the entire book compilation period his depth of knowledge on the topic, suggestions for continuous improvement, encouragement and assistance helped me to finalize the product. We would also like to thank Qasim Zia and M. Mashhood for their wit, intelligence, impeccable professionalism, and vision who provided an endless supply of visionary ideas and showed me the sky, assist me out during the entire project period with various resources, constructive suggestions and ideas. I am deeply thankful and want to express my honest appreciations to my dear friends for their guidance and friendship who helped me time to time with problem solving, idea generation, and providing required project related information. I appreciate their precious time, assistance and moral support. We would like to thank all of the great people at Innovative Research Center, Bahawalpur, Pakistan for their guidance along the way. Also thanks to XXX and XXXX for keeping everything organized and on track. Big thanks to my students Shamas Bilal, M. Faisal, Tariq Baloch, Mazhar Iqbal, Hafiz Shakeel, Jamshed Hashmi, Sanaullah Tariq, Noman Akram, Waqas Ahmad, Faisal Abba, M. Khursheed, Hafiz Sabir Basheer, M. Imran, Hamza Abdul Wahab, Atif Sajjad, Asad Razzaq, M. Nasir,Shahzad Ahmad, Asif Shahzad, Mudassar Hasnain, Jamshed Khan, Faheem Abbasi, M. Habib Sarwar, M. Usman, ZZZZ, who join us for their study and that was the time we were compiling the book in our mind. We would also like to thank our reviewers, XXXX. XXXX not only made sure that the information presented in this book was technically correct, but also that the information was understandable to those new to data structure. I'd like to offer my sincere, heartfelt thanks to everyone who helped Grant and me to produce this book:

XXX extraordinaire for the tremendous job he did in assisting us with sorting out the finer points of Data Structure as my primary technical reviewer. To XXX, for being fun to work with and XXXX for the wonderful comments and insights they provided us on our book. XXX is a true computer guru who has probably forgotten more about programming than I will ever know. Rana Sarfraz Nasir, Rao Muhammad Iqbal, Qamar Saeed, Hamid Khan and Rana Iqrar Hussain helped us morally throughout the manuscript of this book. XXXX created a number of the illustrations and improved the others. XXXX provided support and resources, and we are grateful. A special thanks you to XXX, who added great value to this book. If this book is clear and understandable, it is due to her vigilance. And, in saving the best for last, a special note of thanks and appreciation to, XXX, who has been involved in this work from the start and provided loving support every step of the way. From endless readings and corrections of the manuscript to warm encouragement, you and your efforts are sincerely appreciated. It couldn't have done it without you. Ghulam Gilanie Janjua M. Phil (Computer Science)

Table of Contents TOPIC PAGE NO CHAPTER NO 1 Understanding the Environment What is data? 1 What is information? 1 About which data is to be collected Data type Built in data types 2 Programmer define data types 3 What is data structure? 3 Example of data structure 3 Common operations of data structure 4 Inserting 4 Deleting 4 Updating 4 Transversing 4 Searching 4 Sorting 5 Splitting 5 Merging 5 Types of data structure 5 A primitive data structure 5 A non primitive data structure 5 Physical data structures 5 Logical data structures 6 Static data structures 6 Dynamic data structures 6 Linear data structures 7 Non-linear data structures 7 Contiguous data structures 8 Non contiguous data structures 8 Implementation of data structures in this book 9 General Syntax of Algorithm used though out this book 9

CHAPTER NO 2 Decisions Terms used for array 3 Base Address 3 Memory bytes allocated w.r.t Data Types 3 Examples of Array in computer science 4 Types of Array 5 One dimensional array 5 Memory representation of the array 5 Operations on One Dimensional Array 6 Insertion 6 Algorithm for insertion 8 C++ Code for insertion 9 Traversing 10 Algorithm for Traversing 10 C++ Code for Traversing 11 Searching 12 Equential Searches 12 Algorithm for Linear Search 13 C++ Code for Linear Search 14 Binary Search 15 Algorithm for Binary Search 16 C++ Code for Binary Search 18 Sorting 19 Bubble Sort 20 Algorithm for Bubble Sort 23 C++ Code for Bubble Sort 24 Selection Sort 25 Algorithm for Selection Sort 28 C++ Code for Selection Sort 30 CHAPTER NO 3

Loops Loop 1 Parts of Loop 1 Types of Loops in C 1 while Loop 1 break Statement 13 continue Statement 14 Sentinel Control Loop 15 do-while Loop 16 for Loop 19 Infinite Loops 22 When to use whose loop? 22 Nested loops 23 Points to be noted 29 Exercise 29 CHAPTER NO 4 Modular Structure Function 1 Types of Function 1 Ingredients of Function 1 Arguments 1 Parameters 1 Return Value 1 Signature of a Function 2 Advantages of Using Function 4 Returning More Values from Function 6 Pass by Value 6 Pass by Reference 7 Reference 8 Pointers 11 Some Important Points about Pointers 14 sizeof Operator 15 Pass by address 18

Default/Optional Arguments 20 Function Overloading 21 Macros 22 Passing variable number of arguments 24 Storage classes in C 25 Local and Global variables 25 Automatic variables 26 register variables 27 External variables 27 static variables 29 Recursion 30 Infinite recursive function 30 Finite recursive function 31 File include mechanism 36 Points to be noted 38 Exercise 39 CHAPTER NO 5 Arrays Array 1 Ingredients of an array 2 One dimensional array 2 Multidimensional array 2 Declaration of array variables 2 Memory representation of array variable 2 Initialization of array 2 Input of array 3 Sorting of one dimensional array 7 Bubble Sort 8 Selection Sort 10 Passing one dimensional array into function 12 Searching from one dimensional array 13 Liner Search 13 Binary search 14

Pointers to one dimensional array 16 Two dimensional arrays 18 Input/output into/from 2-D array 18 Passing 2-D array into function 20 Pointers to 2-D arrays 26 Array of pointers 27 Points to be noted 29 Exercise 29 CHAPTER NO 6 String String 1 Examples of string data 1 Declaration of string 1 Initialization of string 1 Built in String Functions 6 strlen 7 Passing string to function 7 strlwr 9 strupr 10 strcat 11 strcpy 12 strrev 13 2-D array of characters 16 Declaration of 2-D array of character 16 Initialization of 2-D array 16 Arrays of Pointers to String 21 Advantages of Pointers of 2-D Arrays of Characters 22 Disadvantages of 2-D array of pointers 23 Points to be noted 27 Exercise 27 CHAPTER NO 7 Collection of data types

Structure 1 Structure Definition 1 Declaring structure variables 1 Unnamed structure 2 Initialization of Structure Variable 2 How can we access the members of structure? 3 Input Into Structure Variable 4 Array of structure 5 Features of structure 5 Nested structured 7 Pointer to Structure 8 Structures Can be Passed to Functions Like Ordinary Variables 8 Returning Structure Variable From Function 10 union 12 enum 15 typedef 17 Bit fields 19 What are bits? 20 Units of Digital Storage 20 Bitwise operators 22 One s complement (~) operator 22 Right shift (>>) operator 23 Left shift (<<) operator 25 Bitwise & (AND) operator 26 Uses of bitwise & (AND) operator 26 Bitwise (OR) operator 28 Uses of bitwise (OR) operator 28 Bitwise ^ (XOR) operator 29 Uses of Bitwise ^ (XOR) operator 29 Practical uses of Right Shift >> and Left Shift << Bitwise Operators 30 Points to be Noted 33 Exercise 33 CHAPTER NO 8 Input & Output

Formatted I/O functions 1 Unformatted I/O functions 1 Table of the all I/O functions 2 Console I/O functions 3 Practical use of sprintf() and sscanf() 4 Disk I/O functions 6 Opening a file 7 Different file opening modes 7 Reading character from file 8 Closing the file 8 Detecting end of file 8 Command line arguments 10 I/O of strings to and from files 13 Reading back formatted data from file 15 Standard Pre-Defined Pointers 16 Redirection of Input and Output in DOS Kingdom 21 Redirection of output 21 Redirection of Input 22 Redirection of both Input and output 22 Binary I/O functions 23 Use of fread() and fwrite() functions 24 Uses of fseek(), ftell() and rewind() functions 26 Error Handling in Reading/Writing 26 Low Level Disk I/O 27 read() and write() functions 29 Exercise 31 CHAPTER NO 9 Built-in-Functions

Arithmetic functions 1 floor() function and ceil() function 2 fmod() function and modf() function 3 Character Identification functions 4 Searching and Sorting Functions 4 bsearch() function 4 qsort() function 5 File Handling Functions 6 remove () function 6 rename () function 6 Directory Control Functions 7 mkdir() function and rmdir() function 7 system() function 7 Buffer Manipulation Functions 8 memset() function 8 Disk I/O Functions 9 getdisk() and setdisk() functions 9 Memory Allocation Functions 10 malloc () function 11 calloc() function 12 Small Data Models 13 Large Data Models 13 Process Control Functions 14 atexit() function 14 Graphics Functions 15 Time Related Functions 16 getdate() and gettime() functions 16 DOS Interface Functions 17 Miscellaneous Functions 18 random macro 18 randomize macro 18 srand macro 19 All Header Files available in C 20 CHAPTER NO 10 Number System

Binary Number System 1 Octal Number System 1 Decimal Number system 1 Hexadecimal Number System 1 Number System Conversion 2 Conversion of decimal to others 2 Decimal to binary 2 Decimal to Octal 3 Decimal to Hexadecimal 3 Conversion from Others to Decimal 4 Binary to Decimal 4 Octal to Decimal 4 Decimal to Hexadecimal 4 Direct Methods for Conversions 4 Conversion of floating point value 8 Conversion of decimal to others 8 Conversion of Decimal to Binary 8 Conversion of Decimal to Octal 9 Conversion of Decimal to Hexadecimal 9 Conversion from other floating point number system to decimal 9 Conversion of Binary to Decimal 9 Conversion of Octal to Decimal 10 Conversion of Hexadecimal to Decimal 10 Examples of number system conversion 11