CS DataStructure-Lecture 4-List (Array and Linked)
CS DataStructure-Lecture 4-List (Array and Linked)
• In a general list:
– new values are added in position determined by the user.
– Element is removed from a position determined by the user.
• Important notice:
– if we keep adding and removing from the first position (the
head of the list) the general list will behave as a stack.
• Application:
– In queues, sometimes we need a priority for some elements.
We may need to put an emergency call prior to others.
© Waleed A. Yousef 2008 4
Motivation: Why Lists?
0
Deleting one element requires too many shifting!!
size entry
© Waleed A. Yousef 2008 12
/* 0<= p <= size-1*/
void RetrieveList(int p, ListEntry *pe, List *pl){
*pe=pl->entry[p];
} // Θ(1)
/* 0<= p <= size-1*/
void ReplaceList(int p, ListEntry e, List *pl){
pl->entry[p]=e;
} // Θ(1)
p +1
p
0
size entry
© Waleed A. Yousef 2008 13
/*List.h*/
#include "Global.h"
size head
0 1 p … size-1
0 1 p … size-1
© Waleed A. Yousef 2008 size head 16
void InsertList(int pos, ListEntry e, List *pl){
size
ListNode *p, *q;
int i; head
p=(ListNode *)malloc(sizeof(ListNode));
p->entry=e;
0
p->next=NULL;
…
q->next=p; 1
}
pl->size++;
} // Θ(n) but without shifting elements. size-1
…
}
pl->size++;
return 1;
} size-1
else return 0;
}
© Waleed A. Yousef 2008 19
void DeleteList(int pos, ListEntry *pe, List *pl){ size
int i; head
ListNode *q, *tmp;
if (pos==0){ 0
*pe=pl->head->entry;
tmp=pl->head->next;
free(pl->head); 1
pl->head=tmp;
}// it works also for one node tmp
else{ pos-1
for(q=pl->head, i=0; i<pos-1; i++)
q=q->next; q
pos
*pe=q->next->entry;
tmp=q->next->next;
free(q->next);
pos+1
q->next=tmp;
}// check for pos=size-1 (tmp will be NULL)
tmp
pl->size--;
} //O(n) but without shifting elements. size-1
head
size-1
Array-based Linked
CreateList Θ(1) Θ(1)
InsertList Θ(n) Θ(n)
DeleteList Θ(n) Θ(n)
RetrieveList Θ(1) Θ(n)
ReplaceList Θ(1) Θ(n)
Many applications processes the entries in order, i.e., moving from one entry to the
next.
Then, our current linked implementation is very inefficient, since it moves from the
head to the element every time!
Then, we need to remember the last position currentpos and start navigating
from it, and we use current to start walking from currentpos
k 0 1 k size-1
currentpos current size head
© Waleed A. Yousef 2008 23
Design Enhancement: Learn how you modify your design to
enhance the performance
k 0 1 k size-1
currentpos current size head
© Waleed A. Yousef 2008 24
void InsertList(int pos, ListEntry e, List *pl){
ListNode *p; 0
p=(ListNode *)malloc(sizeof(ListNode));
p->entry=e;
p->next=NULL;
if (pos==0){ currentpos
p->next=pl->head;
pl->head=p;
pl->currentpos=0; //new pos-1
pl->current=pl->head; //new pl->current
}
else{//pl->current is used in place of q previously.
if (pos<=pl->currentpos){ pos
pl->currentpos=0;//as i=0
pl->current=pl->head;//as q=pl->head
} //new.
…
for(; pl->currentpos!=pos-1; pl->currentpos++)
pl->current=pl->current->next; p
p->next=pl->current->next;
pl->current->next=p; size-1
}
pl->size++;
© Waleed A. Yousef 2008 25
void DeleteList(int pos, ListEntry *pe, List *pl){
ListNode *tmp; 0
if (pos==0){
*pe=pl->head->entry;
pl->current=pl->head->next;
free(pl->head); currentpos
pl->head=pl->current; //new
pl->currentpos=0; //new
} pos-1
else{ pl->current
if (pos<=pl->currentpos){
pl->currentpos=0;
pl->current=pl->head; pos
tmp
}
for(; pl->currentpos!=pos-1; pl->currentpos++)
pl->current=pl->current->next;
…
*pe=pl->current->next->entry;
tmp=pl->current->next->next;
free(pl->current->next);
pl->current->next=tmp; size-1
}
pl->size--;
© Waleed A. Yousef 2008 26
You have to modify ReplaceList and RetrieveList in
the same manner. (Do it as a homework). Check also for other
functions, e.g., CreateList (for initialization)
Having this function may simplify the code for the case of
having current and currentpos.
Read the code from the book and solve the review problems
k 0 1 k size-1
currentpos current size
© Waleed A. Yousef 2008 28