×

Close

- Data Structure using C - DS
- Note
- 10 Topics
**63 Views**- 2 Offline Downloads
- Uploaded 2 months ago

Touch here to read

Page-1

Topic:

Data Structures using ‘C’ Data Structures using ‘C’ DATA It is an unorganized set of values or facts. One element of the set is called data item. Data is simply a value or set of values. There are two types of data items elementary and grouped. Elementary Data: This type of data item cannot be subdivided. Ex: employee number, salary Grouped Data: This type of data item can be divided into elementary data item. Ex: Name is a grouped data item as it can be divided into first name, middle name and last name. ATOMIC and COMPOSITE Data (single or group items) Data is simply a value or set of values. Data items that are divided into sub–items are called group items(composite data), for example an employee’s name may be divided into three sub–items, first name, middle and last name but the employee number would normally be treated as a single item(atomic data). Field: The collection of data item is called a field. Record: The collection fields is called a record. File: It is the collection of same types of records. DATA STRUCTURE The organized collection of data is called a Data Structure. The logical or mathematical model of a particular organization of data is called a data structure. The possible ways in which the data items are logically related, define different data structure. Data may be organized in many different ways. There are two considerations for organizing the data in a particular model: 1. The data structure must be rich enough in structure to mirror the actual relationship of the data. 2. The structure should be simple enough that one can effectively process the data when necessary. A data structure is a way to store and organize data in order to facilitate access and modifications. Study of the data structure include the following: 1) To study the logical or mathematical description of the data structure. 2) implementation of structure on a computer’s memory. 3) Quantitative analysis of the structure i.e. space and time analysis of the structure. Space analysis deals with the amount of memory, required to store the entire data structure. Time analysis deals with the amount of time required to process the entire data structure. STORAGE STRUCTURES The representation of a particular data structure in computer memory is called a storage structure. There are many possible data structures. The storage structure representation of data structure in auxillary memory is often called file structure. DATA STRUCTURES OPERATIONS The data appearing in a data structures are processed by means of these operations. Traversing or visiting, Searching, Inserting, Deleting, Sorting, and Merging /* © All the copy rights are reserved to K. L. Narayana */ By: K. L . Narayana 1

Data Structures using ‘C’ ALGORITHMS An algorithm is a set of ordered statements written in ordinary language (like English) to solve a particular specific problem. Charcterstics of Algorithm: An algorithm is a well–defined sequence of computational steps that takes some value, or set of values, as input and produces some value, or set of values as output, for solving a particular problem. Every algorithm must satisfy the following criteria: 1) Input/Output: It must have I/O statement. There are some input data which are externally supplied to the algorithm, and there will be at least one output as a result. 2) Definiteness: There should not be any ambiguity. Each instruction/step of the algorithm must be clear and unambiguous. 3) Finiteness: The statements should be precise enough to be executed within a finite time. The total algorithm should be completed within finite time. 4) Effectiveness: The steps of an algorithm must be sufficiently clear idea that it can be carried out by a person mechanically using a pen and paper. HOW TO DEVELOP AN ALGORITHM In order to develop an algorithm, the problem has to be properly analyzed. The programmer has to understand the problem and come out with a method or logic that can solve the [problem. The logic must be simple and adaptable on a computer. The following steps can be suggested for developing an algorithm. 1) Understand the problem. 2) Identify the output of the problem. 3) Identify the inputs required by the problem to achieve he desired output. 4) Design a logic or process that will produce the required output from the given inputs 5) Test the algorithm for a given set of input data. 6) Repeat steps 1 to 5 till the algorithm produces the desired results. Algorithm to swap (or exchange) two numbers: Step1 Read the values of X and Y. Step2 Save the initial value of X in TEMP Step3 Move the initial value of Y into X. Step4 Move TEMP into Y. Step5 Display the exchanged values of X and Y. Step6 Stop Algorithm to find sum of the digits of a number: Step –1 start Step–2 Read a number n. Step–3 sum=0 Step–4 repeat step –5 & 6 &7 while (n!=0) Step–5 n=n%10 Step–6 sum = sum + n Step–7 n=n/10 Step–8 end of loop Step–9 write sum Step–10 stop. /* © All the copy rights are reserved to K. L. Narayana By: K. L . Narayana 2 */

