Upcoming SlideShare
×

# Data Structure Lecture 5

686 views
593 views

Published on

Published in: Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
686
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
39
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Data Structure Lecture 5

2. 2.  If the size of the data is not known before hand ◦ It means, at compile time or at the time of object creation Then ◦ Move towards dynamic implementation, the size can be increased or decreased at the run- time even as per need.
3. 3.  The linked list is a very flexible dynamic data structure: items may be added to it or deleted structure from it at will. A programmer need not worry about how many items a program will have to accommodate. This allows us to write robust programs which require much less maintenance. A very common source of problems in program maintenance is the need to increase the capacity of a program to handle larger collections:
4. 4.  In a linked list, each item is allocated space as it is added to the list.  A link is kept with each item to the next item in the list. Each node of the list has two elements 1. the item being stored in the list and 2. a pointer to the next item in the listThe last node in the list contains a NULL pointer to indicate that itis the end or tail of the list.
5. 5.  As items are added to a list, memory for a node is dynamically allocated. Thus the number of items that may be added to a list is limited only by the amount of memory available.
6. 6. Handle for the list The variable (or handle) which represents the list is simply a pointer to the node at the head of the list. This basic singly-linked list is inefficient in those cases when we wish to add elements to both ends of the list. It is easy to add elements at the head of the list, To add elements at the other end (the tail ) we need to locate the last element. If the basic singly-linked list is used, the entire list needs to be traversed in order to find its tail.
7. 7.  Following figure shows a way in which to make adding elements to the tail of a list more efficient. The solution uses a second variable, tail , which refers to the last element of the list. However, this time efficiency comes at the cost of the additional space used to store the variable tail.
9. 9.  Two cases must be considered to add the node in front of the linked list. 1. Either we are creating the first node of the linked list or 1. We are inserting the node in front of the existing linked list.
10. 10.  First Case: Creating first node of a linked list. We follow the following steps to create the first node of the linked list.1. Check the value of the head of the list. If this value is null it means we are creating the first node of the linked list.2. An empty node is created. It is empty in the sense that the program performing insertion does not assign any values to the data members of the node.
11. 11. 3. The nodes info member is initialized to a particular value. 74. Since it is the only node of the linked list therefore its next member will be initialized to null. 7 /5. Since it is the only node of the linked list therefore both head and tail points to this node of the linked list. 7 / head tail
12. 12.  Second Case: Inserting node in front of the existing linked list: We follow the following steps to insert the node in front of the existing linked list.1. Check the value of the head of the list. If this value is not null it means we are inserting node in front of the existing linked list. 7 / head tail
13. 13. 2. An empty node is created. It is empty in the sense that the program performing insertion does not assign any values to the data members of the node. 7 / head tail
14. 14. 3. The nodes info member is initialized to a particular value. 8 7 / head tail 4. Because the node is being included at the front of the list, the next member becomes a pointer to the first node on the list, i.e. the current value of head. 8 7 / head tail
15. 15. 5. The new node precedes all the nodes on the list, but this fact has to be reflected in the value of head; otherwise the new node is not accessible. Therefore head is updated to become the pointer to the new node 8 7 / head tail
16. 16.  Two cases must be considered to add the node at the end of a linked list. 1. Either we are creating the first node of the linked list or 1. We are inserting the node at the end of an existing linked list. The first case is exactly similar as we discussed in AddToHead(x) function.
17. 17.  Second Case: Inserting node at the end of an existing linked list. We follow the following steps to insert the node at the end of an existing linked list.1. Check the value of the head of the list. If this value is not null it means linked list already exists and we insert the node at the end of an existing linked list. 8 7 / head tail
18. 18. 2. An empty node is created. It is empty in the sense that the program performing insertion does not assign any values to the data members of the node. 8 7 /head tail
19. 19. 3. The nodes info member is initialized to a particular value. 8 7 / 9 head tail 4. Because the node is being included at the end of the list, the next member is set to null. 9 5. The node is now included in the list by making the next member of the last node of the list a pointer to the newly created node. 8 7 9 head tail
20. 20. 6. The new node follows all the nodes of the list, but this fact has to be reflected in the value of tail, which now becomes the pointer to the new node. 8 7 9 head tail
21. 21.  Two cases must be considered to delete the node from the head of a linked list. 1. Either a linked list has only one node or 1. A linked list has more than one nodes.
22. 22.  First Case: Linked List has only one node: We follow the following steps to handle this case:1. Check the values of the pointer variables head and tail. If both points to the same node, means 7 / linked list has only one node. tail head2. Set both head and tail pointers to null and return back the memory occupied by the node to the system.
23. 23.  Second Case: Linked List has more than one nodes We follow the following steps to handle this case:1. Check the values of the pointer variables head and tail. If both points to the different nodes, means linked list has more than one nodes. 8 7 9 head tail
24. 24. 2. Set a temporary pointer variable tmp points to the head of a linked list. 8 7 9 head tail tmp3. Since we delete a node from the head of a linked list, therefore move the head pointer to the node next to the head of an existing list. 8 7 9 tail tmp head
25. 25. 4. Delete a node pointed by the pointer variable tmp from a linked list. 8 7 9 tail tmp head 7 9 tail head
26. 26.  Two cases must be considered to delete the node from the end of a linked list. 1. Either a linked list has only one node or 1. A linked list has more than one nodes. The first case is exactly similar as we discussed in DeleteFromHead() function.
27. 27.  Second Case: Linked List has more than one nodes:  We follow the following steps to handle this case: 1. Check the values of the pointer variables head and tail. If both points to the different nodes, means linked list has more than one nodes. 8 5 7 4 9 head tail
28. 28. 2. After removing a node, tail should refer to the new tail of the list. i.e. tail has to be moved backward by one node. 8 5 7 4 9 head tail 8 5 7 4 9 head tail But moving backward is impossible because there is no direct link from the last node to its predecessor.
29. 29. 3. Hence, this predecessor has to be found by searching from the beginning of the list and stopping right before tail. This is accomplished with a temporary variable tmp used to scan a list within the loop. 4. The variable tmp is initialized to the head of 8 the list. 5 7 4 9 head tail tmp
30. 30. 5. In each iteration of the loop, the variable tmp is advanced to the next node of the 8 linked list. 5 7 4 9 head tail tmp After executing the assignment tmp= tmp->next, tmp refers to the second node. 8 5 7 4 9 head tail tmp
31. 31. After the second iteration and executing the same assignment, tmp refers to the third 8 node.5 7 4 9 head tail tmp tmp tmp After the third iteration and executing the same assignment, tmp refers to the forth node. Because next to this node is a last node, i.e. this node is a predecessor of the last node, therefore the loop is exited.
32. 32. 6. Now delete the last node from the linked list. 8 5 7 4 9 head tail tmp 7. Because tail is now pointing to non existing node therefore it is immediately set to point to the node pointed by tmp. 8 5 7 4 / head tail tmp 8. To mark the fact that tail is now pointing to last node of the linked list, the next member of this node is set to null.
33. 33.  Two cases must be considered to delete the node of value x from the linked list. 1. Either a linked list has only one node or 1. A linked list has more than one nodes. The first case is exactly similar as we discussed in DeleteFromHead() function except We first checks that the value of this single node matches with the value of variable x.
34. 34.  Second Case: Linked List has more than one nodes: We follow the following steps to handle this case:1. First matches the value of variable x with the value of first node of the linked list. If the value matches, it means we want to delete first node of a linked list, therefore call the operation DeleteFromHead().2. Otherwise, match the value of variable x with the value of last node of the linked list. If the value matches, it means we want to delete last node of a linked list, therefore call the operation DeleteFromTail().
35. 35. 3. If first two cases fails then locate the node whose value matches with the value of variable x. Such a node has to be found by searching from the beginning of the list and stopping at the node whose value matches with the value of variable x. This is accomplished with a temporary variable tmp used to scan a list within the loop. The variable tmp is initialized to the head of the linked list. 8 5 7 4 9 head tail tmp
36. 36. In each iteration of the loop, the value of the node pointed by variable tmp will be compared with the value of variable x. If the value matches then we exited from loop Otherwise we advance the variable tmp to compare the value of the next node of linked list. 8 5 7 4 9 Consider the following linked list.head tail tmp Suppose the value of the variable x is 4.
37. 37. When we exited from the loop the variable tmp is pointing to the node containing 4. 8 5 7 4 9 head tail tmp We can now remove a node pointed by variable tmp by linking its predecessor to its successor. But because the list has only forward links the predecessor of a node is not reachable from the node. To locate the predecessor we again scan the list from the beginning.
38. 38. 4. This is accomplished with a temporary variable pred used to scan a list within the loop. The variable pred is initialized to the head of the linked list. 7 5 9 8 4head tail pred pred pred tmp After executing the assignment pred=pred->next, pred refers to the second node. After the second iteration and executing the same assignment, pred refers to the third node.
39. 39. Because next to this node is a node pointed by the variable tmp, i.e. this node is a predecessor of the node pointed by variable tmp, therefore the loop is exited. 8 5 7 4 9 head tail pred tmp 5. Now join the node pointed by variable pred to the node next to the node pointed by the variable tmp.
40. 40. 6. Now delete the node pointed by the variable tmp from linked list. 8 5 7 4 9 head tail pred tmp 8 5 7 9 head tail pred
41. 41.  Every node of a linked list has two fields, an information field and a field which points to the next node of a list. Therefore the class which for the node of a list has two data members I.e. ◦ A data member to hold information & ◦ A data member which stores the address of next node of a list
42. 42. template<class T>class Node {public: T info; Node *next; Node() { next = 0; } Node(T el, Node *n = 0) { info = el; next = n; }};
43. 43. Node *p = new Node(10); Node *q = new Node(8); p->next = q; r = new Node(12); q->next = r; As we see this is an inconvenient and cumbersome process.Therefore 10 8 12 /p q r
44. 44.  To implement the singly linked list we implement two classes i.e. ◦ One class for nodes of the list & ◦ Another class for access to the list
45. 45. template<class T>class Node {public: T info; Node *next; Node() { next = 0; } Node(T el, Node *n = 0) { info = el; next = n; }};
46. 46. template<class T>class SinglyLinkedList { private: Node<T> *head; //pointer to first node of a list Node<T> *tail; //pointer to last node of a list public: SinglyLinkedList() { head = tail = 0; } ~SinglyLinkedList(); Continue on next slide…
47. 47. bool isEmpty() { if (head == 0) //list is empty return true;}void addToHead(T el); //inserts an element in front of listvoid addToTail(T el); //inserts an element at the end of listvoid deleteFromHead(); //delete a node from the front of a list anvoid deleteFromTail(); //delete a node form the end of a listvoid Delete(T el); //delete a node form list whose value is elNode *Find(T el); //Find the node from list whose value is el //and returns its pointer.void prints(); //prints the contents of a list};
48. 48. template<class T> void SinglyLinkedList<T> :: addToHead(T el) head { Node *n = new Node<T>(e1, 0); //Creates new node e if (isEmpty) //first node of a list head = tail = n; tail n else // Insert node in front of the list { n->next = head; head head = n; } e a b c d} head head n tail
49. 49. template<class T>void SinglyLinkedList<T>:: addToTail(T el) head{ Node *n = new Node<T>(e1, 0); if(isEmpty) //Creates new //First node of a list node head = tail = n; e else //Insert node at the end of a list tail n { tail->next = n; tail = n; } a b c d e } head tail tail n