Diese Präsentation wurde erfolgreich gemeldet.
Die SlideShare-Präsentation wird heruntergeladen. ×

Introduction linked list

Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Nächste SlideShare
#Jai c presentation
#Jai c presentation
Wird geladen in …3
×

Hier ansehen

1 von 17 Anzeige

Weitere Verwandte Inhalte

Diashows für Sie (20)

Ähnlich wie Introduction linked list (20)

Anzeige

Aktuellste (20)

Introduction linked list

  1. 1. Introduction to Linked List Mohammed Shameer MC Dept. of Computer Science
  2. 2. Contents • Structures • Pointers • Dynamic Memory Allocation • Linked List
  3. 3. Structures Structure is a user defined data type in C Allows grouping of logically related data items of different types. Unlike an array that hold homogeneous data items, structure hold heterogeneous data items. The keyword struct is used to define a structure. struct structure_name { data_type member_variable1; data_type member_variable2; data_type member_variable3; .............. } structure_variable1, structure_variable2, ....structure_variableN;
  4. 4. Pointers • Every variables should be declared in C along with its data type. • When a variable is declared, memory is allocated for the variable. • So a variable is a named memory location. • Computer memory is sequentially addressed. • Pointer is a variable that store the memory address of another variable in memory.
  5. 5. • The general syntax for pointer declaration is datatype * ptr; Initialization ptr = &var;
  6. 6. Pointers and Structures struct struct_name{ type member1; type member2; type member3; }v1,v2; • Pointer variables can be declared and initialized as struct struct_name *ptr; ptr= &v1; • The members can be accessed using arrow (-> ) operator as. ptr->member1; ptr->member2;
  7. 7. Static and dynamic memory allocation • For an array, the size has to be fixed at the time of compilation itself. We allocate memory that satisfies our requirement. • The memory thus allocated may be much larger than actual requirement or will be lesser. Int s[10] • Dynamic memory allocation allocates memory to variables while the program is being executed. • memory is allocated only if it actually required and at run time. So the list may grow at run time.
  8. 8. Dynamic memory management •malloc(), •calloc(), •realloc() and •free(). Stack Heap
  9. 9. Malloc • This method is used to allocate a block of memory of specified size. • Malloc() does not initialize the memory and returns void type pointer that can be cast to any data type. • The syntax of this method is Data_type * ptr = (castType*) malloc (size); float* p = (float*) malloc(sizeof(float));
  10. 10. The calloc() • The calloc() method allocates continuous multiple blocks of memory from heap area and initializes it into zero. Data_type * ptr = (castType*)calloc(n, size); • Int *p =(int *) calloc(10,2); • Char *p1=(char*)calloc (10, sizeof(char)); The free() • This method releases the dynamically allocated memory. • The syntax is free(ptr) free(p); The realloc() • ptr = realloc (p, 20*sizeof(int));
  11. 11. Disadvantages of Arrays • Arrays are data structures of fixed size and length cannot be altered at run time. • Insertion and deletion of elements may require reshuffling of array elements. Thus, array manipulation is time-consuming and inefficient.
  12. 12. Linked List • A linked list is a linear collection of data elements, called nodes, where the linear order is maintained by means of pointers. • Linked list is otherwise called one way list or singly linked list. • Self referential strcuture
  13. 13. Node The basic building block of a list is a node. A node consists of two parts: • Data part that contain the information about the element, and • Link or next pointer field contains the address of the next node in the list
  14. 14. struct node { int data; struct node *link; } ; Memory allocation for a node • Struct node *p, *q, *start; • P= (strcut node*) malloc( sizeof(strcut node)); p->data = 10; • Q= (strcut node*) malloc (sizeof(Strcut node)); q->data =20; • P->link = Q ; q->link = NULL; start =p;
  15. 15. So, A linked list is • Linear DS • Heterogeneous • Dynamic • Non contigeous
  16. 16. Operations on Linked List Linked List is a dynamic data structure which supports following operations. • Traversal • Insertion • Deletion • Searching • Inverting
  17. 17. Thank you..

×