30.08.2014 Views

Arete Zafiriou - Student.cs.uwaterloo.ca

Arete Zafiriou - Student.cs.uwaterloo.ca

Arete Zafiriou - Student.cs.uwaterloo.ca

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CS 234 – Lecture #8 May 30,2013<br />

Linked structures<br />

Python List (review)<br />

1. Adjustable size (it may require allo<strong>ca</strong>tion of a new array)<br />

L = [None]*100000<br />

2. Constant time access<br />

L[i] - O(1)<br />

3. Insertions and Deletions may take linear time, O(n)<br />

Example:<br />

L.pop(i)<br />

0 1 2 3 i <br />

Linked structures<br />

1. Adjustable size (memory allo<strong>ca</strong>ted when needed)<br />

- memory space allo<strong>ca</strong>ted for linked structures may not be<br />

contiguous<br />

2. Random access is not linear in the worst <strong>ca</strong>se O(n)<br />

3. Insertions and Deletions may become constant time O(1)<br />

Linked Structures<br />

1. They are used to store a collection of items<br />

2. Linked structures may be appropriate in certain <strong>ca</strong>ses<br />

3. Linked structures are created using Nodes.<br />

Data type <br />

link <br />

4. Each node contains some data and one or more links to other<br />

nodes.<br />

Linked Structure (singly linked list)<br />

- A linked list is one in which nodes are linked together in a linear<br />

order<br />

<br />

9 <br />

17 52 4 <br />

<br />

head <br />

tail


head – reference to the first node in the list<br />

tail – last node in the list<br />

link in the tail is a None reference<br />

Most nodes have no names or references.<br />

They are referenced by the link in the preceding node.<br />

Head – should be referenced by an external variable<br />

- entry point to the list<br />

- null head reference indi<strong>ca</strong>tes an empty list<br />

The Basic operations<br />

1. List traversal<br />

2. Search for an item<br />

3. Insert an item<br />

4. Delete an item<br />

…<br />

Traversing a List<br />

curNode <br />

<br />

9 17 52 4 <br />

We <strong>ca</strong>n traverse this list by creating a temporary reference<br />

curNode = head<br />

<br />

9 14 17 52 <br />

create a linked list<br />

LL = Node(9, Node(14,Node(17,Node(52,None))))<br />

N = Node(23)<br />

23 <br />

<br />

Traverse the list by advancing the temporary reference<br />

curNode = curNode._next<br />

Repeat again


Until Curnode == None, you have reached the end of the list<br />

<br />

23 <br />

9 14 17 <br />

Searching for an item in SLL<br />

Traverse the list<br />

Produce true if the item is in the list<br />

Produce false otherwise<br />

Inserting items in a SLL<br />

Insert(27)<br />

Node<br />

17 <br />

head <br />

Beginning of the list<br />

head <br />

New node<br />

27 <br />

17 <br />

newNode._next = head<br />

head = newNode<br />

1. newNode._next=head<br />

head = newNode<br />

2. head=newNode<br />

newNode._next = head<br />

Inserting in the middle of the list<br />

<br />

9 <br />

17 14 52 <br />

1 <br />

2 <br />

curNode 27 <br />

newNode


1) NewNode._next = curNode._next<br />

curNode._next = newNode<br />

Insert at the end of the list as an exercise.<br />

Removing an item from SLL<br />

Search for the item that we want to remove<br />

remove(17) predNode._next = curNode.next<br />

curNode._next = None<br />

9 14 17 52 <br />

<br />

<br />

head predNode curNode <br />

deleting the head of the SLL<br />

9 14 17 52 <br />

<br />

preNode <br />

head <br />

curNode <br />

head = head._next<br />

curNode._next = None<br />

We need to update the head in this <strong>ca</strong>se<br />

ADT sequence<br />

Data: linear collection of items<br />

Operations:<br />

Sequence()<br />

__repr__<br />

__getItem__<br />

len<br />

append<br />

insert<br />

extend<br />

pop<br />

remove<br />

reverse<br />

Implement this ADT using SLL


Python List<br />

__getItem__(self,index)<br />

L[i]<br />

Variations of Linked Lists<br />

Circularly linked lists<br />

SLL<br />

entry <br />

9 14 <br />

<br />

17 <br />

52 <br />

Doubly Linked lists<br />

<br />

9 14 17 52 <br />

Forward and Backward traversals<br />

Issues:<br />

- Efficiency of Operations<br />

- Memory requirements<br />

Linked Structures do not have to be linear<br />

15 <br />

71 <br />

55 <br />

<br />

21 <br />

57 <br />

17 <br />

91 <br />

52

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!