Linked List Data Structure
Linked List Data Structure
Data type is a way to classify various types of data such as integer, string, etc. which determines the values th
● Integers
● Boolean (true, false)
● Floating (Decimal numbers)
● Character and Strings
Those data types which are implementation independent as they can be implemented in one or the other way
● List
● Array
● Stack
● Queue
Basic Operations
The data in the data structures are processed by certain operations. The particular data
structure chosen largely depends on the frequency of the operation that needs to be performed
on the data structure.
● Traversing
● Searching
● Insertion
● Deletion
● Sorting
● Merging
What is an Algorithm ?
An algorithm is a finite set of instructions or logic, written in order, to accomplish a certain
predefined task. Algorithm is not the complete code or program, it is just the core
logic(solution) of a problem, which can be expressed either as an informal high level
description as pseudocode or using a flowchart.
Every Algorithm must satisfy the following properties:
An algorithm is said to be efficient and fast, if it takes less time to execute and consumes less
memory space. The performance of an algorithm is measured on the basis of following
properties :
1. Time Complexity
2. Space Complexity
Space Complexity
Its the amount of memory space required by the algorithm, during the course of its execution.
Space complexity must be taken seriously for multi-user systems and in situations where
limited memory is available.
An algorithm generally requires space for following components :
● Instruction Space: Its the space required to store the executable version of the program.
This space is fixed, but varies depending upon the number of lines of code in the program.
● Data Space: Its the space required to store all the constants and variables(including
temporary variables) value.
● Environment Space: Its the space required to store the environment information needed to
resume the suspended function.
To learn about Space Complexity in detail, jump to the Space Complexity tutorial.
Time Complexity
Time Complexity is a way to represent the amount of time required by the program to run till
its completion. It's generally a good practice to try to keep the time required minimum, so
that our algorithm completes it's execution in the minimum time possible. We will study
about Time Complexity in details in later sections.
Space complexity is the amount of memory used by the algorithm (including the input values
to the algorithm) to execute and produce the result.
Sometime Auxiliary Space is confused with Space Complexity. But Auxiliary Space is the
extra space or the temporary space used by the algorithm during it's execution.
A linked list is a linear data structure that includes a series of connected nodes. Here, each node
stores the data and the address of the next node. For example,
You have to start somewhere, so we give the address of the first node a special name called HEAD.
Also, the last node in the linked list can be identified because its next portion points to NULL.
Linked lists can be of multiple types: singly, doubly, and circular linked list. In this article, we will
focus on the singly linked list.
Representation of Linked List
Let's see how each node of the linked list is represented. Each node consists:
● A data item
We wrap both the data item and the next node reference in a struct as:
struct node
{
int data;
struct node *next;
};
Let us create a simple Linked List with three items to understand how this works.
/* Initialize nodes */
/* Allocate memory */
one->data = 1;
two->data = 2;
three->data=3;
/* Connect nodes */
one->next = two;
two->next = three;
three->next = NULL;
head = one;
In just a few steps, we have created a simple linked list with three nodes.
A stack is a linear data structure that follows the principle of Last In First Out (LIFO). This
means the last element inserted inside the stack is removed first.
You can think of the stack data structure as the pile of plates on top of another.
Here, you can:
And, if you want the plate at the bottom, you must first remove all the plates on top. This is
exactly how the stack data structure works.
1. A pointer called TOP is used to keep track of the top element in the stack.
2. When initializing the stack, we set its value to -1 so that we can check if the stack is empty by
comparing TOP == -1.
3. On pushing an element, we increase the value of TOP and place the new element in the
position pointed to by TOP.
4. On popping an element, we return the element pointed to by TOP and reduce its value.
5. Before pushing, we check if the stack is already full
6. Before popping, we check if the stack is already empty
Applications of Stack Data Structure
Although stack is a simple data structure to implement, it is very powerful. The most
common uses of a stack are:
● To reverse a word - Put all the letters in a stack and pop them out. Because of the LIFO order
of stack, you will get the letters in reverse order.
● In compilers - Compilers use the stack to calculate the value of expressions like 2 + 4 / 5
* (7 - 9) by converting the expression to prefix or postfix form.
● In browsers - The back button in a browser saves all the URLs you have visited previously in
a stack. Each time you visit a new page, it is added on top of the stack. When you press the
back button, the current URL is removed from the stack, and the previous URL is accessed.
C Arrays
●
● An array is a variable that can store multiple values. For example, if you want to store
100 integers, you can create an array for it.
● int data[100];
Suppose you declared an array mark as above. The first element is mark[0], the second
element is mark[1] and so on.
a hash function. It is done for faster access to elements. The efficiency of mapping
Let a hash function H(x) maps the value at the index x%10 in an Array. For example if
the list of values is [11,12,13,14,15] it will be stored at positions {1,2,3,4,5} in the array or
What is Collision?
Since a hash function gets us a small number for a key which is a big
integer or string, there is a possibility that two keys result in the same
value. The situation where a newly inserted key maps to an already
occupied slot in the hash table is called collision and must be handled
using some collision handling technique.
How to handle Collisions?
1. Separate Chaining
2. Open Addressing
Separate Chaining:
1. Simple to implement.
2. Hash table never fills up, we can always add more elements to
the chain.
3. Less sensitive to the hash function or load factors.
4. It is mostly used when it is unknown how many and how
frequently keys may be inserted or deleted.
Disadvantages:
n linear probing, the hash table is searched sequentially that starts from
the original location of the hash. If in case the location that we get is
already occupied, then we check for the next location.
S.No
Separate Chaining Open Addressing
.