74
Chapter 5 Linked Lists Dr. Youssef Harrath [email protected]

Chapter 5 Linked Lists Dr. Youssef Harrath [email protected]

Embed Size (px)

Citation preview

Page 1: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

Chapter 5

Linked Lists

Dr. Youssef Harrath

[email protected]

Page 2: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

Outline

1. Introduction

2. Properties

3. Insertion and Deletion

4. Building a Linked List

5. Linked List As an ADT

6. Ordered Linked Lists

7. Doubly Linked Lists

8. Linked Lists with Header and Trailer Nodes

9. Circular Linked Lists

2Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Page 3: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

1. Introduction

A linked list is a collection of components, called nodes.

Every node (except the last node) contains the address of the

next node.

Every node has two components: data to store the relevant

information, and link to store the address of the next node.

3Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

info link

Structure of a node

struct nodeType

{

int info;

nodeType *link;

};

Page 4: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

1. Introduction

4Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

92 9317 45

head node1 node4node3node2

NULL

92 1500 63 360017 2800 45 0head

2000 360015002800

2000

node1 node4node3node2

Memory locations

Page 5: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

2. Properties

5Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

92 1500 63 360017 2800 45 0

head 2000 360015002800

2000

info linkinfoinfoinfo linklink link

Variable

head

head->info

head->link

head->link->info

Value

2000

17

2800

92

Page 6: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

2. Properties

6Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Variable

current

current->info

current->link

current->link->info

Value

2000

17

2800

92

nodeType *current;

current = head;

current

92 1500 63 360017 2800 45 0

head 2000 360015002800

2000

info linkinfoinfoinfo linklink link

Page 7: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

2. Properties

7Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

current

92 1500 63 360017 2800 45 0

head 2000 360015002800

2000

info linkinfoinfoinfo linklink link

current

92 1500 63 360017 2800 45 0

head 2000 360015002800

2000

info linkinfoinfoinfo linklink link

current = current->link;

current 2800

current->info 92

current->link 1500

current->link->info 63

Page 8: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

2. Properties

8Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

current

92 1500 63 360017 2800 45 0

head 2000 360015002800

2000

info linkinfoinfoinfo linklink link

head->link->link 1500

head->link->link->info 63

head->link->link->link 3600

head->link->link->link->info 45

current->link->link 3600

current->link->link->info 45

current->link->link->link 0 (that is, NULL)

current->link->link->link->info Does not exist

Page 9: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

2. Properties: Traversing a Linked List

9Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

The basic operations of a linked list are:

Search the list to determine whether a particular item is in the list.

Insert an item in the list.

Delete an item from the list.

The operations require the list to be traversed.

current = head; // To keep head pointing always the first node.

while(current != NULL)

{

// Process current

current = current->link;

}

Page 10: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

2. Properties: Traversing a Linked List

10Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

current = head;

while(current != NULL) // to output the elements of the linked list

{

cout<<current->info<<" ";

current = current->link;

}

current

92 1500 63 360017 2800 45 0

head 2000 360015002800

2000

info linkinfoinfoinfo linklink link

Page 11: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

3. Insertion And Deletion: Insertion

11Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

struct nodeType{int info;nodeType *link;};…nodeType *head, *p, *q, *newNode;

65 3445 76head

pnewNode = new nodeType;

newNode->info = 50;

65 3445 76head

p50newNode

Page 12: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

3. Insertion And Deletion: Insertion

12Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

newNode = new nodeType;

newNode->info = 50;

65 3445 76head

p50newNode

newNode->link = p->link;

p->link = newNode;

65 3445 76head

p50

newNode

?

Page 13: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

3. Insertion And Deletion: Insertion

13Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

What happen if we inverse the order of the two statements?

p->link = newNode;

newNode->link = p->link;

newNode->link = p->link;

65 3445 76head

p50

newNode

p->link = newNode;

Page 14: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

3. Insertion And Deletion: Insertion

14Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Using two pointers, we can simplify the insertion code.

65 3445 76head

p

50

newNode

q

p->link = newNode;

newNodep->link = q ;

The order of the two statements is not important.

p->link = newNode;

newNodep->link = q ;

Page 15: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

3. Insertion And Deletion: Deletion

15Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Consider the following linked list. Suppose that the node with

info 34 is to be deleted from the list.

p->link = p->link->link; // the link of the node with info 65 will point the node with info 76

65 3445 76head

p

65 3445 76head

pThe node still in memory

Page 16: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

