data structure software college northeastern university chapter 3 linked lists

109
Data Structure Data Structure Software College Northeastern Universit y Chapter 3 Chapter 3 Linked Lists Linked Lists

Upload: belinda-curtis

Post on 19-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data StructureData Structure Software College Northeastern University

Chapter 3Chapter 3

Linked ListsLinked Lists

Page 2: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

OverviewOverview

Linked Lists

Programming details

Common Error

Doubly Linked Lists

Circularly Linked Lists

examples

Cursors Implementation of Linked Lists

Page 3: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Variable-length arrays?

Direct access to element (By indexing) Array size is fixed-length -To expand them, you create a new, longer array, and

copy the contents of the old array into the new array This is used by function realloc() in C This is slow!

Linear time Insertion/Removal due to shift elements due to contiguous storage in memory Half of the list needs to be moved for either

operations

Page 4: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Variable-length arrays?

Solution:

-The list is not need to store contiguously.

-Attach a pointer to each item in the array, which

points to the next item.

-provides the ability to add or remove the items

anywhere in the list in constant time This is a linked list Linked lists are unbounded (maximum number of items limited only by

memory)

Page 5: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

The Linked List data structure

[0][1][2]arrayA B CArray

HeadA B CLinked list

An data item plus its pointer is called a node A node contains data item and one or more links. - The link is a reference to a node. - The link of last node is set to NULL a “head” which is a pointer to the first node in the linked list

node

Page 6: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

ZHAO QIAN SUN LI

ZHOU WU ZHENG WANG ^

H

43131

NULL3771925

Data Item Links

LI

QIANSUN

WANGWU

ZHAOZHENG

ZHOU

Address

17

131925313743

31H

Header

A simple single Linked List

Page 7: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Memory Storage of linked list

We can access all nodes through pointer “head”

HeapHeap

Page 8: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Linked listsLinked lists

Each node contains the address of the next

one in the list. We can access the first node through pointer

“head” two important pointer:

- head: the reference to the first node

- tail: the reference to the last node

Page 9: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

How to implementate?

Page 10: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Definition of the class

Using multi classUsing multi class Class of Class of Node(Node(ListNodeListNode)) Class of Class of ListList Class of Class of IteratorIterator

TType of Definitionype of Definition compoundcompound iinsidenside

Page 11: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

class List; class ListNode { friend class List; private: int data; ListNode *link; };

class List { public: ……… private: ListNode *first, *last; };

List is a Friend class

Page 12: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

class class ListList { { public:public: ………………private:private: classclass ListNodeListNode {{ public:public: int int datadata;; ListNode *linkListNode *link;; };}; ListNode *firstListNode *first, *, *lastlast;;};};

ListNode is a class inside

List

Page 13: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Linked List:Inserting a new node(1)

int int ListList::::InsertInsert ( ( constconst intint xx, , const int const int ii ) ) Insert a node with data equal to x after the i-1’th

element. (i.e., when i = 1, insert the node as the first element; when index = 2, insert the node after the first element, and so on)

If the insertion is successful, return 1. Otherwise, return 0. (If index is < 1 or > length+1 of

the list, the insertion will fail.) Steps

1. Locate i-1’th element2. Allocate memory for the new node3. Point the new node to its successor4. Point the new node’s predecessor to the new node

newNode

i’th element

Page 14: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Insert position Case 1 : insert in front of the first node

newnode→link = first ; first = newnode ;

(( BeforeBefore ) () ( AfterAfter ))

first

newnode newnodefirst

Linked List:Inserting a new node(2)

Page 15: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Case 2Case 2 :: insert in the middle of the listinsert in the middle of the list

newnode→link = p→link; p→link = newnode ;

newnode

p

newnode

p

(( BeforeBefore ) () ( AfterAfter ))

Linked List:Inserting a new node(3)

Page 16: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Case 3Case 3 :: Insert in the rearInsert in the rear

newnode→link = p→link; p→link = last = newnode ;

newnode newnode

lastp

last

p

(( BeforeBefore ) () ( AfterAfter ))

Linked List:Inserting a new node(4)

Page 17: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

