Linked Lists
Linked Lists
Linked Lists
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:
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.
⮚ 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)
http://gtustudymaterial.blogspot.in/
5
Data Structure Management (330701)
http://gtustudymaterial.blogspot.in/
6
Data Structure Management (330701)
4. Algorithm to insert new node in ordered linked list.
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)
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)
A 2010 B C D
2002 2006
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
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
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