Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS212: Data Structures and Algorithms Lecture # 4 Linked List.

Similar presentations


Presentation on theme: "CS212: Data Structures and Algorithms Lecture # 4 Linked List."— Presentation transcript:

1 CS212: Data Structures and Algorithms Lecture # 4 Linked List

2 2 Outline Storing Data Structures in Memory Sequential Storage (Drawbacks) Basic Concepts (Linked List) Linked Vs Sequential Allocation Lists Implementation of Linked List

3 3 Storing Data Structures in Memory There are two ways of storing data structures in the computer’s (main) memory Sequential Allocation/Storage  The first of these storage-allocation methods takes advantage of the one-dimensional property of the computer’s memory, is called sequential allocation  We used array for this method Linked Allocation/Storage  The second allocation method, which is based on the storage of the address or location of each element in the list, is known as linked allocation

4 4 Sequential Storage (Drawbacks) Storage requirements are unpredictable (cannot be predicted at the compile time) The precise amount of data storage in the applications is data-dependent The storage requirements cannot be easily determined before the associated program is executed

5 5 Basic Concepts (Linked List) Information Address of next element The approach is to store the address of the successor/next of a particular element in that element

6 6 Basic Concepts (Linked List) Using this approach, the element of the linear list need not be physically adjacent in memory First nodesecond node 2 nd last node last node Head Pointer variable Head contains the address or location of the first node in the list …………..

7 7 Basic Concepts (Linked List) A linked list consists of a series of structures/objects, which are not necessarily adjacent in memory Each structure contains the element/information, and a pointer to a record containing its successor (The Next Pointer) Last cell’s Next pointer contains zero, which is typically known as null pointer (NULL is defined in ) a1a1 a2a2 a3a3 a4a4 a5a5 a1a1 a2a2 a3a3 a4a4 a5a5 800 7129926920 1000 800 712992692

8 8 ‘Grant’ ‘John’ ‘Paul’ ‘Rick’ 1000 1500 1400 1200 1500 1400 1200 Basic Concepts (Linked List)

9 9 ‘Ken’ ‘Grant’‘John’ ‘Paul’ ‘Rick’ 1000 1500 1400 1200 1300 1300 1500 1400 1200 1400 Basic Concepts (Linked List): Insertion

10 10 ‘Grant’ ‘John’ ‘Paul’ ‘Rick’ 1000 1500 1400 1200 1500 1400 1200 1200 Basic Concepts (Linked List): Deletion

11 11 Linked Vs Sequential Allocation Lists Consider, the operations of insertion and deletion in a sequentially allocated list  Assume that we have an n-element list and that it is required to insert a new element between the second and the third elements  In this case, the last n – 2 elements of the list must be physically moved to make room for the new element  For large-sized lists, which are subjected to many insertions, this insertion approach can be very costly The same conclusion holds in the case of deletion, since all elements after the deleted element must be moved up so as to use the vacated space caused by the deletion

12 12 Linked Vs Sequential Allocation Lists For a search operation in a linked list:  We must follow the link from the first node onwards until the desired node is found  This operation is certainly inferior to the computed-address method of locating an element in a sequentially allocated list If we want to split or join two linked lists:  Then these operations involve the changing of pointer fields without having to move any nodes  Such is not the case for sequentially allocated counterparts Clearly, pointers or links consume additional memory The cost of this additional memory becomes less important as the information contents of a node require more memory

13 13 Linked List Operations The Common operations on linked lists are:  initialize: initializes the linked list to be empty  isEmpty: returns “true” as the function result if list contains no element, or otherwise returns “false”  addToHead: inserts an element at the start of the list  addToTail: inserts an element at the end of the list  deleteFromHead: deletes the first element of list  deleteFromTail: deletes the last element of the list  delete: deletes a specified entry from the list  find: finds an element in the list  print: prints the contents of the list  isInList: retruns “true” as the function result if element x is in list, or otherwise returns “false”  And so on

14 14 Implementation of Linked List To implement a linked list, we need to create two classes:  A node class (representing one entry in the linked list)  And a linked list class (which makes a linked list by creating and linking several nodes) 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 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

