Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.

Similar presentations


Presentation on theme: "Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04."— Presentation transcript:

1 Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04

2 Abstract Data Type (ADT) a set of objects a set of operations Same set of objects, different sets of operations => different ADTs ADTs are implemented using classes, hiding implementation details: encapsulation 2

3 LIST ABSTRACTION Definition: A linear configuration of elements, called nodes. 3

4 Basic operations To create/destroy a list To expand/shrink the list Read/Write operations Changing the current node (moving along the list) To report current position in the list To report status of the list 4

5 Specifications for the ADT List Operations on lists ▫Add new entry – at end, or anywhere ▫Remove an item ▫Remove all items ▫Replace an entry ▫Look at any entry ▫Look for an entry of a specific value ▫Count how many entries ▫Check if list is empty, full ▫Display all the entries 5

6 Limitations of Arrays Limitations of an array are: Size of array is fixed; even dynamically allocated arrays' size cannot be changed. Adding element to front or middle of array is still extremely difficult. 6

7 Potential Problem Operations add, remove, replace work OK when valid position given Remove not meaningful on empty lists A list could become full, what happens to add ? 7

8 A collection of connected nodes with a set of operations. Nodes are divided into to bit of information: 1.Data 2.Pointer 8 Linked List ABSTRACTION

9 Characteristics Insert and delete nodes in any order The nodes are connected Each node has two components ▫ Information (data) ▫ Link to the next node The nodes are accessed through the links between them 9

10 Understanding the linked list Linked list is not a continuous memory location as array so each node in linked list is stored at different memory location, in order to access the whole list or to weave them in one list each node should have the address of the next respective node. And to access the list we should always keep safe the address of first node so that we can access the whole list with the help of the first node, and if in case we lose the address of the first node then the whole list will become a garbage as we cannot find or access this list again. So before we create any node we first create a pointer which will hold the address of the first node in the list 10

11 Linked list consists of 1.A sequence of nodes used to hold individual data Elements 2.Each node will have two Sections a)The value to hold b)The Address of next node in the list 3.A pointer which will hold the address of the first node in the list 11

12 Head Predecessor of X Node X Success- or of X tail For each node the node that is in front of it is called predecessor. The node that is after it is called successor. 12

13 Insertion and Deletion A. Insertion To insert a node X between the nodes A and B:.Create a link from X to B..Create a link from A to X, 13

14 Insertion X A B 14

15 Insertion and Deletion B. Deletion To delete a node X between A and B: Create a link from A to B, Remove node X 15

16 Deletion A X B 16

17 Keep It Simple Let us understand the Linked Structure with the help of this example Following picture depicts linked list holding sequence of numbers (87, 42, 53, 4): Each node include: The data element The link or address to the next node / / means Null,this is used to specify that this is the last node in the list 17

18 Defining the structure of the linked list Structure of node in linked list here is implemented as class in C++ Here the Structure of a node will have 2 parts 1) The Data 2) The Pointer to next node ( i.e the address of the next node) Let us understand with an example: class Node { public: int data; // the value to be stored Node* next; // pointer to next node }; Here the Data is defined as integer and named as data and the pointer to the next node is declared as a pointer and Named as next 18

19 Starting with the linked Structure Linked list is a collection of many individual nodes But before we actually create the nodes we should first create the pointer which will hold the address of the first node in the list so that with this pointer we can actually track the whole list With the following line of code we will create a pointer variable to point towards the first node in the list Example: Node *Head; At this point our list is empty- it has no elements and it is represented by a NULL pointer, i.e. a pointer whose value is 0 (signified by / OR NULL) / Head 19

20 Starting with the linked Structure Example: Head= new Node; Here with this one line of code we did two things 1)we created a new first node with the new operator, 2)and we also saved the address of this new node in the pointer variable knows as Head ( one can give any name ) Head. First node in the list is created and its address is stored in the pointer named Head 20

21 Starting with the linked Structure Now we will insert data into this new node with the following code Head>data = 87; Head. 87 This part is Head->data This part is Head->next We have till now successfully created the structure for the node and added the first node and a pointer which will hold the address of the first node 21

22 Implementing a Linked Stack If we are implementing a linked stack then the stack does all the operations from the front side so the deletion and insertion is from the front of the list. Appending the value 42 into the linked list: Initially we have a pointer named Head and our first node with 87 value Head. 87 / Head> next =Null; // set the next element to null as this is the last now 22

23 We will be adding a node to the linked Stack Node * temp = new Node ; Adding a new node at the front of the linked list Head. 87 With the above code we create a new node, and assign its address to the temporary pointer variable temp. temp / 23

24 Adding a node to the front Assign value to the newly added node Temp-> data=43; Head. 87 temp. 43 / 24

25 Adding a node to the front Set next data member of new node to point to first node in original list temp -> next = Head; Head. 87 temp. 43 /. 25

26 Adding a node to the front Now we can Update original List pointer to point to the new first node as the List pointer always points towards first node in the list Head= temp; Head. 87 temp. 43 /. 26

27 Adding a node to the front Summarized, as a function: void add2front( int val, Node* &Head) { Node *temp = new Node; temp -> data = val; temp -> next = Head; Head= temp; } 27

28 Adding a node at end To add a new node at the back we need to reach at the last node and to go to the last node we will assign a new temp pointer which will hop through each node till it reaches the node which is having a null value. It will check each node if it is == Null, until Null is found / Head. temp. 28

29 Adding a node at end void add2end( int val, Node* Head) { Node *temp ; Node *ptr = new Node; ptr->data = val; ptr -> next = NULL; temp = Head; while(temp->next!=NULL) temp= temp -> next; temp->next= ptr; } 29

30 Adding a node in the middle void add2mid( int val, int newval, Node* H) { Node *temp; Node *ptr = new Node; ptr->data = newval; ptr -> next = NULL; temp = H; while(temp->data!=val && temp->next != NULL) temp= temp -> next; ptr->next=temp->next; temp->next= ptr; } 30

31 Node Linking 1.Single linked lists : Each node contains two links - to the previous and to the next node 2.Double linked lists : Each node contains a link only to the next node 3.Circular lists: The tail is linked to the head. 31

32 32

33 33

34 34

35 List Implementation Static – using an array Dynamic – using linear nodes 35

36 Array Implementation Two parallel arrays are used:  Index array - the number stored in the i-th element shows the index of the "next" node, i.e. node that follows the i-th node  Data array - used to store the informational part of the nodes. 36

37 37


Download ppt "Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04."

Similar presentations


Ads by Google