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

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Stacks, Queues, and Linked Lists
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
CHP-5 LinkedList.
Dynamic memory allocation
M180: Data Structures & Algorithms in Java
Circular Linked List. Agenda  What is a Circularly linked list  Why a Circular Linked List  Adding node in a new list  Adding node to list with nodes.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L11 (Chapter 20) Lists, Stacks,
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
CS 307 Fundamentals of Computer Science 1 Linked Lists many slides taken from Mike Scott, UT Austin.
Linked Lists. Anatomy of a linked list A linked list consists of: –A sequence of nodes abcd Each node contains a value and a link (pointer or reference)
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Data Structures Using C++ 2E
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
Introduction to Data Structures Systems Programming.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 20 Lists, Stacks,
Lists and the Collection Interface Review inheritance & collections.
LinkedList Many slides from Horstmann modified by Dr V.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
Introduction to Data Structures Systems Programming Concepts.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 16 – Basic Data Structures.
Linked Lists © John Urrutia 2013, All Rights Reserved1.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Introduction to Data Structures and Algorithms
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Linked Structures, LinkedStack
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
List Interface and Linked List Mrs. Furman March 25, 2010.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Linked Lists. Introduction In linked list each item is embedded in a link Each item has two parts – Data – Pointer to the next item in the list Insert,
List data type(ADT). Lists Elements : a 1,a 2,a 3,… a i-1,a i, a i+1,…a n Null List contains: 0 elements Types of Operations on list 1.Insertion 2.Deletion.
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
CSCS-200 Data Structure and Algorithms Lecture
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
LINKED LISTS.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Lectures linked lists Chapter 6 of textbook
CS Data Structures Chapter 8 Lists Mehmet H Gunes
COP 3538 Data Structures with OOP
Chapter 17 Object-Oriented Data Structures
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Computer Science and Engineering
ArrayLists 22-Feb-19.
Collections Framework
Linked Lists Chapter 5 (continued)
Programming II (CS300) Chapter 07: Linked Lists
TCSS 143, Autumn 2004 Lecture Notes
Presentation transcript:

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 a1a1 a2a2 anan a3a3 Header Double Linked List a1a2 a3

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

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

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   Data Structure by Yashwanth Kanethkar.  Data Structures and Algorithms in Java, Robert Lafore.  html html  readID=697879&messageID= readID=697879&messageID=  ge.htm ge.htm