Available Balance
Linked Lists – Insertion , Searching and more

In general when people talk about insertion with respect to linked lists of any form they implicitly refer to the adding of a node to the tail of the list. When
you use an API like that of DSA and you see a general purpose method that adds a node to the list, you can assume that you are adding the node to the tail
of the list not the head.

Adding a node to a singly linked list has only two cases:
1. head = ; in which case the node we are adding is now both the head and
tail of the list; or
2. we simply need to append our node onto the end of the list updating the
tail reference appropriately.

1) algorithm Add(value)
2) Pre: value is the value to add to the list
3) Post: value has been placed at the tail of the list
4) n à node(value)
5) if head = ; 6) head à n
7) tail à n
8) else
9) tail.Next à n
10) tail à n
11) end if
12) end Add
As an example of the previous algorithm consider adding the following se-
quence of integers to the list: 1, 45, 60, and 12, the resulting list is that of

Figure 2.2.

 

Searching

Searching a linked list is straightforward: we simply traverse the list checking
the value we are looking for with the value of each node in the linked list. The
algorithm listed in this section is very similar to that used for traversal in x2.1.4.

1) algorithm Contains(head, value)
2) Pre: head is the head node in the list
3) value is the value to search for
4) Post: the item is either in the linked list, true; otherwise false
5) n à head
6) while n 6= ; and n.Value 6= value
7) n à n.Next
8) end while
9) if n = ; 10) return false
11) end if
12) return true
13) end Contains

guys for study about physics so let’s join my post and support

physics – http://literacybase.com/physics-study-electric-charges-and-fields/

If you want to for more accounting for begging

so let’s join our accounting post – Introduction to Accounting – Definition, Meaning

Rate This Content




  • Leave a reply

    Your email address will not be published.