04 LinkedList
04 LinkedList
1
List- What?
Lists
In a general list:
New values are added in position determined
by the user.
Element is removed from a position determined
by the user.
How Lists Work?
Insertion position
0 n MAX-1
Insertion range
Should be shifted one cell right
New element
0 n+1
Insert New Element MAX-1
??
Shifted elements
Deletion position
0 n MAX-1
Deletion range
Should be shifted one cell left
0 n MAX-1
Delete an Element
??
Shifted elements
Data
Link to the next node
Linked List
NULL
Linked List
• They are dynamic data structure: That grow or shrink during the
execution of a program.
info next
List
Linked List Implementation
info next
List
OPERATIONS PERFORMED ON
LINKED LIST
Create operation:
Pre: None.
Post: The list is initialized to be empty.
void CreateList(ListType *L){
*L= NULL;
}
Linked List Implementation
Create operation:
Pre: None.
Post: The list is initialized to be empty.
void CreateList(ListType *L){
If the list is declared as:
*L= NULL;
typedef struct{
} Node * head;
} ListType;
Empty operation:
Pre: The list is initialized.
Post: If the list is empty (1) is returned. Otherwise (0) is returned.
int EmptyList(ListType L){
return (L==NULL);}
Full operation:
Pre: The list is initialized.
Post: If the list is full (1) is returned. Otherwise (0) is returned.
int FullList(ListType L){
return 0;}
Linked List Implementation
Insert operation:
Pre: The list is initialized, not full and 0<=pos<=size of the list. P
Clear operation:
Pre: The list is initialized.
Post: the list is cleared to be empty.
void ClearList(ListType *L){
Node *q;
while(*L){
q = *L;
*L=(*L)->next;
free(q);
}
}
Think
Could you keep track with the list size in the List ADT?!!
How?!! is that useful?!!
1
Mostafa Abdo Salah
22