Upcoming SlideShare
×

# Ch 1 intriductions

907
-1

Published on

Published in: Education, Technology
1 Comment
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• very usefull presentation and very easy to understand

Are you sure you want to  Yes  No
• Be the first to like this

Views
Total Views
907
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
0
1
Likes
0
Embeds 0
No embeds

No notes for slide
• Summary and objectives…
• ### Ch 1 intriductions

1. 1. Data structure <ul><li>A data structure is a way of storing  data  in a computer memory, so that it can be retrieved and manipulate efficiently. </li></ul><ul><li>It is an organization of mathematical and logical concepts of data. </li></ul><ul><li>The choice of particular data structure depends on following considerations </li></ul>
2. 2. <ul><ul><li>It must be able to represent the inherent (essential) relationship of data in the real world . </li></ul></ul><ul><ul><li>It must be simple enough, so that it can process efficiently, when necessary </li></ul></ul><ul><li>The various types of data structure are divided in to fallowing categories. </li></ul><ul><li>Linear data structures:- A data structure whose elements form a sequence and every element in structure has a unique predecessor and unique successor.abc </li></ul><ul><li>Eg . array, linked list, stack, queues </li></ul>
3. 3. <ul><li>b) Non-linear data structures:- A data structure whose elements do not form a sequence, there is no unique predecessor or unique successor. </li></ul><ul><li>Eg . Trees and graphs </li></ul><ul><li>Abstract Data type:- It refers to act of representing essential features without including background details or explanations. </li></ul>
4. 4. <ul><li>1. Arrays:- An array is a list of finite number of elements of same data type. The individual elements of an array are accessed using an index to the array. </li></ul><ul><ul><li>If the number of elements is n in array, then the size of array would be n. </li></ul></ul><ul><ul><li>In general, if lb the smallest index, called lower bound, and ub largest index called upper bound , then the size of linear array is given by </li></ul></ul><ul><ul><li>size= ub – lb +1 </li></ul></ul>9 0 3 14 6 4 7 8
5. 5. <ul><li>2. Stacks </li></ul><ul><li>A stack is defined as a special type of data structure in which insertions and deletions can take place only at one end, called the top. </li></ul><ul><li>Stack is organized as a Last In First Out (LIFO) data structure. </li></ul>top Stack with 4 elements
6. 6. 3-Queues <ul><li>A queue is defined as a special type of data structure where the elements are inserted from one end and elements are deleted from the other end. </li></ul><ul><li>The end from where the elements are inserted is called REAR end. </li></ul><ul><li>The end from where the elements are deleted is called FRONT end . </li></ul><ul><li>Queue is organized as First In First Out (FIFO)Data Structure. </li></ul>
7. 7. In a queue, the elements are always inserted at the rear end and deleted from the front end. Insert delete Front end Rear end 0 1 2 3 4 Pictorial representation of a Queue Queue 10 20 30
8. 8. 4- Linked Lists <ul><li>A linked list is a linear collection of data elements, called nodes. </li></ul><ul><li>Between, each node in the list, there exists a logical relationship </li></ul><ul><li>Each node is divide tow or more parts. </li></ul><ul><li>Each node can hold the data along with a pointer field using which address of the next node can be obtained. </li></ul><ul><li>. </li></ul>
10. 10. <ul><li>Singly linked lists:- In single linked list each node </li></ul><ul><li>is divide in to two part </li></ul><ul><li>First part contain the information of the element. </li></ul><ul><li>Second part called nextpointer field, the address </li></ul><ul><li>of next node in the list. </li></ul>Next pointer field of first node Information field of first node
11. 11. Circular singly linked list <ul><li>Linear linked list containing the address of the first node in the link field of the last node results in a Circular Singly linked list or Circular list. </li></ul>last Pictorial representation of Circular singly linked list
12. 12. <ul><li>Doubly linked lists </li></ul><ul><li>In double linked list each node divided three parts; </li></ul><ul><li>First part called previous pointer field, contains the address of preceding elements in the list. </li></ul><ul><li>Second part contain the information of the elements. </li></ul><ul><li>Third part called next pointer field, contains the address of succeeding elements in the list. </li></ul>
13. 13. <ul><li>Circular doubly linked lists </li></ul>
14. 14. <ul><li>Trees </li></ul><ul><li>A tree is a non-linear data structure that consists of a root node and potentially many levels of additional nodes that form a hierarchy. </li></ul><ul><li>Nodes that have no children are called leaf nodes </li></ul><ul><li>Non-root and non-leaf nodes are called internal nodes </li></ul>A tree data structure leaf nodes root node internal nodes Tree can represent inheritance relationship between classes.
15. 15. <ul><li>Trees </li></ul><ul><li>A tree is data structure that represent a hierarchical relationship between various nodes. </li></ul><ul><li>Each node has zero or more child nodes , </li></ul><ul><li>A child has at most one parent; a node without a parent is called the root node (or root ). </li></ul><ul><li>Nodes with no children are called leaf nodes . </li></ul>
16. 16. <ul><li>Graphs </li></ul><ul><li>A graph is a mathematical structure consisting of a set of vertexes (also called nodes) and a set of edges. An edge is a pair of vertexes . </li></ul><ul><li>The two vertexes are called the edge endpoints . </li></ul><ul><li>They are used to model real-world systems such as the Internet, airline connections or a city road network </li></ul>
17. 17. Lucknow Hyderabad Ahmadabad Chennai Mumbai New Delhi Above graph shows the cities for which there is a direct flight.
18. 18. <ul><li>Common operations on data structures:- the various operations that can be performed on different data structures are </li></ul><ul><li>Traversing – Accessing each element exactly once in order to process it. This operation is called visiting the element. </li></ul><ul><li>Searching – find the location of the element with a given value. </li></ul><ul><li>Insertion – Adding a new element to the list. </li></ul><ul><li>Deletion – removing an element from list. </li></ul><ul><li>Sorting – arranging the elements in some logical order </li></ul><ul><li>Merging – combine the elements of two similar lists to a single list. </li></ul>
19. 19. Introduction to Algorithms <ul><li>An algorithms is a finite set of steps defining the solution of a particular problem. </li></ul><ul><li>An algorithm can be expressed in English like language, called pseudocode . </li></ul><ul><li>Algorithms are used for calculation , data processing , and many other fields. </li></ul>
20. 20. <ul><li>Every algorithm must satisfy the following criteria:- </li></ul><ul><li>Input :- there are zero or more values which are externally supplied. </li></ul><ul><li>Output:- At least one value is produced. </li></ul><ul><li>Definiteness:- each steps must be clear and unambiguous (unmistakable). </li></ul><ul><li>Finiteness:- in every case Algorithm must terminate after a finite number of steps. </li></ul><ul><li>Effectiveness:- each steps must be feasible. </li></ul>
21. 21. <ul><li>Complexity of Algorithm :- It refers to the rate at which the required storage or consumed time grows as a function of the problem size. </li></ul><ul><li>The absolute growth -depends on the machine used to execute the program. </li></ul><ul><ul><li>SPACE COMPLEXITY:- The space complexity of an algorithm or program is the amount of memory it needs to run to completion. </li></ul></ul><ul><ul><li>TIME COMPLEXITY:- The time complexity of an algorithm or a program is the amount of time it needs to run to completion. </li></ul></ul><ul><ul><li>its depends on the amount of data inputted to an algorithm. </li></ul></ul>
22. 22. <ul><li>Big “OH” Notation:- it is a characteristic scheme that measures properties of algorithm complexity performance and/or memory requirements. </li></ul><ul><li>The space and/or time complexity is expressed in the form of a function f(n), </li></ul><ul><li>where n is input size. </li></ul><ul><li>The complexity function f(n) of an algorithm increases as ‘n’ increases. </li></ul><ul><li>Let , f(n) and g(n) are the functions defined on positive number then the function f(n)=O(g(n)) </li></ul><ul><li>can be read as “ f (n) is of the order of g(n) ” </li></ul>
23. 23. <ul><li>If there exists positive constants c and n 0 such that f(n)<=cg(n) for all values of n>n 0 </li></ul>
24. 24. <ul><li>When we analyze an algorithm it depends on the input data , there are three cases : </li></ul><ul><li>Worst case </li></ul><ul><li>Average case </li></ul><ul><li>Best case </li></ul><ul><li>Worst case , the maximum value of f(n) for any possible input. </li></ul><ul><li>Average case , the amount of time a program might be expected to take on typical (or average) input data. </li></ul><ul><li>Best case, the minimum possible value of f(n) for any possible input. </li></ul>
25. 25.
26. 26. <ul><li>There is a theorem, which states that if </li></ul><ul><li>Based on the time complexity representation of the big Oh notation, the algorithm </li></ul><ul><li>can be categorized as : </li></ul><ul><ul><ul><li>1 . Constant time O(1) </li></ul></ul></ul><ul><ul><ul><li>2. Logarithmic time Olog(n) </li></ul></ul></ul><ul><ul><ul><li>3. Linear time O(n) </li></ul></ul></ul><ul><ul><ul><li>4. Polynomial time O(n c ) </li></ul></ul></ul><ul><ul><ul><li>5. Exponential time O(c n ) </li></ul></ul></ul><ul><ul><ul><li>Where c > 1 </li></ul></ul></ul>
27. 27. <ul><li>Limitations of Big Oh Notation:- </li></ul><ul><li>It does not discuss the way and means to improve the efficiency of the program. </li></ul><ul><li>It does not exhibit (show) the potential of the constants </li></ul><ul><li>For example , one algorithm is taking 1000n 2 time to execute and the other n 3 time. </li></ul><ul><li>The first algorithm is O(n 2 ) ,which implies that it will take less time than the other algorithm which is O(n 3 ). </li></ul><ul><li>However in actual execution the second algorithm will be faster for n < 1000. </li></ul>
28. 28. <ul><li>ALGORITHM FOR LINEAR SEARCH </li></ul><ul><li>Let A be an array of n elements, A[1],A[2],A[3], ...... A[n]. “ITEM” is the element to be searched. This algorithm will find the location “loc” of ITEM in A. Set loc = – 1,if the search is unsuccessful. </li></ul><ul><li>Input an array A of n elements and “ITEM” to be searched and initialise loc = – 1. </li></ul><ul><li>Initialise i = 0; and repeat through step 3 if (i < n) by incrementing i by one . </li></ul><ul><li>If (ITEM = A[i]) </li></ul><ul><li>(a) loc = i </li></ul><ul><li>(b) GOTO step 4 </li></ul><ul><li>4 . If (loc > 0) </li></ul><ul><li> Display “ITEM is found and searching is successful” </li></ul><ul><li>5. Else </li></ul><ul><li> Display “ITEM is not found and searching is unsuccessful” </li></ul><ul><li>6. Exit </li></ul>
29. 29. <ul><li>TIME COMPLEXITY </li></ul><ul><li>Time Complexity of the linear search is found by number of comparisons made in searching a record. </li></ul><ul><li>In the worst case the desired element is present in the n th (or last) position of the array, so n comparisons are made. f (n) = O(n). </li></ul><ul><li>In the Average case , the desired element is appear in the array, and equally likely to occur at any position in the A. the number of comparisons can be any numbers 1,2,3,….n, and the each number occurs with probability p=1/n </li></ul><ul><li> then f (n) = 1.1/n+2.1/n+3.1/n+……..+n.1/n </li></ul><ul><li> =(1+2+3+………+n).1/n </li></ul><ul><li> =[(n(n+1))/2] .1/n= (n+1) /2 </li></ul><ul><li> f (n) =O[(n + 1)/2] </li></ul>
30. 30. <ul><li>In the best case , the desired element is present in the first position of the array, i.e., only one comparison is made. So f (n) = O(1). </li></ul>
31. 31. <ul><li>//PROGRAM TO IMPLEMENT LINEAR OR SEQUENTIAL SEARCHING </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>main() </li></ul><ul><li>{ char opt; </li></ul><ul><li>int arr[20],n,i,item; </li></ul><ul><li>printf (&quot;nHow many elements you want to enter in the array : &quot;); </li></ul><ul><li>scanf (&quot;%d&quot;,&n); </li></ul><ul><li>for(i=0; i < n;i++) </li></ul><ul><li>{ </li></ul><ul><li>printf (&quot;nEnter element %d : &quot;,i+1); </li></ul><ul><li>scanf (&quot;%d&quot;, &arr[i]); </li></ul><ul><li>} </li></ul><ul><li>printf (&quot;nEnter the element to be searched : &quot;); </li></ul><ul><li>scanf (&quot;%d&quot;,&item); //Input the item to be searched </li></ul><ul><li>for(i=0;i < n;i++) </li></ul><ul><li>{ </li></ul><ul><li>if (item == arr[i]) </li></ul><ul><li>{ </li></ul><ul><li>printf (&quot;n%d found at position %dn&quot;,item,i+1); </li></ul><ul><li>break; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>if (i == n) </li></ul><ul><li>printf (&quot;nItem %d not found in arrayn&quot;,item); </li></ul><ul><li>} </li></ul>
32. 32. MEMORY ALLOCATION IN C <ul><li>There are two types of memory allocations in C: </li></ul><ul><ul><li>1. Static memory allocation or Compile time </li></ul></ul><ul><ul><li>2. Dynamic memory allocation or Run time </li></ul></ul>
33. 33. <ul><li>Static Memory Allocation </li></ul><ul><li>In static or compile time memory allocations , the required memory is allocated to the variables at the beginning of the program. </li></ul><ul><li>The allocated memory space cannot be expanded to accommodate more data or cannot be reduced to accommodate less data. </li></ul><ul><li>The memory space allocated is fixed and we cannot alter the size of the allocated space any time during execution. </li></ul><ul><li>For example </li></ul><ul><li>int i, j; //Two bytes per (total 2) integer variables </li></ul><ul><li>float a[5], f ; //Four bytes per (total 6) floating point variables </li></ul>
34. 34. 2. Dynamic memory allocation or Run time <ul><li>Dynamic memory allocation is the process of allocating memory space during rum time . </li></ul><ul><li>It makes efficient use of memory by allocating the required amount of memory whenever is needed. </li></ul><ul><li>It is a unique feature in C. </li></ul><ul><li>In situations, where there is an unpredictable storage requirement, this technique is very useful . </li></ul>
35. 35. <ul><li>C provides the following dynamic allocation and de-allocation functions : </li></ul><ul><li>(i) malloc( ) (ii) calloc( ) </li></ul><ul><li>(iii) realloc( ) (iv) free( ) </li></ul>
36. 36. malloc() <ul><li>The function allocates and reserves a block of memory, specified in bytes and returns a pointer to the first byte of allocated space. </li></ul><ul><li>It reserves a block of memory by allocating specified number of bytes from the availability list(i.e from heap). </li></ul><ul><li>It allocates a block of contiguous bytes. </li></ul>
37. 37. malloc():Syntax ptr = (data_type*)malloc(size); ptr is a pointer variable of type data_type data_type can be any of the basic data type or user defined data type size is the number of bytes required
38. 38. <ul><li>Similarly, memory can be allocated to structure variables. For example </li></ul><ul><ul><li>struct Employee </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>int Emp_Code; </li></ul></ul><ul><ul><li>char Emp_Name[50]; </li></ul></ul><ul><ul><li>float Emp_Salary; </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><li>Here the structure is been defined with three variables. </li></ul><ul><li>struct Employee *ptr; </li></ul><ul><li>ptr = (struct Employee *) malloc(sizeof (struct Employee)); </li></ul>
39. 39. <ul><li>// PROGRAM TO FIND THE SUM OF n ELEMENTS USING DYNAMIC MEMORY ALLOCATION </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#define NULL 0 </li></ul><ul><li>void main() </li></ul><ul><li>{ int i=1,n,sum=0, *p,*q; </li></ul><ul><li>printf(&quot;nEnter the number of the element(s) to be added = &quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&n); //Enter the number of elements </li></ul><ul><li>p=(int *)malloc(n*sizeof(int)); //Allocating memory space </li></ul><ul><li>if(p == NULL) </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot;nnMemory allocation is failed&quot;); </li></ul><ul><li>exit(0); </li></ul><ul><li>} </li></ul><ul><li>for(q=p;q<(p+n);q++) </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot;Enter the %d element = &quot;,i++); </li></ul><ul><li>scanf(&quot;%d&quot;,q); </li></ul><ul><li>} </li></ul><ul><li>for(q=p;q<(p+n);q++) </li></ul><ul><li>sum=sum+(*q); </li></ul><ul><li>printf(&quot;nnThe SUM of no(s) is = %d&quot;,sum); </li></ul><ul><li>getch(); free(p); } </li></ul>
40. 40. malloc():An Example void main() { char *str; str=(char *)malloc(10*sizeof(char)) if(str==NULL) { printf(“Out of memoryn”); exit(1); } strcpy( str,”Hello”); printf(“String is %sn”,str); free(str); }
41. 41. calloc() <ul><li>The function allocates multiple blocks of same size,initializes all locations to zero and returns a pointer to the first byte of the allocated space. </li></ul><ul><li>It allocates a block of contiguous bytes. </li></ul><ul><li>If the specified size of memory is not available, the function returns a NULL. </li></ul>
42. 42. calloc():Syntax ptr = (data_type*)calloc(n,size); ptr is a pointer variable of type data_type data_type can be any of the basic data type or user defined data type size is the number of bytes required ‘ n’ is the number of blocks to be allocated if size bytes
43. 43. calloc():An Example void main() { char *str = NULL; str=(char *)calloc(10,sizeof(char)); if(str==NULL) { printf(“Out of memoryn”); exit(1); } strcpy( str,”Hello”); printf(“String is %sn”,str); free(str); }
44. 44. realloc() <ul><li>This function is used to alter the size of the previously allocated space which is allocated either by using malloc or calloc functions. </li></ul><ul><li>This function guarantees that reallocating the memory will not destroy the original contents of memory. </li></ul><ul><li>The contents of the old block will be copied into a newly allocated space and so,this function guarantees that the earlier contents are not lost. </li></ul>
45. 45. realloc():Syntax ptr = (data_type*)realloc(ptr,size); The address of the newly allocated memory after reallocation data_type can be any of the basic data type or user defined data type size is the number of bytes required for reallocation Starting address of allocated memory obtained previously
46. 46. realloc():Return Value <ul><li>On success, the function returns the address of reallocated block of memory. </li></ul><ul><li>If reallocation fails or if size specified is zero, the function return NULL and the original block is freed. </li></ul>
47. 47. realloc():An Example void main() { char *str; str=(char *)malloc(10); strcpy( str,”Embedded”); printf(“Address of String %s is %dn”,str,str); str=(char *)realloc(str,40); strcpy( str,”System Design”); printf(“Address of String %s is %dn”,str,str); free(str); }
48. 48. free() <ul><li>This function is used to release the memory space that has been allocated earlier. </li></ul><ul><li>This function de-allocates the allocated block of memory which is allocated by using the functions malloc,calloc and realloc. </li></ul><ul><li>It is the responsibility of the programmer to de-allocate memory whenever it is not require by the application. </li></ul>
49. 49. free():Syntax and Return Value free(ptr); ptr is a pointer to a memory block which has already been created <ul><li>There is no return value for the free() function. </li></ul>
50. 50. Differences:malloc() & calloc() <ul><li>The syntax of malloc is ptr=(data_type*)malloc(size); </li></ul><ul><li>Allocates a contiguous block of memory of specified size. </li></ul><ul><li>Allocated space will not be initialized. </li></ul><ul><li>Time efficiency is higher than calloc(). </li></ul><ul><li>The syntax of calloc is ptr=(data_type*)calloc(n,size); </li></ul><ul><li>Allocates multiple blocks of memory,each block with the same size. </li></ul><ul><li>Each byte of allocated space is initialized to zero. </li></ul><ul><li>It is more expensive in time efficiency because of zero initialization. </li></ul>malloc() calloc()
51. 51. Memory Leakage main() { int *a; a=(int*)malloc(sizeof(int)); *a=10; a=(int*)malloc(sizeof(int)); *a=20; } 10 20 <ul><li>Allocation of memory is done twice.In this case, a contains the address of the most recently allocated memory. </li></ul><ul><li>The earlier allocated memory remains inaccessable. </li></ul><ul><li>The problem where in memory is reserved but not accessible to any application is called MEMORY LEAKAGE . </li></ul>
52. 52. Dangling Pointer <ul><li>Any pointer pointing to a destroyed object or which does not contain a valid address is called a Dangling Pointer. </li></ul><ul><li>If any pointer is pointing the memory address of any variable but after some time variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem. </li></ul>Initially Latter
53. 53. Dangling Pointer main() { int *a; a=(int*)malloc(sizeof(a)); *a=20; free(a); …… ; …… ; } 20 ?