3. Insertion And Deletion: Deletion

16Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

To delete the node with info 34 from the list and deallocate the

memory occupied by this node:

q = p->link;

p->link = q->link;

delete q;

6545 76head

p

65 3445 76head

p q

Page 17: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

4. Building a Linked List: Forward

17Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

nodeType *first, *last, *newNode;int num;first = NULL; // to point the first nodelast = NULL; // to point the last nodecin>>num;newNode = new nodeType; // Fill the newNode fields newNode->info = num;newNode->info = num;newNode->link = NULL;newNode->link = NULL;if(first == NULL) // empty list{first = newNode;first = newNode;last = newNode;last = newNode;}else // to insert a node at the end of the list{last->link = newNode;last->link = newNode;last = newNode;last = newNode;}

Page 18: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

4. Building a Linked List: Forward

18Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Exercise: Write a function named

buildListForward() with no parameter to

build a linked list (with many integers) and

returns a pointer to the first element. Each

new element has to be inserted at the end

of the list.

Page 19: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

4. Building a Linked List: Forward

19Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

// SolutionnodeType * buildListForward()

{

nodeType *first, *last, *newNode;

int num;

cout<<"Enter a list of integers ending by -1: ";

cin>>num;

first = NULL; // to point the first node

// here a loop to read many elements terminated by -1

return first;

}

Page 20: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

4. Building a Linked List: Forward

20Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

while(num != -1) // the loop{newNode = new nodeType;assert(newNode != NULL);newNode->info = num;newNode->link = NULL;if(first == NULL) // empty list{first = newNode;last = newNode;}else // to insert a node at the end of the list{last->link = newNode;last = newNode;}cin>>num;} // end while

Page 21: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

4. Building a Linked List: Backwards

21Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Algorithm to insert a new node at the beginning of linked list:

1.Initialize first to NULL.

2.For each item in the list:

A. Create the new node, newNode.

B. Store the item in newNode ().

C. Inset newNode before first.

D. Update the value of the pointer first

Question: convert the above algorithm to C++ code.

Page 22: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

4. Building a Linked List: Backwards

22Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

// Solution

nodeType* buildListBackward(){

nodeType *first, *newNode;int num;cout<<"Enter a list of integers ending by -1: ";cin>>num;first = NULL; // to point the first node

while(num != -1){

newNode = new nodeType;assert(newNode != NULL);newNode->info = num;newNode->link = first;first = newNode;cin>>num;

} // end whilereturn first;

}

Page 23: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT

23Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Starting from this section, the linked lists will be treated as an ADT by the

use of templates (general type will be used for the info field).

The basic operations on linked lists are:

1. Initialize the list7. Retrieve the info contained in the last node

2. Check whether the list is empty 8. Search the list for a given item

3.Output the list 9. Insert an item in the list

4. Find the length of the list 10. Delete an item from the list

5. Destroy the list 11. Make a copy of the linked list

6. Retrieve the info contained in the first node

Page 24: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT

24Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type> // Part Istruct nodeType{

Type info;nodeType<Type> *link;

};template<class Type>class linkedListType{

friend ostream& operator<<(ostream&, const linkedListType<Type>&);public:

const linkedListType<Type>& operator=(const linkedListType<Type>&);void initializeList();bool isEmtyList();int length();

Page 25: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT

25Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

void destroyList(); // Part IIType front();Type back();bool serach(const Type& searchItem);void insertFirst(const Type& newItem);void insertLast(const Type& newItem);void deleteNode(const Type& deleteItem);linkedListType();linkedListType(const linkedListType<Type>& otherList);~linkedListType();

protected:int count;nodeType<Type> *first;nodeType<Type> *last;

private:void copyList(const linkedListType<Type> otherList);

};

Page 26: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT

26Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

first

last

count

info link info link info link

list

linkedListType<Type> list;

node1 node2 nodecount

nodeType<Type> objects

Page 27: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT: Remarks

27Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

The data members of the class linkedListType are protected , not private,

because other classes will be derived from this class.

The function copyList is declared private because we use this function

only to implement the copy constructor and the function to overload the

assignment operator.

The definition of the class linkedListType includes a member function to

overload the assignment operator. This must be done for every class

including pointer data members.

For the same reason, the class linkedListType includes a copy

constructor.

Page 28: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT: isEmptyList() and default Constructor

28Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

bool linkedListType<Type>::isEmtyList()

{

return(first == NULL);

}

template<class Type>

linkedListType<Type>::linkedListType()

