Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.

Similar presentations


Presentation on theme: "Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are."— Presentation transcript:

1 Lists II

2 List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are due to the static nature of arrays i.e. the array size needs to be known by the compiler at compile-time.

3 A better implementation would employ a dynamic structure to represent the list i.e. a structure that expands and contracts as the list grows and shrinks. Such an implementation is possible using pointers and dynamic memory allocation in programming languages. This type of implementation is known as a linked list.

4 Pointers Pointers Revisited A pointer variable is a variable that contains a memory address. The size of memory that the pointer points to depends on the type of the pointer i.e. if a pointer points to a floating point number in memory the pointer must be of type float. e.g. float x=7.6; float *p = &x;

5 7.6 variable x memory address &375 &375 variable p Memory p contains the address of x in memory

6 For performance reasons it is much more efficient to pass pointers around a computer system than copies of the data it is pointing to. This is particularly important for large structures in procedural programming languages or objects in object-oriented programming languages. In these cases we almost always use pointers to the structures or objects e.g.

7 class student : public person { int ID; …public:student();…}; int main(void) { student *p = new student …} Name Age ID student() ….. p &476 Memory memory address &476

8 Linked Lists Linked List ADT The central notion to linked list implementations is that of the node. A node consists of an element to be stored and a pointer to the next node in the list e.g. element a node

9 The linked list L is simply a pointer to the first node in the list ( if it is an empty list L is NULL ) The last node in the list has a NULL pointer to indicate that no more nodes follow i.e. the list has terminated. element LNULLempty list L element NULL a list of length 2

10 6.5 L 10.7 4.8 NULL Another Example

11 To traverse the list we only need to follow the pointers through the list until we reach the last node (NULL pointer). List Insertion To insert a new element in the list we need to create a new node and place it in the correct position in the linked list. To find the correct position in the list we first traverse to the node one before the position we wish to insert at e.g.

12 to insert a new element in position 3 we traverse the list L to position 2 i.e. the third node in the list ( remember we start from position 0 ) 5.6 L 7.310.4-15.8 NULL position 3 traverse to position 2 position 0

13 To insert the new node we need to re-assign the next node pointers: 1. The next node pointer in the new node should be identical to the next node pointer of the node we stopped traversing at. 2. The next node pointer of the node we stopped traversing at should be assigned the address of the new node being inserted.

14 e.g. assume we wish to insert a new node with memory address b at position 3 below (with node memory address a) 10.4 a -15.8 NULL position 3 77.4 b new node

15 We insert the new node by performing the following node re-assignments; Simply the next node pointer in the node we stopped traversing at is copied into the new node next pointer and the address of the new node is copied into the next node pointer of the node we stopped traversing at. 10.4 b -15.8 NULL 77.4 a

16 You should be careful that the node re-assignment is performed in the correct order i.e. the next node pointer of the node stopped at isn’t overwritten before it is copied across to the new node next node pointer. Inserting at position 0 The above algorithm for node insertion will work for all nodes but the first.

17 Position 0 insertion is a special case that needs to be handled differently by the algorithm e.g. suppose we wish to insert a new element 2.3 at position 0 6.5 L 10.7 2.3 new node position 0

18 To insert the new node we point the new node to the address pointed to by the list L and then point L to the new node; 6.5 L 10.72.3 new node

19 List Deletion Node deletion also involves traversing of the list to the position one before the position to be deleted. To delete the node we simply re-assign the pointer at the node we stopped traversing at with the next node pointer of the node to be deleted.

20 6.5 L 10.7 4.8 NULL e.g. suppose we wish to delete the element at position 1 below delete this node

21 6.5 L 10.74.8 NULL memory leak

22 By directly re-assigning pointers during node deletion we get memory leaks i.e. the node to be deleted still remains in memory but is not pointed to which results in wasted resources. To avoid memory leaks we should always perform garbage collection i.e. de-allocate unused memory. In linked lists this can be achieved by using a temporary pointer to the node to be deleted.

23 6.5 L 10.7 4.8 NULL temp

24 The temporary pointer to the node to be deleted is created before we re-assign any pointers (otherwise we would lose the ability to reference the deleted node) As soon as the deleted node has been by-passed we can de-allocate it using the temporary pointer (remember deleting dynamic memory in most programming languages requires the address of the memory to be deleted).

25 Linked List Implementation The following is a class declaration and constructor for a linked list in object-oriented C++; class Node { private: float element; Node *next; public: Node(float); float getElement(); void setElement(float); Node *getNext(); void setNext(Node *); … };

26 Node::Node(float f) : element(f), next(NULL) {} Node::Node(float f) : element(f), next(NULL) {} It is important that you familiarise yourself with the code and the implementations of the main linked list operations.


Download ppt "Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are."

Similar presentations


Ads by Google