Software Engineering II Algorithms and Data Structures Linked Lists
Software Engineering II Algorithms and Data Structures Linked Lists
4.1
Outline
Automatic variables
Until now we always declare the variables that will be used
int i; int myArray[25];
These variables are called automatic variables, because their creation and destruction is controlled automatically for you. These variables are created when a function is called and are destroyed when the function terminates.
i myArray Memory
Downside?
3
Dynamic variables
Dynamic variables are created and destroyed while the program is running. Pointers are used in dynamic variable creation and operation.
p Stage 3
p? Stage 2
We can use the new operator to dynamically allocate an object of any type, i.e.,
char* myInit = new char; Point* ptr = new Point;
x y
LINKED LIST
but how can you access the data? Link the data together
A Linked list
A linked list is a dynamic data structure that can grow and shrink during program execution, so you can use as much memory as you need.
Computer Memory Delete operation John Smith 35,000 link Alex Green 45,000 link Ken Sedcole 38,000 link Computer Memory John Smith 35,000 link Alex Green 45,000 link Ken Sedcole 38,000 link
10
What are the advantages of a linked list? Use as much memory as you need Computational efficient to add/delete data
What are the disadvantages of a linked list? Memory overhead. Store of extra information Slow access of the data. You have to traverse the list
11
Linked Lists
A building block of a linked list is a block of memory that has two items: data stored in the list, and a pointer to the rest of the list. A linked list is a set of building blocks where one block points to the next. The start of the list, called head, is a pointer that points to the first element of the list. In other words, a linked list is an element followed by a linked list. So linked lists are recursively defined structures, in other words they are defined in terms of themselves. Finally, theres a special linked list: the empty list. This is usually represented by a special pointer, the null pointer. The end of the list is indicated by the null pointer. A linked list that stores a list of characters: CAT.
12
13
42
Each node includes: A container for individual data structure A link to next node in the chain
14
Example:
struct Node { int data; Node* next; };
data
next
15
3. Create the first node as a dynamic variable using the new operator
4. Fill in the data for the node 5. Make the next pointer pointing to null, denoting the end of the list
hdList 20
NULL
16
A better version
Better to use typedef to make the data type more abstract and hence easier to change in the future. In this case, the integer data element is defined a type named Item. We also define a type named NodePtr as pointer to a Node. typedef int Item; struct Node { Item data; Node* next; }; typedef Node* NodePtr; NodePtr hdList = new Node; hdList->data = 20; hdList->next = NULL;
hdList
20
NULL
17
18
Lists as an ADT
Linked lists, as well as being an example of a dynamic data structure, are also an example of an abstract data type. An abstract data type (ADT) is one whose underlying implementation is invisible to the procedures that use it. Instead, they manipulate the data structure via a number of predefined functions and routines, known as access routines. Access routines come in two main varieties:
19
The Heap
At run-time, every program maintains an area of memory known as the heap. Dynamic data structures, such as linked lists, inhabit the heap. The heap comprises an area of allocated memory and an area of free memory. To grow a data structure to add an element to a list, for example the program grabs a piece of free memory, and allocates it to the new element. When a data structure shrinks when an element is deleted from a list, for example the memory allocated to that element becomes free again. In this way, the memory used up at any time while a program is running is exactly the amount the program needs, and no more.
20
Allocated Memory
C A T
Free Memory
10
Heap
10 ??
??
NULL
hdList
NodePtr hdList; hdList = new Node; hdList -> data = 10; hdList -> next = Null;
21
NULL
45 93
newNode
?? ??
hdList
12
NULL
45 93
newNode
37
hdList newNode
NULL
12 45 93
37
22
11
On exit, hdList will be pointing to a new node. Therefore it must be passed to the function by reference, hence the & in front of hdList. Is the new item added to the beginning (i.e. head) or the end (i.e. tail) of the linked list?
Do I have a memory leak here?
23
12
Important: You do not delete the pointer, you delete the space where this pointer points to. How does the program know how many bytes to delete?
26
13
28
14