Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

1 List

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

List Data Structure

Outline
I. Introduction to List Structure

II. Self-referential Structure

III. Linked Lists

 Singly Linked Lists

 Circular Lists

 Doubly Linked Lists

IV. Lists in java.util


List Data Structure
 List Data Structure:
 A sequence of items of a given base type, where items can be added,
deleted, and retrieved from any position in the list.

 List implementation:
• Can be implemented as an array, or as a dynamic array to avoid
imposing a maximum size.
• An alternative implementation is a linked list, where the items are
stored in nodes that are linked together with pointers. These two
implementations have very different characteristics.
Array
 Array:
 A linear data structure that collects elements of the same data type and
stores them in contiguous and adjacent memory locations.

 Limitations:
 Requires size information for creation
 Inserting/deleting an element in the middle of an array leads to moving
other elemenets around
Self-Referential Structure
 A structure that points to the same type of structure. It contains one or
more pointers that ultimately point to the same structure.

Employee
String name;
int age;

Linked lists trees

DataNode DataNode
Employee info; Employee info;
DataNode next; DataNode left;
DataNode right;
Linked List
 Linked List:
 A linear data structure composed of nodes, each node holding some in-
formation and a reference to another node in the list

Array representation Linked-list representation data


Node
next
Node 1 Node 2 Node 3

24 57 20
24 57 57

data Node2 Node3 Null

 Types of linked lists:


 Singly-Linked List
 Doubly-Linked List
Singly Linked List
 A list whose node includes two datafields: info/data and next.
 Info/data: store information/data/value
 next: link to its successor in this sequence

data
Node
next

Singly Linked List


Node 1 Node 2 Node 3

24 57 57

Node2 Node3 Null


Singly Linked List Implementation
class Node class MyList void add(int x)
{int info; {Node head,tail; { if(isEmpty())
Node next; MyList() {head=tail=null;} head=tail=new Node(x,null);
Node() {} boolean isEmpty() else
Node(int x, Node p) {return(head==null); {Node q =new Node(x,null);
{info=x;next=p; } tail.next=q; tail=q;
} void clear() }
} {head=tail=null; }
} void traverse()
{Node p=head;
while(p!=null)
{System.out.print(" " + p.info);
p=p.next;
}
System.out.println();
}
Node search(int x) {...}
void dele(int x) {...}
}
Singly Linked List Example (1/4)

Inserting a new node at the beginning of a list


Singly Linked List Example (2/4)

Inserting a new node at the end of a list


Singly Linked List Example (3/4)

Deleting a node from the beginning of a list


Singly Linked List Example (4/4)

Deleting a node from the end of a list


Circular Linked List
 A linked list where all nodes are connected to form a circle.
 In a circular linked list:
 The first node and the last node are connected to each other which
forms a circle.
data
Node
next

Circular Linked List


Node 1 Node 2 Node 3

24 57 57

Node2 Node3 Node1


Circular Linked List Example
Inserting a new node
Doubly Linked List
 A list whose node has three datafields: info/data, next, previous.
 Info/data: store information/data/value
 next: link to its successor in this sequence
 previous: link to its predecessor

Doubly Linked List


Node 1 Node 2 Node 3
data
Node
next 24 57 57

previous Node2 Node3 Node1

Null Node1 Node2


Doubly Linked List Implementation

class MyList
{Node head,tail;
MyList() {head=tail=null;}
boolean isEmpty()
{return(head==null); }
void clear() {head=tail=null;}
class Node
void add(int x)
{int info;
Node prev,next; {if(isEmpty())
Node() {} head=tail=new Node(x,null,null);
Node(int x, Node p, Node q) else
{info=x;prev=p; next=q; {Node q =new Node(x,tail,null);
} tail.next=q;
} tail=q;
}
}
...
}
Doubly Linked List Example (1/2)

Inserting a new node at the end of a list


Doubly Linked List Example (2/2)

Deleting a node from the end of a list


Linked List in Java.util (1/2)
boolean add(E o) Appends the specified element to the end of this list.
void addFirst(E o) Inserts the given element at the beginning of this list.
void addLast(E o) Appends the given element to the end of this list.
void clear() Removes all of the elements from this list.
E get(int index) Returns the element at the specified position in this list.
E getFirst() Returns the first element in this list.
E getLast() Returns the last element in this list.
E remove(int index) Removes the element at the specified position in
this list.
E removeFirst()Removes and returns the first element from this list.
E removeLast() Removes and returns the last element from this list.
int size() Returns the number of elements in this list.
Object[] toArray() Returns an array containing all of the elements in this list in
the correct order.
Linked List in Java.util (2/2)

import java.util.*;
class Node
{ String name;
int age; class Main
Node() {} {
Node(String name1, int age1) public static void main(String [] args)
{ name=name1; age=age1; {
} LinkedList t = new LinkedList();
void set(String name1, int age1) Node x; int n,i;
{ name=name1; age=age1; x = new Node("A01",25); t.add(x);
} x = new Node("A02",23); t.add(x);
public String toString() x = new Node("A03",21); t.add(x);
{ String s = name+" "+age; for(i=0;i<t.size();i++)
return(s); System.out.println(t.get(i));
} }
} }
Array-List in Java.util
boolean add(E o) Appends the specified element to the end of this list.
void add(int index, E o) Inserts the given element at the specified pos.
void clear() Removes all of the elements from this list.
E get(int index) Returns the element at the specified position in this list.
E remove(int index) Removes the element at the specified position in this
list.
int size() Returns the number of elements in this list.
void ensureCapacity(int minCapacity) Increases the capacity of this ArrayList
instance, if necessary, to ensure that it can hold at least the number of elements
specified by the minimum capacity argument.
void trimToSize() Trims the capacity of this ArrayList instance to be the list's
current size.
Object toArray() Returns an array containing all of the elements in this list in the
[] correct order.
Summary
 List Data-Structure:
 A sequential data structure, i.e. it is a sequence of items of a given base type.
 It can be implemented as an array, or as a dynamic array to avoid imposing a max-
imum size.

 Linked-list:
 An alternative implementation of the list,
 Items in linked list are stored in nodes that are linked together with pointers.
 Singly linked list:
• Node has a link to its successor (next node) only.
 Circular linked list:
• Nodes form a ring: The list is finite and each node has a successor.
 Doubly linked list:
• Node has links to its previous and to the next nodes.
Reading at home
Text book: Data Structures and Algorithms in Java

• 3 Fundamental Data Structures 103


• 3.1 Using Arrays - . 104
• 3.2 Singly Linked Lists - 122
• 3.3 Circularly Linked Lists - 128
• 3.4 Doubly Linked Lists - 132

You might also like