08 List
08 List
Lecturer:
Contact:
2. Array implementation
3 / 83
Linear list concepts
Definition
A linear list is a data structure in which each element has a unique
successor.
Example
• Array
• Linked
list
Linear list concepts
5 / 83
Linear list concepts
General list:
• No restrictions on which operation can be used on the list.
• No restrictions on where data can be inserted/deleted.
Restricted list:
• Only some operations can be used on the list.
• Data can be inserted/deleted only at the ends of the list.
Definition
A list of elements of type T is a finite sequence of elements of type T.
Basic operations:
• Construct a list, leaving it empty.
• Insert an element.
• Remove an element.
• Search an element.
• Retrieve an element.
• Traverse the list, performing a given operation on each element.
List A D T
Extended operations:
• Determine whether the list is empty or not.
• Determine whether the list is full or not.
• Find the size of the list.
• Clear the list to make it empty.
• Replace an element with another element.
• Merge two ordered list.
• Append an unordered list to another.
Insertion
Any element formerly at position p and all later have their position numbers increased by 1.
Insertion
The element at position p is removed from the list, and all subsequent elements have their
position numbers decreased by 1.
Retrieval
16 /
83
Dynamically Allocated Array
L i s t / / C o n t i g u o u s I m p l e m e n t a t io n o f L i s t
/ / number o f u s e d e l e m e n t s ( m a n d a t o r y )
s i z e < i nteg er >
/ / ( D y na m ica lly A l l o c a t e d A r r a y )
data <dynamic a r r a y of < D a t a T y p e > >
c l a s s Dynamic Array {
private :
int siz e ;
int capacity ;
int ∗storag e ;
Dynamic Array ( i n t c a p a c i t y ) {
t h i s >c a p a c i t y = c a p a c i t y ;
s i z e = 0;
s t o r a g e = new i n t [ c a p a c i t y ] ;
}
~D y n a m i c Array ( ) {
delete [ ] storag e ;
}
Dynamic Array: Implementation in C + +
v o i d range C h e c k ( i n t ) ;
void set ( int , int ) ;
i n t ge t ( i n t ) ;
v o i d remove A t ( i n t ) ;
void insertA t ( int , int ) ;
void print ( ) ;
};
Dynamic Array: Implementation in C + +
v o i d D y n a m i c Array : : s e t C a p a c i t y ( i n t new C a p a c i t y ) {
i n t ∗ new S t o r a g e = new i n t [ new C a p a c i t y ] ;
memcpy( new S t o r ag e , s to r a ge , s i z e o f ( i n t ) ∗ s i z e ) ;
c a p a c i t y = new C a p a c i t y ;
delete [ ] storag e ;
s t o r a g e = new S t o r a g e ;
}
Dynamic Array: Implementation in C + +
v o i d D y n a m i c Array : : e n s u r e C a p a c i t y ( i n t min C a p a c i t y ) {
i f ( min C a p a c i t y > c a p a c i t y ) {
i n t new C a p a c i t y = ( c a p a c i t y ∗ 3 ) / 2 + 1 ;
i f ( new C a p a c i t y < min C a p a c i t y )
new C a p a c i t y = min C a p a c i t y ;
s e t C a p a c i t y ( new C a p a c i t y ) ;
}
}
Dynamic Array: Implementation in C + +
v o i d Dynamic Array : : t r i m ( ) {
i n t new C a p a c i t y = s i z e ;
s e t C a p a c i t y ( new C a p a c i t y ) ;
}
Dynamic Array: Implementation in C + +
v o i d D y n a m i c A r r a y : : ra n ge C h e c k ( i n t i n d e x ) {
i f ( i n d ex < 0 || i n d ex > = s i z e )
throw " In d e x out o f bounds ! " ;
}
v o i d D y n a m i c A r r a y : : s e t ( i n t inde x , i n t v a l u e ) {
ra n ge C h e c k ( i n d e x ) ;
storag e [ index ] = value ;
}
i n t D y n a m i c A r r a y : : ge t ( i n t i n d e x ) {
ra n ge C h e c k ( i n d e x ) ;
return storag e [ index ] ;
}
Dynamic Array: Implementation in C + +
v o i d D y n a m i c Array : : remove A t ( i n t i n d e x ) {
range C h e c k ( i n d e x ) ;
i n t moveCount = s i z e i n d e x 1 ;
i f ( moveCount > 0 )
memmove( s t o r a g e + inde x ,
storag e + ( index + 1) ,
s i z e o f ( i n t ) ∗ moveCount ) ;
s i z e ;
pack ( ) ;
}
Dynamic Array: Using
v o i d D y n a m i c Array : : p r i n t ( ) {
f o r ( i n t i = 0 ; i < t h i s >s i z e ; i + + ) {
cout < < s t o r a g e [ i ] < < " " ;
}
}
i n t main ( ) {
cout < < " Dynamic A r r a y " < < e n d l ;
Dynamic Array ∗ da = new D y n a m i c A r r ay ( 1 0 ) ;
da>i n s e r t A t ( 0 , 5 5 ) ;
// . . .
da>p r i n t ( ) ;
return 0;
}
Contiguous Implementation of List
29 /
83
Linked List
Definition
A linked list is an ordered collection of data in which each element contains the location of the
next element.
l i s t / / L i n k e d I m p l e m e n t a t io n o f L i s t
head < p o i n t e r >
c o u n t < i n t e g e r > / / number o f e l e m e n t s ( optional )
end l i s t
Nodes
• the data,
• the address of the next node.
Nodes
33 /
83
Example
34 /
83
Implementation in C + +
Example
node s t r u c t Node {
data < d a t a T y p e > i n t data ;
l i n k < p o i n t er > Node ∗ l i n k ;
};
end node
Implementation in C + +
Example
#include<iostream>
using namespace std;
struct Node{
int data;
Node *link;
};
int main(){
Node*p=new Node();
p->data=5;
cout<<p->data<<endl;
Node*q=p;
cout<<q->data<<endl;
Node *r=new Node();
r->data=10;
q->link=r;
cout<<p->link->data<<endl;
}
Implementation in C + +
Example
s t r u c t Node { s t r u c t Node {
i n t data ; f l o a t data ;
Node ∗ l i n k ; Node ∗ l i n k ;
}; };
37 /
83
Implementation in C + +
Example
#include<iostream>
using namespace std;
template<class ItemType>
struct Node{
ItemType data;
Node <ItemType>*link;
};
int main(){
Node<int>*p=new Node<int>();
p->data=5;
cout<<p->data<<endl;
Node<int>*q=p;
cout<<q->data<<endl;
Node<int>*r=new Node<int>();
r->data=10;
q->link=r;
cout<<p->link->data<<endl;
}
Node implementation in C + +
template<class ItemType>
class Node{
ItemType data;
Node <ItemType>*link;
public:
Node(){
this->link=NULL;
}
Node(ItemType data){//constructor with initial value
this->data=data;
this->link=NULL;
}
};
Linked list implementation in C + +
template<class List_ItemType>
class LinkedList{ list
Node <List_ItemType>*head;
int count;
head < p o i n t e r >
public: count < i n t eg er >
LinkedList();
~LinkedList();
end l i s t
};
Linked list operations
42 /
83
Create an empty linked list
template<class List_ItemType>
class LinkedList{
Node<List_ItemType>*head;
int count;
public:
LinkedList();
~LinkedList();
};
template<class List_ItemType>
LinkedList<List_ItemType>::LinkedList(){
this->head=NULL;
this->count=0;
}
Insert a node into a linked list
46 /
83
Insert at the beginning
47 /
83
Insert in the middle
48 /
83
Insert at the end
49 /
83
Insert a node into a linked list
• Insertion is successful when allocation memory for the new node is successful.
• There is no difference between insertion at the beginning of the list and insertion into an
empty list.
pNew>l i n k = l i s t . head
l i s t . h e a d = pNew
• There is no difference between insertion in the middle and insertion at the end of the list.
pNew>l i n k = p P r e > l i n k
p P r e > l i n k = pNew
Insert a node into a linked list
allocate(pNew)
if memory overflow then
return false
end
pNew - > data = dataIn
if pPre = null then
/ / Adding at the beginning or into empty list
pNew - > link = list.head list.head = pNew
else
/ / Adding in the middle or at the end
pNew - > link = pPre - > link pPre - > link = pNew
end
list.count = list.count + 1 return true
End insertNode
Insert a node into a linked list
template<class List_ItemType>
int LinkedList<List_ItemType>::InsertNode(Node <List_ItemType>∗pPre,List_ItemType value){
Node <List_ItemType>*pNew=new Node<List_ItemType>();
if(pNew==NULL)
return0;
pNew->data=value;
if(pPre==NULL){
pNew->link=this->head;
this->head=pNew;
}
else{
pNew->link=pPre->link;
pPre->link=pNew;
}
this->count++;
return 1;
}
Delete a node from a linked list
1. Locate the pointer p in the list which points to the node to be deleted (pLoc will hold the
node to be deleted).
• If that node is the first element in the List: p i s list.head.
• Otherwise: p i s pPre->link, where pPre points to the predecessor of the node to be
deleted.
2. p points to the successor of the node to be deleted.
3. Recycle the memory of the deleted node.
Delete first node
55 /
83
General deletion case
56 /
83
Delete a node from a linked list
l i s t . head = pLoc>l i n k
r e c y c l e ( pLoc )
• There is no difference between deleting a node from the middle and deleting a node from
the end of the list.
pPre>l i n k = pLoc>l i n k
r e c y c l e ( pLoc )
Delete a node from a linked list
template<class List_ItemType>
List_ItemType
LinkedList<List_ItemType>::DeleteNode(Node<List_ItemType>*pPre,Node<List_ItemType>*pLoc){
List_ItemType result=pLoc->data;
if(pPre==NULL){
this->head=pLoc->link;
}else{
pPre->link=pLoc->link;
}
this->count--;
delete pLoc;
return result;
}
Searching in a linked list
62 /
83
Searching in a linked list
Post: pLoc points to the first node which is equal target, or is NULL if not found.
pPre points to the predecessor of the first node which is equal target, or points to the last
node if not found.
pPre = NULL
pLoc = list.head
while (pLoc is not NULL) AND (target ! = pLoc ->data) do
pPre = pLoc
pLoc = pLoc ->link
end
if pLoc is NULL then
return notFound
else
return found
end
End Search
Searching in a linked list
template<class List_ItemType>
int LinkedList<List_ItemType>::Search(List_ItemType
value,Node<List_ItemType>*&pPre,Node<List_ItemType>*&pLoc){
pPre=NULL;
pLoc=this->head;
while(pLoc!=NULL&&pLoc->data!=value){
pPre=pLoc;
pLoc=pLoc->link;
}
return(pLoc!=NULL);
//found:1;notfound:0
}
Traverse a linked list
Traverse module controls the loop: calling a user-supplied algorithm to process data
pWalker = list.head
while pWalker not null do
process(pWalker - > data)
pWalker = pWalker - > link
end
End Traverse
Traverse a linked list
template<class List_ItemType>
void LinkedList<List_ItemType>::Traverse(){
Node <List_ItemType>*p=head;
while(p!=NULL){
p->data++;//process data here!!!
p=p->link;
}
}
template<class List_ItemType>
void LinkedList<List_ItemType>::Traverse2(List_ItemType *&visit){
Node <List_ItemType>*p=this->head;
int i=0;
while(p!=NULL && i<this->count){
visit[i]=p->data;
p=p->link;
i++;
}
}
Destroy a linked list
template<class List_ItemType>
void LinkedList<List_ItemType>::Clear(){
Node <List_ItemType>*temp;
while(this->head!=NULL){
temp=this->head;
this->head=this->head->link;
delete temp;
}
this->count=0;
}
template<class List_ItemType>
LinkedList<List_ItemType>::~LinkedList(){
this->Clear();
}
Linked list implementation in C + +
} ;
Linked list implementation in C + +
template<class List_ItemType>
class LinkedList{
protected:
//...
public:
LinkedList();
~LinkedList();
void InsertFirst(List_ItemType value);
void InsertLast(List_ItemType value);
int InsertItem(List_ItemType value,int position);
void DeleteFirst();
void DeleteLast();
int DeleteItem(int postion);
int GetItem(int position,List_ItemType &dataOut);
void Traverse();
LinkedList<List_ItemType>*Clone();
void Print2Console();
void Clear();
};
Linked list implementation in C + +
delete myList;
delete myList2;
return 1;
}
Sample Solution: Insert
template<class List_ItemType>
int LinkedList<List_ItemType>::InsertItem(List_ItemType value,int position){
if(position<0||position>this->count)
return 0;
Node<List_ItemType>*newPtr,*pPre;
newPtr=new Node<List_ItemType>();
if(newPtr==NULL)
return 0;
newPtr->data=value;
if(head==NULL){
head=newPtr;
newPtr->link=NULL;
}else if(position==0){
newPtr->link=head;
head=newPtr;
}
Sample Solution: Insert
else{
//Find the position of pPre
pPre=this->head;
for(int i=0;i<position-1;i++)
pPre=pPre->link;
this->count++;
return 1;
}
Sample Solution: Delete
template<class List_ItemType>
int LinkedList<List_ItemType>::DeleteItem(int position){
if(position<0||position>this->count)
return0;
Node <List_ItemType>*dltPtr,*pPre;
if(position==0){
dltPtr=head;
head=head->link;
}else{
pPre=this->head;
for(int i=0;i<position-1;i++)pPre=pPre->link;
dltPtr=pPre->link;pPre->link=dltPtr->link;
}
delete dltPtr;
this->count--;
return 1;
}
Sample Solution: Clone
template<class List_ItemType>LinkedList<List_ItemType>*LinkedList<List_ItemType>::Clone(){
LinkedList<List_ItemType>*result=new LinkedList<List_ItemType>();
Node<List_ItemType>*p=this->head;
while(p!=NULL){
result->InsertLast(p->data);
p=p->link;
}
result->count=this->count;
return result;
}
Reverse a linked list
Exercise
te m p l a t e < c l a s s L i s t _ I t e m T y p e >
void L ink edL i st < L i s t _ I t e m Type > : : Re verse ( ) {
// . . .
}
Comparison of implementations
of list
79 /
83
Arrays: Pros and Cons
• Pros:
• Access to an array element is fast since we can compute its location quickly.
• Cons:
• If we want to insert or delete an element, we have to shift subsequent elements
which slows our computation down.
• We need a large enough block of memory to hold our array.
Linked Lists: Pros and Cons
• Pros:
• Inserting and deleting data does not require us to move/shift subsequent data
elements.
• Cons:
• If we want to access a specific element, we need to traverse the list from the
head of the list to find it which can take longer than an array access.
Comparison of implementations of list