Linked Lists 2
Linked Lists 2
A linked list is a sequence of data structures, which are connected together via links. They are a dynamic
in nature which allocates the memory when required. Insertion and deletion operations can be easily
implemented.
Lesson Objectives:
At the end of this lesson, you will be able to:
• Understand what is a linked list
• Learn the linked list representation
• Learn linked list basic operation
• Understand the different types of linked list
WHAT IS A LINKED LIST?
• A linked list is a linear data structure, in which the elements are not stored at contiguous memory
locations.
• A linked list consists of nodes where each node contains a data field and a reference (link) to the next
node in the list.
• Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at a
contiguous location; the elements are linked using pointers.
Link- Each link of a linked list can store a data called an element.
Next- Each link of a linked list contains a link to the next link called Next.
LinkedList- contains the connection link to the first link called First.
• Each link is linked with its next link using its next link.
• Last link carries a link as null to mark the end of the list.
INSERTION OPERATION
DELETION OPERATION
REVERSE OPERATION
SINGLY LINKED LIST EXAMPLE PROGRAM (INSERT, DELETE DISPLAY AND COUNT)
OUTPUT:
OUTPUT
C++ Implementation
OUTPUT:
SKIP LIST
• A skip list is a data structure that is used for storing a sorted list of items with a help of hierarchy of
linked lists that connect increasingly sparse subsequences of the items.
• A skip list allows the process of item look up in efficient manner. The skip list data structure skips
over many of the items of the full list in one step, that’s why it is known as skip list.
• The idea is simple, we create multiple layers so that we can skip some nodes.
• See the following example list with 16 nodes and two layers. The upper layer works as an “express
lane” which connects only main outer stations, and the lower layer works as a “normal lane” which
connects every station. Suppose we want to search for 50, we start from first node of “express lane”
and keep moving on “express lane” till we find a node whose next is greater than 50. Once we find
such a node (30 is the node in following example) on “express lane”, we move to “normal lane”
using pointer from this node, and linearly search for 50 on “normal lane”. In following example, we
start from 30 on “normal lane” and with linear search, we find 50.
OUTPUT:
• Self-Organizing list basically updates the list of given range of items on the basis of last searched item.
In this method, the sequential searching approach is used. This algorithm shifts the more important data
to the beginning of the list. The time complexity of this search technique is O(n).
A Self Organizing list reorders its nodes based on searches which are done. The idea is to use locality of
reference (In a typical database, 80% of the access are to 20% of the items). Following are different strategies
used by Self Organizing Lists.
1) Move-to-Front Method: Any node searched is moved to the front. This strategy is easy to implement, but it
may over-reward infrequently accessed items as it always move the item to front.
2) Count Method: Each node stores count of the number of times it was searched. Nodes are ordered by
decreasing count. This strategy requires extra space for storing count.
3) Transpose Method: Any node searched is swapped with the preceding node. Unlike Move-to-front, this
method does not adapt quickly to changing access patterns.
Algorithm
Begin
Function FibonacciSearch().
Calculate the mid value using ‘start+fib[index-2]’ expression.
If the chosen item is equal to the value at mid index, print result and return to main.
If it is lesser than the value at mid index, proceed with the left sub-array.
If it is more than the value at mid index, proceed with the right sub-array.
If the calculated mid value is equal to either start or end then the item is not found in the array.
End
Note: The level of nodes is decided randomly, so output may differ.
OUPUT
SUMMARY
• A linked list is a linear data structure, in which the elements are not stored at contiguous memory
locations.
• A linked list consists of nodes where each node contains a data field and a reference (link) to the next
node in the list.
• Each link of a linked list can store a data called an element.
• Each link of a linked list contains a link to the next link called next.
• Single Linked List has basic operations such as insertion, deletion, display and search.
• In singly linked list, the next pointer of the last node points to the first node.
• Doubly linked list contains element called first and last
• In doubly linked list, the next pointer of the last node points to the first node and the previous pointer of
the first node points to the last node making the circular in both directions.
• Doubly linked list has basic operations such as insertion, deletion, insert last, delete last insert after,
delete last , insert after, delete, display forward, display backward
• The skip list idea is simple, creating multiple layers so can skip some nodes.
• Self-Organizing list basically updates the list of given range of items on the basis of last searched item.