Data Structures using ‘C’ ALGORITHMIC LANGUAGE (PSEUDOCODE) Although several tools are used to define algorithms, one of the most common is pseudocode. The language in which algorithms are written is called algorithmic language or pseudocodes. Pseudocodes are user’s prepared unambiguous sentence that describes the procedure to solve a particular problem. Pseudocodes are modern alternatives of traditional flow chart. Pseudocodes are instructions, which can be considered as a midway between English and highlevel language bet free from ambiguities of English. PSEUDOCODE Pseudocode is an English–like representation of the algorithm logic. It is part English and part structured code. The English part provides a relaxed syntax that describes what must be done without showing unnecessary details such as error messages. The code part consists of an extended version of the basic algorithmic constructs– sequence, selection and iteration. COMPONENTS OF THE ALGORITHM 1) Algorithm Header: Each algorithm begins with a header that names it, lists its parameters, and describes any preconditions and postconditions. Example: ALGORITHM FACTORIAL(DATA) 2) Purpose, Conditions and Return: The purpose is a short statement about what the algorithm does. The precondition lists any precursor requirements for the parameters. For example, the algorithm that calls the FACTORIAL must pass the DATA as a preconditions. The post condition identifies any action taken and the status of any output parameters. If a value is returned it is identified by a return condition. 3) Statement Numbers: Statements are numbered using an abbreviated decimal notation. 4) Variables: To ensure that the menaing is understood, we use intelligent data names– that is names that describe the meaning of the data. Do not use generic names (eg. count) in programs. Rather add an intelligent qualifier to the generic names for example, studcount instead of count. 5) Statement Constructs: Any algorithm could be written using only three programming constructs: sequence, selection and loops. a. Sequence: Sequence is a one or more statements that do not alter the execution path within an algorithm. b. Selection: A selection statement evaluates a condition and executes zero or more alternatives. The two–way selection is the if statement, and multi–way statement is the switch–case statement. c. Loop: Loop statement itrates a block of code. Ex: while, for and do–while statements Algorithm to find the sum of n numbers of an array: Arraysum(array A,int n) Step1: sum=0; Step2: for i Step3: Step4: 1 to n do sum sum + A[i] end Step5: return sum; By: K. L . Narayana 3

Data Structures using ‘C’ Algorithm: (MATRIX ADDITION) Matrix_addition(A, B, C, m, n) The sum of A and B, Matrices gives the resultant matrix C. Step1: for i 1 to m do Step2: Step3: Step4: Step5: end Step6: EXIT for j 1 to n do C[i][j]=A[i][j]+B[i][j] end Algorithm: (TRANSPOSE OF MATRIX) Matrix_transpose(A, B, m, n) Wher A is the first matrix, B is the second matrix to store the resultant TRANSPOSE matrix, and m is the number of rows and n is the number of columns of the first matrix. Step1: for i Step2: 1 to n for j Step3: do 1 to m do Store all the column elements of the array A in to the array B rows wise. B[i, j] = A[j, i] end end Step4: EXIT Algorithm to multiply two matrices A and B: Matrix_Matmul(A, B, C, rows[A], cos[A], rows[B], cols[B]) Step1: if columns[A] != rows[B] then Step2: print error “Incompatible Dimensions” Step3: else Step4: for i 1 to rows[A] do Step5: for j 1 to columns[B] do Step6: C[i, j] = 0 Step7: for k 1 to columns[A] do Step8: C[i, j] = C[i, j] + A[i, k] * B[k, j] Step9: end Step10: Step11: end end Step12: return C /* © All the copy rights are reserved to K. L. Narayana */ By: K. L . Narayana 4

## Leave your Comments