Dsa Lab 06
Dsa Lab 06
Experiment 6
Arrays: Implementation of Linked List(part 2)
Usually, programmers learn about arrays before they learn about linked lists, so it’s useful to
compare the two. Although arrays and linked lists are both data structures for storing multiple
values, there are some major differences between the two. There are pros and cons to using
each.
Memory Allocation
Linked list is one of the fundamental data structures and can be used to implement other data
structures. In a linked list there are different numbers of nodes. Each node consists of two
fields. The first field holds the value or data and the second field holds the reference to the
next node or null if the linked list is empty.
2
Unlike a linked list, an array is both physically and logically contiguous. A good way to think
about an array is to imagine a single chunk of memory cells. The elements of an array are
actually located next to each other as consecutive memory addresses in RAM (Random
Access Memory).
The elements of a linked list, by contrast, are logically contiguous in their implementation,
but not physically contiguous in memory. A linked list doesn’t occupy a single block of
memory, allowing it to use whatever memory “scraps” are available. Because of this
flexibility, node elements can be different sizes as well as different data types.
Accessing Elements
One advantage an array has over a linked list is indexing. Instead of having to traverse down
every element in the array, you can access an element directly by its index number. This costs
only one instruction cycle. To get the fifth element of an array, you only need to perform one
lookup operation. The same operation in a linked list would cost you five instruction cycles
since you have to “inchworm” your way down the list.
In technical terms, retrieving an element by its index number in an array is always an O(1)
operation. By contrast, the same logically equivalent operation in a linked list would take a
variable amount of runtime depending on the number of elements preceding the one you’re
trying to access. Thus, retrieving a particular element in a linked list would be an O(n)
operation at worst and an O(1) operation at best (if the sought-after element happens to be at
the front of the list).
3
Inserting/Removing Elements
One advantage a linked list has over an array is the ease of inserting and removing elements.
In order to insert an element into the middle of an array, you have to shift each element over
to make space for insertion. This can be quite costly. In a linked list, however all you need to
do is reassign the pointer of one node to make it point to the new element and have that new
element point to the next logical node in the sequence.
There are several kinds of linked lists. In a singly linked list, each node has only one pointer,
commonly called “next” since it points to the next node in the sequence. The first node is
called the “head” of the list. The last node in the list points to NULL.
A singly linked list can become a circular node by assigning the address of the head node to
the tail node instead of NULL. This isn’t a particularly useful linked list, but it illustrates the
flexibility of this data structure.
A doubly linked list has two pointer variables, one pointing forward to the next node and one
pointing back to the previous node.
4
Lab Tasks:
Implement singly link list in C ++ and perform following operation on it