Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas.
List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.
-
Upload
justina-mcdowell -
Category
Documents
-
view
216 -
download
0
Transcript of List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.
![Page 1: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/1.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/2.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/3.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/4.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/5.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/6.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/7.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/8.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/9.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/10.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/11.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/12.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/13.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/14.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/15.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/16.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/17.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/18.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/19.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/20.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/21.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/22.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/23.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/24.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/25.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/26.jpg)
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.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/27.jpg)
How to do Deletion???
![Page 28: List Part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.](https://reader035.fdocuments.in/reader035/viewer/2022062519/5697c02b1a28abf838cd872d/html5/thumbnails/28.jpg)
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