You are on page 1of 60

Linked Lists

Lecture 4

Introduction
Linked Lists are the second most commonly used storage structures after arrays.  A versatile mechanism suitable for use in many kinds of general purpose databases.  It can also replace an array as the basis for other storage structures such as stacks and queues.  Basically a linked list comprises of a set of data items (links) connected together

2

SOFTWARE TECHNOLOGY I (C++/Data Structures)

A Todo List

Do Assignment 1 Read Chapter 4 Finish Tutorials Watch Film with friends

SOFTWARE TECHNOLOGY I (C++/Data Structures)

A Linked List

SOFTWARE TECHNOLOGY I (C++/Data Structures)

Array / Linked List of double


0 1 2 3 5.5 2.2 7.7 2.7

2.2 5.5

7.7

2.7

SOFTWARE TECHNOLOGY I (C++/Data Structures)

In brief


A Linked List comprises of a set of data items (links) connected together

SOFTWARE TECHNOLOGY I (C++/Data Structures)

A Linked List

A Link (data item)


7

SOFTWARE TECHNOLOGY I (C++/Data Structures)

A Linked List

A List of data items


8

SOFTWARE TECHNOLOGY I (C++/Data Structures)

Handling the Linked List

one class for a link

SOFTWARE TECHNOLOGY I (C++/Data Structures)

Handling the Linked List

Another class for the Linked List


10

SOFTWARE TECHNOLOGY I (C++/Data Structures)

The Link

Data

pointer to the next link

11

SOFTWARE TECHNOLOGY I (C++/Data Structures)

How the Links are Linked

12

SOFTWARE TECHNOLOGY I (C++/Data Structures)

How the Links are Linked


775AAA A257AA 222777

77AA55

13

SOFTWARE TECHNOLOGY I (C++/Data Structures)

How the Links are Linked


775AAA A257AA 222777

A257AA

222777 77AA55

77AA55

NULL
SOFTWARE TECHNOLOGY I (C++/Data Structures)

Back

14

class Link...
class Link double dData; dData;

Link next; next;

15

SOFTWARE TECHNOLOGY I (C++/Data Structures)

class Link...
class Link double dData;

data values for this object pointer to the next link object in the list

Link *next;

Back to Link Diagram

16

SOFTWARE TECHNOLOGY I (C++/Data Structures)

stubs for class link...


