Presentation is loading. Please wait.

Presentation is loading. Please wait.

LIST( using dynamic memory allocation). Introduction  We need dynamic Memory Allocation, when the data is dynamic in nature.  That is, the number of.

Similar presentations

Presentation on theme: "LIST( using dynamic memory allocation). Introduction  We need dynamic Memory Allocation, when the data is dynamic in nature.  That is, the number of."— Presentation transcript:

1 LIST( using dynamic memory allocation)

2 Introduction  We need dynamic Memory Allocation, when the data is dynamic in nature.  That is, the number of data items keeps on changing during execution of the program.  Such situations can be handled using dynamic data structures in conjunction with dynamic memory management techniques.  Dynamic data structures provides flexibility in adding, deleting or rearranging items at runtime.

3 Dynamic Memory Allocation  Dynamic memory management techniques allows us to allocate additional space or release unwanted space at runtime.  The process of allocating memory at runtime is called Dynamic Memory Allocation.  There are four library routines in C known as “ Memory Management Functions” can be used for allocating and freeing memory during program execution.

4 Memory Management Functions in C  malloc- allocates requested size of bytes and returns a pointer to the first byte of the allocated space.  Calloc – Allocates space for an array of elements,initializes them to zero then returns a pointer to memory.  Free-frees previously allocated space.  Realloc – modifies the size of previously allocated space.

5 Dynamic Memory Allocation in JAVA  Java does not support explicit dynamic memory allocation and deallocation because a sophisticated memory management system is part of the JVM that executes Java programs.  As part of the memory management system, Java uses a mechanism known as the garbage collector that periodically monitors a Java program while it is running.  Whenever a variable goes out of scope it is eligible to be garbage collected, meaning that memory assigned to the variable is deallocated and made available for use by some other part of the program.  A Java programmer never has to worry about manually deallocating memory.

6 Linked List  A linked list is one of the fundamental data structures, and can be used to implement other data structures.  It consists of a sequence of nodes, each containing arbitrary data fields and one or two references ("links") pointing to the next and/or previous nodes.  A linked list is a self-referential data type because it contains a pointer or link to another data of the same type.

7 Contd..  Several different types of linked list exist: singly-linked lists, doubly-linked lists, and circularly-linked lists.  Linked lists can be implemented in most languages.  Procedural or object-oriented languages such as C, C++, and Java typically rely on mutable references to create linked lists.

8 Singly-linked list  The simplest kind of linked list is a singly- linked list (or slist for short)  which has one link per node. This link points to the next node in the list, or to a null value or empty list if it is the final node.

9 Doubly Linked List  A more sophisticated kind of linked list is a doubly-linked list or two-way linked list.  Each node has two links: one points to the previous node, or points to a null value or empty list if it is the first node; and one points to the next, or points to a null value or empty list if it is the final node.

10 Circular-Linked List  In a circularly-linked list, the first and final nodes are linked together.  This can be done for both singly and doubly linked lists.  To traverse a circular linked list, you begin at any node and follow the list in either direction until you return to the original node.  Viewed another way, circularly-linked lists can be seen as having no beginning or end.

11 Linked List a1a1 a2a2 anan a3a3 Header Double Linked List a1a2 a3

12 Inserting  Whenever we insert, we create a new node  Inserting an element X after node a1 a0 a1ana2 X 12

13 Deleting an element a0a0 a1a1 anan a2a2 p While deleting element a 1 Thus while printing a list. It prints a 2 after a 1 Temp=p->next; P->next=temp->next; Delete temp; temp

14 List as Abstract data type  List as a interface, implementation part is hidden.  Implementation could be using arrays or linked list.  ArrayList implements list using Arrays.  LinkedList implements List using Dynamic Memory Allocation i.e. LinkedList.

15 Operations on List  Types of Operations on list  Insertion  Deletion  Search  Print  Isempty

16 List Interface  interface List  {  public boolean add(Object data);   public boolean remove(Object data);   public int indexOf(Object data);   public Object get(int index);  public int lastIndexOf(Object data);  public boolean set(int index,Object data);  public Object[] subList(int fromIndex,int toIndex);  }

17 Link( structure of node)  class Link  {  public Object value;// data item  public Link next; // next Link in list  public Link(Object dd) // constructor  {  value = dd; // ('next' is automatically  } // set to null)   public void displayLink() // display ourself  {  System.out.print(value+”=“);  }  } // end class Link

18 LinkedListDemo(implements ListDemo)  class LinkListDemo implements ListDemo  {  private Link first;//first link/node.  static int size=0;  public LinkListDemo()//constructor  {  first = null;  }  //all this functions implementation part is written  public boolean add(Object data){…}   public boolean remove(Object data){….}   public int indexOf(Object data){….}   public Object get(int index){….} 

19 Contd..  public int lastIndexOf(Object data){….}  public boolean set(int index,Object data){}  public Object[] subList(int fromIndex,int toIndex){……}  public void addFirst(Object data){..}  public void addLast(Object data){..}  public void removeFirst(Object data){..}  public void removeLast(Object data){..}  }

20 Add(element)..  Appends the element to the end of the list  public boolean add(Object val)  {  Link current = first; // search for Link  Link newLink = new Link(val);  if(first==null)  {  = first; // newLink --> old first  first = newLink; // first --> newLink  size++;  }  else  {  while(!=null)//traveserse till the end of list  { ;  } ;//point last element to the new link  }  Size++;  return true;//return true when the element is inserted.  }

21 Remove(element)..  Removes the first occurrence of the element from the list.  public boolean remove(Object val)  {  Link current = first;  Link previous = first;  while(!val.equals(current.value))  {  if( == null)  {  System.out.println("element not found");  return false;  }  else  {  previous = current; // go to next Link  current =;  }  }// found it  if(current == first) // if first Link, first =; // change first  else // otherwise, =; // bypass it  size--;  return true;  }

22 addFirst(element)  Inserts elements at the beginning of the list.  public void addFirst(Object data)  {   Link newLink = new Link(data);  = first; // newLink --> old first  first = newLink; // first --> newLink   }

23 The LinkedList Class  Part of the Java API  Implements the List interface using a double-linked list

24 LinkedList  Linked list implementation of the List interface.  Implements all optional list operations, and permits all elements (including null).  In addition to implementing the List interface, the LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list.  These operations allow linked lists to be used as a stack, queue, or double-ended queue (deque).  This class is a member of the Java Collections Framework.

25 Operations in LinkedList(ADT) Implements all the operations in List Interface. Additional operations this class provides are.  addFirst(element)-Inserts the given element at the beginning of this list.  addLast(element)-Appends the given element to the end of this list. (Identical in function to the add method)  getFirst() -Returns the first element in this list.  getLast()-Returns the last element in this list.  removeFirst() -Removes and returns the first element from this list.  removeLast()-Removes and returns the last element from this list.  And more….

26 References   Data Structure by Yashwanth Kanethkar.  Data Structures and Algorithms in Java, Robert Lafore.  7589.html 7589.html  readID=697879&messageID=4051594 readID=697879&messageID=4051594  ge.htm ge.htm


Download ppt "LIST( using dynamic memory allocation). Introduction  We need dynamic Memory Allocation, when the data is dynamic in nature.  That is, the number of."

Similar presentations

Ads by Google