L27_eng
L27_eng
Faculty of Engineering
Copyright
Disclaimer
This content is provided without warranty or representation of any kind. The use of the content
is entirely at your own risk and Stellenbosch University (SU) will have no liability directly or indir-
ectly as a result of this content.
The content must not be assumed to provide complete coverage of the particular study material.
Content may be removed or changed without notice.
Chap 1: Introduction to
Computer Systems Chap 6: Arrays
Chap 3: Structured
Program Development Chap 10: Structures
Chap 9: Formatted
Input/Output Chap 11: File Processing
1 Introduction (12.1)
Linked lists
Number of elements stored in a “line”
Insertion and deletion can happen at any point
Stacks
Used by compilers and operating systems
Insertion and deletion only takes place at one point (FILO)
Queues
Used as waiting queue/buffer
Data inserted at one end (tail) and removed at the other (head) (FIFO)
Trees
Used to implement very efficient search and sort algorithms
Binary trees are the most basic type
Self-referencing structures
Self-referencing structures contains a member that is a pointer of the same type as the
structure
Pointer nextPtr points to a variable of the same structure type
Pointer nextPtr can be seen as a link between two elements of that type
struct listNode {
ListNode
char data; data
struct listNode *nextPtr;
};
typedef struct listNode ListNode; nextPtr
Self-referencing structures
Self-referencing structures can therefore be “linked” together to form more useful data
structures that can be used more effectively
A NULL pointer indicates that there is no subsequent node
A NULL pointer usually indicates the end of the data structure
Linked lists
A linked list is a linear collection of self-referential structures
Each element that forms part of the list is called a node
Nodes are connected to one another via pointers called links
Linked lists are accessed via a pointer to the first element
Subsequent nodes are accessed via the link pointer member of the current node
Linked lists
By convention, the link in the last node must be set to NULL
This indicates that the current element is the last element
Data is stored in linked lists dynamically
Nodes are created as they are required
A node can contain any data – even other structures – but must contain a self-referential
member
NB! Linked lists can only be accessed via a pointer to the first node – the linked
list is “lost” if one incorrectly make assignments to this pointer
value = 5;
currentPtr = startPtr;
while ( currentPtr != NULL && value != currentPtr->data ) {
currentPtr = currentPtr->nextPtr;
}
currentPtr
startPtr
data data data data
1 4 5 9
nextPtr nextPtr nextPtr nextPtr
value = 5;
currentPtr = startPtr;
while ( currentPtr != NULL && value != currentPtr->data ) {
currentPtr = currentPtr->nextPtr;
}
currentPtr
startPtr
data data data data
1 4 5 9
nextPtr nextPtr nextPtr nextPtr
value = 5;
currentPtr = startPtr;
while ( currentPtr != NULL && value != currentPtr->data ) {
currentPtr = currentPtr->nextPtr;
}
currentPtr
startPtr
data data data data
1 4 5 9
nextPtr nextPtr nextPtr nextPtr
previousPtr currentPtr
startPtr
data data data data
1 4 5 9
nextPtr nextPtr nextPtr nextPtr
newPtr
previousPtr currentPtr
startPtr
data data data data
1 4 5 9
nextPtr nextPtr nextPtr nextPtr
newPtr
data
7
nextPtr
previousPtr currentPtr
startPtr
data data data data
1 4 5 9
nextPtr nextPtr nextPtr nextPtr
newPtr
data
7
nextPtr
previousPtr currentPtr
startPtr
data data data data
1 4 5 9
nextPtr nextPtr nextPtr nextPtr
newPtr
data
7
nextPtr
tempPtr = currentPtr;
previousPtr->nextPtr = currentPtr->nextPtr;
free( tempPtr );
previousPtr currentPtr
startPtr
data data data data
1 4 5 9
nextPtr nextPtr nextPtr nextPtr
tempPtr
tempPtr = currentPtr;
previousPtr->nextPtr = currentPtr->nextPtr;
free( tempPtr );
previousPtr currentPtr
startPtr
data data data data
1 4 5 9
nextPtr nextPtr nextPtr nextPtr
tempPtr
tempPtr = currentPtr;
previousPtr->nextPtr = currentPtr->nextPtr;
free( tempPtr );
previousPtr currentPtr
startPtr
data data data data
1 4 5 9
nextPtr nextPtr nextPtr nextPtr
tempPtr
tempPtr = currentPtr;
previousPtr->nextPtr = currentPtr->nextPtr;
free( tempPtr );
previousPtr currentPtr
startPtr
data data data
1 4 9
nextPtr nextPtr nextPtr
tempPtr
Today
Dynamic Data Structures I
Self-referential structures
Dynamic data allocation
Linked lists
Next lecture
Dynamic Data Structures II
Stacks
Queues