Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Linked Lists

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Data Structure Management (330701)

4. LINKED LISTS
Pointers and Linked Allocation
⮚ Pointer: A pointer is a variable which contains an address of another variable in memory.It
is declared by * indicator.
⮚ We can create a pointer variable in C using following syntax:
Data type *pointer name;
⮚ For example:
int *ptr;
Here, ptr is a pointer to integer data type.
⮚ Suppose one variable called X having value 10 is stored at address(memory location) 2000.
2000 10 🡨 ptr
⮚ If we want a pointer ptr to points to the address 2000 then we have to use following syntax:
ptr = &X;
⮚ If we want to access the value of variable X using pointer variable than we have to use the
following syntax:
Y = *ptr; give 10 to variable Y.
⮚ Thus the use of a pointer(link) to refer to the element of data structure implies that, Elements
which are logically adjacent need not be physically adjacent in Memory is known as linked
allocation.
Linked lists & Sequential list
⮚ A singly linked list is to expand each node to contain a link or pointer to the next node. This
is also called as a one way chain.
⮚ For example:

⮚ First contain the address of the first node of the list.


⮚ Each node in the list consist of two parts:
1. Information(INFO)
2. Address or printer to next node (LINK).

http://gtustudymaterial.blogspot.in/
1
Data Structure Management (330701)

⮚ In link list each node contains a Link and INFO. INFO part contains the actual element of
list. Link or pointer contains the address of next node in the list. Last node indicates end of
list contains a special value, known as a “NULL”. Link(Next)=NULL.
Difference between Linked list and Sequential list
Linked List Sequential List
1. In linked list number of elements in the 1. in sequential list number of elements in
list is not fixed. list is fixed.
2. In linked list allocation of memory is 2. In sequential list allocation of memory is
done at runtime. done at compile time.
3. Insertion and deletion operation are 3. Insertion and deletion operation are very
very easy and less time consuming in lengthy and time consuming in sequential
linked list. list
4. In linked list we require pointer 4. In sequential list there is no need to use
variable which occupies extra memory pointer variable so it does not occupies
space. extra memory space.
5. Searching in a linked list is very time 5. Searching is less time consuming in
consuming because we have to traverse sequential list because we can use binary
entire list even if all the elements in the search method to search an element which
list are sorted. is very efficient.
6. In linked list the elements which are 6. In sequential list elements are logically
logically adjacent need not be physically and physically adjacent to each other.
adjacent.
7. It is very easy to join or split two lists. 7. It is very difficult to split or join two lists.

Singly linked list


⮚ A list in which each node contains a link or pointer to next node in the list is known as singly
linked list or one way chain.
⮚ Representation of Singly linked linier list is shown below:

⮚ Here, the variable FIRST contains an address of the first node in the list.

http://gtustudymaterial.blogspot.in/
2
Data Structure Management (330701)
⮚ The last node of the list does not have any successor node, so it does not contain any address
in its link field. In such case a NULL value is stored as an address. NULL indicates end of
the list.
Operations on Linked list
⮚ Followings are the operations that can be performed on linked list.
1. Traversing a linked list.
2. Insert new node at beginning of the list
3. Insert new node at end of the list
4. Insert new node into ordered list
5. Insert new node at any position or in between the list.
6. Delete first node of the list
7. Delete last node of the list.
8. Delete node from any specific position in the list.
9. Searching element in list.
10. Merging of two linked list.
11. Sorting operation of list.
12. Copy of the list.
Algorithms for Singly linked list.
1. Algorithm to insert new node at beginning of the linked list.

http://gtustudymaterial.blogspot.in/
3
Data Structure Management (330701)

INSERTBEG (VAL,FIRST)
This function inserts a new element VAL at the beginning
of the linked list.
FIRST is a pointer which contains address of first node in
the list.
[Check for availability stack underflow]
If AVAIL = NULL then
Write “Availability stack underflow”
Return
[Obtain address of next free node]
NEWAVAIL
[Remove free node from availability stack]
AVAILLINK (AVAIL)
[Initialize node to the linked list]
INFO (NEW) VAL
LINK (NEW) NULL
[Assign the address of the Temporary node to the

First Node ]
FIRSTNEW
2. Algorithm to insert new node at end of the linked list.

