Presentation is loading. Please wait.

Presentation is loading. Please wait.

LINKED LISTS.

Similar presentations


Presentation on theme: "LINKED LISTS."— Presentation transcript:

1 LINKED LISTS

2

3 INTRODUCTION An array is a data structure where elements are stored in consecutive memory locations. Memory required for the array is allocated in advance. Hence, an array is also called as static data structure. If some data has to be inserted or removed from the array in the middle, some amount of data movement occurs. This consumes computer time and decreases program efficiency. Dynamic data structure where the memory space can be extended. Ex : linked – lists.

4 Array Limitations Arrays - ADVANTAGES Simple, Fast DISADVANTAGES:
Must specify size at construction time Murphy’s law Construct an array with space for n n = twice your estimate of largest collection Tomorrow you’ll need n+1 More flexible data structure – Linked list.

5 A linked list is an ordered collection of data elements called nodes.
Each node consists of two fields : data field and pointer field. Data field has the actual data. Pointer field has the address of the next element. An abstract data type (ADT) is a set of operations. Objects such as lists, sets, and graphs, along with their operations, can be viewed as abstract data types, just as integers, reals, and booleans are data types. next elem node

6 LIST ADT : First() : Returns the address of the first node in the linked list so the other nodes to the right of it can be accessed. Last() : Returns the address of the last node in the linked list so the other nodes to the right of it can be accessed. Insertfirst() : Inserts a node at the front of the linked list. Insertlast() : Inserts a node at the last of the linked list. Insertbefore() : Inserts a node before the specified node’s position. Insertafter() : Inserts a node after the specified node’s position. Remove() : removes a node at the specified position.

7 Replace() : replaces a value stored at a position with some other value.
Swap() : swap the values of the nodes specified by two positions. Benefits of linked lists : As the linked lists are dynamic, memory is utilized optimally. List manipulations (adding new elements, removing elements etc) are very simple pointer manipulations. Linked lists can be used to implement several other complex data structures.

8 Linked Lists Limitations
Linked lists are traversed in uni direction Complex to implement Sequential access to elements Leads to memory problems if not taken care about the pointer manipulations properly. Linked Lists Limitations

9 Singly Linked List A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores data link to the next node next node data (NULL) A B C D

10 Singly Linked List (contd..)
(NULL) Head B C D The very first node of the linked list is called Header node where no data is stored, but holds the address of the very first data element. The elements can be traversed from left to right. Hence, it is also called as one-way list.

11 Singly Linked List Operations
Insertion of a node into a singly linked list Traversing the singly linked list Deletion of a node from a singly linked list Copying a linked list to make a duplicate Merging two linked lists into a larger list Searching for an element in a singly linked list Reversing a singly linked list

12 Insertion of a Node Insertion of a node into a singly linked list
A node can be inserted into a singly linked list in any of the following three positions. i. Insertion of a node at the front. ii. Insertion at the end. iii. Insertion at any position.

13 Insertion of a Node at the Front
ALGORITHM Get a memory block of type NODE and store the starting address of NODE in a pointer variable NEW. i.e. NEW=GETNODE(NODE) If NEW is FULL then print “Memory is Insufficient” exit Else a) NEW.LINK = HEADER.LINK b) NEW.DATA = X c) HEADER.LINK = NEW 3. stop

14 Insertion of a Node at the End
ALGORITHM 1. Get a memory block of type NODE and store the starting address of this NODE in a pointer variable NEW. i.e. NEW=GETNODE(NODE) 2. If NEW = NULL then print “Memory is Insufficient” exit Else a) Move the address of the HEADER node to the pointer variable ptr. i.e., ptr = HEADER b) Now traverse the singly linked list until the last node is reached as following. i.e., ptr = ptr.LINK

15 Insertion of a Node at the End (contd..)
3) When prt.LINK becomes NULL, ptr is the last node of the singly linked list. i. Attach the new node address in the link field of the last node ptr. i.e., ptr.LINK = new ii. Place the data X in the data field of the new node. new.data = X iii. Make the LINK field of the new node to be NULL i.e., make this new node as the last node. NULL=new.LINK

16 Insertion of a Node at any position
ALGORITHM 1. Get a memory block of type NODE and store the address of it in a pointer variable NEW. i.e. NEW=GETNODE(NODE) 2. If NEW = NULL then print “Memory is Insufficient” exit Else a) Move the address of the HEADER node to the pointer variable ptr. i.e., ptr = HEADER b) Get the data item of the Node key after which the new node has to be placed. c) Now traverse the singly linked list from the HEADER node to find the node which contains key as the data item i.e., ptr = ptr.LINK

