Pointer revision We known that pointer is a variable, which will the store address of another variable, declared by using * indicator. For Example: We declare a variable which point to integer data type int *ptr; Another variable x has a value 25 stored at address location (2000) in memory. int x=25; at location 2000 So, a pointer variable store the address 2000 by using ptr =&x The & operator used in as a address of operator, which will put the address of variable x into pointer variable ptr. Pointer revision If we have to access the value of x from the pointer variable So use *(value at) notation with a pointer variable ptr y=*ptr; which will give 25 to variable. A List consisting number of elements, which vary individually in size can be represented by a linked list. The use of pointers to refer to element of a data structure implies that, the element which are logically adjacent need not be physically adjacent in memory. This type of allocation is called linked allocation. Structure revision A structure is a group of variables of different types. The general format to define a structure is given as: struct struct_name{ data_type member_variable1; data_type member_variable2; } In above, word struct is a reserved word and used to define a structure template. struct_name is an optional tag which provides type name to the structure and useful in creation of variables of the structure type. The members of structures are enclosed in pair of{}, Each member consists of a type and variable name followed by semicolon (;) Structure revision Example: #include<stdio.h> printf(“Enter employee detail:”); struct employee scanf(“%d%s%d”,&e1.eno,e1.name,&e1.age); { int eno; printf(“data are:\n”); char name[20]; printf(“%d%s%d”,e1.eno,e1.name,e1.age); int age; printf(“It occupies %d bytes in memory\n” } ,sizeof(struct employee)); void main() { } struct employee e1; Structure revision In example, word employee is representing a tag/name. There are three member. eno integer type name of employee string type age in years, integer type There are two ways to create variables of structure type as discussed below. 1. The structure is declared in example? which is repeated here struct employee { int eno; char name[20]; int age; }; After making above declaration, variables of employee type are created as follows struct employee e1,e2,e3; Structure revision This declares three variables namely e1,e2 and eg3 of type employee. Each one occupies separate memory. Total memory occupied by structure variables depend on number of members. In above case, e1 occupies 24 bytes as 2 + 20+2 (2 bytes for e_no is integer, 20 bytes for name which is character string and 2 bytes for age with integer) 2. The structure prototype and variable are defined together as follows struct employee { int eno; char name[20]; int age; }e1,e2,e3; Structure revision Initialization of structure. Just like any other variable, a structure variable can also be initialized when it is created. Now a variable of employee type can be created and initialized as follow struct employee e1={101,”jay”,30}; OR e1.eno=101; strcpy(e1.name,”jay”); e1.age=30;b Structure using pointer Structures and pointers are used together to design complex data structures used in larger and complex applications using pointer of any type as member of structure, pointer to point to structure variable or array of structure, to create structure dynamically at run time etc. struct point In Example, ptr points to p1 and can be used to access x and y { coordinates of p1.when pointer is used to access structure int x; member, following syntax is used: int y; }; structure_pointermember_name struct point *ptr; Here arrow() operator associates member_name to struct point structure_pointer which points to structure variable p1={6,8}; ptr=&p1 Structure using pointer The following statement ptrx=ptrx+1; increments x coordinates of point p1 by 1, Similarly we can use printf(“%d\n”,ptrx); scanf(“%d %d”,&ptrx, &ptry); (ptry)++; It is also possible that once pointer to structure is declared, structure can be created dynamically as follows: ptr=(struct point *)malloc(sizeof(struct point)); Now ptr points to a structure of type point, following statement initialize it to (6,8) ptrx=6; ptry=8; Dynamic Memory Allocation Using Array, we can represent a Linear data structure as a sequential allocation(static Implementation) method of storage. But this method of allocation is suitable for certain application only there are so many application where sequential allocation method is unacceptable such as Unpredictable storage requirements Extensive manipulation of stored data for example in program operations such as insertion and deletion are performed frequently on the data So another method such as, Linked allocation (Dynamic memory allocation)method of storage are efficient for computer storage and time The dynamic allocation does not allocate the memory space at compile time, but allocate memory at the run time depending upon the requirement put up by the user. Hence, there is no wastage of memory as well as no failure. The dynamic allocation increases the execution time of the program because of run time allocation, but gives total control of memory in hands of programmer. It also improves reliability of a program. If memory requirement is fixed, static allocation is best Dynamic Memory Allocation The C provides memory management functions in library using <malloc.h> header file. The function which is widely used to allocate memory at run time is malloc(). The prototype of this Function is void *malloc(int); Where int argument is number of bytes to be allocated. It returns an address of starting of the block of memory which is allocated. The following statement shows the use of malloc(). ptr=(target_type *)malloc(no_of_bytes); It returns void pointer to block of memory of size no_of_bytes which must be casted to target_type which is the type of pointer ptr. The pointer ptr then can be used to access memory block allocated by the function malloc() Consider following example 1. int *p; p=(int *)malloc(sizeof(int)); The above allocates memory for one integer. To store value, we can write *p=10; Dynamic Memory Allocation 2. float *f; f=(float *)malloc(sizeof(float)); This creates a floating point variable pointed by pointer variable f dynamically. 3. char *str; str=(char *)malloc(10*sizeof(char)); This creates a string of maximum 10 characters pointed by str. 4. int *p=(int *)malloc(5*sizeof(int)); This creates an array of 5 integer pointed by p.Once array is created using above statement, either subscripted variable or pointer notation can be used to access the individual elements of the array Dynamic Memory Allocation Static memory allocation Dynamic memory allocation Memory is allocated at compile time. Memory is allocated at run time. Memory can not be changed while executing Memory can be changed while executing program program. Used in an array Used in a linked list It is fast and saves running time It is a bit slow Allocated memory stays from start to end of Memory can be allocated at any time and can be program released at any time. Implementation of this type of allocation is Implementation of this type of allocation is simple complicated Ex.: int i,j; Ex.: int *p=malloc(sizeof(int)); It is less efficient It is more efficient Linked list A simple way to represent a linear list is to expand each node to contain a link or pointer to the next node. Linked list is also called a self-referential data type. A linked list is a collection of node. Each node has two part: 1. DATA 2. NEXT DATA will store the information part and NEXT will store the address of the next node in sequence. The variable START contain the address of the first node of the list The last node will have no next node connected to it, so it will store a special value called NULL. The list with no node is called empty list, the empty list have START=NULL Linked list Example: Linked list Types of linked list: 1. Singly linked list 2. Singly circular linked list 3. Doubly linked list 4. Doubly circular linked list 5. Ordered Linked list Linked list The basic operations of linked list: 1. Traversing a linked list 2. Insert a new node at beginning 3. Insert a new node at end 4. Insert a new node at any location or in between the list 5. Inserting a new node into an ordered list 6. Delete a first node 7. Delete a last node 8. Delete a node on basis of node number 9. Searching a elements in listed list 10. Count the number of nodes in linked list