http://gtustudymaterial.blogspot.in/
4
Data Structure Management (330701)

INSERTEND (VAL,FIRST)
This function inserts a new element VAL at the end of the
linked list.
FIRST is a pointer which contains address of first node in
the list.
[Check for availability stack underflow]
If AVAIL = NULL then
Write “Availability stack underflow”
Return
[Obtain address of next free node]
NEWAVAIL
[Remove free node from availability stack]
AVAILLINK (AVAIL)
[initialize field of new node]
INFO (NEW) VAL
LINK (NEW) NULL
[If list is empty?]
If FIRST = NULL then
FIRSTNEW
[initialize search for last node]
SAVEFIRST
[Search end of the list]
Repeat while LINK (SAVE) ≠ NULL
SAVELINK (SAVE)
[Set LINK field of last node to NEW ]
LINK (SAVE) NEW
[Finished]
Return (FIRST)

3. Algorithm to insert new node at specific location.

http://gtustudymaterial.blogspot.in/
5
Data Structure Management (330701)

INSPOS (VAL, FIRST, N)


This function inserts a new element VAL into the linked list
specified by address N.
FIRST is a pointer which contains address of first node in
the list.
[Check for availability stack underflow]
If AVAIL = NULL then
Write “Availability stack underflow”
Return
[Obtain address of next free node]
NEWAVAIL
[Remove free node from availability stack]
AVAILLINK (AVAIL)
[initialize field of new node]
INFO (NEW) VAL
[If list is empty?]
If FIRST = NULL then
LINK (NEW) NULL
FIRSTNEW
[Search the list until desired address found]
SAVEFIRST
Repeat while LINK (SAVE) ≠ NULL and SAVE ≠ N
PREDSAVE
SAVELINK (SAVE)
[Node found]
LINK (PRED) NEW
[Finished]
Return (FIRST)

http://gtustudymaterial.blogspot.in/
6
Data Structure Management (330701)
4. Algorithm to insert new node in ordered linked list.

INSORD (VAL, FIRST)


This function inserts a new element VAL into the ordered
linked list.
FIRST is a pointer which contains address of first node in
the list.
[Check for availability stack underflow]
If AVAIL = NULL then
Write “Availability stack underflow”
Return
[Obtain address of next free node]
NEWAVAIL
[Remove free node from availability stack]
AVAILLINK (AVAIL)
[initialize field of new node]
INFO (NEW) VAL
[If list is empty?]
If FIRST = NULL then
LINK (NEW) NULL
FIRSTNEW
[Does the new node precede all nodes in the
list?]
If INFO (NEW) ≤ INFO (FIRST) then
LINK (NEW) FIRST
FIRSTNEW
[Initialize search pointer]
SAVEFIRST
[Search for predecessor of new node]
Repeat while LINK (SAVE) ≠ NULL and INFO (LINK
(SAVE)) ≤ INFO (NEW)
SAVELINK (SAVE)
[Set LINK field of new node and its
predecessor ]
LINK (NEW) LINK (SAVE)
LINK (SAVE) NEW
[Finished]
Return (FIRST)

http://gtustudymaterial.blogspot.in/
7
Data Structure Management (330701)
5. Algorithm to delete first node of linked list

DELFIRST(FIRST)
This function deletes a first node from the list.
FIRST is a pointer which contains address of first node in the list.
[Check for empty list]
If FIRST = NULL then
Write “List is empty”
Return
[Check for the element in the list and delete it]
If LINK (FIRST) = NULL then
YINFO (FIRST)
FIRSTNULL
Else
TEMPFIRST
YINFO (TEMP)
FIRSTLINK (TEMP)
[Finished]
Return (FIRST)

6. Algorithm to delete last node of linked list


DELLAST(FIRST)
This function deletes a last node from the list.
FIRST is a pointer which contains address of first node in the list.
[Check for empty list]
If FIRST = NULL then
Write “List is empty”
Return
[Check for the element in the list and delete it]
If LINK (FIRST) = NULL then
YINFO (FIRST)
FIRSTNULL
Else
(Assign the address pointed by FIRST pointer to TEMP pointer)
TEMPFIRST
Repeat while LINK (TEMP) ≠ NULL
PREDTEMP
TEMPLINK (TEMP)
[Delete Last Node]
YINFO (TEMP)
LINK (PRED) NULL
[Finished]
http://gtustudymaterial.blogspot.in/
8
Data Structure Management (330701)
7. Algorithm to delete specific node from linked list.

