Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Data structure and algorithms using c language, Schemes and Mind Maps of Data Structures and Algorithms

DSA using c language balaguru Swami

Typology: Schemes and Mind Maps

2023/2024

Uploaded on 07/11/2024

vansh-12
vansh-12 🇮🇳

1 / 327

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures
Using C
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Data structure and algorithms using c language and more Schemes and Mind Maps Data Structures and Algorithms in PDF only on Docsity!

Data Structures

Using C

About the Author

E Balagurusamy, former Vice Chancellor, Anna University, Chennai and Member, Union Public Service Commission, New Delhi, is currently the Chairman of EBG Foundation, Coimbatore. He is a teacher, trainer, and ! " # Object-Oriented Software Engineering, E-Governance, Technology Management, Business Process Re-engineering, and Total Quality Management. $ % &! ' ( % % ) ( # ( * ( #! — Fundamentals of Computers — Computing Fundamentals and C Programming — Programming in ANSI C, 6e — Programming in Java, 4e — Programming in BASIC, 3e — Programming in C#, 3e — Numerical Methods — Reliability Engineering $ % &! ' (

      • /

( ( 0 & ) / 12! 0 # 5! 7 & 889 98: Data Structures Using C ; % < 198=! ( 0 & ) / No part of this publication may be reproduced or distributed in any form or by any means, electronic, mechanical, % %!! & ( ) & % & % % ( % (! 5 %! ( may not be reproduced for publication. ( 5% ( % (! 0 & ) / '7 8= > ?@I81J?91?J2@ '7 89 > 81J?91?J2? K > Ajay Shukla L ( # > Vibha Mahajan ( L O > Shalini Jha $ % > Smruti Snigdha " > Amiya Mahapatra L > Satinder S. Baveja

P. L. Pandita ; % > Preyoshi Kundu $ 0 L # > Vijay Sarathi % L K > Tina Jajoriya 0 % L; ) > Meenu Raghav 0 L > Rajender P. Ghansela L > Reji Kumar & # ( ( ( 0 & ( ) ( ( & )! 0 & % % (! 0 & ( % (!! & # % ( & 0 & %%

( % % ) ) Q! %% % % ( % ' #%! '*8I9$! 0 7 #! / 5 7 *889 9?1 % ) /! *8=1! ; % 5!! 7 ; ) > ) / "$/R;";T"'WWW

Contents

C o n t e n t s

    1. C Recap – I Preface ix
    • 8 1 ( )
    • 8 = T) ) & ;
    • 8 2 %
    • 8 J ;
    • 8 : K (
    • 8 @ %
    • 8 I % T % T%
    • 8? T% 5%
    • Summary
    • Review Questions
    • Programming Exercises
    1. C Recap – II
    • 1 1 ;
    • 1 = $
    • 1 J ' * X
    • 1 : Y * X
    • 1 @
    • 1 I Y
    • 1?
    • Summary
    • Review Questions
    • Programming Exercises
    1. Introduction to Algorithm and Data Structures
    • =
    • = 1 $
    • 3.3 Asymptotic Notation
    • =
    • = J %
    • = : T%
    • Summary
    • Key Terms
    • Multiple-Choice Questions vi
    • Review Questions
    • Answers to Multiple-Choice Questions
    1. Arrays
    • 2 1 % $
    • 2 = " % T * $
    • 2 2 $ )
    • 2 J
    • 2 :
    • 2 @ " % * $
    • 2 I " Z Y & * $
    • 2? 5 T%
    • Summary
    • Key Terms
    • Solved Problems
    • Multiple-Choice Questions
    • Review Questions
    • Programming Exercises
    • Answers to Multiple-Choice Questions
    1. Linked Lists
    • J
    • J 1 / # / [ ' ; %
    • J = / # / %
    • J 2 % / # /
    • J J ; / # /
    • J : ( / # /
    • Solved Problems
    • Summary
    • Key Terms
    • Multiple-Choice Questions
    • Review Questions
    • Programming Exercises
    • Answers to Multiple-Choice Questions
    1. Stacks
    • :
    • : 1 #
    • : = # T%
    • : 2 # %
    • Solved Problems
    • Summary vii
    • Key Terms
    • Multiple-Choice Questions
    • Review Questions
    • Programming Exercises
    • Answers to Multiple-Choice Questions
    1. Queues
    • @
    • @ 1 \ L ' ; %
    • @ = \ T%
    • @ 2 \ %
    • @ J ; \
    • @ : \
    • @ @ ( * \
    • Solved Problems
    • Summary
    • Key Terms
    • Multiple-Choice Questions
    • Review Questions
    • Programming Exercises
    • Answers to Multiple-Choice Questions
    1. Trees
    • I
    • I 1 ' ; %
    • I = '
    • I 2 ' " %
    • I J ' )
    • I : '
    • I @ K
    • Summary
    • Key Terms
    • Multiple-Choice Questions
    • Review Questions
    • Programming Exercises
    • Answers to Multiple-Choice Questions
    1. Graphs -? -? 1 ' ; % -? = 0 % -? 2 0 % % -? J $ -? : 0 % ) viii
    • Summary
    • Key Terms
    • Multiple-Choice Questions
    • Review Questions
    • Programming Exercises
    • Answers to Multiple-Choice Questions
    1. Sorting and Searching
    • 89 1 Q
    • 89 = Q
    • Solved Problems
    • Summary
    • Key Terms
    • Multiple-Choice Questions
    • Review Questions
    • Programming Exercises
    • Answers to Multiple-Choice Questions
    1. Application of Data Structures
    • 88 1 $%% #
    • 88 = $%% \
    • 88 2 $%% / # /
    • 88 J $%%
    • 88 : $%% 0 %
    • Summary
  • Index

x In-chapter Features �X #! pseudocodes, flowcharts % % Z on a point or help teach a concept. Commands in bold draw students’ attention to a particular section in the % Other Significant Features 7! % ; #% % ) 5 ) ) & %% related to the content of the chapter.

xi Chapter-end Features Summary reviews the concepts while a list

% ) (

students need to understand the concepts presented in the chapter. Students can assess

& ( & (

) & Q! % 5 % * Q P r e f a c e 260 Data Structures Using C 9.3 As per modified Warshall’s algorithm, which of the following is the correct relation for computing the shortest path between two vertices in a graph? (a) (b) SPSPi, j = Minimum of (SPi, j, SPi, k + SPk, j) (c) SPi, ji, j^ = Maximum of (SP= Minimum of (SPi, ki, j, SP, SPi, kk, j^ + SP+ SPk, ji, j)) 9.4 (d)The number of edges incident on a vertex is referred as ____________.^ None of the above (a) (b) DegreeIndegree (c) Order 9.5 (d)Identify the BFS path for the following graph:^ Outdegree (a) (b) 1–2–3–4–6–51–4–3–2–6– (c) 1–2–3–4–5– (d) None of the above & ' ($ ) %) 9.1 What is a graph? Explain with an example. 9.2 List and explain any five key terms associated with graphs. 9.3 9.4 What are the different methods of representing a graph?What is an adjacency matrix? How can you derive a path matrix from an adjacency matrix? 9.5 9.6 Explain adjacency list implementation of a graph with the help of an example.What is the significance of computing the shortest path in a graph? Explain with the help of an example. 9.7 Write the modified Warshall’s algorithm for computing the shortest path between two nodes of a graph. 9.8 9.9 What is BFS? Explain with the help of an example.What is DFS? Explain with the help of an example. " %! * ) ) 9.1 Write a C function to deduce the adjacency matrix for a given directed graph G. 9.2 Write a C function that takes as input the adjacency matrix and applies Warshall’s algorithm to generate the corresponding path matrix. 9.3 Write a C program to implement a 3-node directed graph using adjacency list. 9.4 Write a C function that takes as input the path matrix and applies the shortest path algorithm to generate the corresponding shortest path matrix. %)' ) $ # + * ($ ) %) 9.1 (b) 9.2 (a) 9.3 (a) 9.4 (b) 9.5 (c) Graphs 259 C a # % $ " , © A graph G(V, E) consists of the following elements: o o A set V of vertices or nodes where V = {vA set E of edges also called arcs where E = {e 1 , v 2 , v 3 , ...., vn} © A graph can be implemented in three ways: adjacency matrix, path matrix, and adjacency list.^1 , e^2 , e^3 , ...., en} © Adjacency matrix and path matrix are the sequential methods of representing a graph. Adjacency matrix signifies whether there is an edge between any two vertices of the graph. Path matrix signifies whether there is a path between any two vertices of the graph. © Adjacency list is a linked representation of a graph. It consists of a list of graph nodes with each node itself consisting of a linked list of its neighboring nodes. © Breadth First Search or BFS is the method of traversing a graph in such a manner that all the vertices at a particular level are visited first before proceeding onto the next level. © Depth First Search or DFS is the method of traversing a graph in such a manner that all the vertices in a given path (starting from the first node) are visited first before proceeding onto the next path.

  • , ) © Weighted graph It signifies that all the edges of the graph are assigned an integer number called © weight.Directed It signifies that each edge of the graph is a pointed arrow that points from one vertex to the other. © Adjacency matrix It is an N ¥ N matrix containing 1s for all the direct edges of the graph and containing 0s for all the non-edges. © Path matrix It is an N ¥ N matrix containing 1s for all the existing paths in a graph and containing © 0s otherwise.Adjacency list It a list of graph nodes with each node itself consisting of a linked list of its neighboring nodes. $ # + * ($ ) %) 9.1 Which of the following is not true for graph? (a) (b) It is a set of vertices and edges.All of its vertices are reachable from any other vertex (c) It can be represented with the help of an N ¥ N matrix. (d) All of the above are true 9.2 As per Warshall’s method, which of the following is the correct relation for computing the path matrix? (a) P (b) Pi, ji, j^ = P= Pi, ji, j^ OR ( PAND ( Pi, k^ i, kAND P OR Pk, jk, j)) (c) Pi, j = Pi, k AND ( Pk, j OR Pi, j) (d) None of the above

Chapter Organization

( # Z 88 %! & 5% % # $! #! ! /

Chapters 1 and 2 % ) Q # % ; % Chapter 3 * % % ) ( % * tures. Chapter 4 ) %!! 5% & % Chapter 5 5% %

& ) Chapters 6 and 7 elucidates the restricted data structures,

