Kazalci in dinamične podatkovne strukture: seznami.

Slides:



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

Linked Lists Geletaw S..
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Linked Lists.
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Ping Zhang 2010/09/29. 2 Anatomy of a linked list A linked list consists of: –A sequence of nodes abcd Each node contains a value and a link.
Linked Lists.
Linked Lists.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
9-1 9 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists. Queues.
7-1 7 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists. Queues.
Linked Lists [AJ 15] 1. 2 Anatomy of a Linked List  a linked list consists of:  a sequence of nodes abcd each node contains a value and a link (pointer.
4 Linked-List Data Structures  Linked-lists: singly-linked-lists, doubly-linked-lists  Insertion  Deletion  Searching © 2008 David A Watt, University.
Data Structures 4 Lists and Linked List Data Structures Prof A Alkhorabi.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists.
Linked Lists. 2 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)
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)
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)
Doubly Linked Lists1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Data Structures and Algorithms Lecture 7,8 and 9 (Linked List) Instructor: Quratulain Date: 25, 29 September and 2 October, 2009 Faculty of Computer Science,
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
Chapter 9: Linked Lists Learn about linked lists. Learn about doubly linked lists. Get used to thinking about more than one possible implementation of.
7.2 Priority Queue ADTs Priority queue concepts
Data Structures Using Java1 Chapter 4 Linked Lists.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Linked Lists. 2 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)
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
10 Binary-Search-Tree Data Structure  Binary-trees and binary-search-trees  Searching  Insertion  Deletion  Traversal  Implementation of sets using.
12 Hash-Table Data Structures
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures A bank is a place where they lend you an umbrella in fair weather and ask for it.
Introduction to Data Structures and Algorithms
Uvod v C# Drugi del. Dedovanje Sintaksa Prirejanje in preverjanje tipov.
4-1 4 Linked List Data Structures Linked lists: singly-linked and doubly-linked. Insertion. Deletion. Searching. © 2001, D.A. Watt and D.F. Brown.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
Binary Tree Data Structures Binary trees and binary search trees. Searching. Insertion. Deletion. Traversal. Implementation of sets using BSTs.
Linked Lists, Queues, Stacks
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
Unit 3 Linked Lists King Fahd University of Petroleum & Minerals
Linked Lists.
Anatomy of a linked list
Doubly Linked Lists 6/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
The Tree Data Structure
Algorithm for deleting a node from a singly linked list
Java collections library
Patricia Practical Algorithm To Retrieve Information Coded In Alphanumeric. Compressed binary trie. All nodes are of the same data type (binary tries use.
Abstraktni podatkovni tipi in podatkovne strukture
R V P 2 Predavanje 04 Animacija RVP2 Animacija.
Linked Lists.
Grafični vmesnik - GUI Izdelava obrazca: lastnosti, odzivne metode
Fotografiranje hrane Predmet: Tipografija in reprodukcija.
Data Structures ADT List
Data Structures ADT List
Algoritmi.
Ela Reven, Katarina urbančič
Ugani število Napišimo program, ki si “izmisli” naključno število, potem pa nas v zanki sprašuje, katero je izmišljeno število. Če število uganemo, nas.
Doubly Linked Lists or Two-way Linked Lists
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Data Structures ADT List
Linked Lists.
8 List ADTs List concepts. List applications.
Linked Lists.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

Kazalci in dinamične podatkovne strukture: seznami

Povezani seznami Enojno povezani, dvojno povezani. Vstavljanje. Brisanje. Iskanje.

Povezani seznami(1) Povezan seznam (linked list) vsebuje zaporedje vozlišč, povezanih s povezavami (links), vsebuje pa tudi kazalec na prvo vozlišče (header, glava). Vsako vozlišče (razen zadnjega) ima svojega naslednika in vsako vozlišče (razen prvega) ima svojega predhodnika. Vsako vozlišče vsebuje en element (objekt ali vrednost) in povezavo (link) na naslednika (in morda predhodnika). antbatcatantbatcat headernode null link element link

Povezani seznami (2) Dolžina povezanega seznama je število vozlišč. Prazen seznam nima vozlišč. V povezanem seznamu: – Lahko rokujemo s posameznimi elementi. – Lahko rokujemo s povezavami in tako celo strukturo spreminjamo (kar je pri poljih nemogoče.)

Enojno povezani seznami(1) Enojno povezani seznami imajo povezave le v eno smer. Vsako vozlišče ima element in povezavo na naslednje vozlišče. Povezava je v bistvu kazalec. Pri zadnjem vozlišču, ki nima naslednika, je ta kazalec enak null. Glava (header) na prvi element je tudi kazalec. Če je seznam prazen, je ta kazalec enak null. pigdogratcatdog

Implementacija vozlišča v Javi public class Node { protected Object element; protected Node next; public Node (Object elem, Node succ) { this.element = elem; this.next = succ; } } Kazalec na objekt istega razreda Konstruktor

Študijski primer: razred z enojno povezanim seznamom public class Seznam { private Node first; public Seznam () { // konstruktor praznega seznama. this.first = null; } … } first Zaenkrat imamo le kazalec, seznam je še prazen Metode bomo podali v nadaljevanju

Študijski primer: Prehod povezanega seznama public void printFirstToLast () { // Zaporedni izpis elementov povezanega seznama. for (Node curr = first; curr != null; curr = curr.next) System.out.println(curr.element); } Animacija: antbatcat first curr antbatcat first curr antbatcat first curr antbatcat first curr antbatcat first

antbatcat first antbatcat first Študijski primer: Brisanje prvega s seznama public void deleteFirst () { // Brisanje prvega vozlisca (ob predpostavki dolzine> 0). this.first = this.first.¸next; } Animacija:

antcat first antbatcat first Študijski primer: brisanje drugega v seznamu public void deleteSecond () { //(velja, ce je dolzina > 1). Node second = first.next; first.next= second.next; } Animacija: antbatcat first second antbatcat first second antbatcat first

Študijski primer: Zamenjava dveh vozlišč public void swapFirstTwo () { Node second = first.next; first.next= second.next; second.next = first; first = second; } Animacija: antbatcat first antbatcat first second antbatcat first second antbatcat first second antbatcat first second antbatcat first

Dvojno povezan seznam Dvojno povezan seznam vsebuje zaporedje vozlišč, povezanih z linki v obeh smereh. Vsako vozlišče vsebuje en element in povezavi na predhodnika in naslednika. Če teh ni, so take povezave (kazalci) enaki null. Glava dvojno povezanega seznama ima kazalca na prvo in zadnje vozlišče. Pri praznem seznamu sta oba kazalca enaka null. pigdogratcatdog

DLL = forward SLL + backward SLL Na dvojno povezani seznam glejmo kot na superpozicijo naprej enojno povezanega in nazaj enojno povezanega seznama: antbatcat DLL: antbatcat Forward SLL: antbatcat Backward SLL:

bat Implementacija vozlišča dvojno povezanega seznama v Javi public class Node { protected Object element; protected Node pred, succ; public Node (Object elem, Node pred, Node succ) { } } antcat DLL: this.element = elem; this.pred = pred; this.succ = succ; bat

Java razred z implementacijo DLL glave public class Seznam { private Node first, last; public Seznam () { // konstruktor za prazen seznam. this.first = null; this.last = null; } … } To naredi konstruktor dog

Primer: Vzvratni prehod po seznamu public void printLastToFirst () { // Izpis vseh elementov od zadnjega do prvega for (Node curr = last; curr != null; curr = curr.pred) System.out.println(curr.element); } Animacija: curr antbatcat first last curr antbatcat first last curr antbatcat first last curr antbatcat first last antbatcat first last curr antbatcat first last antbatcat first last curr antbatcat first last curr antbatcat first last curr antbatcat first last

Primer: Vzvratni prehod po seznamu public void printLastToFirst () { // Izpis vseh elementov od zadnjega do prvega for (Node curr = last; curr != null; curr = curr.pred) System.out.println(curr.element); } Animacija: curr antbatcat first last antbatcat first last curr antbatcat first last curr antbatcat first last curr antbatcat first last

Primer: brisanje prvega vozlišča v DLL public void deleteFirst () { // Delete this DLL’s first node (assuming length > 0). Node second = this.first.succ; second.pred = null; this.first = second; } Animacija: antbatcat first last second antbatcat first last second antbatcat first last second antbatcat first last

Primer: brisanje zadnjega vozlišča v DLL public void deleteLast () { // Delete this DLL’s last node (assuming length > 0). Node penult = last.pred; penult.succ = null; last = penult; } Animacija: antbatcat first last penult antbatcat first last penult antbatcat first last penult antbatcat first last

Vstavljanje novega elementa v seznam Primeri: 1)Vstavljanje v prazen seznam; 2)Vstavljanje pred prvo vozlišče nepraznega seznama; 3)Vstavljanje za zadnje vozlišče v nepraznem seznamu; 4)Vstavljanje med vozlišča v nepraznem seznamu. Algoritem vstavljanja mora popraviti povezave v predhodniku in nasledniku.

Algoritem vstavljanja pri enojnem seznamu (SLL) To insert elem at a given point in the SLL headed by first: 1.Make ins a link to a newly-created node with element elem and successor null. 2.If the insertion point is before the first node: 2.1.Set node ins’s successor to first. 2.2.Set first to ins. 3.If the insertion point is after the node pred: 3.1.Set node ins’s successor to node pred’s successor. 3.2.Set node pred’s successor to ins. 4.Terminate.

Vstavljanje pred prvo vozlišče (animacija) batcat first To insert elem at a given point in the SLL headed by first: 1.Make ins a link to a newly-created node with element elem and successor null. 2.If the insertion point is before the first node: 2.1.Set node ins’s successor to first. 2.2.Set first to ins. 3.If the insertion point is after the node pred: 3.1.Set node ins’s successor to node pred’s successor. 3.2.Set node pred’s successor to ins. 4.Terminate. ant batcat first ins To insert elem at a given point in the SLL headed by first: 1.Make ins a link to a newly-created node with element elem and successor null. 2.If the insertion point is before the first node: 2.1.Set node ins’s successor to first. 2.2.Set first to ins. 3.If the insertion point is after the node pred: 3.1.Set node ins’s successor to node pred’s successor. 3.2.Set node pred’s successor to ins. 4.Terminate. ant batcat first ins To insert elem at a given point in the SLL headed by first: 1.Make ins a link to a newly-created node with element elem and successor null. 2.If the insertion point is before the first node: 2.1.Set node ins’s successor to first. 2.2.Set first to ins. 3.If the insertion point is after the node pred: 3.1.Set node ins’s successor to node pred’s successor. 3.2.Set node pred’s successor to ins. 4.Terminate. ant batcat first ins To insert elem at a given point in the SLL headed by first: 1.Make ins a link to a newly-created node with element elem and successor null. 2.If the insertion point is before the first node: 2.1.Set node ins’s successor to first. 2.2.Set first to ins. 3.If the insertion point is after the node pred: 3.1.Set node ins’s successor to node pred’s successor. 3.2.Set node pred’s successor to ins. 4.Terminate. ant batcat first To insert elem at a given point in the SLL headed by first: 1.Make ins a link to a newly-created node with element elem and successor null. 2.If the insertion point is before the first node: 2.1.Set node ins’s successor to first. 2.2.Set first to ins. 3.If the insertion point is after the node pred: 3.1.Set node ins’s successor to node pred’s successor. 3.2.Set node pred’s successor to ins. 4.Terminate.

Vstavljanje za danim vozliščem (animacija) dogfox first To insert elem at a given point in the SLL headed by first: 1.Make ins a link to a newly-created node with element elem and successor null. 2.If the insertion point is before the first node: 2.1.Set node ins’s successor to first. 2.2.Set first to ins. 3.If the insertion point is after the node pred: 3.1.Set node ins’s successor to node pred’s successor. 3.2.Set node pred’s successor to ins. 4.Terminate. pred dogfox first To insert elem at a given point in the SLL headed by first: 1.Make ins a link to a newly-created node with element elem and successor null. 2.If the insertion point is before the first node: 2.1.Set node ins’s successor to first. 2.2.Set first to ins. 3.If the insertion point is after the node pred: 3.1.Set node ins’s successor to node pred’s successor. 3.2.Set node pred’s successor to ins. 4.Terminate. eel predins dogfox first To insert elem at a given point in the SLL headed by first: 1.Make ins a link to a newly-created node with element elem and successor null. 2.If the insertion point is before the first node: 2.1.Set node ins’s successor to first. 2.2.Set first to ins. 3.If the insertion point is after the node pred: 3.1.Set node ins’s successor to node pred’s successor. 3.2.Set node pred’s successor to ins. 4.Terminate. eel predins dogfox first To insert elem at a given point in the SLL headed by first: 1.Make ins a link to a newly-created node with element elem and successor null. 2.If the insertion point is before the first node: 2.1.Set node ins’s successor to first. 2.2.Set first to ins. 3.If the insertion point is after the node pred: 3.1.Set node ins’s successor to node pred’s successor. 3.2.Set node pred’s successor to ins. 4.Terminate. eel predins dogfox first To insert elem at a given point in the SLL headed by first: 1.Make ins a link to a newly-created node with element elem and successor null. 2.If the insertion point is before the first node: 2.1.Set node ins’s successor to first. 2.2.Set first to ins. 3.If the insertion point is after the node pred: 3.1.Set node ins’s successor to node pred’s successor. 3.2.Set node pred’s successor to ins. 4.Terminate. eel

Vstavljanje: Implementacija v Javi public void insert (Object elem Node pred) { // Insert elem at a given point in this SLL, either after the node // pred, or before the first node if pred is null. Node ins = new Node(elem, null); if (pred == null) { ins.next = first; first = ins; } else { ins.next = pred.next; pred.next = ins; } }

Vstavljanje v dvojni seznam To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate.

DLL insertion (2) Auxiliary forward SLL insertion algorithm: To insert node ins at a given point in the forward SLL headed by first: 1.If the insertion point is before the first node: 1.1.Set node ins’s successor to first. 1.2.Set first to ins. 2.If the insertion point is after the node pred: 2.1.Set node ins’s successor to node pred’s successor. 2.2.Set node pred’s successor to ins. 3.Terminate.

Vstavljanje pred prvo vozlišče v DLL (animacija) To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. batcat first last To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins ant batcat first last To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins ant batcat first last To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins ant batcat first last succ To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins ant batcat first last succ To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ant batcat first last

Vstavljanje za zadnje vozlišče v DLL (animacija) To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. batcat first last To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins dog batcat first last To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins dog batcat first last To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins dog batcat first last succ To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins dog batcat first last succ To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. dog batcat first last

DLL : vstavljanje med vozlišči To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. fox dog first last To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins fox dog eel first last To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins fox dog eel first last To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins fox dog eel first last succ To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. ins fox dog eel first last succ To insert elem at a given point in the DLL headed by (first, last): 1.Make ins a link to a newly-created node with element elem, predecessor null, and successor null. 2.Insert ins at the insertion point in the forward SLL headed by first. 3.Let succ be ins’s successor (or null if ins has no successor). 4.Insert ins after node succ in the backward SLL headed by last. 5.Terminate. fox dog eel first last

Brisanje danega vozla s seznama Primeri: 1)deletion of a singleton node; 2)deletion of the first (but not last) node; 3)deletion of the last (but not first) node; 4)deletion of an intermediate node. The deletion algorithm needs links to the deleted node’s successor and predecessor.

Brisanje v enojno povezanem seznamu To delete node del from the SLL headed by first: 1.Let succ be node del’s successor. 2.If del = first: 2.1.Set first to succ. 3.Otherwise (if del  first): 3.1.Let pred be node del’s predecessor. 3.2.Set node pred’s successor to succ. 4.Terminate. But there is no link from node del to its predecessor, so step 3.1 can access del’s predecessor only by following links from first!

Brisanje prvega vozlišča (animacija) To delete node del from the SLL headed by first: 1.Let succ be node del’s successor. 2.If del = first: 2.1.Set first to succ. 3.Otherwise (if del  first): 3.1.Let pred be node del’s predecessor. 3.2.Set node pred’s successor to succ. 4.Terminate. antbatcat first del To delete node del from the SLL headed by first: 1.Let succ be node del’s successor. 2.If del = first: 2.1.Set first to succ. 3.Otherwise (if del  first): 3.1.Let pred be node del’s predecessor. 3.2.Set node pred’s successor to succ. 4.Terminate. antbatcat first delsucc To delete node del from the SLL headed by first: 1.Let succ be node del’s successor. 2.If del = first: 2.1.Set first to succ. 3.Otherwise (if del  first): 3.1.Let pred be node del’s predecessor. 3.2.Set node pred’s successor to succ. 4.Terminate. antbatcat first delsucc To delete node del from the SLL headed by first: 1.Let succ be node del’s successor. 2.If del = first: 2.1.Set first to succ. 3.Otherwise (if del  first): 3.1.Let pred be node del’s predecessor. 3.2.Set node pred’s successor to succ. 4.Terminate. antbatcat first garbage

Brisanje vmesnega ali zadnjega vozlišča To delete node del from the SLL headed by first: 1.Let succ be node del’s successor. 2.If del = first: 2.1.Set first to succ. 3.Otherwise (if del  first): 3.1.Let pred be node del’s predecessor. 3.2.Set node pred’s successor to succ. 4.Terminate. dogeelfox first del To delete node del from the SLL headed by first: 1.Let succ be node del’s successor. 2.If del = first: 2.1.Set first to succ. 3.Otherwise (if del  first): 3.1.Let pred be node del’s predecessor. 3.2.Set node pred’s successor to succ. 4.Terminate. dogeelfox first delsucc To delete node del from the SLL headed by first: 1.Let succ be node del’s successor. 2.If del = first: 2.1.Set first to succ. 3.Otherwise (if del  first): 3.1.Let pred be node del’s predecessor. 3.2.Set node pred’s successor to succ. 4.Terminate. dogeelfox first delsuccpred To delete node del from the SLL headed by first: 1.Let succ be node del’s successor. 2.If del = first: 2.1.Set first to succ. 3.Otherwise (if del  first): 3.1.Let pred be node del’s predecessor. 3.2.Set node pred’s successor to succ. 4.Terminate. dogeelfox first delsuccpred To delete node del from the SLL headed by first: 1.Let succ be node del’s successor. 2.If del = first: 2.1.Set first to succ. 3.Otherwise (if del  first): 3.1.Let pred be node del’s predecessor. 3.2.Set node pred’s successor to succ. 4.Terminate. dogeelfox first garbage

Časovna kompleksnost brisanja Analiza: Naj bo n dolžina enojno povezanega seznama. S korakom 3.1 moramo obiskati vsa vozlišča od prvega do brisanega. Takih vozlišč je med 0 in n–1. Povprečno število obiskanih vozlišč= (n – 1)/2 Časovna kompleksnost je torej O(n).

Implementacija brisanja v Javi public void delete (Node del) { // Delete node del from this SLL. Node succ = del.next; if (del == first) { first = succ; } else { Node pred = first; while (pred.next != del) pred = pred.next; pred.next = succ; } }

Algoritem brisanja v dvojno povezanem seznamu (DLL) To delete node del from the DLL headed by (first, last): 1.Let pred and succ be node del’s predecessor and successor. 2.Delete node del, whose predecessor is pred, from the forward SLL headed by first. 3.Delete node del, whose successor is succ, from the backward SLL headed by last. 4.Terminate.

Animacija brisanja prvega (ne pa zadnjega) vozlišča To delete node del from the DLL headed by (first, last): 1.Let pred and succ be node del’s predecessor and successor. 2.Delete node del, whose predecessor is pred, from the forward SLL headed by first. 3.Delete node del, whose successor is succ, from the backward SLL headed by last. 4.Terminate. antbatcat first last del To delete node del from the DLL headed by (first, last): 1.Let pred and succ be node del’s predecessor and successor. 2.Delete node del, whose predecessor is pred, from the forward SLL headed by first. 3.Delete node del, whose successor is succ, from the backward SLL headed by last. 4.Terminate. pred antbatcat first last succ del To delete node del from the DLL headed by (first, last): 1.Let pred and succ be node del’s predecessor and successor. 2.Delete node del, whose predecessor is pred, from the forward SLL headed by first. 3.Delete node del, whose successor is succ, from the backward SLL headed by last. 4.Terminate. pred antbatcat first last succ del To delete node del from the DLL headed by (first, last): 1.Let pred and succ be node del’s predecessor and successor. 2.Delete node del, whose predecessor is pred, from the forward SLL headed by first. 3.Delete node del, whose successor is succ, from the backward SLL headed by last. 4.Terminate. pred antbatcat first last succ del To delete node del from the DLL headed by (first, last): 1.Let pred and succ be node del’s predecessor and successor. 2.Delete node del, whose predecessor is pred, from the forward SLL headed by first. 3.Delete node del, whose successor is succ, from the backward SLL headed by last. 4.Terminate. antbatcat first last

Brisanje vmesnega vozlišča (animacija) To delete node del from the DLL headed by (first, last): 1.Let pred and succ be node del’s predecessor and successor. 2.Delete node del, whose predecessor is pred, from the forward SLL headed by first. 3.Delete node del, whose successor is succ, from the backward SLL headed by last. 4.Terminate. del fox dogeel first last To delete node del from the DLL headed by (first, last): 1.Let pred and succ be node del’s predecessor and successor. 2.Delete node del, whose predecessor is pred, from the forward SLL headed by first. 3.Delete node del, whose successor is succ, from the backward SLL headed by last. 4.Terminate. del fox dogeel first last predsucc To delete node del from the DLL headed by (first, last): 1.Let pred and succ be node del’s predecessor and successor. 2.Delete node del, whose predecessor is pred, from the forward SLL headed by first. 3.Delete node del, whose successor is succ, from the backward SLL headed by last. 4.Terminate. del fox dogeel first last predsucc To delete node del from the DLL headed by (first, last): 1.Let pred and succ be node del’s predecessor and successor. 2.Delete node del, whose predecessor is pred, from the forward SLL headed by first. 3.Delete node del, whose successor is succ, from the backward SLL headed by last. 4.Terminate. del fox dogeel first last predsucc To delete node del from the DLL headed by (first, last): 1.Let pred and succ be node del’s predecessor and successor. 2.Delete node del, whose predecessor is pred, from the forward SLL headed by first. 3.Delete node del, whose successor is succ, from the backward SLL headed by last. 4.Terminate. fox dogeel first last

Primerjava algoritmov vstavljanja in brisanja AlgoritemSLLDLL VstavljanjeO(1) BrisanjeO(n)O(n)O(1)

Iskanje dane vrednosti v seznamu Algoritem iskanja v neurejenem enojno povezanem seznamu (SLL): Iščemo obstoj vozlišča z vsebino target v seznamu SLL, ki je naslovljen s kazalcem first: 1.For each node curr in the SLL headed by first, repeat: 1.1.If target is equal to node curr’s element, terminate with answer curr. 2.Terminate with answer none. Linearno iskanje v DLL je podobno, le smer lahko izbiramo.

Kompleksnost iskanja Analiza (štejmo primerjave): Naj bo n dolžina enojno povezanega seznama. Če je iskanje uspešno: Povprečno število primerjav= (n + 1)/2 Če je iskanje neuspešno: Število primerjav = n V obeh primerih imamo časovno kompleksnost O(n).

Implementacija v Javi (primer SLL) public Node search (Object target) { // Find which (if any) node of this SLL contains an element equal to // target. Return a link to the matching node (or null if there is // none). for (Node curr = this.first; curr != null; curr = curr.succ) { if (target.equals(curr.element)) return curr; } return null; }