+ All Categories
Home > Documents > List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

Date post: 21-Jan-2016
Category:
Upload: justina-mcdowell
View: 216 times
Download: 0 times
Share this document with a friend
28
List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009
Transcript
Page 1: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

List Part2

Dr. Bernard Chen Ph.D.University of Central Arkansas

Spring 2009

Page 2: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

Outline

linked lists implementation An Array-Based Implementation of

Linked Lists

Page 3: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

List Implantation by Linked list In any structure used to store the

elements of a list, it must be possible to perform at least the following operation:

1. Locate the First element2. Given the location of any list element,

find its successor3. Locate the end of the list

Page 4: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

Linked List

Linked list nodes contain Data part – stores an element of the

list Next part – stores link/pointer to next

element (when no next element, null

value)

Page 5: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

Design the list class

Should contain at least the following function members Constructor empty() insert() delete() display()

Page 6: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

Construction

To construct an empty list, we simply make first a null link to indicate that it does not refer to any node:

first = null_value;

Page 7: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

Empty

We can then perform the Empty operation--- determining whether a list is empty, simply by checking whether first is null:

first == null_value?

Page 8: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

Traverse We begin by initializing an auxiliary

variable ptr to point to the first node: Initialize a variable ptr to point to first

node

Process data where ptr points

Page 9: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

Traverse

Traverse (ctd) set ptr = ptr->next, process ptr->data

Continue until ptr == null

Page 10: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

Insertion

To insert a new data value into a linked list, we must first obtain a new node and store the value in its data part

The second step is to connect this new node to existing list Two cases in this situation: (1) insertion

after some element in the list and (2) insertion at the beginning of the list

Page 11: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

Insertion

Insertion To insert 20 after 17 Need address of item before point of insertion predptr points to the node containing 17 Get a new node pointed to by newptr and store 20

in it Set the next pointer of this new node equal to the

next pointer in its predecessor, thus making it point to its successor.

Reset the next pointer of its predecessor to point to this new node

20newptr

predptr

Page 12: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

Insertion Note: insertion also works at end of list

pointer member of new node set to null Insertion at the beginning of the list

predptr must be set to first pointer member of newptr set to that value

(Where first points to) first set to value of newptr

In all cases, no shifting of list elements is required !

Page 13: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

Deletion

For deletion, there are also two cases to consider: Deleting an element that has a

predecessor Delete the first element in the list

Page 14: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

Deletion

Delete node containing 22 from list. Suppose ptr points to the node to be deleted predptr points to its predecessor (the 17)

Do a bypass operation: Set the next pointer in the predecessor to

point to the successor of the node to be deleted

Deallocate the node being deleted.

predptr ptr

To free space

Page 15: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

Deletion

The second case is easier Just set the first points to the

second node in the list and then returning the deleted node to the storage pool

Page 16: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

Linked Lists - Advantages

Access any item as long as external link to first item maintained

Insert new item without shifting Delete existing item without shifting Can expand/contract as necessary

Page 17: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

Linked Lists - Disadvantages

No longer have direct access to each element of the list Many sorting algorithms need direct

access Binary search needs direct access

Access of nth item now less efficient must go through first element, and then

second, and then third, etc.

Page 18: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

Outline

linked lists implementation An Array-Based Implementation of

Linked Lists

Page 19: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

Pointer Based Node Since each node has two different parts, a

data part and a next part, it is nature to have a node class with two data members.

class Node{

ElementType data;Node *next;

}

Page 20: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

Pointer Based Node To declare a pointer to a node:

Node *ptr

To allocate a new node pointer to by ptr:ptr = new Node;ptr = new Node(data_nalue);ptr = new Node(data_value, link_value);

To access the data nad next part of the node pointed to by ptr:ptr -> dataptr -> next

Page 21: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

Array-Based Implementation of Linked Lists

Given a list with names

Implementation would look like this

Page 22: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

How to do it???

First of all, define a 2D array

int array[10][2];

Page 23: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

How to do it??? Constructor( )

we make “first” variable equals to 7 to indicate we start with position 7

first ==7;

How to choose a start point??

Page 24: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

How to do it??? Put 88 into data

position and set next position to NULL

array[first][0]=88;array[first][1]=-1;size==1;

Page 25: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

How to do Insertion??? Find a empty position (in

this example it’s node 1)

Insert(array, 1){

ptr==1; // find a empty positionpre_ptr==7; // you need a function to

find pre_ptrarray[new_node][0]==54;array[new_node][1]==array[pre_ptr][1];array[pre_ptr][1]==ptr;size++;

}

Page 26: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

Insertion

Insertion To insert 20 after 17 Need address of item before point of insertion predptr points to the node containing 17 Get a new node pointed to by newptr and store 20

in it Set the next pointer of this new node equal to the

next pointer in its predecessor, thus making it point to its successor.

Reset the next pointer of its predecessor to point to this new node

20newptr

predptr

Page 27: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

How to do Deletion???

Page 28: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.

Implementation Details For Insertion, there are also two cases

to consider: insertion after some element in the list and insertion at the beginning of the list

For deletion, there are also two cases to consider: Deleting an element that has a predecessor Delete the first element in the list


Recommended