1 / 123

Data_structure using C

Basic concepts of Data_structure using C

Adisesha
Download Presentation

Data_structure using C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structures using C Prof. K ADISESHA Prof. K ADISESHA (Ph. D) (Ph. D)

  2. 2 Data Structures Data Structures Introduction Types of Data structures Please bring to class each day Arrays Stacks and Queues Linked lists

  3. 3 Introduction Data Structures: Data ➢ Data is a collection of facts, numbers, letters or symbols that the computer process into meaningful information. Data structure ➢ Data structure is representation of the logical relationship existing between individual elements of data. ➢ Data structure is a specialized format for organizing and storing data in memory that considers not only the elements stored but also their relationship to each other. Prof. K. Adisesha (Ph. D)

  4. 4 Introduction Why to Learn Data Structure: As applications are getting complex and data rich, there are three common problems that applications face now-a-days ➢ Data Search − Consider an inventory of 1 million(106) items of a store. If the application is to search an item, it has to search an item in 1 million(106) items every time slowing down the search. As data grows, search will become slower. ➢ Processor speed − Processor speed although being very high, falls limited if the data grows to billion records. ➢ Multiple requests − As thousands of users can search data simultaneously on a web server, even the fast server fails while searching the data.. Prof. K. Adisesha (Ph. D)

  5. 5 Introduction Data Structures: Data structure affects the design of both structural & functional aspects of a program. ➢ Aalgorithm is a step by step procedure to solve a particular function. ➢ Program=algorithm + Data Structure Prof. K. Adisesha (Ph. D)

  6. 6 Introduction Applications of Data Structure and Algorithms: Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a certain order to get the desired output. Algorithms are generally created independent of underlying languages. ➢ From the data structure point of view, following are some important categories of algorithms. ❖ Search − Algorithm to search an item in a data structure. ❖ Sort − Algorithm to sort items in a certain order. ❖ Insert − Algorithm to insert item in a data structure. ❖ Update − Algorithm to update an existing item in a data structure. ❖ Delete − Algorithm to delete an existing item from a data structure. Prof. K. Adisesha (Ph. D)

  7. 7 Introduction Characteristics of an Algorithm: An algorithm should have the following characteristics −. ➢ Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or phases), and their inputs/outputs should be clear and must lead to only one meaning. ➢ Input − An algorithm should have 0 or more well-defined inputs. ➢ Output − An algorithm should have 1 or more well-defined outputs, and should match the desired output. ➢ Finiteness − Algorithms must terminate after a finite number of steps. ➢ Feasibility − Should be feasible with the available resources. ➢ Independent − An algorithm should have step-by-step directions, which should be independent of any programming code. Prof. K. Adisesha (Ph. D)

  8. 8 Introduction Characteristics of a Data Structure: Data Structure is a systematic way to organize data in order to use it efficiently. Following terms are the Characteristics of a data structure. ➢ Correctness − Data structure implementation should implement its interface correctly. ➢ Time Complexity − Running time or the execution time of operations of data structure must be as small as possible. ➢ Space Complexity − Memory usage of a data structure operation should be as little as possible. Prof. K. Adisesha (Ph. D)

  9. 9 Introduction Algorithm Analysis: Efficiency of an algorithm can be analyzed at two different stages, before implementation and after implementation. They are the following . ➢ A Priori Analysis − This is a theoretical analysis of an algorithm. Efficiency of an algorithm is measured by assuming factors, like processor speed, are constant and have no effect on the implementation. ➢ A Posterior Analysis − This is an empirical analysis of an algorithm. The selected algorithm is implemented using programming language. In this analysis, actual statistics like running time and space required, are collected. Prof. K. Adisesha (Ph. D)

  10. 10 Introduction Algorithm Complexity: The complexity of an algorithm represents the amount of memory space and required by the algorithm in its life cycle. ➢ Space complexity − Space complexity of an algorithm represents the amount of memory time space required by the algorithm in its life cycle.. ➢ Time complexity − Time complexity of an algorithm represents the amount of time required by the algorithm to run to completion. Prof. K. Adisesha (Ph. D)

  11. 11 Introduction Asymptotic Analysis of an algorithm: Asymptotic analysis of an algorithm refers to Asymptotic analysis refers to computing the running time of any operation in mathematical units of computation of its run-time performance. ➢ The Asymptotic analysis of an algorithm falls under three types: ❖ Best Case − Minimum time required for program execution. ❖ Average Case − Average time required for program execution. ❖ Worst Case − Maximum time required for program execution. Prof. K. Adisesha (Ph. D)

  12. 12 Introduction Asymptotic Notations of an algorithm: Following are the commonly used asymptotic notations to calculate the running time complexity of an algorithm.. ➢ The Asymptotic Notations of an algorithm falls under three types: ❖ Big Oh Notation, Ο− It measures the worst case time complexity. ❖ Omega Notation, Ω− It measures the best case time complexity. ❖ Theta Notation, θ− It measures the Average case time complexity. Prof. K. Adisesha (Ph. D)

  13. 13 Introduction Big Oh Notation, ( Ο )of an algorithm: The notation Ο(n) is the formal way to express the upper bound of an algorithm's running time. ➢ It measures the worst case time complexity or the longest amount of time an algorithm can possibly take to complete. ▪ For example, for a function f(n) ▪ It is represented as follows ▪ Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that f(n) ≤ c.g(n) for all n > n0. } Prof. K. Adisesha (Ph. D)

  14. 14 Introduction Big Oh Notation, ( Ο )of an algorithm: The notation Ο(n) is the formal way to express the upper bound of an algorithm's running time. Prof. K. Adisesha (Ph. D)

  15. 15 Introduction Omega Notation, Ω of an algorithm: The notation Ω(n) is the formal way to express the lower bound of an algorithm's running time. It measures the best case time complexity or the best amount of time an algorithm can possibly take to complete • For example, for a function f(n) • It is represented as follows • Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that g(n) ≤ c.f(n) for all n > n0. } Prof. K. Adisesha (Ph. D)

  16. 16 Introduction Theta Notation, θ of an algorithm: The notation θ(n) is the formal way to express both the lower bound and the upper bound of an algorithm's running time. ▪ For example, for a function f(n) ▪ It is represented as follows ▪ θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all n > n0. } Prof. K. Adisesha (Ph. D)

  17. 17 Introduction Common Asymptotic Notations: Following is a list of some common asymptotic notations. ❖ constant − Ο(1) ❖ logarithmic− Ο(log n) ❖ linear − Ο(n) ❖ n log n − Ο(n log n) ❖ quadratic − Ο(n2) ❖ cubic − Ο(n3) ❖ polynomial − nΟ(1) ❖ exponential− 2Ο(n) Prof. K. Adisesha (Ph. D)

  18. 18 Introduction Common Asymptotic Notations: Algorithmic Examples of Memory Footprint Analysis: The algorithms with examples are classified from the best-to-worst performance (Space Complexity) based on the worst- case scenarios are mentioned below: . Prof. K. Adisesha (Ph. D)

  19. 19 Introduction Basic Terminology: The basic terminologies which are usually used to compare various data structure's are: ➢ Data − Data are values or set of values. ➢ Data Item − Data item refers to single unit of values. ➢ Group Items − Data items that are divided into sub items are called as Group Items. ➢ Elementary Items − Data items that cannot be divided are called as Elementary Items. ➢ Attribute and Entity − An entity is that which contains certain attributes or properties, which may be assigned values. ➢ Entity Set − Entities of similar attributes form an entity set. ➢ Field − Field is a single elementary unit of information representing an attribute of an entity. ➢ Record − Record is a collection of field values of a given entity. ➢ File − File is a collection of records of the entities in a given entity set. Prof. K. Adisesha (Ph. D)

  20. 20 Data Structures Data Type: Data type is a way to classify various types of data which determines the values that can be used with the corresponding type of data, the type of operations that can be performed on the corresponding type of data. There are two data types: ➢ Built-in Data Type: Those data types for which a language has built-in support are known as Built-in Data type. ➢ Derived Data Type: Those data types which are implementation independent as they can be implemented in one or the other way are known as derived data types. Prof. K. Adisesha (Ph. D)

  21. 21 Data Structures Classification of Data Structure using C: Data structure are normally divided into two broad categories: ➢ Primitive Data Structure ➢ Non-Primitive Data Structure Prof. K. Adisesha (Ph. D)

  22. 22 Data Structures Primitive Data Structure: Data structures that are directly operated upon the machine-level instructions are known as primitive data structures: ➢ There are basic structures and directly operated upon by the machine instructions. ➢ The Data structures that fall in this category are. ❖ Integer ❖ Floating-point number ❖ Character constants ❖ string constants ❖ pointers etc., Prof. K. Adisesha (Ph. D)

  23. 23 Data Structures Primitive Data Structure: Data structures that are directly operated upon the machine-level instructions are known as primitive data structures: ➢ The most commonly used operation on data structure are broadly categorized into following types: ❖ Create ❖ Selection ❖ Updating ❖ Destroy or Delete Prof. K. Adisesha (Ph. D)

  24. 24 Data Structures Non-Primitive Data Structure: The Data structures that are derived from the primitive data structures are called Non- primitive data structure: ➢ There are more sophisticated data structures ➢ The non-primitive data structures emphasize on structuring of a group of homogeneous (same type) or heterogeneous (different type) data items: ❖ Linear Data structures ❖ Non-Linear Data structures Prof. K. Adisesha (Ph. D)

  25. 25 Data Structures Non-Primitive Data Structure: Linear Data structures Linear Data structures are kind of data structure that has homogeneous elements. ➢ The data structure in which elements are in a sequence and form a liner series. ➢ Linear data structures are very easy to implement, since the memory of the computer is also organized in a linear fashion. ➢ Some commonly used linear data structures are: ❖ Stack ❖ Queue ❖ Linked Lists Prof. K. Adisesha (Ph. D)

  26. 26 Data Structures Non-Primitive Data Structure: Non-Linear Data structures A Non-Linear Data structures is a data structure in which data item is connected to several other data items. ➢ Non-Linear data structure may exhibit either a hierarchical relationship or parent child relationship. ➢ The data elements are not arranged in a sequential structure. ➢ Some commonly used non-linear data structures are: ❖ Trees ❖ Graphs Prof. K. Adisesha (Ph. D)

  27. 27 Data Structures Non-Primitive Data Structure: The most commonly used operation on data structure are broadly categorized into following types: ❖ Traversal ❖ Insertion ❖ Selection ❖ Searching ❖ Sorting ❖ Merging ❖ Destroy or Delete Prof. K. Adisesha (Ph. D)

  28. 28 Data Structures Differences between Data Structure: The most commonly used differences between on data structure are broadly categorized into following types: ➢ A primitive data structure is generally a basic structure that is usually built into the language, such as an integer, a float etc.,. ➢ A non-primitive data structure is built out of primitive data structures linked together in meaningful ways, such as a or a Strings, Arrays, linked-list, binary search tree, AVL Tree, graph etc. Prof. K. Adisesha (Ph. D)

  29. 29 Data Structures Abstract Datatype: The abstract datatype is special kind of datatype, whose behavior is defined by a set of values and set of operations. ➢ The keyword “Abstract” is used as we can use these datatypes, we can perform different operations. ➢ But how those operations are working that is totally hidden from the user. ➢ TheADT is made of with primitive datatypes, but operation logics are hidden. ➢ Some examples ofADT are String, Stack, Queue, List etc.. Prof. K. Adisesha (Ph. D)

  30. 30 Data Structures Strings: Strings are actually one-dimensional array of characters terminated by a null character '\0’. Thus a null-terminated string contains the characters that comprise the string followed by a null.: ➢ To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "Hello.“ char Str[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; ➢ Following is the memory presentation of the above defined string in C/C++ char Str[] = "Hello"; Prof. K. Adisesha (Ph. D)

  31. 31 Data Structures Strings: C supports a wide range of functions that manipulate null-terminated strings : Function Operation Copies string s2 into string s1. strcpy(s1, s2); Concatenates string s2 onto the end of string s1. strcat(s1, s2); Returns the length of string s1. strlen(s1); Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2. strcmp(s1, s2); Returns a pointer to the first occurrence of character ch in string s1 strchr(s1, ch); Returns a pointer to the first occurrence of string s2 in string s1. strstr(s1, s2); Prof. K. Adisesha (Ph. D)

  32. 32 Data Structures Strings: The following example uses some of the String manipulation functions: Prof. K. Adisesha (Ph. D)

  33. 33 Data Structures Strings: The following C Program to perform some basic operations on string. #include <stdio.h> #include <conio.h> #include <string.h> printf ( "\nString s1: %s", s1 ) ; len = xstrlen ( s1 ) ; printf ( "\nlength of the string s1: %d", len ) ; printf ( "\nString s2: %s", s2 ) ; xstrcpy ( s3, s1 ) ; printf ( "\nString s3 after copying s1 to it: %s", s3 ) ; xstrcat ( s3, s2 ) ; printf ( "\nString s3 after concatenation: %s", s3 ) ; if ( xstrcmp ( s1, s2 ) == 0 ) printf ( "\nThe strings s1 and s2 are similar" ) ; else printf ( "\nThe strings s1 and s2 are not similar" ) ; int xstrlen ( char * ) ; void xstrcpy ( char *, char * ) ; void xstrcat ( char *, char * ) ; int xstrcmp ( char *, char * ) ; void show ( char * ) ; void main( ) { char s1[ ] = "Bangalore" ; char s2[ ] = "City" ; char s3[20] ; int len ; getch( ) ; } Prof. K. Adisesha (Ph. D) clrscr( ) ;

  34. 34 Data Structures Strings: The following example uses some of the String manipulation functions: /* finds the length of the string */ int xstrlen ( char *s ) { int l = 0 ; while ( *s ) { l++ ; s++ ; } return l ; } /* copies source string s to the target string t */ void xstrcpy ( char *t, char *s ) { while ( *s ) { *t = *s ; t++ ; s++ ; } *t = '\0' ; } Prof. K. Adisesha (Ph. D)

  35. 35 Data Structures Strings: The following example uses some of the String manipulation functions: /* concatenates the two strings */ void xstrcat ( char *t, char *s ) { while ( *t ) t++ ; while ( *s ) *t++ = *s++ ; *t = '\0' ; } /* compares two strings s and t for equality */ int xstrcmp ( char *s, char *t ) { while ( *s == *t ) { if ( ! ( *s ) ) return 0 ; s++ ; t++ ; } return ( *s - *t ) ; } Prof. K. Adisesha (Ph. D)

  36. 36 Arrays Arrays: An array is defined as a set of finite number of homogeneous elements or same data items: ➢ Following are the important terms to understand the concept ofArray. ❖ Element − Each item stored in an array is called an element. ❖ Index − Each location of an element in an array has a numerical index, which is used to identify the element. ➢ Declaration of array is as follows: ❖ Syntax: Datatype Array_Name [Size]; ❖ Example: int arr[10]; ✓ Where int specifies the data type or type of elements arrays stores. ✓ “arr” is the name of array & the number specified inside the square brackets is the number of elements an array can store, this is also called sized or length of array. Prof. K. Adisesha (Ph. D)

  37. 37 Arrays Arrays: Represent a Linear Array in memory: ➢ The elements of linear array are stored in consecutive memory locations. ➢ It is shown below: intA[5]={23, 4, 6, 15, 5, 7} Prof. K. Adisesha (Ph. D)

  38. 38 Arrays Calculating the length of the array: The elements of array will always be stored in the consecutive (continues) memory location. ➢ The number of elements that can be stored in an array, that is the size of array or its length is given by the following equation: o A[n] is the array size or length of n elements. o The length of the array can be calculated by: L= UB – LB + 1 o To Calculate the address of any element in array: Loc(A[P])=Base(A)+W(P-LB) o Here, UB is the largest Index and LB is the smallest index ➢ Example: If an array A has values 10, 20, 30, 40, 50, stored in location 0,1, 2, 3, 4 the UB = 4 and LB=0 Size of the array L = 4 – 0 + 1 = 5 Prof. K. Adisesha (Ph. D)

  39. 39 Arrays Types of Arrays: The elements of array will always be stored in the consecutive (continues) memory location. The various types of Arrays are: ➢ Single DimensionArray: ❖ Array with one subscript ❖ Ex: intA[i]; ➢ Two DimensionArray ❖ Array with two subscripts (Rows and Column) ❖ Ex: intA[i][j]; ➢ Multi DimensionArray: ❖ Array with Multiple subscripts ❖ Ex: intA[i][j]..[n]; Prof. K. Adisesha (Ph. D)

  40. 40 Arrays Basic operations of Arrays: Some common operation performed on array are: ❖ Traversing ❖ Insertion ❖ Deletion ❖ Searching ❖ Sorting ❖ Merging Prof. K. Adisesha (Ph. D)

  41. 41 Arrays Traversing Arrays: Traversing: It is used to access each data item exactly once so that it can be processed: ➢ We have linear arrayAas below: 1 2 3 4 5 10 20 30 40 50 ➢ Here we will start from beginning and will go till last element and during this process we will access value of each element exactly once as below: A [0] = 10 A [1] = 20 A [2] = 30 A [3] = 40 A [4] = 50 Prof. K. Adisesha (Ph. D)

  42. 42 Arrays Traverse Operation: Following program traverses and prints the elements of an array: Prof. K. Adisesha (Ph. D)

  43. 43 Arrays Insertion into Array: Insertion: It is used to add a new data item in the given collection of data items: ➢ We have linear arrayAas below: 1 2 3 4 5 10 20 50 30 15 ➢ New element to be inserted is 100 and location for insertion is 3. ➢ So shift the elements from 5th location to 3rd location downwards by 1 place. ➢ And then insert 100 at 3rd location Prof. K. Adisesha (Ph. D)

  44. 44 Arrays Insertion into Array: Insertion into Array: ➢ Insertion 100 intoArray at Pos=3 A [0] = 10 A [1] = 20 A [2] = 50 A [3] = 30 A [4] = 15 Prof. K. Adisesha (Ph. D)

  45. 45 Arrays Insertion into Array: Add a new data item in the given array of data: Insertion into Array: A [0] = 1 A [1] = 3 A [2] = 5 A [3] = 7 A [4] = 8 Prof. K. Adisesha (Ph. D)

  46. 46 Arrays Deletion from Array: Deletion: It is used to delete an existing data item from the given collection of data items: ➢ Deletion 30 fromArray at Pos 3 A [0] = 10 A [1] = 20 A [2] = 30 A [3] = 40 A [4] = 50 Prof. K. Adisesha (Ph. D)

  47. 47 Arrays Deletion from Array: A [0] = 1 A [1] = 3 A [2] = 5 A [3] = 7 A [4] = 8 Prof. K. Adisesha (Ph. D)

  48. 48 Arrays Searching in Arrays: Searching: It is used to find out the location of the data item if it exists in the given collection of data items: ➢ E.g. We have linear arrayAas below: 1 2 3 4 5 10 20 50 30 35 ➢ Suppose item to be searched is 35. We will start from beginning and will compare 35 with each element. ➢ This process will continue until element is found or array is finished. ➢ Types of searching Algorithms: ❖ Linear searching ❖ Binary Searching Prof. K. Adisesha (Ph. D)

  49. 49 Arrays Linear search: Linear Searching: Also called Sequential Searching. ➢ It is used to find out the location of the data item if it exists in the given collection of data items. ➢ Example Searching element 33 from the array of elements: Prof. K. Adisesha (Ph. D)

  50. 50 Arrays Linear search: Linear Searching: Also called Sequential Searching. Prof. K. Adisesha (Ph. D)

More Related