
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

































Data Structure: A data structure is a special way of organizing and storing data in a computer so that it can be used efficiently. Array, Linked List, Stack, Queue, Tree, Graph etc. are all data structures that stores the data in a special way so that we can access and use the data efficiently. We have two types of data structures: Classification of Data Structure: Data Structures are normally classified into two categories.
1. Primitive Data type: ‘C’ supports mainly four primitive data types. a. Character Data Type b. Integer Data Type c. Float Data Type d. Double Data Type e. Void Data Type a. Character Data Type: The character data type accepts single character only. Characters are either signed or unsigned. But mostly characters are used an unsigned type. The size of the character data type is 1 byte in the memory. The range of unsigned character is 0to 255. The range of signed are character is – 128 to + 127. Char is the keyword of the character data type. Syntax: char list of variables; E.g. char ch1, ch2, ch3; b. Integer Data Type: An integer type accepts integer values only. It does not contains any real or float values. The range of an integer variable is - 32, 768 to +327,67. int is the keyword for integer data type. In generally 2 bytes of memory is required to store an integer value. Syntax: int list of variables; E. g: int a, b, c; c. Float Data Type: The float data type accepts real values it can contains any floating point values. The range of the floating variable is 3.4E – 38 to 3.4E + 38. Float is the keyword for floating Data type. In generally 4 bytes of memory is required to store an float value with 6 digits of precision. Syntax: float list of variable; E.g. float f1, f2, f3; d. Double Data Type : The double data type accepts large floating value. The range of the double variable is1.7E – 308 to 1.7E + 308. Double is the key word for double data type. In generally 8 bytes of memory is required to store double value. Syntax: double list of variables; E.g. double d, e, f; e. Void Data type: Void is an empty data type that has no value.. The void keyword specifies that the function does not return a value.
type definition
2. DerivedData Types: Derived data types are derived from the primary data types. The derived data types may be used for representing a single or multiple values. These are called secondary data type. The derived data types are arrays, pointers, functions, etc. Array: An array is defined as the collection of similar type of data items stored at contiguous memory locations. Pointer: A pointer is a variable that stores the address of another variable. Function: A function is a group of statements that together perform a task. Every C program has at least one function, which is main(). 3. User Defined Data type: C allows the feature called which allows programmers to define their identifier that would represent an existing data type. There are three such types: Enum: Enumeration is a special data type that consists of integral constants, and each of them is assigned with a specific name. "enum" keyword is used to define the enumerated data type. Structure: It is a package of variables of different types under a single name. This is done to handle data efficiently. "struct" keyword is used to define a structure. Union: These allow storing various data types in the same memory location. Programmers can define a union with different members, but only a single member can contain a value at a given time. Linear Data Structure: If a data structure is organizing the data in sequential order, then that data structure is called as Linear Data Structure. Array : An array is defined as the collection of similar type of data items stored at contiguous memory locations. Stack: Stack is a Linear Data structure in which, insertion and deletion operations are performed at one end only. Stack is also called as Last in First out (LIFO) data structure. The insertion operation is referred to as ‘PUSH’ and deletion operation is referred to as ‘POP’ operation. Queue : The data structure which allow the insertion at one end and Deletion at another end, known as Queue. End at which deletion is occurs is known as FRONT end and another end at which insertion occurs is known as REAR end. Queue is also called as First in First out (FIFO) data structure. Linked list: Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at a contiguous location the elements are linked using pointers.
C Programming Tips: C is one of the most important and widely used of all programming languages. It is a powerful language that can be used not only to build general-purpose applications but also to write “low-level” programs that interact very closely with the computer hardware. Experienced C programmers have all kinds of tricks to make the most of the C language. Here is a list of the top 10 tips for both new and experienced C programmers.
1. Function pointers Sometimes it is useful to store a function in a variable. This isn’t a technique that is normally used in day-to-day programming, but it can be used to increase the modularity of a program by, for example, storing the function to be used in handling an event in the event’s data (or control) structure. 2. Variable-length argument lists Normally you declare a function to take a fixed number of arguments. But it is also possible to define functions capable of taking variable numbers of arguments. The standard C function printf() is a function of this sort. 3. Testing and setting individual bits Manipulating the individual bits of items such as integers is sometimes considered to be a dark 6 art used by advanced programmers. It’s true that setting individual bit values can seem a rather obscure procedure. But it can be useful, and it is a technique that is well worth knowing. 4. Short circuit operators C’s logical operators, && (“and”) and || (“or”), let you chain together conditions when you want to take some action only when all of a set of conditions are true (&&) or when any one set of conditions is true (||). But C also provides the & and | operators. 5. Ternary operators A ternary operation is one that takes three arguments. In C the ternary operator (? can be used as a shorthand way of performing if else tests. 6. Stacks – pushing and popping A “stack” is a last-in, first-out storage system. You can use address arithmetic to add elements to a stack (pushing) or remove elements from the stack (popping). When programmers refer to “the stack”, they typically mean the structure that is used by the C compiler to store local Variables declared inside a function. 7. Copying data Here are three ways of copying data. The first uses the standard C function, memcpy(), which copies n bytes from the src to the dst buffer. 8. Testing for header inclusion C uses “header” (“.h”) files that may contain declarations of functions and constants. A header file may be included in a C code file by importing it using its name between angle brackets when it is one of the headers supplied with your compiler (#include < string.h >) or between double-quotes when it is a header that you have written: (#include “mystring.h”). 9. Parentheses – to use or not to use? A competent and experienced C programmer will neither overuse nor underuse parentheses the round bracket delimiters “(” and “)”. But what exactly is the correct way to use parentheses?
10. Arrays as addresses Programmers who come to C from another language frequently get confused when C treats an array as an address and vice versa. C is correct: an array is just the base address of a block of memory, and the array notation you may have come across when learning a language, such as Java or JavaScript, is merely syntactic sugar. **Algorithm design approaches:
Example: Fractional Knapsack, Activity Selection.
2. Divide and Conquer: The Divide and Conquer strategy involves dividing the problem into sub- problem, recursively solving them, and then recombining them for the final answer. Example: Merge sort, Quicksort. 3. Dynamic Programming: The approach of Dynamic programming is similar to divide and conquer. The difference is that whenever we have recursive function calls with the same result, instead of calling them again we try to store the result in a data structure in the form of a table and retrieve the results from the table. Thus, the overall time complexity is reduced. “Dynamic” means we dynamically decide, whether to call a function or retrieve values from the table. Example: 0 - 1 Knapsack, subset-sum problem. 4. Linear Programming: In Linear Programming, there are inequalities in terms of inputs and maximizing or minimizing some linear functions of inputs. Example: Maximum flow of Directed Graph 5. Reduction(Transform and Conquer): In this method, we solve a difficult problem by transforming it into a known problem for which we have an optimal solution. Basically, the goal is to find a reducing algorithm whose complexity is not dominated by the resulting reduced algorithms. Example: Selection algorithm for finding the median in a list involves first sorting the list and then finding out the middle element in the sorted list. These techniques are also called transform and conquer.
4. Double Data Type : The double data type accepts large floating value. The range of the double variable is1.7E – 308 to 1.7E + 308. Double is the key word for double data type. In generally 8 bytes of memory is required to store double value. Syntax: double list of variables; E.g. double d, e, f; Abstract Data Type: Abstract Data type (ADT) is a type (or class) for objects whose behaviour is defined by a set of value and a set of operations. The definition of ADT only mentions what operations are to be performed but not how these operations will be implemented. It does not specify how data will be organized in memory and what algorithms will be used for implementing the operations. It is called “abstract” because it gives an implementation-independent view. The process of providing only the essentials and hiding the details is known as abstraction. The user of data type does not need to know how that data type is implemented, for example, we have been using Primitive values like int, float, and char data types only with the knowledge that these data type can operate and be performed on without any idea of how they are implemented. So a user only needs to know what a data type can do, but not how it will be implemented. Think of ADT as a black box which hides the inner structure and design of the data type. Now we’ll define three ADTs namely List ADT, Stack ADT, Queue ADT.
a) Recursion: The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function. The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function. Otherwise it will go into an infinite loop. Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc. b) Algorithm Analysis: A complete analysis of the running time of an algorithm involves the following steps: Implement the algorithm completely. Determine the time required for each basic operation. Identify unknown quantities that can be used to describe the frequency of execution of the basic operations. Develop a realistic model for the input to the program. Analyze the unknown quantities, assuming the modelled input. Calculate the total running time by multiplying the time by the frequency for each operation, then adding all the products. Steps to design an algorithm Step 1: Obtain a description of the problem. This step is much more difficult than it appears. Step 2: Analyze the problem. Step 3: Develop a high-level algorithm. Step 4: Refine the algorithm by adding more detail. Step 5: Review the algorithm. void recursion() { recursion(); /* function calls itself */ } int main() { recursion(); }
Structure Programming Approach: Structured Programming Approach , as the word suggests, can be defined as a programming approach in which the program is made as a single structure. It means that the code will execute the instruction by instruction one after the other. It doesn’t support the possibility of jumping from one instruction to some other with the help of any statement like GOTO, etc. Therefore, the instructions in this approach will be executed in a serial and structured manner. The languages that support Structured programming approach are: C C++ Java C# ..etc. On the contrary, in the Assembly languages like Microprocessor 8085, etc., the statements do not get executed in a structured manner. It allows jump statements like GOTO. So the program flow might be random. The structured program mainly consists of three types of elements: Selection Statements Sequence Statements Iteration Statements The structured program consists of well-structured and separated modules. But the entry and exit in a structured program is a single-time event. It means that the program uses single- entry and single-exit elements. Therefore a structured program is well maintained, neat and clean program. This is the reason why the Structured Programming Approach is well accepted in the programming world. Refinement stages: Refinement is the idea that software is developed by moving through the levels of abstraction, beginning at higher levels and, incrementally refining the software through each level of abstraction, providing more detail at each increment. At higher levels, the software is merely its design models; at lower levels there will be some code; at the lowest level the software has been completely developed. Software engineering: Software engineering is a detailed study of engineering to the design, development and maintenance of software. Software engineering was introduced to address the issues of low-quality
software projects. Problems arise when a software generally exceeds timelines, budgets, and reduced levels of quality. Complexity: The purpose of Complexity is to report important advances in the scientific study of complex systems. Complex systems are characterized by interactions between their components that produce new information — present in neither the initial nor boundary conditions — which limit their predictability. Big ,,O”Notation: Big O notation is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity.
Example Code: int marks [] = { 89, 90, 76, 78, 98, 86 } ; char studentName [] = "btechsmartclass" ; In the above example declaration, size of the array 'marks' is 6 and the size of the array 'studentName' is 16. This is because in case of character array, compiler stores one extra character called \ 0 (NULL) at the end. Accessing Elements of Single Dimensional Array In c programming language, to access the elements of single dimensional array we use array name followed by index value of the element that to be accessed. Here the index value must be enclosed in square braces. Index value of an element in an array is the reference number given to each element at the time of memory allocation. The index value of single dimensional array starts with zero (0) for first element and incremented by one for each element. The index value in an array is also called as subscript or indices. We use the following general syntax to access individual elements of single dimensional array... Syntax: arrayName [ indexValue ] Example Code: marks [2] = 99 ; In the above statement, the third element of 'marks' array is assigned with value '99'.
2. Two Dimensional Array: The 2 - D arrays are used to store data in the form of table. We also use 2 - D arrays to create mathematical matrices. Declaration of Two Dimensional Array We use the following general syntax for declaring a two dimensional array... Syntax: datatype arrayName [ row Size ] [ column Size ] ; Example Code: int matrixA [2][3] ; The above declaration of two dimensional array reserves 6 continuous memory locations of 2 bytes each in the form of 2 rows and 3 columns. Initialization of Two Dimensional Array We use the following general syntax for declaring and initializing a two dimensional array with specific number of rows and coloumns with initial values. datatype arrayName [rows][colmns] = {{r1c1value, r1c2value, ...},{r2c1, r2c2,...}...} ; Example Code: int matrix A [2][3] = { {1, 2, 3},{4, 5, 6} } ; The above declaration of two-dimensional array reserves 6 contiguous memory locations of 2 bytes each in the form of 2 rows and 3 columns. And the first row is initialized with values 1, 2 & 3 and second row is initialized with values 4, 5 & 6. We can also initialize as follows...
Example Code int matrix_A [2][3] = { {1, 2, 3}, {4, 5, 6} } ; Accessing Individual Elements of Two Dimensional Array In a c programming language, to access elements of a two-dimensional array we use array name followed by row index value and column index value of the element that to be accessed. Here the row and column index values must be enclosed in separate square braces. In case of the two-dimensional array the compiler assigns separate index values for rows and columns. We use the following general syntax to access the individual elements of a two-dimensional array... Syntax: arrayName [ rowIndex ] [ columnIndex ] Example Code: matrix A [0][1] = 10 ; In the above statement, the element with row index 0 and column index 1 of matrix A array is assigned with value 10. One Dimensional Array with an example: Single Dimensional Array (or) One Dimensional Array In c programming language, single dimensional arrays are used to store list of values of same data type. In other words, single dimensional arrays are used to store a row of values. In single dimensional array, data is stored in linear form. Single dimensional arrays are also called as one-dimensional arrays, Linear Arrays or simply 1 - D Arrays. Declaration of Single Dimensional Array We use the following general syntax for declaring a single dimensional array... Syntax : datatype arrayName [ size ] ; Example Code: int rollNumbers [60] ; The above declaration of single dimensional array reserves 60 continuous memory locations of 2 bytes each with the name roll Numbers and tell the compiler to allow only integer values into those memory locations. Initialization of Single Dimensional Array We use the following general syntax for declaring and initializing a single dimensional array with size and initial values.
Linked list: When we want to work with an unknown number of data values, we use a linked list data structure to organize that data. The linked list is a linear data structure that contains a sequence of elements such that each element links to its next element in the sequence. Each element in a linked list is called "Node". Types of linked list:
“Double linked list is a sequence of elements in which every element has links to its previous element and next element in the sequence.” In a double linked list, every node has a link to its previous node and next node. So, we can traverse forward by using the next field and can traverse backward by using the previous field. Every node in a double linked list contains three fields and they are shown in the following figure... Example Important Points to be Remembered In double linked list, the first node must be always pointed by head. Always the previous field of the first node must be NULL. Always the next field of the last node must be NULL.
3. Circular linked list: In single linked list, every node points to its next node in the sequence and the last node points NULL. But in circular linked list, every node points to its next node in the sequence but the last node points to the first node in the list. “A circular linked list is a sequence of elements in which every element has a link to its next element in the sequence and the last element has a link to the first element.” That means circular linked list is similar to the single linked list except that the last node points to the first node in the list Example