O slideshow foi denunciado.
Seu SlideShare está sendo baixado. ×

DS Module 1.pptx

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Próximos SlideShares
ES Module_1.pptx
ES Module_1.pptx
Carregando em…3
×

Confira estes a seguir

1 de 195 Anúncio

Mais Conteúdo rRelacionado

Mais recentes (20)

Anúncio

DS Module 1.pptx

  1. 1. MODULE -1 Introduction to Data Structures
  2. 2. MO D U L E 1 Introduction:  Data Structures  Classifications (Primitive & Non Primitive)  Data structure Operations
  3. 3. MO D U L E 1 Array Operations:  Traversing, Inserting , Deleting , Searching and sorting  Multidimensional Arrays  Polynomials  Sparse Matrices Strings:  Basic Terminology  Storing  Operations and Pattern Matching algorithms.  Programming Examples.
  4. 4. INTRODUCTION  Data may be organized in many different ways. (Data are simply values or sets of values)  The logical or mathematical model of a particular organization of data is called a data structure.  Data Structure is a way of collecting and organizing data in such a way that we can perform operations on these data in an effective way.  Data Structures is about rendering data elements in terms of some relationship, for better organization and storage.
  5. 5. INTRODUCTION  The study of data structures includes the following three steps: 1. Logical or mathematical description of the structure. 2. Implementation of the structure on a computer. 3. Quantitative analysis of the structure which includes determining the amount of memory needed to store the structure and the time required to process the structure.
  6. 6. INTRODUCTION  Example: We have player's name “Virat" and age 32. Here " Virat " is of String data type and 32 is of integer data type.  We can organize this data as a record like Player record. Now we can collect and store player's records in a file or database as a data structure. For example: "Gayle" 30, "Sachin" 31, “Pointing" 33.  In simple language, Data Structures are structures programmed to store ordered data so that various operations can be performed on it easily.
  7. 7. INTRODUCTION
  8. 8. INTRODUCTION Primitive Data Structures  Primitive data types are the data types available in most of the programming languages.  These data types are used to represent single value.  It is a basic data type available in most of the programming language.
  9. 9. INTRODUCTION  Based on the structure and arrangement of data, non- primitive data structures is further classified into 1. Linear Data Structure 2. Non-linear Data Structure  Data structure where data elements are arranged sequentially or linearly where the elements are attached to its previous and next adjacent in what is called a linear data structure.  In linear data structure, single level is involved.
  10. 10. INTRODUCTION  Therefore, we can traverse all the elements in single run only.  Linear data structures are easy to implement because computer memory is arranged in a linear way.  Its examples are array, stack, queue, linked list etc.
  11. 11. INTRODUCTION Arrays:  The array is a type of data structure that stores elements of the same type.  These are the most basic and fundamental data structures.  Data stored in each position of an array is given a positive value called the index of the element.  The index helps in identifying the location of the elements in an array.
  12. 12. INTRODUCTION Stack:  An element may be inserted or deleted at one end which is known as Top of the stack.  The data structure follows the rule of LIFO (Last In- First Out) where the data last added element is removed first.  Push operation - adding an element of data on a stack, pop operation - deleting the data from the stack.
  13. 13. INTRODUCTION Queue  This structure is almost similar to the stack as the data is stored sequentially.  The difference is that the queue data structure follows FIFO which is the rule of First In-First Out where the first added element is to exit the queue first.  Front and rear are the two terms to be used in a queue.
  14. 14. INTRODUCTION Linked list:  Linked lists are the types where the data is stored in the form of nodes which consist of an element of data and a pointer.  The use of the pointer is that it points or directs to the node which is next to the element in the sequence.  The data stored in a linked list might be of any form, strings, numbers, or characters.  Both sorted and unsorted data can be stored in a linked list along with unique or duplicate elements.
  15. 15. INTRODUCTION
  16. 16. INTRODUCTION Non-Linear Data Structure  Data structures where data elements are not arranged sequentially or linearly are called non-linear data structures.  In a non-linear data structure, single level is not involved.  Therefore, we can’t traverse all the elements in single run only.  Non-linear data structures are not easy to implement in comparison to linear data structure.  It utilizes computer memory efficiently in comparison to a linear data structure. Its examples are trees and graphs.
  17. 17. INTRODUCTION Trees:  A tree data structure consists of various nodes linked together.  The structure of a tree is hierarchical that forms a relationship like that of the parent and a child.  The structure of the tree is formed in a way that there is one connection for every parent-child node relationship.  Only one path should exist between the root to a node in the tree.  Various types of trees are present based on their structures like AVL tree, binary tree, binary search tree etc.
  18. 18. INTRODUCTION
  19. 19. INTRODUCTION Graph:  Graphs are those types of non-linear data structures which consist of a definite quantity of vertices and edges.  The vertices or the nodes are involved in storing data and the edges show the vertices relationship.  The difference between a graph to a tree is that in a graph there are no specific rules for the connection of nodes.  Real-life problems like social networks, telephone networks etc. can be represented through the graphs.
  20. 20. INTRODUCTION
  21. 21. INTRODUCTION
  22. 22. INTRODUCTION
  23. 23. INTRODUCTION Data Structures Operations 1. Traversing: Accessing each record/node exactly once so that certain items in the record may be processed. (This accessing and processing is sometimes called “visiting” the record.) 2. Searching: Finding the location of the desired node with a given key value or finding the locations of all such nodes which satisfy one or more conditions. 3. Inserting: Adding a new node/record to the structure. 4. Deleting: Removing a node/record from the structure.
  24. 24. INTRODUCTION The following two operations, which are used in special situations: 1. Sorting: Arranging the records in some logical order (e.g., alphabetically according to some NAME key, or in numerical order according to some NUMBER key, such as social security number or account number) 2. Merging: Combining the records in two different sorted files into a single sorted file.
  25. 25. Abstract Data Type • Abstract Data type (ADT) is a type (or class) for objects whose behaviour is defined by a set of values 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.
  26. 26. Abstract Data Type
  27. 27. List ADT Functions The List ADT Functions is given below: get() – Return an element from the list at any given position. insert() – Insert an element at any position of the list. remove() – Remove the first occurrence of any element from a non- empty list. removeAt() – Remove the element at a specified location from a non-empty list. replace() – Replace an element at any position by another element. size() – Return the number of elements in the list. isEmpty() – Return true if the list is empty, otherwise return false. isFull() – Return true if the list is full, otherwise return false.
  28. 28. Stack ADT • push() – Insert an element at one end of the stack called top. • pop() – Remove and return the element at the top of the stack, if it is not empty. • peek() – Return the element at the top of the stack without removing it, if the stack is not empty. • size() – Return the number of elements in the stack. • isEmpty() – Return true if the stack is empty, otherwise return false. • isFull() – Return true if the stack is full, otherwise return false.
  29. 29. Queue ADT • enqueue() – Insert an element at the end of the queue. • dequeue() – Remove and return the first element of the queue, if the queue is not empty. • peek() – Return the element of the queue without removing it, if the queue is not empty. • size() – Return the number of elements in the queue. • isEmpty() – Return true if the queue is empty, otherwise return false. • isFull() – Return true if the queue is full, otherwise return false.
  30. 30. Features of ADT: • Abstraction: The user does not need to know the implementation of the data structure. • Better Conceptualization: ADT gives us a better conceptualization of the real world. • Robust: The program is robust and has the ability to catch errors.
  31. 31. Algorithm : Complexity: Space – Time Tradeoffs An Algorithm is a well defined list of steps for solving a particular problem. • Time & Space are major measures of the efficiency of an algorithm. • The complexity of algorithm is function which gives the running time or space in terms of the input size. • Choosing best algorithms & data structures involves time-space tradeoff. • By increasing the amount of storing the data can reduce the time needed for processing the data.
  32. 32. Examples for Complexity. Linear Search: • Search each record of the file, one at a time, until finding the given name. • Time required to execute the algorithm is proportional to the no. of comparisons. • The avg no. of comparisons will be equal to n/2 • Therefore complexity of the linear search algorithm is C(n)= n/2. Binary Search: • Compare the name with the name in the middle of the list, this tells which half of the list contains Name. • Continue process till find the name • Complexity of Binary search algorithm is C(n) = log2 n.
  33. 33. Algorithm Notation Algorithm ia a finite step by step list f well defined instructions for solving particular problem.
  34. 34. Steps to write algorithm: 1. Paragarph - tells about problem statement. 2. Identifying no. 3.Steps, Control, Exit 4. Comments 5. Variable Names 6. Assignment Statements 7. Input & Output
  35. 35. Complexity of Algorithms Efficiency of an Algorithm: Depends on Time & Space measures. Time measured by counting the no. of key operations. Space is measured by counting the maximum of memory needed by the algorithm. Space is simply multiple of the data size. time not only depends on input size but also on the particular data. Worst case - Max value of f(n) for any possible input. Aveage case - the expected value f(n).
  36. 36. Asymptotic Notation O big oh - Upper bound of function Big Omega - lower bound theta - Average bound
  37. 37. INTRODUCTION Review of arrays:  AnArray is an ordered set of similar data items.  All the data items of an array are stored in consecutive memory locations.  The data items of an array are of same type and each data items can be accessed using the same name but different index value.  An array is a set of pairs, <index, value > such that each index has a value associated with it.  It can be called as corresponding or a mapping.
  38. 38. INTRODUCTION  Array is a collection of elements of same data type.  The elements are stored sequentially one after the other in memory. Examples: 1. List of employees in an organization. 2. List of products and their cost sold by a store. 3. Test scores of a class of students.  Any element in an array can be accessed by using → name of the array. → position of elementC iH nET thH eAN aC rray.
  39. 39. INTRODUCTION  Each element of the array has its own storage location, which can be referenced like any other variable.  The elements of an array are stored in consecutive storage locations in memory and identified by the name of the array followed by a number in brackets.  These numbers start at 0.  int a[5]={10, 60, 80, 40, 50};  The five elements of a are represented as a[0], a[1], a[2], a[3] and a[4].  So what is the value of a[2]? 80.
  40. 40. INTRODUCTION  The number written in the square brackets 0,1,2,3,4 is called an index or subscript.  The index of all arrays in C are numbered starting with 0 and increase each time by 1.  Consider an array of 5 integers : int n[5] = {35,40,20,57,19};  Here n[0] = 35 , n[1] = 40 , n[2] = 20 , n[3] = 57 and n[4] = 19 n[0] n[1] n[2] n[3] n[4]
  41. 41. INTRODUCTION Rules : 1. The array should be declared with some data type. 2. All the arrays elements should be of same data type. 3. The size of the array is finite and it should be specified at the time of its declaration. 4. Only one element can be added or removed from the array at a time. 5. The index of the first element is always zero. 6. Each element is accessed using the name of the array but with different indexes.
  42. 42. INTRODUCTION memory.  Arrays are classified into three types: 1. Single dimensional Arrays / One dimensional array. 2. Two dimensional Arrays. 3. Multidimensional Arrays.  Single dimensional array is one in which the array variable uses single index and single pair of square brackets to store multiple values of similar data types.  Like any other variable, arrays must be declared before they are used so that the compiler can allocate space for them in
  43. 43. INTRODUCTION datatype variable_name[SIZE];  Here data type can be like int or char or float.  Size is the maximum number of elements that can be stored inside the array.  Examples: float height[50]; int group[10]; char name[10];  After array is declared, the elements needs to be initialised. Otherwise they will containC H gE aT H rbA N agC e values.  The general form of array declaration is:
  44. 44. INTRODUCTION  An array can be initialized at either of the following stages : 1. At compile time. 2. At run time. At compile time : The initialization is done during declaration or anywhere inside the program before running a program.  The general form of initialization of arrays is : Datatype variable_name[SIZE] = { list of values };  Examples: int number[3] = {0, 0, 0} float total[5] = {0.0, 15.75, -10}
  45. 45. INTRODUCTION  The different methods of compile time initialization are: a) Initialization with size. b) Initialization without size. c) Partial initialization. d) Initializing all values zero. Initialization with size:  We can initialize values to all the elements of the array. Syntax: data_type array_name[size]={list of values};  Examples: float temperature[5]={29.5, 30.7, 35.6, 45.7, 19.5};
  46. 46. INTRODUCTION Initialization without size:  Here we don’t have to specify the size of array provided.  We will be initializing the values in beginning itself. Syntax: data_type array_name[ ]={list of values};  Size will be automatically created based on the number of elements.  Examples: int marks[ ]={ 95,35, 67, 87}; float temperature[ ]={29.5, 30.7, 35.6, 45.7, 19.5};
  47. 47. INTRODUCTION Partial initialization:  If we not specify the all the elements in the array, the unspecified elements will be initialized to zero.  Example: int a[5]={10,12,20};  Here a[3] and a[4] will be initialized to zero. Initializing all the elements zero:  If we want to store zero to all the elements in the array then we can do so by initializing all the elements to zero.  Examples: int marks[4]={0}; float temperature[5]={0};  Here all the 4 elements in array marks and all the 5 elements in array temperature will be equal to zero.
  48. 48. INTRODUCTION 2.At run time:  The initialization is done during run time or program execution. This approach is usually applied for initializing large arrays. Example: printf(“Enter the marks”); for(i=0; i<4; i++) { scanf(“ %d”, &marks[i]); }  Every iteration of for loop will help us to fill values into marks array.  (marks[0]=95, marks[1]=35, marks[2]=67, marks[3]=87)
  49. 49. INTRODUCTION CharacterArray:  If the elements of array is made up of characters, the array is said to be character array. Declaration of character array: char name[size]; Example: char message[20]; Using a constant as array size:  The constant can also be used to define the array size.  But the constants must have been defined earlier before the usage of the constants in the array index. #define NUM 150 //Header file to define values. int test[NUM];
  50. 50. INTRODUCTION  If the memory address of list[i] need to compute by the compiler, then the size of the int would get by sizeof (int), then memory address of list[i] is as follows: list[i] = α + i * sizeof (int)  α is base address.
  51. 51. INTRODUCTION MultidimensionalArrays:  Array with two or more indexes are called multidimensional arrays.  Many applications require that data be stored in more than one dimension.  int a[10][10];  int a[10][5][20];  int a[5][5]----[n];
  52. 52. INTRODUCTION
  53. 53. INTRODUCTION Structures:  A Structure is a collection of one or more variables of similar or dissimilar data types, grouped together under a single name.  A structure is a derived data type containing multiple variable of homogeneous or heterogeneous data types.  Example: it can represent set of attributes such as studentname, rollno, marks in a student database.  The structure is defined using the keyword struct followed by an identifier.  This identifier is called struct_tag.
  54. 54. INTRODUCTION struct struct_tag { type var_1; type var_2; ------------ ------------ type var_n; } ;  The word struct is a keyword in C.  The item identified as struct_tag is called a tag/Identifier and identifies the structure later in the program.  Each type1, - - - - - - type n can be any data type in C.
  55. 55. INTRODUCTION  The item var_1, var_2, - - - - - var_n are the members of the structure. struct student { char name[20]; int rollnumber; float marks; };
  56. 56. INTRODUCTION Defining a Structure:  Structures must be defined first for their format that may be used later to declare structure variables.  Consider a book database consisting of bookname, author, number of pages and price. Struct book_bank { char title[20]; char author[15]; int pages; float price; };
  57. 57. INTRODUCTION  The keyword struct declares a structures to hold details of four fields - title, author, pages and price.  These fields are called structure elements members. book_bank is called structure tag. Declaring Structure Variables: or structure  After defining a structure format we have declare variables of that type.  A Structure variable declaration is similar to the declaration of variables of any other data types.
  58. 58. INTRODUCTION  It includes the following elements. 1. The keyword struct 2. The structure tag name. 3. List of variable names separated by commas. 4. Aterminating semicolon. Example : struct bookbank book1, book2, book3; declares book1, book2 and book3 as variables of type struct bookbank.
  59. 59. INTRODUCTION struct bookbank { char title[20]; char author[15]; int pages; float price; }; struct bookbank book1, book2, book3; It can also be done like given below: struct bookbank { char title[20]; char author[15]; int pages; float price; }book1, book2, book3;
  60. 60. INTRODUCTION  The keyword struct declares a structures to hold details of four fields - title, author, pages and price.  These fields are called structure elements members. book_bank is called structure tag. Things to Remember: or structure 1. The template is terminated with a semicolon. 2.Even though the entire definition is considered as a statement, each member is declared independently for its name and type in a separate statement inside the template.
  61. 61. INTRODUCTION Declaring Structure Variables:  After defining a structure format we have declare variables of that type.  A Structure variable declaration is similar to the declaration of variables of any other data types.  It includes the following elements. 1. The keyword struct 2. The structure tag name. 3. List of variable names separated by commas. 4. Aterminating semicolon.
  62. 62. INTRODUCTION CHETHAN C Example : struct bookbank book1, book2, book3; declares book1, book2 and book3 as variables of type struct bookbank. struct bookbank { char title[20]; char author[15]; int pages; float price; }; struct bookbank book1, book2, book3;  It can also be done like given below: struct bookbank { char title[20]; char author[15]; int pages; float price; }book1, book2, book3;
  63. 63. INTRODUCTION Structure Initialization:  Assigning the values to the structure member fields is structure initialization.  The syntax of initializing structure variables is similar to that of arrays i.e. all the elements will be enclosed within braces { } and are separated by commas.  The syntax is: struct struct_tag variable = { v1, v2, v3 ------- vn};  Example: struct student cse = {“Raju”, 18, 87.5};
  64. 64. INTRODUCTION Structure Definition: struct employee { char name[20]; int salary; int id; }; Structure Declaration struct employee emp1, emp2; Structure Initialization: struct employee emp1 = {“Yash”, 20000, 1000}; struct employee emp2 = { “Radhika”, 10050, 200};
  65. 65. INTRODUCTION Rules to be remembered while initializing structure variables: 1. We cannot initialize individual members inside the structure template. 2. The order of values enclosed in braces must match the order of members in the structure definition. 3. It is permitted to have a partial initialization. We can initialize only the first few members and leave the remaining blank. 4. The uninitialized members should be only at the end of the list. 5. The uninitialized members will be assigned default values as follows: Zero for integer and floating point numbers. ‘0’for character and strings.
  66. 66. INTRODUCTION Accessing Members of Structures :  The member of structure is identified and accessed using the dot operator (.)  The dot operator connects the member name to the name of its containing structure.  The general syntax is : struct_variable.membername;  Consider the following structure: struct employee { char name[10]; float salary ; int id ; };  struct employee E1 = {“RAMA”, 10950.0, 2001};  struct employee E2 = {“KRISH”, 11950.0, 2002};
  67. 67. INTRODUCTION Reading the values for various members of structures :  The values of members of structure variables can be read using scanf or gets or getc and dot operator gets(E1.name); or scanf(“%s”, E1.name); scanf(“%f”, &E1.salary); scanf(“%d”, &E1.id);  gets(E1.name); //reads name of the employee 1  scanf(“%f”,&E1.salary); //reads the salary of employee 1  scanf(“%d”,&E2.id); //reads the employee 2 id
  68. 68. INTRODUCTION Displaying the various members of Structures:  The values of members of structure variable can be displayed using the printf and dot operator printf(“%sn”, E1.name); //prints the name of the employee 1 printf(“%fn”, E2.salary); //prints the salary of the employee 2 printf(“%dn”, E2.id); //prints the id of the employee 2.  What will the following statement do? printf(“%fn”, E1.salary);  It will print the salary of the employee 1.
  69. 69. INTRODUCTION Memory Representation of Structure:  When structure is declared the sum of memory required for each members will get allocated contiguously.  Consider the employee structure: struct employee { char name[10]; float salary ; int id ; }; struct employee E1 = { “RAMA”, 10950.0, 2001};  For the structure employee the total amount of memory of 16 Bytes will get allocated.
  70. 70. INTRODUCTION  10 Bytes for name, 4 Bytes for Salary and 2 Bytes for id.
  71. 71. INTRODUCTION Nested Structures:  Astructure can be a member of another structure.  A structure which includes another structure is called nested structure.  Anested structure can be created in 2 ways: 1. Structure is defined separately and a variable of structure can be placed inside the definition of another structure definition. 2. By placing the complete definition of one structure inside the definition of one more structure.
  72. 72. INTRODUCTION Example 1: struct date { int dd; int mm; int yyyy; }; struct Employee { char empname[20]; int empid; float salary; struct date doj; } In the above example the date structure is nested within employee structure.
  73. 73. INTRODUCTION Example 2: struct Employee { char empname[20]; int empid; float salary; struct date { int dd; int mm; int yyyy; }doj; };
  74. 74. INTRODUCTION Accessing nested structure: Syntax: OuterStructureVariable.InnerStructureVariable.InnerStructuremember  To access the member of date structure, we need to first create a variable of employee structure. struct Employee E1;  Now we can access member of date structure as follows: E1.doj.dd ; E1.doj.mm; E1.doj.yyyy;
  75. 75. INTRODUCTION Programming Example for Nested Structure: #include<stdio.h> struct date { int dd; int mm; int yyyy; }; struct employee { char empname[20]; int empid; float salary; struct date doj; };
  76. 76. INTRODUCTION void main() { struct employee e1; e1.doj.dd= 10; e1.doj.mm=1; e1.doj.yyyy=2015; printf("Date of Joining of employee = %dn", e1.doj.dd); printf("Month of Joining of employee = %dn", e1.doj.mm); printf("Year of joining of employee = %dn", e1.doj.yyyy); }
  77. 77. INTRODUCTION Array of Structures:  Just like how we have array of integers we can also have an array of structures.  Example: If we want to store the information of 10 students consisting of name and usn.  The structure definition can be written as shown below: struct student { char name[20]; int usn; };  To store the information of 10 students the structure variable has to be declared as follows: struct student s[10];  The general syntax for structure array declaration is : struct structurename structurevariable[size];
  78. 78. INTRODUCTION Programming Example forArray of Structures: #include<stdio.h> struct student { char name[20]; int usn; float marks; }; int main() { struct student s[20]; int i,n; printf("Enter the number of studentsn"); scanf("%d",&n);
  79. 79. INTRODUCTION for(i = 0; i < n; i++ ) { printf("Enter details of student %dn", i+1); printf("Enter namen"); scanf("%s", s[i].name); printf("Enter usnn"); scanf("%d", &s[i].usn); printf("Enter marksn"); scanf("%f", &s[i].marks); } for(i =0; i<n; i++) { printf("student %d name is %sn", i+1, s[i].name); printf("student %d USN is %dn", i+1, s[i].usn); printf("student %d marks is %fn", i+1, s[i].marks); } }
  80. 80. INTRODUCTION Type Definition:  The typedef is a keyword using which the programmer can create a new datatype name for an already existing datatype name.  So typedef is to used redefine or rename the name of an existing data type.  This is same like defining alias names.  Syntax: typedef datatype newname_1, newname_2, ------- newname_n;  The new names that are given by the user for the already existing data types are called user defined data types. Example 1: typedef int Percentage, USN;  In this example, Percentage and USN are user defined datatypes. Using this any number of integer variables under the datatypes Percentage & USN can be created.
  81. 81. INTRODUCTION Example 2: typedef float salary;  Here salary is a user defined dataype. Using this any number of floating variables under the datatypes salary can be created. Example 1: typedef int Percentage , USN; Percentage p1, p2, p3; USN u1,u2,u3; Example 2: typdef intArray[100]; Array s; Here s is an integer array of size 100
  82. 82. INTRODUCTION Example 3: typedef char String[100]; String s; Here s is a string of size 100. Example:Using 'typedef definition', computing simple interest. #include <stdio.h> //Principal is the new name for float //time is the new name for int datatype //roi is the new name for float datatype typedef float Principal ; datatype typedef int time; typedef float roi; int main( ) { Principal p; time t; roi r; //p is of datatype principal //t is of datatype time //r is of datatype roi
  83. 83. INTRODUCTION float si; printf("Enter p,t and rn"); scanf("%f%d%f",&p,&t,&r); si = (p*t*r)/100; printf("SI = %fn", si); } Type Defined Structure:  Also known as defining Structure Data types using typedef.  The C programming language provides a keyword called typedef, which is used to give a datatype a new name.  The structure defined using the keyword typedef is called type defined structure.
  84. 84. INTRODUCTION The syntax of the typedefined structure: typdef struct { data_type1 member1; data_type2 member2; ---------------------- } struct_ID ; Example: typedef struct { char name[10]; int USN; float marks; } Student ;  Here Student is the new datatype using which variables can be declared like : Student S1, S2, S3;
  85. 85. INTRODUCTION The typedef structure can also be defined as illustrated below : struct employee { char name[10]; int Empid; float Salary; } typedef struct employee EMPLOYEE; EMPLOYEE e1, e2, e3;  employee is now renamed as EMPLOYEE.
  86. 86. INTRODUCTION Programming Example : Structure using typedef typedef struct student { int id; char name[20]; float percentage; } status; int main() { status record; record.id=1; strcpy(record.name, "Raju"); record.percentage = 86.5; printf(" Id is: %d n", record.id); printf(" Name is: %s n", record.name); printf(" Percentage is: %f n", record.percentage); } CHETHAN C
  87. 87. INTRODUCTION Programming Example : Structure using typedef typedef struct employee { char name[50]; int salary; }emp; void main( ) { emp e1; printf("Enter Employee recordn"); printf("Employee namen"); scanf("%s", e1.name); printf("Enter Employee salaryn"); scanf("%d", &e1.salary); printf("student name is = %sn", e1.name); printf("salary is = %dn", e1.salary); } CHETHAN C
  88. 88. INTRODUCTION Structure and Functions:  Just like how the normal variables can be passed to the functions, it is also possible to pass structure variables to functions.  Structure variable can be passed to functions using two techniques: 1. Passing actual value as an argument. (Pass by Value) 2. Passing the address of an argument. (Pass by reference) Passing structure by value:  The entire structure is passed as a function argument.  When the structure is passed as a function argument, it is passed using call by value method.  If structure is passed by value, changes made in structure variable in function definition(called function) doesn’t reflect in original structure variable in the calling function CHETHAN C
  89. 89. INTRODUCTION Example: struct student { char name[50]; int usn; float marks; }; void display(struct student stu);  In this example, the structure variable s1 is passed as an argument of display(s1) function.  The value of the members variables are displayed in disp(struct student stu) function. CHETHAN C
  90. 90. INTRODUCTION void main() { struct student s1; printf("Enter student namen"); scanf("%s",s1.name); printf("Enter student usnn"); scanf("%d",&s1.usn); printf("Enter student marksn"); scanf("%f",&s1.marks); display(s1); } void display(struct student stu) { printf("Name is %sn", stu.name); printf("USN is %dn", stu.usn); printf("Marks is %fn", stu.marks); } CHETHAN C
  91. 91. INTRODUCTION Passing the address of the entire structure/Pass by reference:  The address of the structure variable is passed to the function while passing it as reference.  We pass the address using the pointers.  Accessing the members of the structures using pointers is done by ( )arrow operator.  If structure is passed by reference, change made in structure variable in function definition reflects in original structure variable in the calling function. CHETHAN C
  92. 92. INTRODUCTION struct student { char name[50]; int usn; }; void display(struct student *stu); void main() { struct student s1; printf("Enter the student namen"); scanf("%s",s1.name); printf("Enter USNn"); scanf("%d",&s1.usn); display(&s1); printf("In main Functionn"); printf("Name is: %sn", s1.name); printf("USN is : %dn", s1.usn); } CHETHAN C
  93. 93. INTRODUCTION void display(struct student *stu) { strcpy(stu->name, "xyz"); stu->usn=30; printf("In Display Functionn"); printf("Name is: %sn", stu->name); printf("USN is : %dn", stu->usn); }  When the name and usn are changed in the display( ) function, the changes are reflected even in the main( ) function.
  94. 94. INTRODUCTION Self Referential Structures:  Self Referential structures are those structures that have one or more pointers which point to the same type of structure, as their member.
  95. 95. INTRODUCTION struct structure_name { datatype datatype_name; structure_name * pointer_name; }  In other words, structures pointing to the same type of structures are self-referential in nature. Example: struct node { int data; struct node *next; };
  96. 96. INTRODUCTION Example: struct node { int data1; char data2; struct node* link; }; int main() { struct node ob; }
  97. 97. INTRODUCTION  In the above example ‘link’ is a pointer to a structure of type ‘node’.  Hence, the structure ‘node’ is a self-referential structure with ‘link’ as the referencing pointer. Types of Self Referential Structures  Self Referential Structure with Single Link  Self Referential Structure with Multiple Links
  98. 98. INTRODUCTION Self Referential Structure with Single Link:  These structures can have only one self-pointer as their member.  The connection formed is shown in the following figure.
  99. 99. INTRODUCTION Self Referential Structure with Multiple Links:  Self referential structures with multiple links can have more than one self-pointers.  Many complicated data structures can be easily constructed using these structures.  Such structures can easily connect to more than one nodes at a time.
  100. 100. INTRODUCTION  In the above example we can see that ‘ob1’, ‘ob2’ and ‘ob3’ are three objects of the self referential structure ‘node’.  They are connected using their links in such a way that any of them can easily access each other’s data.  This is the beauty of the self referential structures.  The connections can be manipulated according to the requirements of the programmer.
  101. 101. INTRODUCTION Advantages:  The self-referential structures are beneficial in many applications that involves linked data members, such as trees and lists.  Unlike a static data structure such as an array where the size of the array limits the number of elements that can be inserted into the array, the self- referential structure can dynamically be contracted or expanded.  Operations such as insertion or deletion of nodes in a self-referential structure involve simple alteration of the pointers present within them.
  102. 102. INTRODUCTION Unions  Union is an user defined datatype in C.  It is a collection of variables of different datatypes in the same memory location.  A union is a user-defined type similar to structs in C except for one key difference.  Unlike structure members which are stored in separate memory locations, all the members of union must share the same memory space.
  103. 103. INTRODUCTION
  104. 104. INTRODUCTION To define a union:  We use the union keyword to define unions. union car { char name[50]; int price; }; Create union variables:  When a union is defined, it creates a user-defined type.  However, no memory is allocated.  To allocate memory for a given union type and work with it, we need to create variables.
  105. 105. INTRODUCTION union car { char name[50]; int price; }; int main() { union car car1, car2, *car3; } Another way of creating union variables is: union car { char name[50]; int price; } car1, car2, *car3;
  106. 106. INTRODUCTION Union Declaration:  A union declaration is similar to a structure, but the fields of a union must share their memory space.  This means that only one field of the union is "active" at any given time.
  107. 107. INTRODUCTION Access members of a union  We use the . (dot) operator to access members of a union. And to access pointer variables, we use the -> operator. In the above example, • To access price for car1, car1.price is used. •To access price using car3, either (*car3).price or car3- >price can be used.
  108. 108. INTRODUCTION Similarities between Structure and Union  ‘.’ (dot) operator is used for accessing members.  Both are user-defined data types used to store data of different types as a single unit.  Their members can be objects of any type, including other structures and unions or arrays.  Both structures and unions support only assignment (=) and sizeof() operators.  A structure or a union can be passed by value to functions and returned by value by functions. The argument must have the same type as the function parameter.
  109. 109. INTRODUCTION
  110. 110. INTRODUCTION POINTERS  A pointer is a variable which contains the address in memory of another variable.  The two most important operator used with the pointer type are 1 . & : The unary operator & which gives the address of a variable 2. * : The indirection or dereference operator * gives the content of the object pointed to by a pointer.
  111. 111. INTRODUCTION  Consider the following declaration of two variables a and b: int a = 25; int b = 45;  The value of a and b can be accessed and printed as shown below: printf(“Value of a =%dn”, a); printf(“Value of b =%dn”, b);  But what if we need the address of a and b ?  In order to access the address of the variables we can use the address operator (&) as below: printf(“Address of a = %d n”, &a); printf(“Address of b = %dn”, &b);
  112. 112. INTRODUCTION Declaration  Pointer variables should be declared before they are used.  The syntax to declare a pointer variable is: datatype *identifier; int i, *pi; i is the integer variable and pi is a pointer to an integer pi = &i; &i returns address of i and assigns it as the value of pi
  113. 113. INTRODUCTION The steps to be followed to use pointers: 1. Declare a data variable. 2. Declare a pointer variable. 3. Initialize a pointer variable. 4. Access data using pointer variable. Initialization of Pointer variables: Ex: int x; Ex: int *p; Ex: p = &x; Ex: y= *p  It is the process of assigning an address to the pointer variable.  Consider the following declaration statements : int a; int *p ;  After declaration the variable a gets allocated in some memory location (address) say 1000 and the pointer p gets the address 3000.
  114. 114. INTRODUCTION CHETHAN C After initialization, the variable a holds the value 65 and the variable p holds the value of address of a :
  115. 115. INTRODUCTION  The process of accessing the value of the variables indirectly by pointer pointing to the address of variable is called indirection. Accessing Variables Through Pointers:  The contents of variable can be accessed by de-referencing a pointer.  If a pointer p is pointing to a variable num, then its value can be accessed by just saying *p.  Here * operator acts as the dereference operator.
  116. 116. INTRODUCTION
  117. 117. INTRODUCTION Multiple Pointers:  Asingle variable can be accessed using multiple pointers.  Multiple pointers has to declared and all the pointers has to be initialized to single variable. Example: #include<stdio.h> void main() { int num=1004; int *p1,*p2,*p3; p1 = &num; p2 = &num; p3 = &num;
  118. 118. INTRODUCTION printf("The value of num = %dn", num); printf("The value of num = %dn", *p1); printf("The value of num = %dn", *p2); printf("The value of num = %dn", *p3); }  Three pointers (*p1,*p2 and *p3) are used to access the value of single variable num. Output: The value of num = 1004 The value of num = 1004 The value of num = 1004 The value of num = 1004
  119. 119. INTRODUCTION NULLPointer:  Apointer pointing to nowhere in memory is called NULL Pointer.  It will not be containing the address of any object or function.  Null pointers are initialized to NULL. Example: int *p = NULL; #include <stdio.h> int main() { int *ptr = NULL; printf("The value of ptr is %u", ptr); } Output: The value of ptr is 0
  120. 120. INTRODUCTION Dangling Pointer:  The pointer which does not contain valid address or pointing to no existing memory location is called dangling pointer.  They contain garbage values.  Dangling pointer arises when an object is de-allocated, without modifying the value of the pointer so that the pointer still points to the memory location of the de-allocated memory.  The problem of dangling pointer can be avoided by initializing it to NULL.  After de-allocating memory, initialize pointer to NULL so that pointer will be no longer dangling.  Assigning NULL value means pointer is not pointing to any memory location.
  121. 121. INTRODUCTION Pointer and arrays:  In C, the pointers can be used to create and handle arrays.  The operations that can be performed on arrays and its elements are: • Traversing array using a pointer • Accessing array element • Reading an array • Printing an array  The name of the array is actually a pointer representing the address of memory location of first element of array.
  122. 122. INTRODUCTION  Example: Consider the following declaration int a[5] = {10,20,30,40,50} ; Assuming memory layout of the array a and its elements are:  The name of the array a is a pointer to integer and holds the address of first element a[0] i.e a = &a[0] = 1000.
  123. 123. INTRODUCTION  The relationship of arrayname a (pointer) and different elements is given below : a = &a[0]; a+1 = &a[1] a+2 = &a[2] a+3 = &a[3] a+4 = &a[4]  In general &a[i] is same as a+ i ;  The data in those address can be obtained using the indirection operator * as : *&a[i] *(a+i) *(i+a) *&i[a]; (or) (or) (or)
  124. 124. INTRODUCTION
  125. 125. INTRODUCTION Array of Pointers:  Array of pointers is a collection of multiple pointers of particular data type.  Each element of such array will be capable to point and hold the address of set variables whose data type is of same type.  Consider a declaration int *pi[3];  pi is array of pointers which can hold three pointers of integer type – *pi[0], *pi[1] and *pi[2].
  126. 126. INTRODUCTION Program to show how values of a, b and c is accessed and printed using array of pointers: #include<stdio.h> int main() { int *pi[3], a, b, c; printf(“Enter the values of a ,b and c n”); scanf(“%d%d%d”, &a, &b, &c); pi[0] = &a; pi[1]=&b; pi[2]= &c; printf(“The Entered value of a,b and cn”); printf(“%d%d%dn”, *pi[0],*pi[1],*pi[2]); }  pi[0], pi[1] and pi[2] holds the address of variables a, b and c respectively.  *pi[0], *pi[1] and *pi[2] holds the values of a, b and c respectively.
  127. 127. INTRODUCTION Pointer to Pointer :  Consider the declaration : int **p;  p is a pointer to pointer to integer.  These types of pointers are called as double pointer or pointer to pointer.  Single pointers are used to store the address of normal variables.  Double pointer is to store the address of pointer variables. Example: int a = 101; int *p1; int **pd; p1 = &a; pd =&p1;
  128. 128. INTRODUCTION  The address of normal variable a is stored in p1.  Address of pointer variable p1 is stored in pd.  Assuming the address of a as 5000, address of pointer p1 as 8000 and address of double pointer pd as 9000, we have the memory representation as :
  129. 129. INTRODUCTION Pointer to pointer program: #include<stdio.h> void main() { int a = 101; int *p1; int **pd; p1 = &a; pd = &p1; //address of a is in p1 //address of p1 is in pd printf(“%dn”, a); printf(“%dn”, p1); printf(“%dn”, pd); //Prints value in a //Prints address of a. //Prints address of p1 }
  130. 130. INTRODUCTION Advantages of pointers:  Pointers make the programs simple and reduce their length.  Pointers are helpful in allocation and de-allocation of memory during the execution of the program thus reduces the execution time of the program.  Provides an alternate way to access array elements.  Pointers allows us to perform dynamic memory allocation and deallocation.  Pointers helps us to build complex data structures like linked list, stack, queues, trees, graphs etc.
  131. 131. INTRODUCTION Disadvantages of pointers:  Uninitialized pointers might cause segmentation fault.  Pointers are slower than normal variables.  If pointers are updated with incorrect values, it might lead to memory corruption.
  132. 132. INTRODUCTION Dynamic MemoryAllocation:  Since C is a structured language, it has some fixed rules for programming - One of them includes changing the size of an array.  An array is a collection of items stored at contiguous memory locations.  The length (size) of the array is 9.  But what if there is a requirement to change this length (size).
  133. 133. INTRODUCTION Example:  If there is a situation where only 5 elements are needed to be entered in this array, the remaining 4 indices are just wasting memory in this array.  So there is a requirement to lessen the length (size) of the array from 9 to 5.  Think all 9 indices filled. But there is a need to enter 3 more elements in this array.  In this case, 3 indices more are required. So the length (size) of the array needs to be changed from 9 to 12.  This procedure is referred to as Dynamic MemoryAllocation in C.
  134. 134. INTRODUCTION  Therefore, Dynamic Memory Allocation can be defined as a procedure in which the size of a data structure (like Array) is changed during the runtime.  The memory allocation is done either before or at the time of program execution. There are two types of memory allocations: 1. Compile-time or Static Memory Allocation 2. Run-time or Dynamic MemoryAllocation
  135. 135. INTRODUCTION Static MemoryAllocation:  Static Memory is allocated for declared variables by the compiler.  The address can be found using the address of operator and can be assigned to a pointer.  The memory is allocated during compile time. Dynamic MemoryAllocation:  Memory allocation done at the time of execution(run time) is known as dynamic memory allocation.  In the Dynamic allocation of memory space is allocated by using certain functions when the value is returned by functions and assigned to pointer variables.
  136. 136. INTRODUCTION  C provides 4 library functions defined under <stdlib.h> header file to facilitate dynamic memory allocation.  They are: 1. malloc() - allocates single block of requested memory. 2. calloc() - allocates multiple block of requested memory. 3. realloc() - reallocates the memory occupied by malloc() or calloc() functions. 4. free() - frees the dynamically allocated memory.
  137. 137. INTRODUCTION malloc():  The “malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified number of bytes.  It returns a pointer of type void which can be cast into a pointer of any form.  Syntax: ptr = (cast-type*) malloc(byte-size)  Example: ptr = (int*) malloc(100 * sizeof(int));
  138. 138. INTRODUCTION  Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory.  And, the pointer ptr holds the address of the first byte in the allocated memory.  The expression results in a NULL pointer if the memory cannot be allocated. (If space is insufficient, allocation fails and returns a NULL pointer.)
  139. 139. INTRODUCTION calloc() :  “calloc” or “contiguous allocation” method in C is used to dynamically allocate the specified number of blocks of memory of the specified type.  It is similar to malloc() but has two different points : 1. The malloc() function allocates memory and leaves the memory uninitialized whereas the calloc() function allocates memory and initializes all bits to zero. 2. It has two parameters or arguments.
  140. 140. INTRODUCTION Syntax: ptr = (cast-type*)calloc(n, element-size);  n is the no. of elements and element-size is the size of each element. Example: ptr = (float*) calloc(25, sizeof(float));  This statement allocates contiguous space in memory for 25 elements each with the size of the float.
  141. 141. INTRODUCTION realloc():  “realloc” or “re-allocation” is used to dynamically change the memory allocation of a previously allocated memory.  In other words, if the memory previously allocated with the help of malloc() or calloc() is insufficient, realloc() can be used to dynamically re-allocate memory.  Re-allocation of memory maintains the already present value and new blocks will be initialized with the default garbage value.
  142. 142. INTRODUCTION Syntax: ptr = realloc(ptr, newSize);  ptr is reallocated with new size 'newSize'
  143. 143. INTRODUCTION free() :  “free” is used to dynamically de-allocate the memory.  The memory allocated using functions malloc() and calloc() is not de-allocated on their own. Hence the free() method is used, whenever the dynamic memory allocation takes place.  It helps to reduce wastage of memory by freeing it.  Syntax: free(ptr);  This will cause the space in memory pointer by ptr to be deallocated.
  144. 144. INTRODUCTION
  145. 145. INTRODUCTION
  146. 146. INTRODUCTION
  147. 147. INTRODUCTION
  148. 148. INTRODUCTION Representation of Linear Arrays in Memory:  A linear array is a list of a finite number ‘n’ of homogeneous data element such that a.The elements of the array are referenced respectively by an index set consisting of n consecutive numbers. b.The element of the array are respectively in successive memory locations.  The number n of elements is called the length or size of the array.
  149. 149. INTRODUCTION  The length or the numbers of elements of the array can be obtained from the index set by the formula: When LB = 0, Length = UB – LB + 1 When LB = 1, Length = UB  UB is the largest index called the Upper Bound  LB is the smallest index, called the Lower Bound
  150. 150. INTRODUCTION
  151. 151. INTRODUCTION Representation of linear arrays in memory  Let LA be a linear array in the memory of the computer.  The memory of the computer is simply a sequence of address location as shown below: LOC (LA [K]) = address of the element LA [K] of the array LA
  152. 152. INTRODUCTION  The elements of LA are stored in successive memory cells.  The computer does not keep track of the address of every element of LA but must to keep track only the address of the first element of LA denoted by, Base (LA) called the base address of LA.  Using the base address of LA, the computer calculates the address of any element of LA by the formula : L O C (LA[K]) = Base(LA) + w(K – lower bound) w is the number of words per memory cell for the array LA.
  153. 153. INTRODUCTION
  154. 154. INTRODUCTION Dynamically Allocated Arrays  While writing programs, if we find ourselves in a situation where we cannot determine how large an array to use, then a good solution to this problem is to defer this decision to run time and allocate the array when we have a good estimate of the required array size.  The arrays can be dynamically allocated using malloc(), calloc(), realloc() functions.
  155. 155. INTRODUCTION Creation of One Dimensional Array dynamically int i, n, *list; printf(“Enter the number of numbers to generate:”); scanf(“%d”, &n); if(n<1) { printf (“Improper value of nn”); exit(); } malloc (list, n*sizeof(int));
  156. 156. INTRODUCTION Creation of Two Dimensional Array dynamically int make2dArray(int rows, int cols) { int x, i; malloc(x, rows * sizeof (x)); for (i= 0;i<rows; i++) malloc(x[i], cols * sizeof(x)); return x; }
  157. 157. ARRAY OPERATIONS Array Operations:  Traversing  Insertion  Deletion  Sorting  Searching
  158. 158. ARRAY OPERATIONS
  159. 159. ARRAY OPERATIONS
  160. 160. STRINGS  Each programming languages contains a character set that is used to communicate with the computer.  The character set is: Alphabet: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Digits: 0 1 2 3 4 5 6 7 8 9 S pecial characters: + - /* ( ) , . $ = ‘ _  String: A finite sequence S of zero or more Characters is called string.
  161. 161. STRINGS  Astring is an array of characters.  Strings in C are classified into two types: 1. String literals 2. String variables  String literals are also known as string constants.  The sequence of characters enclosed within two double quotes is called string constant.  The string constant is stored in consecutive bytes in memory and the compiler places the null character at end to denote the end of string.
  162. 162. STRINGS  In other words, A string is terminated by null character which is denoted by the escape sequence ‘0‘.  Consider the string “SVCE  The delimiter ‘0’ represents that the string has ended in the memory. Declaration of Strings: The general form of declaration of a string variable is : char stringname[size];  The size determines the number of characters in the string name including the null character.
  163. 163. STRINGS  Example: char hero [21];  Size 21 means it can store up to 20 characters plus the null character.  Entire storage location name is divided in to 21 boxes called bytes, each of which holds one character. Each character is element of data type char. char s[ ] = {“dog”}; char t[ ] = {“house”}; Using these declarations, the C compiler will allocate just enough space to hold each word including the null character.
  164. 164. STRINGS  C library supports a number of string manipulation functions and are stored in header file “string.h”.  Major String functions are: 1. strcpy() - copies one string over another. 2. strlen() - finds the length of a string. 3. strcmp() - compare two strings. 4. strcat() - concatenates two strings.
  165. 165. STRINGS Substring: A string Y is called substring of a string S if there exist string X and Z such that S = X // Y // Z  If X is an empty string, then Y is called an Initial substring of S and Z is an empty string then Y is called a terminal substring of S.  Example: ‘BE OR NOT’is a substring of ‘TO BE OR NOT TO BE’ ‘THE’is an initial substring of ‘THE END’
  166. 166. STRINGS Storing Strings Strings are stored in three types of structures 1. Fixed length structures 2. Variable length structures with fixed maximum 3. Linked structures. Record Oriented Fixed length structures  In fixed length structures each line of print is viewed as a record, where all have the same length i.e., where each record accommodates the same number of characters.
  167. 167. STRINGS  Example: Suppose the input consists of the program. Using a record oriented fixed length storage medium, the input data will appear in memory as pictured below.
  168. 168. STRINGS  If new record needs to be inserted, then it requires that all succeeding records be moved to new memory location.  This disadvantages can be easily overcome by using a linear array POINT which gives the address of successive record, so that the records need not be stored in consecutive locations in memory.  Inserting a new record will require only an updating of the array POINT.
  169. 169. STRINGS
  170. 170. STRINGS The main advantages of this method are 1. The ease of accessing data from any given record. 2. The ease of updating data in any given record. (as long as the length of the new data does not exceed the record length) The main disadvantages are 1.Time is wasted reading an entire record if most of the storage consists of inessential blank spaces. 2. Certain records may require more space than available. 3.When the correction consists of more or fewer characters than the original text, changing a misspelled word requires record to be changed.
  171. 171. STRINGS Variable length structures with fixed maximum:  The storage of variable-length strings in memory cells with fixed lengths can be done in two general ways 1.Using a marker, such as two dollar signs ($$), to signal the end of the string [Records with Sentinals] 2.List the length of the string—as an additional item in the pointer array. [Records whose lengths are listed]
  172. 172. STRINGS
  173. 173. STRINGS
  174. 174. STRINGS Advantages:  These ways of storing strings will save space and are sometimes used in secondary memory when records are relatively permanent and require little changes. Disadvantages:  These types of methods of storage are usually inefficient when the strings and their lengths are frequently being changed.
  175. 175. STRINGS Linked Storage:  A linked list is a linear collection of data elements, called nodes. Each node is divided into two parts:  The first part contains the information of the element(data).  The second part called the link field or nextpointer field, contains the address of the next node in the list.
  176. 176. STRINGS Strings using linked lists may be Stored as follows:  Each memory cell is assigned one character or a fixed number of charactersand a link contained in the cell the next gives the address of the cell containing character or group of character in the string.  Example : Storing TO B E OR NOT TO B E
  177. 177. STRINGS String Operations: 1. Substring 2. Indexing 3. Concatenation 4. Length Substring:  Accessing a substring from a given string requires three pieces of information: (1)The name of the string or the string itself (2)The position of the first character of the substring in the given string (3)The length of the substring or the position of the last character of the substring.
  178. 178. STRINGS Syntax: SUBSTRING (string, initial, length) The syntax denote the substring of a string S beginning in a position K and having a length L. Example: SUBSTRING ('TO B E OR NOT TO BE’, 4, 9) = 'BE OR NOT’ SUBSTRING ('THE END', 4, 4) = ' END'
  179. 179. STRINGS Indexing  Indexing also called pattern matching refers to finding the position where a string pattern P first appears in a given string text T.  This operation is called INDEX Syntax: INDEX (text, pattern)  If the pattern P does not appears in the text T, then INDEX is assigned the value 0.  The arguments “text” and “pattern” can be either string constant or string variable.
  180. 180. STRINGS Concatenation: Let S1 and S2 be string. The concatenation of S1 and S2 which is denoted by S1//S2 denoting string consisting of the characters of S1 followed by the character of S2. Example: If S1 = 'MARK' and S2= ‘TWAIN' S 1 / /S 2 = ‘MARKTWAIN’ Concatenation is performed in C language using strcat() function as shown below strcat (S1, S2); Concatenates string S1 and S2 and stores the result in S1
  181. 181. STRINGS Length  The number of characters in a string is called its length. Syntax: LENGTH (string) Example: LENGTH (‘computer’) = 8  String length is determined in C language using the strlen() function, as shown below: X = strlen ("sunrise");  Strlen() function returns an integer value 7 and assigns it to the variable X
  182. 182. STRINGS Pattern MatchingAlgorithms  One of the primary applications of computers is in the field of word processing.  This usually involves some type of pattern matching which is to check to see if a particular word “S” appears in a given text “T”.  The Pattern Searching algorithms are also referred to as String SearchingAlgorithms.  These algorithms are useful in the case of searching a string within another string.  Pattern matching is the problem of deciding whether or not a given string pattern Pappears in a string text T.
  183. 183. STRINGS  The length of P does not exceed the length of T.
  184. 184. STRINGS Pattern Matching Algorithms: 1. First Pattern Matching Algorithm 2. Second Pattern Matching Algorithm First Pattern Matching Algorithm  The comparison is done by a given pattern P with each of the substrings of T, moving from left to right until a match is found. WK = SUBSTRING (T, K, L E N GT H (P)) WK denote the substring of T having the same length as P and beginning with the Kth character of T.
  185. 185. STRINGS  First compare P, character by character with the first substring, W1.  If all the characters are the same, then P = W1 and so P appears in T and INDEX (T, P) = 1.  If it is found that some character of P is not the same as the corresponding character of W1. Then P ≠ W1  Immediately move on to the next substring, W2 compare P with W2.  If P ≠ W2 then compare P with W3 and so on. i.e,
  186. 186. STRINGS  This process will stop when P is matched with some substring WK and so P appears in T and INDEX(T,P) = K or  when all the WK are completed with no match and hence P does not appear in T.  The maximum value MAX of the subscript K is equal to LENGTH(T) -LENGTH(P) +1.
  187. 187. STRINGS
  188. 188. STRINGS  Algorithm contains two loops, one inside the other.  The outer loop runs through each successive R-character substring WK = T[K] T[K + 1] ... T[K+R-l] of T.  The inner loop compares P with WK, character by character.  If any character does not match, then control transfers to Step 5, which increases K and then leads to the next substring of T.  If all the R characters of P do match those of some WK then P appears in T and K is the INDEX of P in T.  If the outer loop completes all of its cycles but P does not appear in T and so INDEX = 0.
  189. 189. STRINGS Second Pattern Matching Algorithm:  This uses a table which is derived from a particular pattern P but is independent of the text T.  Example : Let P = aaba  aaba can be split into 5 substrings. (Empty, a, aa, aab, aaba) Let Qi denote the initial substring of P of length i. Q0 = (empty string) Q1 = a Q2 = aa Q3 = aab Q4 = aaba = P
  190. 190. STRINGS  This algorithm contains the table that is used for the pattern P = aaba
  191. 191. STRINGS The graph is obtained with the table as follows.  First, a node in the graph corresponding to each initial substring Qi of P.  The Q's are called the states of the system, and Q0 is called the initial state.  There is an arrow (a directed edge) in the graph corresponding to each entry in the table.  If f(Qi, t) = Qj then there is an arrow labeled by the character t from Qi to Qj  Example: f(Q2, b) = Q3 so there is an arrow labeled b from Q2 to Q3  For notational convenience, all arrows labeled x are omitted, which must lead to the initial state Qo.
  192. 192. STRINGS  The second pattern matching algorithm for the pattern P = aaba.  Let T = T1 T2 T3 ... TN denote the n-character-string text which is searched for the pattern P.  Beginning with the initial state Q0 and using the text T, we will obtain a sequence of states S1, S2, S3, ... as follows.  Let S1 = Q0 and read the first character T1.  The pair (S1, T1) yields a second state S2; that is, F(S1, T1) = S2,  Read the next character T2, The pair (S2, T2) yields a state S3, and so on.
  193. 193. STRINGS There are two possibilities:  Some state SK = P, the desired pattern. In this case, P does appear in T and its index is K - LENGTH(P).  No state S1, S2, ... , S N +1 is equal to P. In this case, P does not appear in T.
  194. 194. STRINGS

×