5 Lists Implementation
5 Lists Implementation
0 First element
Second element
List
Max_list_size - 1
LIST: Array Implementation
• type elementtype
• type LIST
• type Boolean
• type windowtype
LIST: Array Implementation
/* array implementation of LIST ADT */
#include <stdio.h>
#include <math.h>
#include <string.h>
typedef struct {
int number;
char *string;
} ELEMENT_TYPE;
LIST: Array Implementation
typedef struct {
int last;
ELEMENT_TYPE a[MAX_LIST_SIZE];
} LIST_TYPE;
list->a[w] = e;
list->last = list->last + 1;
return(list);
}
}
LIST: Array Implementation
/*** delete an element from a list ***/
/* list is empty */
WINDOW_TYPE w;
ELEMEN_TYPE e;
LIST_TYPE list;
int i;
empty(&list);
print(&list);
w = previous(w, &list);
delete(w, &list);
print(&list);
}
LIST: Array Implementation
• Key points:
– we have implemented all list manipulation
operations with dedicated access functions
– we never directly access the data-structure
when using it but we always use the access
functions
– Why?
LIST: Array Implementation
• Key points:
– greater security: localized control and more
resilient software maintenance
– data hiding: the implementation of the data-
structure is hidden from the user and so we
can change the implementation and the user
will never know
LIST: Array Implementation
Header node
List
LIST: Linked-List Implementation
List window
List window
List window
List window
temp
List window
List window
• type elementtype
• type LIST
• type Boolean
• type windowtype
LIST: Linked-List Implementation
/* linked-list implementation of LIST ADT */
#include <stdio.h>
#include <math.h>
#include <string.h>
#define FALSE 0
#define TRUE 1
typedef struct {
int number;
char *string;
} ELEMENT_TYPE;
LIST: Linked-List Implementation
typedef struct node *NODE_TYPE;
else {
error(“can’t find previous to a non-existent
node”);
}
}
else {
error(“can’t find previous before first element of
list”);
return(w);
}
}
LIST: Linked-List Implementation
/*** position at last element in a list ***/
if (is_empty(list)) {
p = end(list);
}
else {
p = *list;
q = p=>next;
while (q->next != NULL) {
p = q;
q = q->next;
}
}
return(p);
} }
LIST: Linked-List Implementation
/*** insert an element in a list ***/
else {
/* insert it after w */
temp = w->next;
if ((w->next = (NODE_TYPE) malloc(sizeof(NODE))) =
NULL)
error(“function insert: unable to allocate
memory”);
else {
w->next->element = e;
w->next->next = temp;
}
return(list);
}
}
LIST: Linked-List Implementation
/*** delete an element from a list ***/
if (*list == NULL) {
error(“cannot retrieve from a non-existent list”);
}
else {
return(w->next->element);
}
}
LIST: Linked-List Implementation
/*** print all elements in a list ***/
WINDOW_TYPE w;
ELEMEN_TYPE e;
LIST_TYPE list;
int i;
empty(&list);
print(&list);
w = previous(w, &list);
delete(w, &list);
print(&list);
}
LIST: Linked-List Implementation
• Key points:
– All we changed was the implementation of
the data-structure and the access routines
– But by keeping the interface to the access
routines the same as before, these changes
are transparent to the user
– And we didn’t have to make any changes in
the main function which was actually
manipulating the list
LIST: Linked-List Implementation
• Key points:
– In a real software system where perhaps
hundreds (or thousands) of people are using
these list primitives, this transparency is
critical
– We couldn’t have achieved it if we
manipulated the data-structure directly
LIST: Linked-List Implementation
List
We can also have a doubly-linked list;
this removes the need to have a header node
and make finding the previous node more efficient
LIST: Linked-List Implementation
List
Lists can also be circular