Tải bản đầy đủ (.ppt) (36 trang)

Data structure linked list lecture

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (814.53 KB, 36 trang )

Introduction

Weakness of storing an ordered list in array:

Insertion and deletion of arbitrary elements are
expensive.

Example:
Given an array which is arranged in ascending order.

Discuss how to insert a new element ‘1’ and how to
delete the element ‘4’.

Storage allocation is not flexible.
2 4 6 7
Possible Improvements

The elements in an ordered list don’t
need to be stored in consecutive
memory space.

The insertion and deletion of an element will
not induce excessive data movement.

The element can be “dynamically”
allocated.
Linked Representation

Data structure for a linked list:


first

Data

Link (pointer): used to store the address of the next node.
Node
Example
BAT 3 CAT 4 FAT 0
8
first
0
1
2
CAT 4
3
FAT 0
4
5
6
7
BAT 3
8
9
8
first
Insertion
BAT 3 CAT 4 FAT 0
8
first
0

1
2
CAT 4
3
FAT 0
4
5
6
7
BAT 3
8
9
8
first

Insert EAT into an ordered linked list
1) Get a new node a
2) Set the data field of a to
EAT.
3) Set the link field of a to point
the node after CAT, which
contains FAT.
Find the position where EAT is
to be inserted.
EATEAT EAT
EAT
6
EAT
6
EAT 4

6
EAT 4
CAT 6
CAT 6
3
4) Set the link field of the node
containing CAT to a.
Deletion
BAT 3 CAT 6 EAT 4
8
first
1
2
CAT 6
3
FAT 0
4
5
EAT 4
6
7
BAT 3
8
9
8
first

Remove CAT from the linked list
1) Set the link of BAT to EAT.
2) Deallocate CAT

Find the address of CAT
FAT 0
BAT 6
8
BAT 6
3
Representation of a Linked List
class ListNode
{
friend class LinkedList;
public:
ListNode();
ListNode(DataField
value);
~ListNode();
private:
DataField data;
ListNode *link;
};
class LinkedList {
private:
ListNode * first;
};
first
class LinkedList {
private:
ListNode * first;
};
class ListNode
{

friend class LinkedList;
public:
ListNode();
ListNode(DataField
value);
~ListNode();
private:
DataField data;
ListNode *link;
};
data link
Linked Ordered List

Suppose elements are arranged in ascending
order.

ADT
class LinkedOrderedList
{
public:
LinkedOrderedList();
~ LinkedOrderedList();
void Insert(DataField value);
bool Delete(DataField value); //return false if value is not found.
bool IsEmpty();
private:
ListNode *first;
};
Initialization


The constructor of ListNode:

The constructor of LinkedOrderedList:
LinkedOrderList::LinkedOrderList()
{
first = NULL;
}
ListNode::ListNode(DataField value)
{
data = value;
link = NULL;
}
Algorithm of Insert()
01 void LinkedOrderList::Insert(DataField value)
02 {
03 curr = first;
04 while (curr != NULL)
05 {
06 if (curr->data >= value)
07 {
08 ListNode *a = new ListNode(value);
09 a->link = curr;
10 previous->link = a;
11 break;
12 }
13 previous = curr;
14 curr = curr->link;
15 }
16 }

Insertion
BAT 3 CAT 4 FAT 0
8
first

Insert EAT into an ordered linked list
currcurr curr
EATEAT 4
CAT 6
previous previous
03 curr = first;
04 while (curr != NULL)
05 {
06 if (curr->data >= value)
07 {
08 ListNode *a = new ListNode(value);
09 a->link = curr;
10 previous->link = a;
11 break;
12 }
13 previous = curr;
14 curr = curr->link;
15 }
a
Boundary Condition: Case 1

Consider to insert AT.

There will be no previous node for AT.


The update of first is required.
BAT CAT FAT
first
AT
Boundary Condition: Case 2

Consider to insert GAT.
BAT CAT FAT
first
GAT
03 curr = first;
04 while (curr != NULL)
05 {
06 if (curr->data >= value)
07 {
08 ListNode *a = new ListNode(value);
09 a->link = curr;
10 previous->link = a;
11 break;
12 }
13 previous = curr;
14 curr = curr->link;
15 }
No statement is written to insert
GAT at the end of the list.
Problem of Insert()

The function Insert() fails to deal with
boundary conditions.


The insertion is always performed between two
existing nodes.

Improvements

Add codes before- and after the while-statement
for dealing with the boundary conditions.

Always maintain two (dummy) nodes so that
insertion can always be performed between two
nodes.
Improvement Using Two
Dummy Nodes

Maintain two dummy nodes at each end
of the list.
class LinkedList {
private:
ListNode * first, *last;
};
LinkedOrderList::LinkedOrderList()
{
first = new ListNode();
last = new ListNode();
first->link = last;
last->link = NULL;
}
first last

No need to update the pointer

first.

Boundary conditions are
eliminated (Insertion and Deletion
always take place between two
nodes).
New Version of Insert()
01 void LinkedOrderList::Insert(DataField value)
02 {
03 previous = first;
04 curr = first->link;
05 while (curr != NULL)
06 {
07 if (curr == last || curr->data >= value)
08 {
09 ListNode *a = new ListNode(value);
10 a->link = curr;
11 previous->link = a;
12 break;
13 }
14 previous = curr;
15 curr = curr->link;
16 }
17 }
Algorithm of Delete()
01 bool LinkedOrderList::Delete(DataField value)
02 {
03 if (IsEmpty())
04 return false;
05

06 previous = first;
07 curr = first->link;
08 while (curr != last)
09 {
10 if (curr->data == value)
11 {
12 previous->link = curr->link;
13 Deallocate curr;
14 return true;
15 }
16 previous = curr;
17 curr = curr->link;
18 }
19 return false;
20 }
Deletion

Consider to remove BAT from the list.
first last
BAT
10 if (curr->data == value)
11 {
12 previous->link = curr->link;
13 Deallocate curr;
14 return true;
15 }
previous curr curr->link
Destruction of Nodes

Remember to deallocate each node in

the destructor.
01 LinkedOrderList ::~ LinkedOrderList()
02 {
03 curr = first;
04 while (curr != NULL)
05 {
06 next = curr->link;
07 Deallocate curr;
08 curr = next;
09 }
10 }
Performance Analysis

Suppose there are n nodes in a linked list.

Space complexity:

A linked list uses an exact amount of memory space to
store these n nodes.

Space complexity to perform a insertion or deletion
O(1).

Time complexity to perform a insertion or deletion:

Consider a worst case in which nodes are always
inserted and deleted at the end of the list. Therefore,
the complexity is O(n).

Excessive request of allocation or deallocation of

memory space for a node increases loading for the OS
system (and may lower efficiency).
Linked Stack
B
A
C
E 0
data link
top
Pop
Pop
D
Push D

×