




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
DSA using c language balaguru Swami
Typology: Schemes and Mind Maps
1 / 327
This page cannot be seen from the preview
Don't miss anything!
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
C o n t e n t s
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.
Chapters 1 and 2 % ) Q # % ; % Chapter 3 * % % ) ( % * tures. Chapter 4 ) %!! 5% & % Chapter 5 5% %
% %% 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
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:
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.
even or 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
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.
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
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
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