LIST( using dynamic memory allocation). Introduction We need dynamic Memory Allocation, when the...

Post on 16-Dec-2015

232 views 0 download

Tags:

Transcript of LIST( using dynamic memory allocation). Introduction We need dynamic Memory Allocation, when the...

LIST( using dynamic memory

allocation)

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.

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.

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.

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.

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.

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.

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.

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.

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.

Linked List

a1a2 ana3

Header

Double Linked List

a1 a2 a3

Inserting

Whenever we insert, we create a new node

Inserting an element X after node a1

a0 a1 ana2

X

12

Deleting an element

a0a1 ana2

p

While deleting element a1

Thus while printing a list. It prints a2 after a1

Temp=p->next;

P->next=temp->next;

Delete temp;

temp

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.

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

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); }

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

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){….}

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){..}

}

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) { newLink.next = first; // newLink --> old first first = newLink; // first --> newLink size++; } else { while(current.next!=null)//traveserse till the end of list { current=current.next; } current.next=newLink;//point last element to the new link } Size++; return true;//return true when the element is inserted. }

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(current.next == null) { System.out.println("element not found");

return false; } else { previous = current; // go to next Link current = current.next; } }// found it

if(current == first) // if first Link,

first = first.next; // change first else // otherwise,

previous.next = current.next; // bypass it size--; return true; }

addFirst(element) Inserts elements at the beginning of the

list. public void addFirst(Object data) {

Link newLink = new Link(data); newLink.next = first; // newLink --> old first first = newLink; // first --> newLink

}

The LinkedList Class Part of the Java API Implements the List interface using a

double-linked list

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.

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….

References http://en.wikipedia.org/wiki/Linked_list Data Structure by Yashwanth Kanethkar. Data Structures and Algorithms in Java,

Robert Lafore. http://www.thescripts.com/forum/

thread17589.html http://forum.java.sun.com/thread.jspa?

threadID=697879&messageID=4051594 http://www.bearcave.com/software/

garbage.htm