class Link { public double dData; // data public Link next; // reference to next link //Link(double data); //void displayLink(); }// end class Link

17

SOFTWARE TECHNOLOGY I (C++/Data Structures)

Variables of the Link class


class Link { double dData; // data item Link next; // reference to next link . .

18

SOFTWARE TECHNOLOGY I (C++/Data Structures)

The Constructor of Link class


Link::Link(double data) //constructor { dData = data; next = NULL; }

19

SOFTWARE TECHNOLOGY I (C++/Data Structures)

The displayLink method


public void displayLink() // display ourself displayLink() { System.out.print( System.out.print( { + dData + } ); }

20

SOFTWARE TECHNOLOGY I (C++/Data Structures)

The linkedList Class l


The linked list class has only one data item: item: a pointer to the first link on the list. list. This pointer is called first . It s the only permanent information the list maintains about the location of any of the links. It links. finds the other links by following the chain of pointers from first, using each link s first, next field. field.
21

SOFTWARE TECHNOLOGY I (C++/Data Structures)

Linked List keeps a pointer of first Link!!!


775AAA A257AA 222777

A257AA

222777

77AA55 77AA55

first =775AAA NULL


SOFTWARE TECHNOLOGY I (C++/Data Structures)
22

public class linkedList...

first

null
23

SOFTWARE TECHNOLOGY I (C++/Data Structures)

The Linked List Class


class LinkedList { private: Link first; LinkedList() //constructor { first = NULL; } } // end class Link
24

SOFTWARE TECHNOLOGY I (C++/Data Structures)

methods for the linkedList class


class LinkedList { public: void insertFirst(double dd); Link find(double data); void deleteFirst (); bool deleteNode(double data); void displayList(); } // end class LinkedList
25

SOFTWARE TECHNOLOGY I (C++/Data Structures)

Inserting a Node at the beginning

first

7.7

5.7

2.7

26

SOFTWARE TECHNOLOGY I (C++/Data Structures)

Inserting a Node at the beginning


2.2 first

7.7

5.7

2.7

27

SOFTWARE TECHNOLOGY I (C++/Data Structures)

Inserting a Node at the beginning


2.2 first

7.7

5.7

2.7

28

SOFTWARE TECHNOLOGY I (C++/Data Structures)

Inserting a Node at the beginning


2.2 first

7.7

5.7

2.7

29

SOFTWARE TECHNOLOGY I (C++/Data Structures)

Insert a node...
first id1 dd1 next1 In the constructor set first to null; . . . insertFirst (...) { create a new link node set nodes next field to same thing as first set first to pointer the new node }
SOFTWARE TECHNOLOGY I (C++/Data Structures)
30

Insert another node...


first id2 dd2 next2 id1 dd1 next1

insertFirst (...) { create a new link node set nodes next field to same thing as first set first to pointer the new node }
SOFTWARE TECHNOLOGY I (C++/Data Structures)
31

Insert yet another node...


first id3 dd3 next3 id2 dd2 next2 id1 dd1 next1

insertFirst (...) { create a new link node set nodes next field to same thing as first set first to pointer the new node }
SOFTWARE TECHNOLOGY I (C++/Data Structures)
32

Compare the pseudocode to the Java implementation...


insertFirst (...) { create a new link node set nodes next field to same thing as first set first to pointer the new node }

33

SOFTWARE TECHNOLOGY I (C++/Data Structures)

The insertFirst Method insertFirst


void LinkList::insertFirst(double data) { Link temp = new Link (data); //make new link temp.next = first;//newLink old first first = temp; // first newLink }

34

SOFTWARE TECHNOLOGY I (C++/Data Structures)

Deleting a Node from the beginning

first

7.7

5.7

2.7

35

SOFTWARE TECHNOLOGY I (C++/Data Structures)

Deleting a Node from the beginning

first

7.7

5.7

2.7

36

SOFTWARE TECHNOLOGY I (C++/Data Structures)

Deleting a Node from the beginning

first

7.7

5.7

2.7

37

SOFTWARE TECHNOLOGY I (C++/Data Structures)

Deleting an Element from a Linked List


We would discuss how to delete the first link in the link list. The method in our implementation is called deleteFirst(). It disconnects the first link by rerouting first to point to the second link. This second link is found by looking at the next field in the first link.

38

SOFTWARE TECHNOLOGY I (C++/Data Structures)

The deleteFirst Method


double LinkList::deleteFirst() { Link temp = first; first = first.next; double ret = temp.dData; temp. ext=null; return ret; }

???

39

SOFTWARE TECHNOLOGY I (C++/Data Structures)

first

Displaying the Linked List

7.7

5.7

2.7

To display the list, you start at first and follow the chain of pointers from link to link. A variable current points to (or technically refers to) each link in turn. It starts off pointing to first, which holds a pointer to the first link.
40

SOFTWARE TECHNOLOGY I (C++/Data Structures)

Displaying the Linked List


first current current 5.7 current

7.7

2.7

current

41

SOFTWARE TECHNOLOGY I (C++/Data Structures)

The displayList Method


void LinkedList::displayList() { current = first while current is not null display the current link make current equal to the next link }

42

SOFTWARE TECHNOLOGY I (C++/Data Structures)

Displaying the Linked List


To display the list, you start at first and follow the chain of pointers from link to link. A variable current points to (or technically refers to) each link in turn. It starts off pointing to first, which holds a pointer to the first link. The statement. current = current.next; changes current to point to the next link, because thats whats in the next field in each link.
43

SOFTWARE TECHNOLOGY I (C++/Data Structures)

The displayList Method


void LinkList:: displayList() { System.out.print(Printing list from first to last); Link current = first; // start at the beginning of the list while (current != NULL) // until end of list { current.displayLink(); current = current.next; } }
44

SOFTWARE TECHNOLOGY I (C++/Data Structures)

Displaying the Linked List Contd


The displayLink method would need to be written to display the data item in the link. This would depend on the data item(s) stored. For the Link class it would be as follows.

45

SOFTWARE TECHNOLOGY I (C++/Data Structures)

The displayLink Method


public void displayLink() // display ourself { System.out.print({+ dData + } ); }

46

SOFTWARE TECHNOLOGY I (C++/Data Structures)

Finding and Deleting Specified Links


In both cases (finding and deleting the data items) the data item is searched by going through each of the links. The links are traversed starting from the link shown by the first variable of the link list and then following each links next link. Note that the type of the data item that you would search for is the type that the link class stores. In this example it is double because the link class was written to keep a double value. When the data item is found, the link that contains the data item is returned.
47

SOFTWARE TECHNOLOGY I (C++/Data Structures)

Find a node with key id1...


first dData3 next3 dData2 next2 dData1 next1

current

current

current

48

SOFTWARE TECHNOLOGY I (C++/Data Structures)

The find Method


Link LinkList::find(double data) { current = first while current is not null if current elements data = search data return current else make current equal to the next link return null }
49

SOFTWARE TECHNOLOGY I (C++/Data Structures)

The find Method

???

Link LinkList::find(double data) { Link current = first; // start at first while (current != NULL) { if (current.dData == data) return current; else current = current.next; } return NULL; } If required item is not in the linked list, a null value is returned.
SOFTWARE TECHNOLOGY I (C++/Data Structures)

50

Deleting a Node from the beginning


previous first current

7.7

5.7

2.7

51

SOFTWARE TECHNOLOGY I (C++/Data Structures)

Deleting a Node from the beginning


previous first current

7.7

5.7

2.7

52

SOFTWARE TECHNOLOGY I (C++/Data Structures)

The delete Method


bool LinkedList::deleteNode(double data) { current = previous = first while current is not null if current elements data = search data delete current element return true else previous = current make current equal to the next link return false
53

SOFTWARE TECHNOLOGY I (C++/Data Structures)

The delete Method


bool LinkedList::deleteNode(double data) { Link current = first // start at first Link previous = current; while (current != NULL) { if (current.dData == data) { previous.next = current.next; current.next =null; return true; // found it } else { previous = current; // go to next link current = current.next; } } return false; // did not find it }
54

SOFTWARE TECHNOLOGY I (C++/Data Structures)

What will happen if we are deleting the first element in the Linked List
previous first current

7.7

5.7

2.7

Previous next = current next; Doesnt work


SOFTWARE TECHNOLOGY I (C++/Data Structures)

55

What will happen if we are deleting the first element in the Linked List
previous first current

7.7

5.7

2.7

first = first next


56

SOFTWARE TECHNOLOGY I (C++/Data Structures)

The delete Method


bool LinkedList::deleteNode(double data) { Link current = first // start at first Link previous = current; while(current != NULL) { if (current.dData == data) { if(current == first) // if first link, { first = first.next; } // change first else { previous.next = current.next; } current.next=null; return true; // found it } else { previous = current; // go to next link current = current.next; } } SOFTWARE TECHNOLOGY I (C++/Data Structures) return false; // did not find it }

57

What else might we need?




If we re going to use lists to implement stacks and queues, what else do we need? Stacks are LIFO Queues are FIFO This suggests we should create a list in which we can insert at the front or the end of the list!
58

  

SOFTWARE TECHNOLOGY I (C++/Data Structures)

Efficiency of Linked Lists


Insertion and deletion at the beginning of a linked list are very fast. They involve changing only one or fast. two pointers, which takes O(1) time. O(1 time. Finding, deleting, or insertion next to a specific item requires searching through on the average, half the items in the list. This requires O(N) list. comparisons. comparisons. An array is also O(N) for these operations, but linked list is nevertheless faster because nothing needs to be moved when an item is inserted or deleted. The increased efficiency can deleted. be significant, especially if a copy takes much longer than a comparison. comparison.
59

SOFTWARE TECHNOLOGY I (C++/Data Structures)

Efficiency of Linked Lists Contd


Of course another important advantage of linked list over arrays is that linked list uses exactly as much memory as it needs, and can expand to fill all of the available memory. The size of an array is memory. fixed when its created; this usually leads to created; inefficiency because the array is too large, or to running out of room because the array is too small. small. Vectors, which are expandable arrays may solve this problem to some extent, but they usually expand in fixed-sized increments (such as fixeddoubling the size of the array whenever its about it to overflow). This is still not an efficient use of overflow). memory as a linked list. list.
60

SOFTWARE TECHNOLOGY I (C++/Data Structures)

You might also like