Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked List.

Similar presentations


Presentation on theme: "Linked List."— Presentation transcript:

1 Linked List

2 Course Objectives At the end of the class, students are expected to : Describe linear list concepts using array and linked list. Able to lists variations of linked list and basic operations of linked lists. Able to explain in detail the implementation and operations of link lists using pointers. Able to write program that can implement linked list concept.

3 1.0 Introduction to linked list

4 INTRODUCTION Lists Definition
Lists is a group of objects which is organized in sequence. List categories: linear list and nonlinear list. Linear list – a list in which the data is organized in sequence, example: array, linked list, stack and queue Non-Linear list – a list in which the data is stored not sequence, example: tree and graph

5 Introduction to Linear List
Array and linked lists are linear lists that doesn’t have any restrictions while implementing operations such as, insertion, deletion and accessing data in the lists. The operations can be done in any parts of the lists, either in the front lists, in the middle or at the back of the lists. Stack and queue is a linear lists that has restrictions while implementing its operations. Stack – to insert, delete and access data can only be done at the top of the lists. Queue - Insert data in a queue can be done at the back of the lists while to delete data from a queue can only be done at the front list.

6 Introduction to Linear List
An array named Pelajar which contains attributes nama, pelajar, kursus and tahun Pelajar. The array is sorted and can only be accessed based on the index or subscript of the array. Example: to access information for a student named Mohd Saufi, we can use: Pelajar[3].Nama, Pelajar[3].Kursus dan Pelajar[3].Tahun.

7 Introduction to Linear List
Linked lists which contain several nodes which is sorted in ascending order. Each node contains at least A piece of data (any type) Pointer to the next node in the list Need pointer variable Head [Senarai]: to point to the first node

8 Introduction to Linear List
Basic operations for linear lists: Insert new data in the lists. Delete data from a lists. Update data in the list. Sort data in the lists and Find data in the list.

9 Preliminaries Figure 4-1 (a) A linked list of integers; (b) insertion; (c) deletion

10 2.0 Array as linear list

11 Array as linear list Sized is fixed during array declaration.
Data insertion is limited to array size In order to insert data, need to check whether the array is full or not. If the array is full, the insertion cannot be done.

12 Array as linear list Data in the array can be accessed at random using the index of the array. Example, if we access Nama, Kursus and Tahun for Pelajar[3] information of Pelajar[3], Mohd Saufi taking Sains Pendidikan course and in year 2 will be given. By random access, accessing data in an array can be done faster.

13 Array Implementation...the drawbacks
Requires an estimate of the maximum size of the list waste space printList and find: linear access findKth: constant insert and delete: slow insert at position 0 (making a new element) requires first pushing the entire array down one spot to make room delete at position 0 requires shifting all the elements in the list up one On average, half of the lists needs to be moved for either operation

14 Array Implementation...the drawbacks
Need space to insert item in the middle of the list. Insert Fatimah Adam in between students named Durrani Nukman and Mohd Saufi. insert at index 3 : requires first pushing the entire array from index 3 down one spot to make room

15 Array Implementation...the drawbacks
New item is inserted at index 3, after shifting the data from index 3 onwards. .

16 Array Implementation...the drawbacks
To delete item in the middle of the array will leave a blank space in the middle. It requires shifting all the elements in the list up one in order to eliminate the space.. Example: when information about Durrani Nukman is deleted, all elements below it, is shifted up.

17 List (Instances inside Array)
The class need to be defined with its basic member data and functions Instances of class are then created by defining an array with its data type is a new class defined previously Example: Class of student with member data name and mark might be defined as shown in the next slides

18 List (Instances inside Array)
Class definition: class student { private: char name[10]; int mark; public: void set_info(char [], int); void print_info(); };