int int ListList::::InsertInsert ( ( constconst intint xx, , const int const int ii ) ) {{// Insert a node with data equal to x after the i-1’th ListNodeListNode *p = first*p = first; int ; int kk = 0 = 0;; while while ( ( pp != != NULL NULL &&&& k < k < i i -1 ) -1 ) { { p = pp = p→→linklink; ; kk++++;; }} // Locate i-1’th element if if ( i<0 || ( i<0 || p == NULLp == NULL &&&& first first !=!= NULL NULL ) ) {{ coutcout << “ << “ 无效的插入位置无效的插入位置 !\n”!\n”;; return return 00;; }} ListNode *newnode=ListNode *newnode= newnew ListNodeListNode((xx, , NULLNULL));; // // Allocate memory for the new node

Linked List:Inserting a new node(5) From 0

Page 18: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

if ( first == NULL || i == 0 ) { //Insert in the front of list newnode→link = first; if ( first == NULL ) last = newnode; first = newnode; } else { //else newnode→link = p→link;

if ( p→link == NULL ) last = newnode; p→link = newnode; } return 1; }

Linked List:Inserting a new node(6)

Page 19: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

when we insert a new node : ( 1 ) insert before the i’th element , which pointer should we get first ? ( 2 ) illegal inserting position is ?

How can we judge inserting position is illegal ? ( 3 ) We should change two links when inserting , Is there an order ?

Linked List:Inserting a new node

Page 20: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Case 1: delete the firstCase 1: delete the first

Case 2: delete the other nodeCase 2: delete the other node

Delete the i’th element

According the According the pointer to i’th pointer to i’th

element ,Can we element ,Can we accomplish the accomplish the

operationoperation ?

Linked List:Deleting a new node(1)

Page 21: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

int List::Remove ( int i ) {//Delete the i’th element Node *p = first, *q; int k = 0; while ( p != NULL && k< i-1 ) { p = p→link; k++; } //locate the i-1’th element if (i<0 || p == NULL || p→link == NULL ) { cout << “无效的删除位置 !\n”; return 0; } if ( i == 0 ) { //delete the first q = first; //q point the deleted node p = first = first→link; //Update first }

Linked List:Deleting a new node(2)

Page 22: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

else { q = p→link; p→link = q→link; } if ( q == last ) last = p; //if necessary update last k = q→data; delete q; //free the node pointed by q return k;}

Linked List:Deleting a new node(3)

Page 23: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Linked List with headerLinked List with header

A node A node linking to the first node is called Header, the header cell only contains references to the first.

Non-empty listNon-empty list empty list empty list

0ana1first

last

first 0last

Page 24: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Inserting in a linked list with Inserting in a linked list with headerheader

newnode→link = p→link; if ( p→link == NULL ) last = newnode; p→link = newnode;

Page 25: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

q = p→link; p→link = q→link; delete q; if ( p→link == NULL ) last = p;

Deleting in a linked list with Deleting in a linked list with headerheader

Page 26: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Template of linked list(1)

template <class Type> class List;

template <class Type> class ListNode {friend class List<Type>; Type data; // 结点数据 ListNode<Type> *link; // 结点链接指针public: ListNode ( ); // 链表结点构造函数 ListNode ( const Type& item ); ListNode<Type> *NextNode ( ) { return

link; } // 给出当前结点的下一结点地址

Page 27: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

void InsertAfter ( ListNode<Type> *p ); // 在当前结点后插入结点 p ListNode<Type> *RemoveAfter ( ); // 摘下当前结点的下一结点};

template <class Type> class List { ListNode<Type> *first, *last;public: ListNode<Type> *GetNode ( const Type& item, ListNode<Type> *next ); // 创建数据为 item ,指针为 next 的新结点

Template of linked list(2)

Page 28: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

List ( const Type & value ) { last =first = new ListNode<Type>( value ); } // 构造函数 ~List ( ); // 析构函数 void MakeEmpty ( ); // 链表置空 int Length ( ) const; // 求链表长度 ListNode<Type> *Find ( Type value ); ListNode<Type> *Find ( int i ); int Insert ( Type value, int i ); Type *Remove ( int i ); Type *Get ( int i );};

Template of linked list(3)

Page 29: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type> ListNode<Type> :: ListNode ( ) : link (NULL) { }

template <class Type>ListNode<Type>::ListNode( const Type& item ) :

data (item), link (NULL) { }

template <class Type>void ListNode<Type>::InsertAfter ( ListNode<Type> *p ) { p→link = link; link = p; }

Implementation of linked list(1)Implementation of linked list(1)

Page 30: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type> ListNode<Type>*ListNode<Type>::RemoveAfter ( ) {// 摘下当前结点的下一结点 ListNode<Type> *tempptr = link; if ( link == NULL ) return NULL; // 没有下一结点则返回空指针 link = tempptr→link; // 重新链接 return tempptr; // 返回下一结点地址}

Implementation of linked list(2)Implementation of linked list(2)

Page 31: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type> ListNode<Type>*List<Type>::GetNode ( const Type& item, ListNode<Type> *next = NULL ) { ListNode<Type> *newnode = new ListNode<Type> ( item ); newnode →link = next; return newnode;}template <class Type> List<Type> :: ~List ( ){// 析构函数 (( 链表的公共操作链表的公共操作 )) MakeEmpty ( ); delete first; // 链表置空,再删去表头结点}

Implementation of linked list(3)Implementation of linked list(3)

Page 32: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type> void List<Type> :: MakeEmpty ( ) {// 删去链表中除表头结点外的所有其他结点 ListNode<Type> *q; while ( first→link != NULL ) { q = first→link; first→link = q→link; // 将表头结点后第一个结点从链中摘下 delete q; // 释放它 } last = first; // 修改表尾指针}

Implementation of linked list(4)Implementation of linked list(4)

Page 33: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type>int List<Type>::Length ( ) const {// 求单链表的长度 ListNode<Type> *p = first→link; // 检测指针 p 指示第一个结点 int count = 0; while ( p != NULL ) { // 逐个结点检测 p = p→link; count++; } return count;}

Implementation of linked list(5)Implementation of linked list(5)

Page 34: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type> ListNode<Type>*List <Type>::Find ( Type value ) {// 在链表中从头搜索其数据值为 value 的结点 ListNode<Type> *p = first→link; // 检测指针 p 指示第一个结点 while ( p != NULL && p→data != value )

p = p→link; return p; // p 在搜索成功时返回找到的结点地址 // p 在搜索不成功时返回空值}

Implementation of linked list(6)Implementation of linked list(6)

Page 35: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type> ListNode<Type> *List<Type> :: Find ( int i ) {// 在链表中从头搜索第 i 个结点,不计头结点 if ( i < -1 ) return NULL; if ( i == -1 ) return first; // i 应 0 ListNode<Type> *p = first→link; int j = 0; while ( p != NULL && j < i ) // j = i 停 { p = p→link; j++; } return p;}

Implementation of linked list(7)Implementation of linked list(7)

Page 36: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type> int List<Type> :: Insert ( Type value, int i ) {// 将含 valuevalue 的新元素插入到链表第 i i 个位置 ListNode<Type> *p = Find ( i-1 ); // p 指向链表第 i-1 个结点 if ( p == NULL ) return 0; ListNode<Type> *newnode = // 创建结点 GetNode ( value, p→link ); if ( p→link == NULL ) last = newnode; p→link = newnode; // 重新链接 return 1;}

Implementation of linked list(7)Implementation of linked list(7)

Page 37: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type>Type *List<Type>::Remove ( int i ) {// 从链表中删去第 ii 个结点 ListNode<Type> *p = Find (i-1), *q; if ( p == NULL || p→link == NULL ) return NULL; q = p→link; p→link = q→link; // 重新链接 Type value = new Type ( q→data ); if ( q == last ) last = p; delete q; return &value;}

Implementation of linked list(8)Implementation of linked list(8)

Page 38: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type> Type *List<Type>::Get ( int i ) {// 提取第 ii 个结点的数据 ListNode<Type> *p = Find ( i ); // p 指向链表第 ii 个结点 if ( p == NULL || p == first ) return NULL; else return & p→data;}

Implementation of linked list(8)Implementation of linked list(8)

Page 39: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Array versus Linked Lists

Linked lists are more complex to code and management than arrays, but they have some distinct advantages. Dynamic: a linked list can easily grow and shrink in size.

-We don’t need to know how many nodes will be in the list. They are created in memory as needed.

-In contrast, the size of a C array is fixed at compilation time. Easy and fast insertions and deletions

-To insert or delete an element in an array, we need to copy to temporary variables to make room for new elements or close the gap caused by deleted elements.

-With a linked list, no need to move other nodes. Only need to reset some pointers.

Page 40: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Arrays versus linked lists

Space (storage) considerations

A linked list requires pointers to nodes

An array requires the maximum number of elements to be known in advance. If that maximum is not required, space is wasted at the end of the array.

Page 41: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Arrays versus linked lists

Time considerations Most methods in a linked list require more

statements than those in an array, which may indicate more time required

Arrays are quicker at finding and altering

‘in the middle’

Linked lists are quicker at additions and removals ‘in the middle’

Page 42: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Iterator class of listIterator class of list

Iterator class of list is used to traverse nodes of list.

priciple : Iterator class is friend of List and ListNode Iterator refer to the nodes of list 。 Data member current point to the node

currently used Iterator privides some test and find

operation

Page 43: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

enum Boolean { False, True };template <class Type> class List;template <class Type> class ListIterator;

template <class Type> class ListNode { // 表结点friend class List <Type>;friend class ListIterator <Type>;public: ……… private: Type data; ListNode<Type> *link;};

Template definition(1)

Page 44: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type> class List { // 链表类public: ………private: ListNode<Type> *first, *last;};template <class Type> class ListIterator {public: ListIterator ( const List<Type> & l ) : list ( l ), current ( l.first->link ) { } // 构造函数 : 引用链表 ll, , 表头为当前结点表头为当前结点private: list<Type> list; ListNode<Type> * current;}

Template definition(2)

Page 45: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type>Boolean ListIterator<Type> :: NotNull ( ) {// 检查链表中当前元素是否非空 if ( current != NULL ) return True; else return False;}

current current

case 1 return True case 2 return False

Template definition(3)

Page 46: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type> Boolean ListIterator<Type>::NextNotNull ( ) {// 检查链表中下一元素是否非空 if ( current != NULL && current→link != NULL ) return True; else return False; }

current current

case 1 return True case 2 return False

Template definition(4)

Page 47: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type>ListNode<Type>* ListIterator<Type> :: Firster ( ) {// 返回链表中头结点的地址 current = list.first; return current;}

list.first

current list with header

Template definition(5)

Page 48: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type>Type * ListIterator<Type> :: First ( ) {// 返回链表中第一个元素的地址 if ( list.first→link != NULL ){ current = list.first->link; return &current→data; } else { current = NULL; return NULL; }}list.first

current list with header

Template definition(5)

Page 49: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type>Type* ListIterator<Type> :: Next ( ) {// 返回链表中当前结点的下一个结点的地址 if ( current != NULL && current→link != NULL ) { current = current→link; return & current→data; } else { current = NULL; return NULL; }}

current current

Template definition(6)

Page 50: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

int sum ( const List<int> &l ) { ListIterator<int> li ( l ); // 链表为空时返回 0 int *p= li.First( ); retval = 0 // 指向第一个结点 while ( p != null ){ // 链表未扫描完

retval += *p; // 累加 p= li.Next( ); } return retval;}

Calculating using Calculating using iteratoriterator

Page 51: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Variations of Linked Lists

Two problems

- we can’t get back to the beginning of the list

- from the end, and we can’t go backwards through the list.

So, circular linked lists and doubly linked lists were invented.

Page 52: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Variations of Linked Lists

Circular linked lists The last node points to the first node of the

list

How do we know when we have finished traversing the list? (Tip: check if the pointer of the current node is equal to the head.)

Head

A B

Page 53: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type> class CircList;

template <class Type> class CircListNode {friend class CircList;public: CircListNode ( Type d = 0, CircListNode<Type> *next = NULL ) : data ( d ), link ( next ) { } // 构造函数private: Type data; CircListNode<Type> *link;}

Implementation of Circular linked lists(1)

Page 54: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type> class CircList {public: CircList ( Type value ); ~CircList ( ); int Length ( ) const; Boolean IsEmpty ( ) { return first→link == first; } Boolean Find ( const Type & value ); Type getData ( ) const; void Firster ( ) { current = first; } Boolean First ( ); Boolean Next ( );

Implementation of Circular linked lists(2)

Page 55: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Boolean Prior ( ); void Insert ( const Type & value); void Remove ( );private: CircListNode<Type> *first, *current, *last;}; 插入插入

Implementation of Circular linked lists(3)

Page 56: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Variations of Circular Linked Lists

Circular linked lists construct an empty Circular linked list

CircList ( const Type & value ) { last =first = new CircListNode<Type>( value ); first->link = first; }

List ( const Type & value ) { last =first = new ListNode<Type>( value ); }

Page 57: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type> CircListNode<Type>*List <Type>::Find ( Type value )

{// 在链表中从头搜索其数据值为 value 的结点 CircListNode<Type> *p = first→link; // 检测指针 p 指示第一个结点 while ( p != first && p→data != value )

p = p→link; return (p!=first)?p:NULL; // p 在搜索成功时返回找到的结点地址 // p 在搜索不成功时返回空值}

Linked Lists: Searching in a CLL

Page 58: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Variations of Linked Lists

Doubly linked lists Each node points to not only successor but the

predecessor There are two NULL: at the first and last nodes in the

list Advantage: given a node, it is easy to visit its

predecessor. Convenient to traverse lists backwards

Head

A B

Page 59: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

p == p→lLink→rLink == p→rLink→lLinkp == p→lLink→rLink == p→rLink→lLink

Non_empty listNon_empty list empty list empty list

Doubly linked lists

Page 60: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type> class DblList;

template <class Type> class DblNode {friend class DblList<Type>;private: Type data; // 数据 DblNode<Type> *lLink, *rLink; // 指针 DblNode ( Type value, // 构造函数 DblNode<Type> *left, DblNode<Type> *right ) : data (value), lLink (left), rLink (right) { }

Implementation of Doubly linked lists(1)

Page 61: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

DblNode ( Type value ) : data (value), lLink (NULL), rLink (NULL) { }};template <class Type> class DblList {public: DblList ( Type uniqueVal ); ~DblList ( ); int Length ( ) const; int IsEmpty ( ) { return first→rlink == first; } int Find ( const Type & target ); Type getData ( ) const;

Implementation of Doubly linked lists(2)

Page 62: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

void Firster ( ) { current = first; } int First ( ); int Next ( ); int Prior ( ); int operator ! ( ) { return current != NULL; } void Insert ( const Type & value ); void Remove ( );private: DblNode<Type> *first, *current;};

Implementation of Doubly linked lists(3)

Page 63: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Doubly linked lists:SearchingSearching

Page 64: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Doubly Linked Lists:constructing

template <class Type> DblList<Type>::DblLIst ( Type uniqueVal ) {// 双向循环链表的构造函数 , 创建表头结点 first = new DblNode<Type> ( uniqueVal ); first→rLink = first→lLink = first; current = NULL;}

Page 65: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type> int DblList<Type>::Find ( const Type & target ) {// 在双向循环链表中搜索含 target 的结点,// 搜索成功返回 1 ,否则返回 0 。 DblNode<Type> *p = first→rLink; while ( p != first && p→data != target )

p = p→rLink; // 循链搜索 if ( p != first ) { current = p; return 1; } return 0;}

Doubly Linked Lists:searching

Another method?

Page 66: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

1. p→lLink = current;2. p→rLink =current→rLink;3. current→rLink = p;4. current = current→rLink;5. current→rLink→lLink = current;

11

2233

44

55

Doubly Linked Lists:Inserting(1)

Page 67: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type> void DblList<Type>::Insert ( const Type & value ) { if ( current == NULL ) // 空表情形 current = first→rLink =

new DblNode ( value, first, first ); else { // 非空表情形 current→rLink =new DblNode

( value, current, current→rLink ); current = current→rLink;

} current→rLink→lLink = current;}

Doubly Linked Lists:Inserting(2)

Page 68: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

1.1. current→rLink→lLink = current→rLink→lLink = current→lLink; current→lLink;

2.2. current→lLink→rLink = current→lLink→rLink = current→rLink;current→rLink;

11

22

Doubly Linked Lists:Deleting(1)

Page 69: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type> void DblList<Type>::Remove ( ) { if ( current != NULL ) { DblNode *temp = current; // 被删结点 current = current→rLink; // 下一结点 current→lLink = temp→lLink; // 从链中摘下 temp→lLink→rLink = current; delete temp; // 删去 if ( current == first ) if ( IsEmpty ( ) ) current = NULL; else current = current→rLink; }}

Doubly Linked Lists:Deleting(2)

Page 70: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type>int DblList<Type>::Length ( ) const {// 求双向循环链表的长度 ( 不计表头结点 ) DblNode<Type> * p = first→rLink; int count = 0; while ( p != first ) { p = p→rLink; count++; } return count; }

Doubly Linked Lists:counting

Page 71: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type> int DblList<Type>::First ( ) { if ( !IsEmpty ( ) ) // 跳过表头结点的第一个 { current = first→rLink; return 1; } current = NULL; return 0;}

template <class Type> int DblList<Type>::Next ( ) { if ( current→rLink == first )

{ current = NULL; return 0; } current = current→rLink; return 1;}

Doubly Linked Lists:Changing

Page 72: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

template <class Type> int DblList<Type>::Prior ( ) { if ( current→lLink == first )

{ current = NULL; return 0; } current = current→lLink; return 1;}

Doubly Linked Lists:Changing

Page 73: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

DLL compared to SLL

Advantages: Can be traversed in

either direction (may be essential for some programs)

Some operations, such as deletion and inserting before a node, become easier

Disadvantages: Requires more space

List manipulations are slower (because more links must be changed)

Greater chance of having bugs (because more links must be manipulated)

Page 74: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Problem:

Inserting or Deleting use new and delete to obtain and release a piece of space

dynamic management

If operations of getting or returning are frequently, it may indicate more time required

Two resolving way: Cursor Implementation of linked lists using static

memory Initially We allocate many nodes to linked into spare

lists

Linked Lists: Cursor Implementation

Page 75: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Tow important features of linked list:The data are stored in a collection of

structure, each structure contains data and a pointer to the next structure

A new structure can be obtained from the system’s global memory by a call to new and released by a call to delete

Implementation:Have a global array of structureArray index can be used in place of an

address

Linked Lists: Cursor Implementation

Page 76: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Global array of structures

Address: Array index

0 11 22 33 44 55 66 77 88 99 10

10 0

#define SpaceSize 1000typedef struct Node{ ElementType data; int next; }Node,CursorSpace[Spacesize];

Linked Lists: Cursor Implementation

Page 77: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Simulation of malloc and free

0 11 22 33 44 55 66 77 88 99 10

10 00

Equivalent for malloc and free in cursor space array:Freelist: cells that are not in any listsUse cell 0 as a header of freelistA value of 0 for next is equivalent to NULL

free list head

Linked Lists: Cursor Implementationheader

Page 78: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

freelist 1

23456788

91000

012345678910

freelistfreelist 22

header 11

3344

55

667788

99101000

012345678910

Linked Lists: Cursor Implementation

CursorAlloc to simulate malloc()

Page 79: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

int CursorAlloc(void)

{ //allocate a cell from freelist

int p;

p = CursorSpace[0].next;

CursorSpace[0].next = CursorSpace[p].next;

return p;

} //end of CursorAlloc

Linked Lists: Cursor Implementation

Page 80: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

freelist 8header 2

c 3b 4e 5g 6f 7d 11

910

00

012345678910

freelistfreelist 44header 2

c 3b 55e 88g 6f 7d 11

910

00

012345678910

Linked Lists: Cursor Implementation

CursorFree to simulate Free()

Page 81: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

void CursorFree(int p)

{ //release a cell to freelist

CursorSpace[p].next = CursorSpace[0].next;

CursorSpace[0].next = p;

} //end of CursorFree

Linked Lists: Cursor Implementation

Page 82: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Check if the node is tail node CursorSpace[r].next != first

Status Insert(elementType e,List L, int p){ int Tmpcell; Tmpcell = CursorAlloc(); if(Tmpcell == 0) return ERROR; CursorSpace[Tmpcell].data = e; CursorSpace[Tmpcell].next = CursorSpace[p].next; CursorSpace[p].next = Tmpcell; return OK;}

Linked Lists: Cursor Implementation

Page 83: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

struct Term { int coef; int exp; Term ( int c, int e ) { coef = c; exp = e; }};

class Polynomial { List<Term> poly; // 构造函数 friend Polynomial & operator + ( Polynomial &, Polynomial &); // 加法};

Linked Lists: Polynomial Implementation

UnboundedConvenient to insert or delete

Page 84: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

AH = 1 - 10x6 + 2x8 +7x14

BH = - x4 + 10x6 - 3x10 + 8x14 +4x18

Polynomial Implementation:Adding(1)

Page 85: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

PolynomialPolynomial & operator & operator + ( + ( Polynomial Polynomial && ah, ah, Polynomial Polynomial && bh bh ) ) {{ //// 多项式加法多项式加法 , , 结果由结果由 ahah 表头结点指示表头结点指示 ListNodeListNode<<Term> *paTerm> *pa,, *pb *pb,, *pc *pc,, *p *p;; ListIterator<Term> Aiter ListIterator<Term> Aiter ( ( ah.polyah.poly ) );; ListIterator<Term> Biter ListIterator<Term> Biter ( ( bh.poly bh.poly ));; //// 建立两个多项式对象 建立两个多项式对象 AiterAiter 、、 BiterBiter pa = pc = Aiter.Firsterpa = pc = Aiter.Firster( )( ); ; //// ahah 检测指针检测指针 pb = p = Biter.Firsterpb = p = Biter.Firster ( ) ( );; // // bhbh 检测指针检测指针 pa = Aiter.Next pa = Aiter.Next ( )( );; pb = Biter.Next pb = Biter.Next ( )( );; // // papa,, pb pb 越过表头结点越过表头结点 deletedelete pp;;

Polynomial Implementation:Adding(2)

Page 86: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

while ( Aiter.NotNull ( ) && Biter.NotNull ( ) ) switch ( compare ( pa→exp, pb→exp ) ) { case ‘=’ : // pa 指数等于 pb 指数 pa→coef = pa→coef + pb→coef; p = pb; pb = Biter.Next ( ); delete p; if ( !pa→coef ) { // 指数相加结果为 0 p = pa; pa = Aiter.Next ( ); delete p; } else { // 指数相加结果不为 0 pc→link = pa; pc = pa; // 链入结果链 pa = Aiter.Next ( ); }

Polynomial Implementation:Adding(3)

Page 87: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

break; case ‘>’ : // pa 指数大于 pb 指数

pc→link = pb; pc = pb; pb = Biter.Next ( ); break;

case ‘>’ : // pa 指数小于 pb 指数 pc→link = pa; pc = pa; pa = Aiter.Next ( );

} if ( Aiter.NotNull ( ) ) pc→link = pa; else pc→link = pb;}

Polynomial Implementation:Adding(4)

Page 88: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Multilists

We can get even more complicated. Suppose we have 11,000 students and 600

courses at this university. We want to record every course that a student takes and record every student in a class. This would require an 11,000 x 600 array, which is huge. Furthermore, most of the entries in the array would be empty!

S1S2S3S4S5..

C1 C2………………………………………….

Page 89: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

AA ==

0 12 9 0 0 0 0

0 0 0 0 0 0 0

-3 0 0 0 0 14 0

0 0 24 0 0 0 0

0 18 0 0 0 0 0

15 0 0 -7 0 0 0

row col row col valuevalue00

11

22

33

44

55

66

77

88

2 0 -32 0 -3

5 0 155 0 15

0 1 120 1 12

4 1 184 1 18

2 2 92 2 9

3 2 243 2 24

5 3 -75 3 -7

2 5 142 5 14

Sparse Matrix:array of Triple

A.rowsA.columnsA.TermsA.elem

678

Problem:It’s Hard to insert and remove element.

Changes to 17

Inserting(3,5,17)

Page 90: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Use Multilists Instead

S1

S2

Sn

C1 Cm

Page 91: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Multilists continued…

To see which students are in course C1, we simply go down the first column of the multilist.

To see which courses student S1 is taking, we simply go along the first row of the multilist.

Page 92: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

head

down

next

right

(a) 表头结点 (b) 非零元素结点

rightvaluedown

head raw col

a[i][j]

False i j

(c) 建立 a[i][j] 结点

Multilists:Node

Page 93: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Multilists

Page 94: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

enum Boolean { False, True };struct Triple { int row, col; float value; };

class Matrix;

class MatrixNode { // 矩阵结点定义friend class Matrix;friend istream &operator >> ( istream &, Matrix & ); // 矩阵输入重载函数private: MatrixNode *down, *right; // 列 / 行链指针 Boolean head; // 结点类型

Multilists:Implementation

Page 95: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Union { Triple triple; MatrixNode *next; } // 矩阵元素结点 (False) 或链头结点 (True) MatrixNode ( Boolean, Triple* ); // 结点构造函数}

MatrixNode::MatrixNode ( Boolean b, Triple *t ) { // 矩阵结点构造函数 head = b; // 结点类型 if ( b ) { right = next = this; } else triple = *t;}

Multilists:Implementation

Page 96: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

typedef MatrixNode *MatrixNodePtr;// 一个指针数组 , 用于建立稀疏矩阵

class Matrix {friend istream& operator >> ( istream &, Matrix & ); // 矩阵输入public: ~Matrix ( ); // 析构函数private: MatrixNode *headnode; // 稀疏矩阵的表头};

Multilists:Implementation

Page 97: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Process:( 1 ) read column number and row number of a spare matrix( 2 ) create header matrix,column headers,row headers( 3 ) initialize the pointer of header( 4 ) Iteration - read a triple of a nonzero element - allocate a node from system - link it into column linked list - link it into row linked list

Implementation of Sparse matrix:Construction

Page 98: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

istream & operator >> ( istream & is, Matrix & matrix ) { Triple s; int p; is >> s.row >> s.col >> s.value; // 输入矩阵的行数 , 列数和非零元素个数 if ( s.row > s.col ) p = s.row; else p = s.col; // 取行、列数大者 matrix.headnode = // 整个矩阵表头结点 new MatrixNode ( False, &s ); if ( !p ) { // 零矩阵时 matrix.headnode→right = matrix.headnode;

Multilists:Implementation

Page 99: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

0 1 2 3 4 5 6

Page 100: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

return is; } MatrixNodePtr *H = new MatrixNodePtr [ p ]; // 建立表头指针数组 , 指向各链表的表头 for ( int i = 0; i < p; i++ ) H[i] = new MatrixNode ( True, 0 ); int CurrentRow = 0; MatrixNode *last = H[0]; // 当前行最后结点 for ( i = 0; i < s.value; i++ ) { // 建立矩阵 Triple t; is >> t.row >> t.col >> t.value; // 输入非零元素的三元组

Multilists:Implementation

Page 101: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

if ( t.row > CurrentRow ) { // 如果行号大于当前行 , 闭合当前行 last→right = H[CurrentRow]; CurrentRow = t.row; last = H[CurrentRow]; } last = last→right = // 链入当前行 new MatrixNode ( False, &t ); H[t.col]→next = H[t.col]→next→down = last; // 链入列链表 } last→right = H[CurrentRow]; // 闭合最后一行

Multilists:Implementation

Page 102: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

// 闭合各列链表 for ( i = 0; i < s.col; i++ )

H[i]→next→down = H[i]; // 链接所有表头结点 for ( i = 0; i < p-1; i++ ) H[i]→next =H[i+1]; H[p-1]→next = matrix.headnode; matrix.headnode→right = H[0]; delete [ ] H; return is;}

Multilists:Implementation

Page 103: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

List in STL

List: a container of STL

Interface:

Reversible traversal

Front Insertion Sequence

Back Insertion Sequence

Structure : Doubly Linked list

Page 104: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

STL Example

A simple example using STL list list is defined under namespace std in list file Add the following two lines in your .h/.cpp

files#include <list>using namespace std;

list is a template container. In this example, we store integer data into the list.typedef list<int> LISTINT;

Page 105: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

STL Example

int rgTest1[] = {5,6,7};int rgTest2[] = {10,11,12};LISTINT listInt;LISTINT::iterator i;// Insert one at a timelistInt.insert (listInt.begin(), 2);listInt.insert (listInt.begin(), 1);listInt.insert (listInt.end(), 3);// output: 1 2 3cout << "listInt:";for (i = listInt.begin(); i != listInt.end(); i++)

cout << " " << *i;cout << endl;

Page 106: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

STL Example// Insert 3 fourslistInt.insert (listInt.end(), 3, 4);// output: 1 2 3 4 4 4cout << "listInt:";for (i = listInt.begin(); i != listInt.end(); ++i)

cout << " " << *i;cout << endl;// Insert an array at the endlistInt.insert (listInt.end(), rgTest1, rgTest1 + 3);// output: 1 2 3 4 4 4 5 6 7cout << "listInt:";for (i = listInt.begin(); i != listInt.end(); ++i)

cout << " " << *i;cout << endl;

Page 107: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

STL Example// Insert another LISTINTLISTINT listAnother;listAnother.insert (

listAnother.begin(), rgTest2, rgTest2+3);

listInt.insert (listInt.end(), listAnother.begin(), listAnother.end());

// output: 1 2 3 4 4 4 5 6 7 10 11 12cout << "listInt:";for (i = listInt.begin(); i != listInt.end(); ++i)

cout << " " << *i;cout << endl;

Page 108: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Exercises

1.Write a function to add two polynomials,Do not destroy the input.Use a linked list implementation.If the polynomials have M and N terms,respectively,What is the time complexity of your program?2.Write a program to find a particular element in a singly linked list.Do this both recursively and nonrecursively,and compare the running times,How big does the list have to be before the recursive version crashes?3.Write a nonrecursive procedure to reverse a single linked list in O(N) time using constant extra space.4.An Alternative to deletion strategy we have given is to use lazy deletion.to delete an element,we merely mark it deleted(using an extra bit field).The number of deleted and

Page 109: Data Structure Software College Northeastern University Chapter 3 Linked Lists

Data Structure Software College Northeastern University

Exercises

nondeleted elements in the list is kept as part of the data structure.If there are as many as deleted elements as nondeleted elements,we traverse the entie list,performing the standard deletion algorithm on all marked nodes.a.List the advantages and disadvantages of lazy deletion.b.Write routines to implement the standard linked list operations using laze deletion.

Please prepare the programming assignment of experiment 1.