Data Structures and Organization · Doubly Linked Lists What’s the advantage of a doubly linked...
Transcript of Data Structures and Organization · Doubly Linked Lists What’s the advantage of a doubly linked...
![Page 1: Data Structures and Organization · Doubly Linked Lists What’s the advantage of a doubly linked list? A potential problem with ordinary linked lists is that it’s difficult to](https://reader035.fdocuments.net/reader035/viewer/2022070718/5ede3bc3ad6a402d66698cc1/html5/thumbnails/1.jpg)
Data Structures and Organization
(p.4 – Linked Lists)
Yevhen Berkunskyi,
Computer Science dept., [email protected]
http://www.berkut.mk.ua
![Page 2: Data Structures and Organization · Doubly Linked Lists What’s the advantage of a doubly linked list? A potential problem with ordinary linked lists is that it’s difficult to](https://reader035.fdocuments.net/reader035/viewer/2022070718/5ede3bc3ad6a402d66698cc1/html5/thumbnails/2.jpg)
Linked Lists
In a linked list, each data item is embedded in a link. A link is an object of a class called something like Link.
Because there are many similar links in a list, it makessense to use a separate class for them, distinct from the linked list itself.
Each Link object contains a reference (usually called next) to the next link in the list. A field in the list itself contains a reference to the first link
![Page 3: Data Structures and Organization · Doubly Linked Lists What’s the advantage of a doubly linked list? A potential problem with ordinary linked lists is that it’s difficult to](https://reader035.fdocuments.net/reader035/viewer/2022070718/5ede3bc3ad6a402d66698cc1/html5/thumbnails/3.jpg)
A Simple Linked List
• Basic operations allowed in this simple list will be:• Inserting an item at the beginning of the list• Deleting the item at the beginning of the list• Iterating through the list to display its contents
![Page 4: Data Structures and Organization · Doubly Linked Lists What’s the advantage of a doubly linked list? A potential problem with ordinary linked lists is that it’s difficult to](https://reader035.fdocuments.net/reader035/viewer/2022070718/5ede3bc3ad6a402d66698cc1/html5/thumbnails/4.jpg)
Class Link
public class Link<T> {
private Object data;
private Link<T> next;
public Link(Object data, Link<T> next) {
this.data = data;
this.next = next;
}
public T getData() {
return (T)data;
}
public void setData(T data) {
this.data = data;
}
public Link<T> getNext() {
return next;
}
public void setNext(Link<T> next) {
this.next = next;
}
public void showLink() {
System.out.println(data);
}}
![Page 5: Data Structures and Organization · Doubly Linked Lists What’s the advantage of a doubly linked list? A potential problem with ordinary linked lists is that it’s difficult to](https://reader035.fdocuments.net/reader035/viewer/2022070718/5ede3bc3ad6a402d66698cc1/html5/thumbnails/5.jpg)
The LinkList Class
public class LinkList<T> {
private Link<T> first;
public boolean isEmpty() { return first == null; }
public void addFirst(T value) {
Link<T> t = new Link<>(value, first);
first = t;
}
public T removeFirst() {
T data = first.getData();
first = first.getNext();
return data;
}
public void showList() {
Link<T> current = first;
while (current!=null) {
current.showLink();
current = current.getNext();
}
}
}
![Page 6: Data Structures and Organization · Doubly Linked Lists What’s the advantage of a doubly linked list? A potential problem with ordinary linked lists is that it’s difficult to](https://reader035.fdocuments.net/reader035/viewer/2022070718/5ede3bc3ad6a402d66698cc1/html5/thumbnails/6.jpg)
Example of addFirst
![Page 7: Data Structures and Organization · Doubly Linked Lists What’s the advantage of a doubly linked list? A potential problem with ordinary linked lists is that it’s difficult to](https://reader035.fdocuments.net/reader035/viewer/2022070718/5ede3bc3ad6a402d66698cc1/html5/thumbnails/7.jpg)
Example of removeFirst
![Page 8: Data Structures and Organization · Doubly Linked Lists What’s the advantage of a doubly linked list? A potential problem with ordinary linked lists is that it’s difficult to](https://reader035.fdocuments.net/reader035/viewer/2022070718/5ede3bc3ad6a402d66698cc1/html5/thumbnails/8.jpg)
The displayList() Method
![Page 9: Data Structures and Organization · Doubly Linked Lists What’s the advantage of a doubly linked list? A potential problem with ordinary linked lists is that it’s difficult to](https://reader035.fdocuments.net/reader035/viewer/2022070718/5ede3bc3ad6a402d66698cc1/html5/thumbnails/9.jpg)
Finding and Deleting Specified Links
![Page 10: Data Structures and Organization · Doubly Linked Lists What’s the advantage of a doubly linked list? A potential problem with ordinary linked lists is that it’s difficult to](https://reader035.fdocuments.net/reader035/viewer/2022070718/5ede3bc3ad6a402d66698cc1/html5/thumbnails/10.jpg)
Double-Ended Lists
A double-ended list is similar to an ordinary linked list, but ithas one additional feature: a reference to the last link as well asto the first.
![Page 11: Data Structures and Organization · Doubly Linked Lists What’s the advantage of a doubly linked list? A potential problem with ordinary linked lists is that it’s difficult to](https://reader035.fdocuments.net/reader035/viewer/2022070718/5ede3bc3ad6a402d66698cc1/html5/thumbnails/11.jpg)
Double-Ended Lists
![Page 12: Data Structures and Organization · Doubly Linked Lists What’s the advantage of a doubly linked list? A potential problem with ordinary linked lists is that it’s difficult to](https://reader035.fdocuments.net/reader035/viewer/2022070718/5ede3bc3ad6a402d66698cc1/html5/thumbnails/12.jpg)
Doubly Linked Lists
What’s the advantage of a doubly linked list? A potential problem with ordinary linked lists is that it’s difficult to traverse backward along the list. A statement like
current=current.nextsteps conveniently to the next link, but there’s no corresponding way to go to the previous link.
![Page 13: Data Structures and Organization · Doubly Linked Lists What’s the advantage of a doubly linked list? A potential problem with ordinary linked lists is that it’s difficult to](https://reader035.fdocuments.net/reader035/viewer/2022070718/5ede3bc3ad6a402d66698cc1/html5/thumbnails/13.jpg)
Doubly Linked Lists
![Page 14: Data Structures and Organization · Doubly Linked Lists What’s the advantage of a doubly linked list? A potential problem with ordinary linked lists is that it’s difficult to](https://reader035.fdocuments.net/reader035/viewer/2022070718/5ede3bc3ad6a402d66698cc1/html5/thumbnails/14.jpg)
Doubly Linked Lists
![Page 15: Data Structures and Organization · Doubly Linked Lists What’s the advantage of a doubly linked list? A potential problem with ordinary linked lists is that it’s difficult to](https://reader035.fdocuments.net/reader035/viewer/2022070718/5ede3bc3ad6a402d66698cc1/html5/thumbnails/15.jpg)
Example
![Page 16: Data Structures and Organization · Doubly Linked Lists What’s the advantage of a doubly linked list? A potential problem with ordinary linked lists is that it’s difficult to](https://reader035.fdocuments.net/reader035/viewer/2022070718/5ede3bc3ad6a402d66698cc1/html5/thumbnails/16.jpg)
Questions?
![Page 17: Data Structures and Organization · Doubly Linked Lists What’s the advantage of a doubly linked list? A potential problem with ordinary linked lists is that it’s difficult to](https://reader035.fdocuments.net/reader035/viewer/2022070718/5ede3bc3ad6a402d66698cc1/html5/thumbnails/17.jpg)
Data Structures and Organization
(p.4 – Linked Lists)
Yevhen Berkunskyi,
Computer Science dept., [email protected]
http://www.berkut.mk.ua