{

first = NULL;

last = NULL;

count = 0;

}

Page 29: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT: destroyList()

29Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

65 3445 76first

temp last

65 3445 76first

last

65 3445 76first

temp last

65 34 76first

last

Page 30: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT: destroyList()

30Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

void linkedListType<Type>::destroyList()

{

nodeType<Type> *temp;

while(first != NULL)

{

temp = first;

first = first->link;

delete temp;

}

last = NULL;

count = 0;

}

Page 31: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT: initializeList()

31Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

void linkedListType<Type>::initializeList()

{

destroyList();

}

Page 32: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT: << operator

32Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

ostream& operator<<(ostream& osObject, const linkedListType<Type>& list)

{

nodeType<Type> *current; // pointer to traverse the list

current = list.first;

while(current != NULL)

{

osObject<<current->info<<" ";

current = current->link;

}

return osObject;

}

Page 33: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT: length() and front()

33Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

int linkedListType<Type>::length()

{

return count;

}

template<class Type>

Type linkedListType<Type>::front()

{

assert(last != NULL);

return first->info;

}

Page 34: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT: back()

34Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

Type linkedListType<Type>::back()

{

assert(last != NULL);

return last->info;

}

Page 35: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT: searchItem()

35Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Algorithm

The member function search searches for a given item. If the item is

found, it returns true. The search must start from the first node.

1. Compare the search item with the current node in the list. If

the info of the current node is the same as the search item,

stop the search; otherwise, make the next node the current

node.

2. Repeat step 1 until either the item is found or no more data is

left in the list to compare with the search item.

Page 36: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT: searchItem()

36Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

bool linkedListType<Type>::search(const Type& searchItem)

{

nodeType<Type> *current;

bool found;

current = first;

found = false;

while(current != NULL && !found)

if(current->info == searchItem)

found = true;

else

current = current->link;

return found;

}

Page 37: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT: insertFirst()

37Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Algorithm

The member function insertFirst inserts the new item at the

beginning of the list. The Algorithm is:

1. Create a new node.

2. If unable to create the node, terminate the program.

3. Store the new item in the new node.

4. Insert the node before first.

5. Increment count by 1.

Page 38: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT: insertFirst()

38Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

void linkedListType<Type>::insertFirst(const Type& newItem)

{

nodeType<Type> *newNode;

newNode = new nodeType<Type>;

assert(newNode != NULL);

newNode->info = newItem;

newNode->link = first;

first = newNode;

count++;

if(last == NULL)

last = newNode;

}

Page 39: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT: insertLast()

39Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>void linkedListType<Type>::insertLast(const Type& newItem){

nodeType<Type> *newNode;newNode = new nodeType<Type>;assert(newNode != NULL);newNode->info = newItem;newNode->link = NULL;if(first == NULL){

first = newNode;last = newNode;

}else{

last->link = newNode;last = newNode;

}count++;

}

Page 40: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT: deleteNode()

40Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Case1:deleteNode(10)

Case2: deleteNode(10)

first

last

count 0

first

last

count 3

10 34 76

Case4: deleteNode(10)

first

last

count 3

25 34 76

Case3: deleteNode(34) or deleteNode(76)

first

last

count 3

10 34 76

Page 41: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT: deleteNode(): ALGORITHM

41Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

if the list is empty

Output(cannot delete from an empty list);

else

{

if the first node is the node with the given info,

adjust first, last (if necessary), count, and deallocate the memory;

else

{

search the list for the node with the given info

if such a node is found, delete it and adjust the values of last (if necessary),

and count.

}

}

Page 42: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT: deleteNode()

42Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Program

Page 43: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT: copyList()

43Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Algorithm

• copyList makes an identical copy of a linked list.

• We traverse the list to be copied starting from the first node.

• For each node in the original list, we:

1. Create a new node.

2. Copy the info of the node (in the original list) into the new

node.

3. Insert the new node at the end of the list being created.

Page 44: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT: copyList()

44Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Program

Page 45: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT: Destructor and Copy Constructor

45Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

linkedListType<Type>::~linkedListType() //destructor

{

destroyList();

}

template<class Type>

linkedListType<Type>::linkedListType(const linkedList<Type>& otherList)

{

first = NULL;

copyList(otherList);

}

Page 46: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

5. Linked List As an ADT: Overloading the Assignment Operator

46Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

const linkedListType<Type>& linkedListType<Type> ::operator=

(const linkedListType<Type>& otherList)