15 15 class Node { public: int info; Node *next; Node():info(0), next(0) { } Node(int el, Node *n=0) { info = el; next = n; } }; Implementation of Linked List: Node Class

16 16 class SinglyLinkedList { private: Node *head; //pointer to first node of list Node *tail; //pointer to last node of list public: SinglyLinkedList():head(0),tail(0) { } ~SinglyLinkedList(); Continue on next slide… Implementation of Linked List: Linked List Class

17 17 bool isEmpty(); //list is empty void addToHead(int el);//inserts an element in front of list void addToTail(int el);//inserts an element at the end of list void deleteFromHead();//delete a node from the front of a list void deleteFromTail();//delete a node form the end of a list void delete(int el);//delete a node form list whose value is el Node* find(int el);//Find the node from list whose value is el //and returns its pointer. void prints();//prints the contents of a list bool isInList(int el);//check that an element el exists in a list or not }; Implementation of Linked List: Linked List Class

18 18 Insert Node in front of a list Adding a node at the beginning of a linked list is performed in four steps. 1. 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. a head bcd tail 2.The node's info member is initialized to a particular value. a head bcd tail e

19 19 Insert Node in front of a list 3. 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 4.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 a head bcd tail e a head bcd tail e

20 20 Insert Node in front of a list void SinglyLinkedList::addToHead(int el) {Node *n = new Node(e1, 0); //Creates new node tail head if (isEmpty)//first node of a list head = tail = n; else// Insert node in front of the list { n e a head bcd tail n e n->next = head; head = n; head }

21 21 Insert Node at end of a list The process of adding a new node to the end of the list has five steps. 1. An empty node is created. a head bcd tail 2.The node’s info member is initialized to a value a head bcd tail e 3.Because the node is being included at the end of the list, the next member is set to null. a head bcd tail e

22 22 Insert Node at end of a list 4. 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. 5. 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. a head bcd tail ea head bcd tail e

23 23 Insert Node at end of a list void SinglyLinkedList:: addToTail(int el) {Node *n = new Node(e1, 0);//Creates new node n e tail head if(isEmpty)//First node of a list head = tail = n; else//Insert node at the end of a list { n ea head bcd tail tail->next = n; tail = n; tail }

24 24 Delete Node from front of a list void SinglyLinkedList:: deleteFromHeadl() {if(head != 0)//Non empty list { Node *n = head; If(head == tail)//if only one node in the list n d tail head else a head bcd tail n head = head->next; head delete n; } } head = tail = 0;

25 25 Delete Node from end of a list void SinglyLinkedList:: deleteFromTail() { if(head != 0)//Non empty list if(head = = tail)//Only one node in a list { a head bcd tail n else {Node *n = head; while(n->next != tail) n = n->next; n tail = n; delete tail; tail->next = 0; } d tail head delete head; head = tail = 0 } tail

26 26 Delete Node from List void SinglyLinkedList:: Delete(int el) This function deletes a node whose entry is el from the list a b c d header Deletion of a node containing c from a linked list The figure shows how the node containing c is deleted..

27 27 Delete Node from List void SinglyLinkedList:: Delete(T el) {if(head != 0)//Non empty list if(head == tail && el == head -> info) //only one node in a list { d tail head delete head; head = tail = 0; } else if(el == head -> info) //List has more then one nodes { a head bcd tail temp Node *temp = head; head = head -> next; head delete temp; //An Old head is deleted } Continue on next slide…..

28 28 Delete Node from List else { //List has more then one nodes and non head node is deleted Node *tmp = head; while(tmp != 0 && tmp -> next ->info != el) tmp = tmp -> next; a head bcd tail If(tmp != 0) { //Node of value el exists tmp -> next = tmp -> next -> next; If(d == tail) //last node of a list tail = tmp; tmp delete d; } Node * d = tmp -> next; d

29 29 Destructor void SinglyLinkedList :: ~SinglyLinkedList() { if(!isEmpty())//List is not empty while(head != 0) deleteFromHead(); }


Download ppt "CS212: Data Structures and Algorithms Lecture # 4 Linked List."

Similar presentations


Ads by Google