17 Insertion of a Node at any position(contd..)
3. If ( ptr.LINK = NULL) print “ key is not available in the list” exit. Else a) Make the LINK field of the new node to point the node pointed out by ptr. i.e., NEW.LINK = ptr.LINK b) Place the data X in the DATA field of the NEW node. i.e., NEW.DATA = X c) Now make the LINK field of the ptr node to point the NEW node ptr.LINK=NEW 4. EXIT

18 Traversing through Singly Linked list
Visiting each node in the singly linked list starting from the first node to the last node. ALGORITHM i. Get the first node address from the pointer field of the HEADER node. i.e., ptr = HEADER.LINK ii. Now traverse the singly linked list as long as ptr is not NULL. i.e. while (ptr is not NULL) do steps (a) and (b). a) Print the data in the ptr node. i.e. print ptr.DATA. b) Go to the next node of ptr. i.e. ptr = ptr.LINK. iii. Stop.

19 Deletion of a Node A node can be deleted from a singly linked list in any of the following three positions. i. Deletion of a node at the front. ii. Deletion of a node at the end. iii. Deletion of a node at any position.

20 Deletion of a Node at the Front
ALGORITHM Get the address of the first node from the link field of the HEADER node. i.e. ptr = HEADER.LINK 2. If ( ptr is NULL ) print “ The singly linked list is empty” print “ So deletion cannot be performed” EXIT. 3. Otherwise do the following: a) Get the address of the node which is pointed out by the first node. i.e., ptr = ptr.LINK b) Now the HEADER node should be made to point to the second node. i.e. , HEADER.LINK = ptr. c) Deallocate or free the space allocated for the ptr node. i.e., return (ptr) to the free store. 4. stop

21 Deletion of a Node at the End
ALGORITHM Start from the HEADER node. i.e., ptr = HEADER. If the LINK field of HEADER node is NULL print “ the list is empty; no deletion”. 3. Otherwise do the following : a) Traverse the singly linked list from the first node to the last node to find the last but one node. i.e., while (ptr.LINK is not NULL) i. ptr1 = ptr. ii. Ptr = ptr.LINK b) Now ptr1 is the last but one node. Make its LINK field to be NULL. i.e., ptr1.LINK = NULL c) ptr is the last node of the singly linked list. 4. Stop.

22 Deletion of a Node at any position
ALGORITHM Start from the HEADER node. i.e., ptr1 = HEADER Get the LINK field of the HEADER node. i.e., ptr = ptr1.LINK Accept the data X of the node which has to be deleted. Check whether the LINK field of the HEADER node is NULL a) If so, print “ linked list is empty” exit. 5. Otherwise do the following : a) Traverse the singly linked list from the first node to the last node to find the node with the value X. i.e., while (ptr is not NULL) do step i.

23 Deletion of a Node at any position(contd..)
If the data field of ptr is not X Go to the next node Store the previous node in ptr1 i.e., ptr1 = ptr ptr = ptr.LINK Otherwise make the previous node’s LINK field to point to the next node. i.e., ptr1.LINK = ptr.LINK Now return ptr to the free store. Exit.

24 Copy A Linked List To Make A Duplicate
The entire singly linked list is copied into another by allocating new memory space. ALGORITHM: Start from the HEADER node. i.e., ptr = HEADER. Allocate memory space for the single node in a singly linked list and store it in HEADER1. 3. a) Assume that ptr1 is the pointer variable which points or holds the HEADER node of duplicate linked list. i.e., ptr1 = HEADER1 b) Make the data field of ptr1 to be NULL i.e., ptr1.DATA = NULL

25 Copy A Linked List To Make A Duplicate (contd..)
4. Now traverse the singly linked list from the first node to the last node and do the following i.e., while (ptr is not NULL) do the following Allocate memory space for a node and store the starting address of it in NEW i.e., NEW = GETNODE(NODE) b) Move the DATA field of ptr node to the NEW node i.e., NEW.DATA = ptr.DATA c) Make the LINK field of ptr1 node to point to the NEW node i.e., ptr1.LINK = NEW d) Make the LINK field of the NEW node to be NULL i.e., NEW.LINK = NULL e) Make the NEW node to be the current node of the duplicate linked list. i.e., ptr1 = NEW f) Go to the next node of the original linked list i.e., ptr = ptr.LINK 5. Stop.

26 Merging of two singly linked list into one list
Two singly linked lists are combined to form a singly linked list. ALGORITHM: Start from the HEADER node of the first singly linked list i.e., ptr = HEADER1 2. Traverse the first singly linked list until the last node of this is reached. a) Go to the next node. i.e., ptr = ptr.LINK 3. Once the last node is reached (ptr), make the LINK field of this last node to point to the HEADER2 node’s LINK content i.e., ptr.LINK = HEADER2.LINK 4. Now return the HEADER2 node to the free store. 5. Now HEADER1 is the HEADER node of the merged singly linked list. i.e., HEADER = HEADER1. 6. Stop.

