Lecture 2 & 3 Single-Linked List
Lecture 2 & 3 Single-Linked List
An obvious choice for implementing the List ADT is to use a fixed size array A, where A[i] stores the element at
index i.
Then we have the following structure
The structure of storage is
struct List
{
int capacity; //current array size
int * elem = new int [capacity]; //the array in which elements have been sored
int last= -1; // Last is the index of the last element in list initially list is
//empty
}
Algorithms for implementing ADT list operations using Array implementation
1. Algorithm END(L) returns the next of last position ( Defined function )
END (L ) : Pos // we will implement the function END(L) using array implementation
return L.last+1;
End
CT = O(1)
2. Algorithm FIRST(L) returns the first position on list L ( Defined function ).
FIRST (L): Pos // we will implement the function FIRST(L) using array implementation
return 0;
End
CT = O(1)
Algorithms for implementing ADT list operations using Array implementation
3. Algorithm NEXT(p , L)
▪ NEXT(p, L) returns the positions following position p on list L.
▪ If p is the last position on L, then NEXT(p, L) = END(L).
▪ NEXT is undefined if p is END(L).
Next(p , L): Pos
if p>=L.capacity -1
{ error(“p is at the end position of the list”); return -1;}
else if pos>L.last+1 or pos<0
{ error(‘pos is out of range ’);
return -1;
}
else
return p+1;
End CT = O(1)
Algorithms for implementing ADT list operations using Array implementation
End CT = O(n)
Algorithms for implementing ADT list operations
6. Algorithm DELETE(p, L). Delete the element at position p of list L.
Delete(pos, L)
if L.last == -1
error(‘List is Empty’)
else if pos>L.last or pos<0
error(‘pos is out of range’)
else
begin
for q = pos+1 to L.last do {shift Left}
L.elem[q-1]←L.elem[ q]
end
L.last--;
end CT = O(n)
Algorithms for implementing ADT list operations
7. Algorithm LOCATE(x , L). returns the position of x on list L.
If x appears more than once, then the position of the first occurrence is returned. If x does not appear at all, then END(L)
is returned.
Locate(ElementType e, List L): Pos
for q = 0 to L.last do
if elem[q]=e
return q;
return L.END(); {not found}
end
CT = O(n)
Algorithms for implementing ADT list operations
8. Algorithm RETRIEVE (p, L) returns the element at position p on list L.
RETRIEVE(int pos, list L) :ElementType
if pos>L.last or pos<0
error(‘pos is out of range’)
return -9999
else
return L.elem[pos];
End
O(1)
Algorithms for implementing ADT list operations
9. Algorithm MAKENULL(L) causes L to become an empty list and returns position END(L).
MAKENULL (L)
L.last= -1;
End O(1)
10. Algorithm PRINTLIST(L). Print the elements of L in the order of occurrence.
PRINTLIST(L )
for i=0 to L.last
print(L.elem[i])
end
End O(n)
Test ADT List
void main()
{
List L; // last = -1 last
Insert(9,First(L),L); 9
Insert(20,END(L),L); 9 20
Insert(100,First(L),L); last 100 9 20
PRINTLIST(L ); // 100 20
Delete(Locat(9,L),L); // pos is out of range
PRINTLIST(L ); // 100 20
}
ADT List
Pointer-Based
Linked-List based
Implementation
Single Linked List
The Single linked list is a way to store a collection of elements. Each element in the single linked list
is stored in the form of a node. The node is a collection of two sub-elements or parts:
1. The data part that stores the element
2. The next part that stores the address of next node.
The Single linked list is formed when many such nodes are linked together to form a chain. Each
node points to the next node present in the order. The first node is always used as a reference to
traverse the list and is called head.
Declaring the single linked list using pointer implementation:
struct node{
eType element;
node* next;
node( ) Header Sentinel
{ next = Null;
▪To simplify programming, it is convenient to add special
}
nodes at front of a linked list: a header node just before the
};
Algorithm create-dummy-head(node * head)
head of the list.
{
// creat dummy head cell ▪This “dummy” or sentinel node do not store any elements.
head= new node;
typedef node * position; ▪It provide quick access to the first of the list.
} ▪In particular, the header’s next pointer points to the first
node of the list.
Creating a linked list by inserting from the back
head temp
Algorithm Create(node * head)
Begin
node * temp = new node( ) We create node *temp2. Transfer the address of
node * temp2 = head; *head to *temp2. So *temp2 is also pointed at the
while (temp2->next != NULL)
temp2 = temp2->next;
front of the linked list. We then traverse the list
temp2->next = temp; CT = O(n)
End
head
Algorithm Insert an element at position p
end; CT=O(1)
Deleting an element at position p
p is the address of the node we want to delete after it
CT=O(n)
Retrieve. Returns the element at position p
on list.
x null
}
CT=O(1)
Empty(p, L) .
Make List empty.
display(L) .
Display list L
Function display(node* head){
position p = head;
while(p->next!=NULL)
{
etype x=p->next->element;
cout<<x ;
endl;
p=p->next;}} CT=O(n)
ADT linked list
Delete last element
Insert b after x
Append()
Write an Append() function that takes two lists, 'a' and 'b',
appends 'b' onto the end of 'a'