DELPOS(FIRST,N)
This function deletes a node from specific location from the
list.
FIRST is a pointer which contains address of first node in
the list.
[Check for empty list]
If FIRST = NULL then
Write “List is empty”
Return
[If there is only one node?]
If LINK (FIRST) = NULL then
YINFO (FIRST)
FIRSTNULL
[If list contains more than one node?]
TEMPFIRST
Repeat while LINK (TEMP) ≠ NULL and TEMP ≠ N
PREDTEMP
TEMPLINK (TEMP)
[Node found?]
If TEMP ≠ N then
Write “Node not found”
Else
YINFO (TEMP)
LINK (PRED) LINK (TEMP)
[Finished]
Return (FIRST)

http://gtustudymaterial.blogspot.in/
9
Data Structure Management (330701)
9. Algorithm to copy linked list

COPY(FIRST)
This function copy one list into another list.
FIRST is a pointer which contains address of first node in
the list.
BEGIN is a pointer which points to the address of first node
in the list.
[Empty List?]
If FIRST = NULL then
BEGINNULL
[Copy First Node]
If AVAIL = NULL then
Write “Availability stack underflow”
Else
NEWAVAIL
AVAILLINK (AVAIL)
FIELD (NEW) INFO (FIRST)
BEGINNEW
[Initialize traversal]
SAVEFIRST
[Move to next node if not at end of the list]
Repeat thru step 6 while LINK (SAVE) ≠ NULL
[Update predecessor and save pointers]
PREDNEW
SAVELINK (SAVE)
[Copy node]
If AVAIL = NULL then
Write “availability stack underflow”
Else
NEWAVAIL
AVAILLINK (AVAIL)
FIELD (NEW) INFO (SAVE)
PTR (PRED) NEW
[Set link of last node and return]
PTR (NEW) NULL

http://gtustudymaterial.blogspot.in/
10
Data Structure Management (330701)

Circular Linked list


⮚ A list in which last node contains a link or pointer to the first node in the list is known as
circular linked list.
⮚ Representation of circular linked list is shown below:

A 2010 B C D
2002 2006

FIRST=2000 2010 2002 2006

⮚ Advantage of circular linked list over singly linked linier list:


1. In singly linked list the last node contains NULL address. So if we are at middle of the
list and want to access the first node we can not go backward in the list. Thus every node
in the list is not accessible from given node. While in Circular linked list last node
contains an address of the first node so every node is in the list is accessible from given
node.
2. In singly linked list if we want to delete node at location X, first we have to find out the
predecessor of the node. To find out the predecessor of the node we have to search entire
list from starting from FIRST node in the list. So in order to delete the node at Location
X we also have to give address of the FIRST node in the list. While in Circular linked list
every node is accessible from given node so there is no need to give address of the first
node in the list.
3. Concatenation and splitting operations are also efficient in circular linked list as
compared to the singly linked list.
⮚ Disadvantage:
1. Without some care in processing it is possible to get into the infinite loop. So we must
able to detect end of the list.
2. To detect the end of the list in circular linked list we use one special node called the
HEAD node.
3. Such a circular linked list with HEAD node is shown below:
HEAD

B C D
2010 2002 2006

http://gtustudymaterial.blogspot.in/
11
Data Structure Management (330701)
Doubly Linked list
⮚ In Singly linked list we are able to traverse the list in only one direction. However in some
cases it is necessary to traverse the list in both directions.
⮚ This property of linked list implies that each node must contain two link fields instead of one
link field.
⮚ One link is used to denote the predecessor of the node and another link is used to denote the
successor of the node.
⮚ Thus each node in the list consist of three fields:
1. Information
2. LPTR
3. RPTR
NODE
LPTR INFO RPTR

⮚ A list in which each node contains two links one to the predecessor of the node and one to
the successor of the node is known as doubly linked linier list or two way chain.
⮚ Representation of doubly linked linier list is shown below:
L R
A B C

⮚ L is a pointer denotes the left most node in the list.