{

if(this != &otherList) //avoid self-copy

copyList(otherList);

return *this;

}

Page 47: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

6. Ordered Linked Lists

47Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

This section discusses how to build ordered linked list

The basic operations on linked lists are:

1. Initialize the list 6. Search the list for a given item

2. Check whether the list is empty 7. Insert an item in the list

3.Output the list 8. Delete an item from the list

4. Output the list 9. Find the length of the list

5. Destroy the list 10. Make a copy of the list

Page 48: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

6. Ordered Linked Lists

48Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

class orderedLinkedListType: public

linkedListType<Type>

{

public:

bool serach(const Type& searchItem);

void insertNode(const Type& newItem);

void deleteNode(const Type& newItem);

};

Page 49: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

6. Ordered Linked Lists: Remarks

49Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

The class orderedLinkedListType is derived from the class

linkedListType.

The elements of the ordered list are to be ordered according to a criteria.

Some functions can be used from the class linkedListType (like

insertFirst and insertLast.

No need for the pointer last (it is set to NULL) because the elements are

ordered.

The function back is not to be used to return the last element because

the pointer last is set to NULL.

Page 50: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

6. Ordered Linked Lists: search()

50Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Algorithm

The function search is similar to the search function for general lists.

Here because the list is sorted, we can improve the search

algorithm.

1. Compare the search item with the current node in the list. If

the info of the current node is greater than or equal to the

search item, stop the search; otherwise, make the next node

the current node.

2. Repeat step 1 until either an item in the list is greater than or

equal to the search item is found, or no more data is left in the

list to compare with the search item.

Page 51: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

6. Ordered Linked Lists: search()

51Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>bool orderedLinkedListType<Type>::search(const Type& searchItem){

bool found;nodeType<Type> *current;found = false;current = first;while(current != NULL && !found)

if(current->info >=searchItem) found = true;else current = current->link;

if(found) found = (current->info == searchItem);return found;

}

Page 52: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

6. Ordered Linked Lists: insertNode()

52Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Case 1: The list is initially empty: insert the new item at the

beginning and increment count by 1.

Case 2: The list is not empty and the new item is smaller than the

smallest in the list: insert the new item at the beginning and

increment count by 1.

Case 3: The list is not empty and the new item is larger than the

first item.

Case 3a: The new item is larger than the last item. Insert the new

item at the end of the list and increment count by 1.

Case 3b: The new item is to be inserted somewhere in the middle.

Page 53: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

6. Ordered Linked Lists: insertNode()

53Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Program

Page 54: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

6. Ordered Linked Lists: deleteNode()

54Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Case 1: The list is initially empty. We have an error.

Case 2: The item to be deleted is contained in the first node of the

list. We must adjust the head pointer of the list – first .

Case 3: The item to be deleted is somewhere in the list. In this

case, current points to the node containing the item to be deleted,

and trailCurrent points to the node just before the node pointed to

by current.

Case 4: The list is not empty, but the item to be deleted is not in

the list.

After deleting the node, count is decremented by 1.

Page 55: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

6. Ordered Linked Lists: deleteNode()

55Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Program

Page 56: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

7. Doubly Linked Lists

56Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

A doubly linked list is a linked list in which every node has a next

pointer and a back pointer.

15 20 39

last

first

Page 57: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

7. Doubly Linked Lists: Class Part I

57Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>struct nodeType{

Type info;nodeType<Type> *next;nodeType<Type> *back;

};

template<class Type>class doublyLinkedList{

friend ostream& operator<< (ostream&, const doublyLinkedList<Type>&);public:

const doublyLinkedList<Type>& operator=(const doublyLinkedList<Type>&);void initializeList();bool isEmptyList();void destroy();void reversePrint();int length();

Page 58: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

7. Doubly Linked Lists: Class Part II

58Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Type front();

Type back();

bool search(const Type& searchItem);

void insertNode(const Type& insertItem);

void deleteNode(const Type& deleteItem);

doublyLinkedList();

doublyLinkedList(const doublyLinkedList<Type>& otherList);

protected:

int count;

nodeType<Type> *first;

nodeType<Type> *last;

private:

void copyList(const doublyLinkedList<Type>& otherList);

};

Page 59: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

7. Doubly Linked Lists: Default constructor and isEmptyList

59Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>doublyLinkedList<Type>::doublyLinkedList(){

first = NULL;last = NULL;count = 0;

}

template<class Type>

bool

doublyLlinkedList<Type>::isEmtyList()

{

return(first == NULL);

}

Page 60: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

7. Doubly Linked Lists: destroy

60Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

void doublyLinkedList<Type>::destroy()

{

nodeType<Type> *temp;

while(first != NULL)

{

temp = first;

first = first->next;

delete temp;

}

last = NULL;

count = 0;

}

Page 61: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

7. Doubly Linked Lists: initializeList and length

61Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

void doublyLinkedList<Type>::initializeList()

{

destroyList();

}

template<class Type>

int doublyLinkedList<Type>::length()

{

return count;

}

Page 62: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

7. Doubly Linked Lists: operator<<

62Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

ostream& operator<<(ostream& osObject, const doublyLinkedList<Type>& list)

{

nodeType<Type> *current; // pointer to traverse the list

current = list.first;

while(current != NULL)

{

cout<<current->info<<" ";

current = current->next;

}

return osObject;

}

Page 63: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

7. Doubly Linked Lists: reversePrint

63Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

void doublyLinkedList<Type>::reversePrint()

{

nodeType<Type> *current;

current = last;

while(current != NULL)

{

cout<<current->info<<" ";

current = current->back;

}

}

Page 64: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

7. Doubly Linked Lists: search

64Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>void doublyLinkedList<Type>::search(const Type& searchItem){

bool found = false;nodeType<Type> *current;current = first;while(current != NULL && !found)

if(current->info >= searchItem) found true;else

current = current->next;if (found) found = (current->info == searchItem);return found;

}

Page 65: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

7. Doubly Linked Lists: front and back

65Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

void doublyLinkedList<Type>::front()

{

assert(first != NULL);

return first->info;

}

template<class Type>

void doublyLinkedList<Type>::back()

{

assert(first != NULL);

return last->info;

}

Page 66: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

7. Doubly Linked Lists: insertNode

66Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Program

Page 67: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

7. Doubly Linked Lists: deleteNode

67Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Program

Page 68: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

8. Linked Lists with Header and Trailer Nodes

68Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Insertion and deletion of items from a linked list includes special

cases: insertion/deletion at the beginning or in an empty list.

Hint: never insert before the first item or after the last item and

never delete the first node.

In the case of an ordered list, set up two virtual nodes header at

the beginning and trailer at the end.

Afirst zzzzzzzz

A Ahmad Ali Marwafirst zzzzzzzz

Page 69: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

8. Linked Lists with Header and Trailer Nodes

69Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

The usual operations on a linked list with header and trailer nodes are:

1. Initialize the list7. Retrieve the info contained in the last node

2. Check whether the list is empty 8. Search the list for a given item

3.Output the list 9. Insert an item in the list

4. Find the length of the list 10. Delete an item from the list

5. Destroy the list 11. Make a copy of the linked list

6. Retrieve the info contained in the first node

Page 70: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

8. Linked Lists with Header and Trailer Nodes

70Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Exercise1.Write the definition of the class that defines a linked list

with header and trailer nodes as an ADT.

2.Write the definitions of the member functions of the

class defined in 1 (you may assume that the elements of

the linked list with header and trailer nodes are sorted in

an ascending order).

3.Write a program to test various operations of the class

defined in 1.

Page 71: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

8. Linked Lists with Header and Trailer Nodes

71Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Exercise1.Write the definition of the class that defines a linked list

with header and trailer nodes as an ADT.

2.Write the definitions of the member functions of the

class defined in 1 (you may assume that the elements of

the linked list with header and trailer nodes are sorted in

an ascending order).

3.Write a program to test various operations of the class

defined in 1.

Page 72: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

9. Circular Linked Lists

72Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

A circular linked list is a linked list in which the last node points to the first node.

first

first 34

34 20 15 -12

first

Page 73: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

9. Circular Linked Lists

73Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

The usual operations on a circular linked list are:

1. Initialize the list7. Retrieve the info contained in the last node

2. Check whether the list is empty 8. Search the list for a given item

3.Output the list 9. Insert an item in the list

4. Find the length of the list 10. Delete an item from the list

5. Destroy the list 11. Make a copy of the linked list

6. Retrieve the info contained in the first node

Page 74: Chapter 5 Linked Lists Dr. Youssef Harrath yharrath@uob.edu.bh

9. Circular Linked Lists

74Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Exercise1.Write the definition of the class that defines a sorted

circular linked list as an ADT.

2.Write the definitions of the member functions of the

class defined in 1 (you may assume that the elements

are sorted in an ascending order).

3.Write a program to test various operations of the class

defined in 1.