Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 26 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
26
Dung lượng
0,92 MB
Nội dung
Trần Thị Thanh Nga ngattt@hcmuaf.edu.vn Khoa Công nghệ thông tin, ĐH Nông Lâm HCM Linked List Arrays Advantages: Arrays are nice and simple for storing things in a certain order Drawbacks Arrays are not very adaptable, since we have to fix the size N of an array in advance, Have to use integer indices to access its contents Linked List Linked List A linked list, is a collection of nodes that together form a linear ordering Each node is an object that stores a reference to an element and a reference, called next, to another node The next reference inside a node can be viewed as a link or pointer to another node The first of a linked list usually are called the head of the list Linked List Linked List A singly linked list keeps its elements in a certain order This order is determined by the chain of next links going from each node to its successor in the list A singly linked list does not have a predetermined fixed size, and uses space proportional to the number of its elements Do not keep track of any index numbers for the nodes in a linked list Linked List Type of List Depending on the way in which the links are used to maintain adjacency, several different types of linked lists are possible Linear singly-linked list (or simply linear list) Circular linked list Doubly linked list Linked List Singly linked list Linear singly-linked list (or simply linear list) Advantages: • Simple implementation, • Efficient, constant time O(1) insertion and removal operation Linked List Circular linked list The pointer from the last element in the list points back to the first element Linked List Doubly Linked List Pointers exist between adjacent nodes in both directions • The list can be traversed either forward or backward • Usually two pointers are maintained to keep track of the list, header and trailer • Advantage: faster (bi-directional) traversal • But, more control data (links) stored Linked List Implement a Singly Linked List To implement a singly linked list, we define a Node class which specifies the type of objects stored at the nodes of the list Given the Node class, we can define a class, LinkedList, defining the actual linked list This class keeps a reference to the head node and a variable counting the total number of nodes Linked List Node class public class Node { private T data; private Node next; public Node(T data, Node next) { this.data = data; this.next = next; } public T getData() { return data; } public void setData(T data) { this.data = data; } Linked List LinkedList class public class LinkedList { protected Node head; public LinkedList() { head = null; } } Linked List Traversing Start with the head and access each node until you reach null Linked List Traversing Node tmp = head; while(tmp != null) tmp = tmp.next; Linked List Adding an item at the head We can easily insert an element at the head of the list The main idea is: Create a new node, set its next link to refer to head Set head to point to the new node Linked List Adding an item at the head public void addFirst(T item) { Node newNode = new Node(item, head); head = newNode; } Linked List Adding an Element at the end Create a new node newNode, assign its next reference to point to the null object (1 statement) Traversing the list and stop at the last node (2 statements) Set the next reference of the last node to point to newNode (1 statement) Linked List Adding an Element at the end public void addLast(T item) { if (head == null) addFirst(item); else { Node newNode = new Node(item, null); Node tmp = head; while (tmp.next != null) tmp = tmp.next; tmp.next = newNode; } } Linked List Inserting after a node Finding a node containing "key" and insert a new node after it Linked List Inserting after a node public void insertAfter(T key, T toInsert) { Node tmp = head; while(tmp != null && !tmp.getData().equals(key)) tmp = tmp.next; if(tmp != null) tmp.next = new Node(toInsert, tmp.next); } Linked List Inserting before a node Finding a node containing "key" and insert a new node before it Linked List Inserting before a node public void insertBefore(T key, T toInsert) { if(head == null) return; if(head.getData().equals(key)) { addFirst(toInsert); return; } Node prev = null; Node cur = head; while(cur != null && !cur.getData().equals(key)) { prev = cur; cur = cur.next; } if(cur != null) prev.next = new Node(toInsert, cur); Linked List } Deletion Find a node containing "key" and delete it Linked List Deletion public void remove(T key) { if(head == null) throw new RuntimeException("cannot delete"); if( head.getData().equals(key) ) { head = head.next; return; } Node cur = head; Node prev = null; Linked List Deletion while(cur != null && !cur.getData().equals(key) ) { prev = cur; cur = cur.next; } if(cur == null) throw new RuntimeException("cannot delete"); //delete cur node prev.next = cur.next; } Linked List Question? Linked List ... singly -linked list (or simply linear list) Circular linked list Doubly linked list Linked List Singly linked list Linear singly -linked list (or simply linear list) Advantages: • Simple implementation,... linked list Linked List Type of List Depending on the way in which the links are used to maintain adjacency, several different types of linked lists are possible Linear singly -linked list (or... link or pointer to another node The first of a linked list usually are called the head of the list Linked List Linked List A singly linked list keeps its elements in a certain order This order