Q % % (

% %% Chapters 8 and 9

C RECAP– I 1 1.1 Introduction 1.2 Introduction to Problem Solving 1.2.1 Algorithms 1.2.2 Flowcharts 1.2.3 Pseudocode 1.2.4 Problem Solving—Examples 1.3 Overview of C 1.4 Sample Program 1.5 Constants 1.6 Variables 1.7 Data Types 1.8 Input and Output Operations 1.8.1 Input Operations 1.8.2 Output Operations 1.8.3 File Input/Output Operations 1.9 Operators and Expressions 1.9.1 Arithmetic Operators 1.9.2 Assignment Operators 1.9.3 Bitwise Operators 1.9.4 Conditional Operator 1.9.5 Increment and Decrement Operators 1.9.6 Logical Operators 1.9.7 Relational Operators 1.9.8 Special Operators 1.9.9 Precedence of Operators Summary Review Questions Programming Exercises

1 C h a p t e r O u t l i n e

2 Data Structures Using C Before we start exploring the different types of data structures and learn how to implement them to solve real world problems, we must first get ourselves familiar with the basics of problem solving techniques and the C language. Since the early days of programming, problem solving methods, like algorithms and flowcharts, and the C programming language have been used to represent and explain the concepts of data structures. They provide a simplified method of learning and implementing the different types of data structures. In this chapter, we will explore the different problem solving techniques. We will also get introduced to the C language and its various programming constructs. If you are already familiar with problem solving techniques and the C language, then you may choose to skip this chapter. A problem is a situation presented to a computer so that its solution can be found. It is associated with a well-defined set of inputs and an output that signifies the problem resolution. Problem solving involves applying a series of methods to a given problem so that its solution can be achieved in a systematic manner. Following are the three problem-solving methods or techniques that are applied in sequence to solve a given problem:

  1. Developing the algorithm
  2. Creating the flowchart
  3. Writing the pseudocode Development of computer program becomes quite easy after we have applied the above techniques to a given problem. Let us understand each of these techniques one by one.

An algorithm is a sequence of steps written in simple English phrases to describe the solution to a given problem. It basically breaks the solution of a problem into a series of simple descriptive steps.

"#!$ An integer num is given as input. Write an algorithm to find out whether num is

even or odd.

% & A number is an even number if it is completely divisible by 2; alternatively, it is an odd

number if it is not completely divisible by 2 and leaves 1 as remainder. Let us apply this logic to develop the algorithm for the given problem. Step 1 Start Step 2 Accept num as the input Step 3 Divide num by 2. If the remainder is 0 then go to Step 4 else go to Step 5 Step 4 Show num as is an even number and go to Step 6 Step 5 Show num as is an odd number Step 6 Stop

4 Data Structures Using C #' Flowchart symbols Name Symbol Description Start and End Represents the start or end of a process. Input and Output Represents an input or output operation. Process Represents a processing step that performs certain computations. Decision Represents a decision-making step. Arrow Represents the flow of control and information. Connector Represents the continuation of steps when a flowchart spans across multiple pages. Additional Symbols Represent advanced operations. Similar to algorithms, flowcharts also have certain key characteristics, advantages and disadvantages associated with them. These are described in Table 1.3. #' + Characteristics Advantages Disadvantages Standard flow of control in a flowchart is either from top to bottom or from left to right. The visual representation of flow of program control is easier to understand. It does not work well for large programs. The flowchart becomes too complex and confusing. Flowchart instructions are simple and concise. It eases the process of actual develop- ment of program code. A slight modification in a flowchart may require significant amount of rework. None of the arrows in a flowchart intersect each other. Flowcharts ease debugging and help in identifying and removing logical errors. The use of graphical elements makes it a little tedious and time consuming to draw a flowchart. A flowchart always begins with a Start symbol and ends with a Stop symbol. It acts as documentation for program flow. At times, excessive use of connectors may become a little more confusing.

C RECAP – I 5

C

a $ &

Pseudocode takes the algorithm one step further. It represents the problem solution with the help of generic syntax and normal English phrases. It makes the development of program code easier by utilizing high-level programming constructs for representing conditional and looping scenarios.

"#!$ + Using pseudocode, solve the problem given in Example 1.1.
% & The logic to solve the problem given in Example 1.1 remains the same. Let us apply the

logic to write the corresponding pseudocode. BEGIN DEFINE: Integer num DISPLAY: “Enter a number: “ READ: num IF: num%2= DISPLAY: “‘num’ is an even number” ELSE DISPLAY: “‘num’ is an odd number” END IF END As we can see in the above pseudocode, the solution to the given problem has been described with the help of various labels each representing certain programming action. Each pseudocode starts and ends with a BEGIN and END statement, respectively. Table 1.4 lists the key characteristics, advantages and disadvantages of pseudocodes. #' / Characteristics, advantages, and disadvantages of pseudocodes Characteristics Advantages Disadvantages It represents each solution step with a simple and concise action statement. It is simple and easy to understand. It lacks visual representation of programming logic like flowcharts. It uses generic labels to describe programming constructs. Converting a pseudocode into actual program code is a lot easier. There are no standard specifications for developing pseudocodes. It completely solves the given problem. It is easy to modify and update pseudocodes as compared to algorithms and flowcharts. Unlike flowcharts, it may become a little difficult to understand the flow of program control in a pseudocode. It starts with BEGIN statement and ends with an END statement. It is quite flexible and does not require the programmer to memorize any special symbols. A pseudocode written for a complex problem may become quite lengthy.

Table 1.5 shows the application of problem solving techniques to solve common problems.

C RECAP – I 7

C

a $ & '! Apply the problem solving techniques to find the roots of a quadratic equation. Algorithm Pseudocode Step 1 - Start Step 2 – Accept three numbers (a, b, c) from the user for the quadratic equation ax^2 + bx + c Step 3 – Calculate root1=((-1) b+sqrt(bb-4ac))/2a Step 4 – Calculate root2=((-1)b- sqrt(bb-4ac))/2a Step 5 – Display the computed roots of the quadratic equation Step 6 - Stop BEGIN DEFINE: Integer a, b, c DEFINE: Real root1, root DISPLAY: “Enter the values of a, b and c for the quadratic equation ax^2

  • bx + c: “ READ: a, b, c COMPUTE: root1=((-1)b+sqrt(bb- 4ac))/2a COMPUTE: root2=((-1)b-sqrt(bb- 4ac))/2a DISPLAY: “The roots of the quadratic equation are ‘root1’ and ‘root2’ END FLOWCHART

8 Data Structures Using C '! + Apply the problem solving techniques to determine whether the given year is a leap year or not. Algorithm Pseudocode Step 1 - Start Step 2 – Accept an year value from the user (year) Step 3 – If remainder of year value divided by 4 (year%4) is 0 then go to Step 4 else go to Step 5 Step 4 – Display “‘year’ is a leap year” and go to Step 6 Step 5 – Display “‘year’ is not a leap year”] Step 6 - Stop BEGIN DEFINE: Integer year DISPLAY: “Enter the year value: ” READ: year IF: year%4= DISPLAY: “‘year’ is a leap year” ELSE DISPLAY: “‘year’ is not a leap year” END IF END FLOWCHART