Linked list


Published on

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Linked list

  1. 1. Linked List L i n k e d
  2. 2. Limitations of Array <ul><li>To insert or remove an element at an interior location in an ArrayList requires shifting of data and is an O(n) operation. </li></ul>
  3. 3. Limitations of Array <ul><li>An array has a limited number of elements </li></ul><ul><ul><li>routines inserting a new value have to check that there is room </li></ul></ul><ul><ul><li>Can’t increase or decrease its size during execution </li></ul></ul><ul><li>Can partially solve this problem by reallocating the array as needed (how much memory to add?) </li></ul><ul><ul><li>adding one element at a time could be costly </li></ul></ul><ul><ul><li>one approach - double the current size of the array </li></ul></ul><ul><li>Array elements are stored in contiguous memory locations </li></ul><ul><ul><li>Always we won’t have enough contiguous memory </li></ul></ul><ul><li>Q: how do we know what is part of the array? </li></ul><ul><ul><li>A: have the elements keep track of each other </li></ul></ul><ul><ul><li>use pointers to connect the elements together as a LIST of things </li></ul></ul>
  4. 4. Introduction to Linked List <ul><li>It is linear collection of data items, called nodes – where linear order is maintain by means of pointers </li></ul><ul><li>Where each item contains two parts or fields </li></ul><ul><ul><li>Data : Information or actual value of an element </li></ul></ul><ul><ul><li>Link : It contains the address to its successor (and sometimes its predecessor) </li></ul></ul> Data Link
  5. 5. Introduction to Linked List <ul><li>How to define the linked list data strcture in ‘C’ </li></ul><ul><li>struct linklist </li></ul><ul><li>{ </li></ul><ul><ul><li>int data; </li></ul></ul><ul><ul><li>struct linklist *link; </li></ul></ul><ul><li>} </li></ul> Data Link
  6. 6. Introduction to Linked List <ul><li>The entire linked list is accessed from an external pointer list that points to first node in the list </li></ul><ul><li>The link field of the last node in the list contains a special value, known as NULL </li></ul><ul><li>The NULL pointer is used to signal the end of a list </li></ul> ‘ S’ 45 ‘ O’ 22 ‘ N’ 99 ‘ U’ NULL 45 22 99 list
  7. 7. Introduction to Linked List <ul><li>The list with no nodes on it is called the empty list or the null list </li></ul><ul><li>A list can be initialized to the empty list by list = NULL </li></ul><ul><li>Need way to indicate end of list (NULL pointer) </li></ul><ul><li>Need to know where list starts (first element) </li></ul><ul><li>Each element needs pointer to next element (its link) </li></ul><ul><li>Need way to allocate new element (use malloc) </li></ul><ul><li>Need way to return element not needed any more (use free) </li></ul><ul><li>Divide element into data and pointer </li></ul> ‘ S’ 45 ‘ O’ 22 ‘ N’ 99 ‘ U’ NULL 45 22 99 list
  8. 8. Types of Linked List <ul><li>There are 4 different kinds of linked lists: </li></ul><ul><ul><li>Linear singly linked list </li></ul></ul><ul><ul><li>Circular singly linked list </li></ul></ul><ul><ul><li>Two way or doubly linked list </li></ul></ul><ul><ul><li>Circular doubly linked list. </li></ul></ul> ‘ S’ 45 ‘ O’ 22 ‘ N’ 99 ‘ U’ NULL 45 22 99 list
  9. 9. Types of Linked List <ul><li>The basic operations of Linked List </li></ul><ul><ul><li>Traverse </li></ul></ul><ul><ul><li>Insert </li></ul></ul><ul><ul><ul><li>At Beginning </li></ul></ul></ul><ul><ul><ul><li>At End </li></ul></ul></ul><ul><ul><ul><li>At given position </li></ul></ul></ul><ul><ul><li>Delete </li></ul></ul><ul><ul><ul><li>At Beginning </li></ul></ul></ul><ul><ul><ul><li>At End </li></ul></ul></ul><ul><ul><ul><li>At given position </li></ul></ul></ul><ul><ul><li>Update </li></ul></ul><ul><ul><ul><li>At Beginning </li></ul></ul></ul><ul><ul><ul><li>At End </li></ul></ul></ul><ul><ul><ul><li>At given position </li></ul></ul></ul> ‘ S’ 45 ‘ O’ 22 ‘ N’ 99 ‘ U’ NULL 45 22 99 list
  10. 10. Sample Linked List Operations <ul><li>Struct linklist </li></ul><ul><li>{ </li></ul><ul><li> int data; </li></ul><ul><li>Struct linklist *next; </li></ul><ul><li>} </li></ul>ListStart = (linklist *) malloc(sizeof(linklist)); /* ListStart points to memory allocated at location 108 */
  11. 11. Sample Linked List Operations (cont) <ul><li>ListStart->data = 5; </li></ul>ListStart->next = (EPtr) malloc(sizeof(EStruct)); ListStart->next = NULL; ListStart->next->data = 9; ListStart->next->next = NULL;