19 List (Instances inside Array)
Class implementation: void student::set_info(char n[], int m) { strcpy(name, n); mark = m; // reformat name int offset = 10 - strlen(name); if (offset > 0) { strncat(name, " ", offset); } void student::print_info() { cout << name << " - " << mark;

20 List (Instances inside Array)
Array of instances definition, instances data members assignment & basic list manipulation: int i = 0, list_size = 5; student sl[list_size]; sl[0].set_info(“KHUZAIMAH”, 46); sl[1].set_info(“ADAM”, 51); sl[2].set_info(“ARRIF”, 86); for (i = 0; i < max_item, i++) { sl[i].print_info(); cout << “\n”; }

21 List (Instances inside Array)
The previous example is the simplest implementation and operations of list using array The operations involved are adding three items into the list and listing back all items start from index 0 to 4 (though there are only 3 items in the list) In the real application we have to consider other operations such as: interactively add/delete item to/from the list control the listing back operation so it will be limited to the current number of items exist in the list mechanism to detect current list either it is empty, or full

22 List (Instances inside Array)
Base on the array definition student sl[list_size]; the list of instances may can be represented as follows: Index Name Mark 1 2 3 4

23 List (Instances inside Array)
Obviously adding the first item inside the list will be done at the index 0 of the array as the list is still empty Index Name Mark 1 2 3 4

24 List (Instances inside Array)
Obviously adding the first item inside the list will be done at the index 0 of the array as the list is still empty Index Name Mark KHUZAIMAH 46 1 2 3 4

25 List (Instances inside Array)
What about adding next new items? Index Name Mark KHUZAIMAH 46 1 2 3 4

26 List (Instances inside Array)
What about adding next new items? Need special marking point on the index Index Name Mark KHUZAIMAH 46 1 2 3 4

27 List (Instances inside Array)
What about adding next new items? Need special marking point on the index Index Name Mark KHUZAIMAH 46 1 2 3 4 index

28 List (Instances inside Array)
Adding new item into the list is done at the index marking point Index Name Mark KHUZAIMAH 46 1 2 3 4 index

29 List (Instances inside Array)
Adding new item into the list is done at the index marking point Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 3 4 index

30 List (Instances inside Array)
Each time after new item is added into the list, need to move this marking point to the next index (increase by 1) Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 3 4 index

31 List (Instances inside Array)
Each time after new item is added into the list, need to move this marking point to the next index (increase by 1) Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 3 4 index

32 List (Instances inside Array)
In term of programming it can be depicted as follows: int i = 0, list_size = 5, index = 0;

33 List (Instances inside Array)
In term of programming it can be depicted as follows: int i = 0, list_size = 5, index = 0; student sl[list_size];

34 List (Instances inside Array)
In term of programming it can be depicted as follows: int i = 0, list_size = 5, index = 0; student sl[list_size]; Index Name Mark 1 2 3 4

35 List (Instances inside Array)
In term of programming it can be depicted as follows: int i = 0, list_size = 5, index = 0; student sl[list_size]; Index Name Mark 1 2 3 4

36 List (Instances inside Array)
In term of programming it can be depicted as follows: int i = 0, list_size = 5, index = 0; student sl[list_size]; Index Name Mark 1 2 3 4 index

37 List (Instances inside Array)
In term of programming it can be depicted as follows: int i = 0, list_size = 5, index = 0; student sl[list_size]; sl[index].set_info(“KHUZAIMAH”, 46); Index Name Mark 1 2 3 4 index

38 List (Instances inside Array)
In term of programming it can be depicted as follows: int i = 0, list_size = 5, index = 0; student sl[list_size]; sl[index].set_info(“KHUZAIMAH”, 46); Index Name Mark KHUZAIMAH 46 1 2 3 4 index

39 List (Instances inside Array)
In term of programming it can be depicted as follows: int i = 0, list_size = 5, index = 0; student sl[list_size]; sl[index].set_info(“KHUZAIMAH”, 46); index++; Index Name Mark KHUZAIMAH 46 1 2 3 4 index

40 List (Instances inside Array)
In term of programming it can be depicted as follows: int i = 0, list_size = 5, index = 0; student sl[list_size]; sl[index].set_info(“KHUZAIMAH”, 46); index++; Index Name Mark KHUZAIMAH 46 1 2 3 4 index

41 List (Instances inside Array)
In term of programming it can be depicted as follows: int i = 0, list_size = 5, index = 0; student sl[list_size]; sl[index].set_info(“KHUZAIMAH”, 46); index++; sl[index].set_info(“ADAM”, 51); Index Name Mark KHUZAIMAH 46 1 2 3 4 index

42 List (Instances inside Array)
In term of programming it can be depicted as follows: int i = 0, list_size = 5, index = 0; student sl[list_size]; sl[index].set_info(“KHUZAIMAH”, 46); index++; sl[index].set_info(“ADAM”, 51); Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 3 4 index

43 List (Instances inside Array)
In term of programming it can be depicted as follows: int i = 0, list_size = 5, index = 0; student sl[list_size]; sl[index].set_info(“KHUZAIMAH”, 46); index++; sl[index].set_info(“ADAM”, 51); index++; Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 3 4 index

44 List (Instances inside Array)
In term of programming it can depicted as follows: int i = 0, list_size = 5, index = 0; student sl[list_size]; sl[index].set_info(“KHUZAIMAH”, 46); index++; sl[index].set_info(“ADAM”, 51); index++; Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 3 4 index

45 List (Instances inside Array)
The situation where the list is already full The index marking point has the value equal to the size of array (index == list_size) Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 ARRIF 86 3 FIRDAUS 58 4 ENNIE 79 index 5

46 List (Instances inside Array)
The index marking point itself becomes an items counter of the list and also can be used as an indicator for the list status The information above is very useful as it provides the basic idea on how to wisely listing back or add items from/into the list Marking Point List Status index == 0 empty index < list_size still have space for new item index == list_size full

47 List (Instances inside Array)
Refined codes for listing back all items from the list list is empty if (index == 0) { cout << “Sorry list is empty!!!\n”; } else { for (i = 0; i < index, i++) { sl[i].print_info(); cout << “\n”; } items counter

48 List (Instances inside Array)
Refined codes for adding new items into the list still have space for new item if (index < list_size) { sl[index].set_info(“???”, ??); index++; } else { cout << “Sorry list is full!!!\n”; }

49 Exercise Week 7_2 Write a complete program to perform the insert operation and produce the following list. Check if the list if full before insert element to the list and check if the list is empty before delete element from the list. Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 ARRIF 86 3 FIRDAUS 58 4 ENNIE 79

50 List (Instances inside Array)
How to delete item number 3 (ARRIF) from the list? Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 ARRIF 86 3 FIRDAUS 58 4 ENNIE 79 index 5

51 List (Instances inside Array)
How to delete item number 3 (ARRIF) from the list? Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 ARRIF 86 3 FIRDAUS 58 4 ENNIE 79 index 5

52 List (Instances inside Array)
Just shift up all items after item number 3 Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 ARRIF 86 3 FIRDAUS 58 4 ENNIE 79 index 5

53 List (Instances inside Array)
Just shift up all items after item number 3 Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 ARRIF 86 3 FIRDAUS 58 4 ENNIE 79 index 5

54 List (Instances inside Array)
Just shift up all items after item number 3 Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 ARRIF 86 3 FIRDAUS 58 4 ENNIE 79 index 5

55 List (Instances inside Array)
Just shift up all items after item number 3 Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 FIRDAUS 58 3 4 ENNIE 79 index 5

56 List (Instances inside Array)
Just shift up all items after item number 3 Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 FIRDAUS 58 3 4 ENNIE 79 index 5

57 List (Instances inside Array)
Just shift up all items after item number 3 Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 FIRDAUS 58 3 ENNIE 79 4 index 5

58 List (Instances inside Array)
Also shift up index marking point Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 FIRDAUS 58 3 ENNIE 79 4 index 5

59 List (Instances inside Array)
Also shift up index marking point Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 FIRDAUS 58 3 ENNIE 79 4 index 5

60 List (Instances inside Array)
Let’s delete other item (ADAM) where item_num = 2 Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 FIRDAUS 58 3 ENNIE 79 4 index 5

61 List (Instances inside Array)
Let’s delete other item (ADAM) where item_num = 2 Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 FIRDAUS 58 3 ENNIE 79 4 index 5

62 List (Instances inside Array)
Let’s delete other item (ADAM) where item_num = 2 Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 FIRDAUS 58 3 ENNIE 79 4 index 5

63 List (Instances inside Array)
Let’s delete other item (ADAM) where item_num = 2 Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 FIRDAUS 58 3 ENNIE 79 4 index 5 Note that we always start shifting at: sl[item_num - 1] & sl[item_num]

64 List (Instances inside Array)
Let delete other item (ADAM) where item_num = 2 Index Name Mark KHUZAIMAH 46 1 ADAM 51 2 FIRDAUS 58 3 ENNIE 79 4 index 5 Note that we always start shifting at: sl[item_num - 1] & sl[item_num]

65 List (Instances inside Array)
Let delete other item (ADAM) where item_num = 2 Index Name Mark KHUZAIMAH 46 1 FIRDAUS 58 2 3 ENNIE 79 4 index 5 Note that we always start shifting at: sl[item_num - 1] & sl[item_num]

66 List (Instances inside Array)
Let delete other item (ADAM) where item_num = 2 Index Name Mark KHUZAIMAH 46 1 FIRDAUS 58 2 3 ENNIE 79 4 index 5

67 List (Instances inside Array)
Let delete other item (ADAM) where item_num = 2 Index Name Mark KHUZAIMAH 46 1 FIRDAUS 58 2 ENNIE 79 3 4 index 5

68 List (Instances inside Array)
Let delete other item (ADAM) where item_num = 2 Index Name Mark KHUZAIMAH 46 1 FIRDAUS 58 2 ENNIE 79 3 4 index 5 Note that we always stop shifting at: sl[index - 2] & sl[index - 1]

69 List (Instances inside Array)
Let delete other item (ADAM) where item_num = 2 Index Name Mark KHUZAIMAH 46 1 FIRDAUS 58 2 ENNIE 79 3 4 index 5

70 List (Instances inside Array)
Shift up index marking point Index Name Mark KHUZAIMAH 46 1 FIRDAUS 58 2 ENNIE 79 3 4 index 5

71 List (Instances inside Array)
Shift up index marking point Index Name Mark KHUZAIMAH 46 1 FIRDAUS 58 2 ENNIE 79 3 4 index 5

72 List (Instances inside Array)
Though ENNIE – 79 still exist at index 3 and index 4, but index 3 has been marked as index marking point Index Name Mark KHUZAIMAH 46 1 FIRDAUS 58 2 ENNIE 79 3 4 index 5

73 List (Instances inside Array)
Next new items can be added start from this index marking point (as we delete two items we should have another two empty space in the list) Index Name Mark KHUZAIMAH 46 1 FIRDAUS 58 2 ENNIE 79 3 4 index 5

74 List (Instances inside Array)
Listing back all items is limited until index 2 (for list that contains 5 items, delete 2 two items will left 3 items in the list) Index Name Mark KHUZAIMAH 46 1 FIRDAUS 58 2 ENNIE 79 3 4 index 5

75 List (Instances inside Array)
How about delete item number 3 (ENNIE) which is the last item in the list? Index Name Mark KHUZAIMAH 46 1 FIRDAUS 58 2 ENNIE 79 3 4 index 5

76 List (Instances inside Array)
We only have to shift up the index marking point Index Name Mark KHUZAIMAH 46 1 FIRDAUS 58 2 ENNIE 79 3 4 index 5

77 List (Instances inside Array)
We only have to shift up the index marking point Index Name Mark KHUZAIMAH 46 1 FIRDAUS 58 2 ENNIE 79 3 4 index 5

78 List (Instances inside Array)
We only have to shift up the index marking point Index Name Mark KHUZAIMAH 46 1 FIRDAUS 58 2 ENNIE 79 3 4 index 5

79 List (Instances inside Array)
Exercise 7_4: Add functionality to delete item from the list base on program in exercise 7_3.

80 Array List Conclusions – Advantage & Disadvantage
Arrays provide most convenient way to create a list that contains simple ADT or primitive data type Allows quick random access to item in the list. Access to item number 1000 for example is simply done by using the syntax: array_var_name[999] List size limit to the size of array Require items rearrangement (shifting) for add/delete items in the middle of the list. Example of the worst case is to delete first item from the list that currently contains n items – O(n).

81 3.0 Linked list as linear list

82 Pointer Implementation (Linked List)
Ensure that the list is not stored contiguously use a linked list a series of structures that are not necessarily adjacent in memory Each node contains the element and a pointer to structure containing its successor the last cell’s next link points to NULL Compared to the array implementation, the pointer implementation uses only as much space as is needed for the elements currently on the list but requires space for the pointers in each cell

83 Linked list variations
Singly linked list Doubly linked list Circular linked list Circular doubly linked list Sorted linked list Unsorted linked list

84 Singly Linked Lists A linked list is a series of connected nodes
B C Head A linked list is a series of connected nodes Each node contains at least A piece of data (any type) Pointer to the next node in the list Head: pointer to the first node The last node points to NULL node

85 Variations of Linked Lists
Circular linked lists The last node points to the first node of the list How do we know when we have finished traversing the list? (Tip: check if the pointer of the current node is equal to the head.) A B C Head

86 Variations of Linked Lists
Doubly linked lists Each node points to not only successor but the predecessor There are two NULL: at the first and last nodes in the list Advantage: given a node, it is easy to visit its predecessor. Convenient to traverse lists backwards A B C Head

87 Variations of Linked Lists
Circular doubly linked list No NULL value at the first and last nodes in the list Convenient to traverse lists backwards and forwards

88 Variations of Linked Lists
Sorted Linked list : The nodes in the lists is sorted in certain order. UnSorted Linked list : The nodes in the lists is not sorted in any order.

89 Linked List The most important concept in linked list is the instances that point/link to other instances In linked list this kind of instances usually referred as nodes A data pointer

90 Linked List We can use the student class in the previous array list example as a basic class for the nodes p s1 KHUZAIMAH 46 next s2 ADAM 51 next s3 ARRIF 86 next NULL

91 3.0 Linked list Implementation

92 A Simple Linked List Class
We use two classes: Node and List Declare Node class for the nodes data: double-type data in this example next: a pointer to the next node in the list class Node { public: double data; // data Node* next; // pointer to next };

93 Declare node for account
struct nodeAccount { char accountName[20]; char accountNo[15]; float balance; nodeAccount *next; };

94 Exercise Week 8_1 Figure above represents a node in a link list that store information of a book. The node consists of the following attributes : bookTitle, ISBNno, price and pointer next. Write a struct/class declaration for the node that is able to store the information.

95 A Simple Linked List Class
Declare List, which contains head: a pointer to the first node in the list. Since the list is empty initially, head is set to NULL Operations on List class List { public: List(void) { head = NULL; } // constructor ~List(void); // destructor bool IsEmpty() { return head == NULL; } Node* InsertNode(int index, double x); int FindNode(double x); int DeleteNode(double x); void DisplayList(void); private: Node* head; };

96 A Simple Linked List Class
Operations of List IsEmpty: determine whether or not the list is empty InsertNode: insert a new node at a particular position FindNode: find a node with a given value DeleteNode: delete a node with a given value DisplayList: print all the nodes in the list

97 Inserting a new node Node* InsertNode(int index, double x) Steps
Insert a node with data equal to x. After insertion, this function generates a sorted list : in ascending order. Find the location of the value to be inserted so that the value will be in the right order in the list. (i.e., when index = 0, insert the node as the first element; when index = 1, insert the node after the first element, and so on) Steps Locate index to insert the element Allocate memory for the new node Point the new node to its successor Point the new node’s predecessor to the new node index’th element newNode

98 Inserting a new node Possible cases of InsertNode
Insert into an empty list Insert in front Insert at back Insert in middle But, in fact, only need to handle two cases Insert as the first node (Case 1 and Case 2) Insert in the middle or at the end of the list (Case 3 and Case 4)

99 Inserting a new node Try to locate index of the node being inserted.
Node* List::InsertNode(double x) { int currIndex = 0; Node * currNode = head; Node* prevNode = NULL; while (currNode && x > currNode->data) { prevNode = currNode; currNode = currNode->next; currIndex++; } Node* newNode = new Node; newNode->data = x; if (currIndex == 0) { newNode->next = head; head = newNode; else { newNode->next = prevNode->next; prevNode->next = newNode; return newNode;

100 Inserting a new node Create a new node
Node* List::InsertNode(double x) { int currIndex = 0; Node * currNode = head; Node* prevNode = NULL; while (currNode && x > currNode->data) { prevNode = currNode; currNode = currNode->next; currIndex++; } Node* newNode = new Node; newNode->data = x; if (currIndex == 0) { newNode->next = head; head = newNode; else { newNode->next = prevNode->next; prevNode->next = newNode; return newNode; Create a new node

101 Inserting a new node Insert as first element
Node* List::InsertNode(double x) { int currIndex = 0; Node * currNode = head; Node* prevNode = NULL; while (currNode && x > currNode->data) { prevNode = currNode; currNode = currNode->next; currIndex++; } Node* newNode = new Node; newNode->data = x; if (currIndex == 0) { newNode->next = head; head = newNode; else { newNode->next = prevNode->next; prevNode->next = newNode; return newNode; Insert as first element head newNode

102 Inserting a new node Insert after currNode
Node* List::InsertNode(double x) { int currIndex = 0; Node * currNode = head; Node* prevNode = NULL; while (currNode && x > currNode->data) { prevNode = currNode; currNode = currNode->next; currIndex++; } Node* newNode = new Node; newNode->data = x; if (currIndex == 0) { newNode->next = head; head = newNode; else { newNode->next = prevNode->next; prevNode->next = newNode; return newNode; Insert after currNode currNode newNode

103 Inserting a new node at certain index (will create unsorted list)
Try to locate index’th node. If it doesn’t exist return NULL Node* List::InsertNode (int index, double x) { if (index < 0) return NULL; int currIndex = 0; Node * currNode = head; Node* prevNode = NULL; while (currNode && x > currNode->data) { prevNode = currNode; currNode = currNode->next; currIndex++; } if (index > 0 && currNode == NULL) return NULL; Node* newNode = new Node; newNode->data = x; if (currIndex == 0) { newNode->next = head; head = newNode; else { newNode->next = prevNode->next; prevNode->next = newNode; return newNode; 103

104 Inserting a new node at certain index (will create unsorted list)
Node* List::InsertNode (int index, double x) { if (index < 0) return NULL; int currIndex = 0; Node * currNode = head; Node* prevNode = NULL; while (currNode && x > currNode->data) { prevNode = currNode; currNode = currNode->next; currIndex++; } if (index > 0 && currNode == NULL) return NULL; Node* newNode = new Node; newNode->data = x; if (currIndex == 0) { newNode->next = head; head = newNode; else { newNode->next = prevNode->next; prevNode->next = newNode; return newNode; Create a new node 104

105 Inserting a new node Insert as first element
Node* List::InsertNode (int index, double x) { if (index < 0) return NULL; int currIndex = 0; Node * currNode = head; Node* prevNode = NULL; while (currNode && x > currNode->data) { prevNode = currNode; currNode = currNode->next; currIndex++; } if (index > 0 && currNode == NULL) return NULL; Node* newNode = new Node; newNode->data = x; if (currIndex == 0) { newNode->next = head; head = newNode; else { newNode->next = prevNode->next; prevNode->next = newNode; return newNode; Insert as first element head newNode 105

106 Inserting a new node Insert after currNode
Node* List::InsertNode (int index, double x) { if (index < 0) return NULL; int currIndex = 0; Node * currNode = head; Node* prevNode = NULL; while (currNode && x > currNode->data) { prevNode = currNode; currNode = currNode->next; currIndex++; } if (index > 0 && currNode == NULL) return NULL; Node* newNode = new Node; newNode->data = x; if (currIndex == 0) { newNode->next = head; head = newNode; else { newNode->next = prevNode->next; prevNode->next = newNode; return newNode; Insert after currNode currNode 106 newNode

107 Printing all the elements
void DisplayList(void) Print the data of all the elements Print the number of the nodes in the list void List::DisplayList() { int num = 0; Node* currNode = head; while (currNode != NULL){ cout << currNode->data << endl; currNode = currNode->next; num++; } cout << "Number of nodes in the list: " << num << endl;

108 Exercise Week 8_2 Rewrite your program in Exercise Week 7_3 to perform the insert operation using linked-list.

109 Finding a node int FindNode(double x)
Search for a node with the value equal to x in the list. If such a node is found, return its position. Otherwise, return 0. int List::FindNode(double x) { Node* currNode = head; int currIndex = 1; while (currNode && currNode->data != x) { currNode = currNode->next; currIndex++; } if (currNode) return currIndex; else return 0;

110 Deleting a node Steps Like InsertNode, there are two special cases
int DeleteNode(double x) Delete a node with the value equal to x from the list. If such a node is found, return its position. Otherwise, return 0. Steps Find the desirable node (similar to FindNode) Release the memory occupied by the found node Set the pointer of the predecessor of the found node to the successor of the found node Like InsertNode, there are two special cases Delete first node Delete the node in middle or at the end of the list

111 Deleting a node Try to find the node with its value equal to x
int List::DeleteNode(double x) { Node* prevNode = NULL; Node* currNode = head; int currIndex = 1; while (currNode && currNode->data != x) { prevNode = currNode; currNode = currNode->next; currIndex++; } if (currNode) { if (prevNode) { prevNode->next = currNode->next; delete currNode; else { head = currNode->next; return currIndex; return 0; Try to find the node with its value equal to x

112 Deleting a node int List::DeleteNode(double x) {
Node* prevNode = NULL; Node* currNode = head; int currIndex = 1; while (currNode && currNode->data != x) { prevNode = currNode; currNode = currNode->next; currIndex++; } if (currNode) { if (prevNode) { prevNode->next = currNode->next; delete currNode; else { head = currNode->next; return currIndex; return 0; prevNode currNode

113 Deleting a node int List::DeleteNode(double x) {
Node* prevNode = NULL; Node* currNode = head; int currIndex = 1; while (currNode && currNode->data != x) { prevNode = currNode; currNode = currNode->next; currIndex++; } if (currNode) { if (prevNode) { prevNode->next = currNode->next; delete currNode; else { head = currNode->next; return currIndex; return 0; head currNode

114 Destroying the list ~List(void)
Use the destructor to release all the memory used by the list. Step through the list and delete each node one by one. List::~List(void) { Node* currNode = head, *nextNode = NULL; while (currNode != NULL) { nextNode = currNode->next; // destroy the current node delete currNode; currNode = nextNode; }

115 Using List result int main(void) { List list;
4 5 6 7 Number of nodes in the list: 4 5.0 found 4.5 not found Number of nodes in the list: 2 result int main(void) { List list; list.InsertNode(7.0); // successful list.InsertNode(5.0); // successful list.InsertNode(6.0); // successful list.InsertNode(4.0); // successful // print all the elements list.DisplayList(); if(list.FindNode(5.0) > 0) cout << "5.0 found" << endl; else cout << "5.0 not found" << endl; if(list.FindNode(4.5) > 0) cout << "4.5 found" << endl; else cout << "4.5 not found" << endl; list.DeleteNode(7.0); return 0; }

116 Exercise Week 8_3 Class Node and List Constructor and Destructor
Modify your program in Exercise 8_2 to produce a complete link list program based on the implementation of the following classes and functions given in the notes. Class Node and List Constructor and Destructor IsEmpty(), InsertNode(), FindNode() DeleteNode() and DisplayList() Provide main() program to execute the list

117 Array versus Linked Lists
Linked lists are more complex to code and manage than arrays, but they have some distinct advantages. Dynamic: a linked list can easily grow and shrink in size. We don’t need to know how many nodes will be in the list. They are created in memory as needed. In contrast, the size of a C++ array is fixed at compilation time.

118 Array versus Linked Lists
Easy and fast insertions and deletions To insert or delete an element in an array, we need to copy to temporary variables to make room for new elements or close the gap caused by deleted elements. With a linked list, no need to move other nodes. Only need to reset some pointers. 118

119 4.0 Linked list Application

120 Example: The Polynomial ADT
An ADT for single-variable polynomials Array implementation

121 The Polynomial ADT… Acceptable if most of the coefficients Aj are nonzero, undesirable if this is not the case E.g. multiply most of the time is spent multiplying zeros and stepping through nonexistent parts of the input polynomials

122 The Polynomial ADT… Implementation using a singly linked list
Each term is contained in one cell, and the cells are sorted in decreasing order of exponents 122

123 Linked List - Conclusion
Try make your own conclusion by comparing array list and linked list in term of: Size of items/nodes in the list Add new items/nodes at the beginning or in the middle of the list Delete items at the beginning or in the middle of the list Get items at nodes n


Download ppt "Linked List."

Similar presentations


Ads by Google