⮚ R is a pointer denotes the right most node in the list.
⮚ The left link of the left most node and right link of right most node are set to NULL to
indicate end of the list in each direction.
⮚ Advantage:
1. In singly linked list we can traverse only in one direction while in doubly linked list we
can traverse the list in both directions.
2. Deletion operation is faster in doubly linked list as compared to the singly linked list.

Operations on Doubly linked list


⮚ Followings are the operations that can be performed on doubly linked list.
1. Insert new node in doubly linked list
2. Delete node from doubly linked list
3. Traverse doubly linked list.

http://gtustudymaterial.blogspot.in/
12
Data Structure Management (330701)
Doubly linked circular list
⮚ A doubly linked list in which left link of the left most node points to the right most node and
right link of the right most node points to the left most node is known as Doubly circular
linked list.
⮚ Representation of doubly circular linked list shown below:

A B C

⮚ Advantage:
1. Every node is accessible from given node.
⮚ Disadvantage:
1. Without some care in processing it is possible to get into the infinite loop. So we must
able to detect end of the list.
2. To detect the end of the list in doubly circular linked list we use one special node called
the HEAD node.
3. Such a doubly circular linked list with HEAD node is shown below:

B C

Algorithms for Doubly linked list


1. Algorithm to insert new node in doubly linked list
DOUBLEINS (L, R, M, X)
⮚ This function inserts an element into double linked list.
⮚ L is a pointer which contains address of left most node in the list.
⮚ R is a pointer which contains address of right most node in the list.
⮚ X contains the value to be inserted in to the list
1. [Create new node]
If AVAIL = NULL then
Write “Availability stack underflow”
Else
NEW🡨AVAIL
AVAIL🡨LINK (AVAIL)
http://gtustudymaterial.blogspot.in/
13
Data Structure Management (330701)
2. [Copy information field]
INFO (NEW) 🡨X
3. [insertion into an empty list]
If R = NULL then
LPTR (NEW) 🡨RPTR (NEW) 🡨NULL
L🡨R🡨NEW
4. [left most insertion]
If M = L then
LPTR (NEW) 🡨NULL
RPTR (NEW) 🡨M
LPTR (M) 🡨NEW
L🡨NEW
5. [insert in middle]
LPTR (NEW) 🡨LPTR (M)
RPTR (NEW) 🡨M
LPTR (M) 🡨NEW
RPTR (LPTR (NEW)) 🡨NEW
6. [Finished]
2. Algorithm to delete node from doubly linked list.
⮚ DOUBLEDEL(L, R,OLD)
⮚ This function inserts an element into double linked list.
⮚ L is a pointer which contains address of left most node in the list.
⮚ R is a pointer which contains address of right most node in the list.
⮚ OLD contains address of node which we want to delete.
1. [Underflow]
If R = NULL then
Write “Underflow”
2. [Delete Node]
If L = R then
L🡨R🡨NULL
Else if OLD = L then
L🡨RPTR (L)
LPTR (L) 🡨NULL
Else if OLD = R then
R🡨LPTR(R)

http://gtustudymaterial.blogspot.in/
14
Data Structure Management (330701)
RPTR(R) 🡨NULL
Else
RPTR (LPTR (OLD)) 🡨RPTR (OLD)
LPTR (RPTR (OLD)) 🡨LPTR (OLD)
3. [Return Delete node]
Restore (OLD)
Return
Application of Linked list

⮚ Linked lists are used as a building block for many other data structures, such as stacks,
queues and their variations.
⮚ The "data" field of a node can be another linked list. By this device, one can construct many
linked data structures with lists; this practice originated in the Lisp programming language,
where linked lists are a primary (though by no means the only) data structure, and is now a
common feature of the functional programming style.
⮚ Sometimes, linked lists are used to implement associative arrays, and are in this context
called association lists. There is very little good to be said about this use of linked lists; they
are easily outperformed by other data structures such as self-balancing binary search trees
even on small data sets. However, sometimes a linked list is dynamically created out of a
subset of nodes in such a tree, and used to more efficiently traverse that set.
⮚ Polynomial manipulation
⮚ Linked dictionary
⮚ Multiple precision arithmetic

http://gtustudymaterial.blogspot.in/
15

You might also like