27 Search for an element in a singly linked list
The nodes in a singly linked list are traversed to find a node with the data X ALGORITHM: a) Start from the HEADER node. i.e., ptr = HEADER b) Go to the first node in the singly linked list. i.e., ptr = ptr.LINK 2. Assume f = 0, loc = NULL. Get the data to be searched – X 3. Now traverse the singly linked list from the first node to the last node as long as f is 0. a) If the DATA field of ptr node is X. i. f = 1 ii. Loc = ptr iii. Return loc iv. Go to step 5 otherwise i. Go to the next node of the singly linked list 4. If there is no match. Print “ Search is not successful” 5. Stop. SEARCH FOR AN ELEMENT IN A SINGLY LINKED LIST

28 Doubly linked list Also called as two-way lists or two-way chains where it is possible to traverse either from left to right or from right to left. Each node can have any number of data fields and two link fields which are used to point to the previous node and next node. There are two NULL: at the first and last nodes in the list. Advantages: Given a node, it is easy to visit its predecessor. Convenient to traverse lists backwards A B C Head

29 Operations in a Doubly linked list
Insertion Traversing the Doubly linked list Deletion Copying a doubly linked list to make a duplicate copy Merging two linked lists into a larger list Searching for an element in a doubly linked list

30 Circular linked lists A Circular Linked List is a special type of Linked List. It supports traversing from the end of the list to the beginning by making the last node point back to the head of the list. A Rear pointer is often used instead of a Head pointer 10 20 40 55 70 REAR

31 Circular linked lists (contd..)
Circular linked lists are usually sorted. Applications: Circular linked lists are useful for playing video and sound files in “looping” mode. They are also a stepping stone to implementing graphs. Advantages: Any node can be accessible from any other node by chaining through the list. To delete a particular node, it is not necessary to start from the very first node of the linked list. Operations like concatenation and splitting are more efficient.

32 Types of Circular linked lists
Singly Linked Circular List : The last node points to the first node and there is only link between the nodes of the linked list. 10 20 40 55 70 Doubly Linked Circular List : The last node points to the first node and there are two links between the nodes of the linked list. A B C Head

33 Operations on a Circular linked list
Insertion splitting Traversing Deletion Copying Merging Searching

34 Polynomial ADT Structure of the polynomial node coef expon link
Operations : i. Addition of two polynomials ii. Multiplication of two polynomials

35 Algorithm for adding two polynomials linked lists
Let p,q be two linked lists set p, q to point to the two first nodes (no headers) . Initialize a linked list r for a zero polynomial while p != null and q != null if (p.exp > q.exp) create a node storing p.coeff and p.exp, then insert at the end of list r; advance p else if (q.exp > p.exp) create a node storing q.coeff and q.exp, then insert at the end of list r; advance q else if (p.exp == q.exp) if (p.coeff + q.coeff) != 0 create a node storing p.coeff + q.coeff and p.exp, then insert at the end of list r advance p, q if p != null copy the remaining terms of p to end of r else if q != null copy the remaining terms of q to end of r

36 Time Complexity - adding two polynomials linked lists
Each iteration of the while loop takes O(1) time, moving at least one of the two pointers p and q one position over. Any remaining terms will be copied after the loop. Thus the total time is O(m+n) because there are a total of m+n terms in the two polynomials.

37 Algorithm for multiplying two polynomials linked lists
set p to the first node (no header) of one polynomial. initialize a linked list r for a zero polynomial while p != null copy list q to a new list t ,multiply the term pointed by p to each term of t by set t.exp += p.exp set t.coeff *= p.coeff add polynomial t to polynomial r (then discard t) advance p

38 Algorithm for multiplying two polynomials linked lists
Complexity: If polynomial p, q have m, n terms, respectively, the while loop runs m iterations. In each iteration, producing the polynomial t (copy and multiply) costs O(n) time since the length of t is O(n). Adding polynomial t to r runs in time O(n + length of r). Since the length of r in the beginning of iteration k is O((k –1)n), so the total time of the algorithm is the choice of m and n is arbitrary, so we can achieve O(m,n(min(m,n)) time.

39 Examples a null b -3 10 null

40 Adding Polynomials 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14
2 8 1 0 a -3 10 b 11 14 a->expon == b->expon d 2 8 1 0 a -3 10 b 11 14 -3 10 a->expon < b->expon d

41 Adding Polynomials (Continued)
2 8 1 0 a -3 10 b 11 14 -3 10 2 8 d a->expon > b->expon

42 THE END


Download ppt "LINKED LISTS."

Similar presentations


Ads by Google