Linear Data Structure
Linear Data Structure
INTRODUCTION:
In the world of computer science and data management, linear data
structures stand as the building blocks of many algorithms and data
management systems. These structures provide a simple yet powerful way to
organize and access data in a linear, one-dimensional fashion, which is crucial
for various computational tasks.
In this exploration of linear data structures, we will delve into each type,
understanding their properties, use cases, and the fundamental role they play in
computer science and software development. Whether you're a beginner in
programming or an experienced developer, a solid grasp of linear data structures
is essential for building efficient and effective algorithms and data management
systems.
Linear data structures are data structures in which data elements are stored in a
linear sequence.
They include:
1. Arrays: A collection of elements stored in contiguous memory locations .
2. Linked Lists: A collection of nodes, each containing an element and a
reference to the next node.
3. Stacks: A collection of elements with Last-In-First-Out (LIFO) order.
4. Queues: A collection of elements with First-In-First-Out (FIFO) order.
5. Linear data structures are used in many computer science applications such
as searching, sorting, and manipulating data. They offer efficient data
access, but may require additional memory for maintaining pointers
between elements.
ARRAY
The array is a data structure used to store homogeneous elements at contiguous
locations. The size of an array must be provided before storing data.
An array is a linear data structure that store a sequence of elements.
An array is defined as it is a collection of items stored at memory (contiguous
memory locations).
We can also say that arrays are the set of homogeneous(it can hold only one
type of data the data that is all floating numbers or all characters or all integers
numbers); data elements stored multiple items of the same type together in one
place in memory.
Array use an index-based data structure which helps to identify each of the
elements in array .makes it easier to calculate, what the position of each
element is by simply adding an offset to a base value.
Single sub-scripted values are called linear array or one- dimensional array
and Array can also handle complex data structures by storing data in a two-
subscripted variables are called as two-dimensional array.
Example:
For example, let us say, we want to store marks of all students in a class,
we can use an array to store them. This helps in reducing the use of a number
of variables as we don’t need to create a separate variable for marks of every
subject. All marks can be accessed by simply traversing the array.
Advantages of arrays:
1. Constant-time Access: Arrays allow for constant-time access to elements
by using their index, making it a good choice for implementing algorithms
that need fast access to elements.
2. Memory Allocation: Arrays are stored in contiguous memory locations,
which makes the memory allocation efficient.
3. Easy to Implement: Arrays are easy to implement and can be used with
basic programming constructs like loops and conditionals.
Disadvantages of arrays:
1. Fixed Size: Arrays have a fixed size, so once they are created, the size
cannot be changed. This can lead to memory waste if an array is too large
or dynamic resizing overhead if an array is too small.
3. Cache Misses: Arrays can suffer from cache misses if elements are not
accessed in sequential order, which can lead to poor performance.
Linked List
A linked list is a linear data structure (like arrays) where each element is a
separate object. A linked list is made up of two items that are data and a
reference to the next node. A reference to the next node is given with the help
of pointers and data is the value of a node. Each node contains data and links
to the other nodes. It is an ordered collection of data elements called a node
and the linear order is maintained by pointers. It has an upper hand over the
array as the number of nodes i.e. the size of the linked list is not fixed and can
grow and shrink as and when required, unlike arrays.
Types of Linked Lists:
1. Singly Linked List: In this type of linked list, every node stores the
address or reference of the next node in the list and the last node has the next
address or reference as NULL. For example 1->2->3->4->NULL
2. Doubly Linked List: In this type of Linked list, there are two references
associated with each node, One of the reference points to the next node and
one to the previous node. The advantage of this data structure is that we can
traverse in both directions and for deletion, we don’t need to have explicit
access to the previous node. Eg. NULL<-1<->2<->3->NULL
3. Circular Linked List: Circular linked list is a linked list where all nodes
are connected to form a circle. There is no NULL at the end. A circular linked
list can be a singly circular linked list or a doubly circular linked list. The
advantage of this data structure is that any node can be made as starting node.
This is useful in the implementation of the circular queues in the linked list.
Eg. 1->2->3->1 [The next pointer of the last node is pointing to the first]
4. Circular Doubly Linked List: The circular doubly linked list is a
combination of the doubly linked list and the circular linked list. It means that
this linked list is bidirectional and contains two pointers and the last pointer
points to the first pointer.
Stack
A stack or LIFO (last in, first out) is an abstract data type that serves as a
collection of elements, with two principal operations: push, which adds an
element to the collection, and pop, which removes the last element that was
added. In stack both the operations of push and pop take place at the same end
that is top of the stack. It can be implemented by using both array and linked
list.
It is defined as ordered collection of elements represented by a real physical
stack or pile. Linear data structure features insertion and deletion of items take
place at one end called top of the stack. You can use these concepts or
structures all throughout programming.
Insertion : O(1)
Deletion : O(1)
Access Time : O(n) [Worst Case]
Insertion and Deletion are allowed on one end.
Example:
Stacks are used for maintaining function calls (the last called function
must finish execution first), we can always remove recursion with the help of
stacks. Stacks are also used in cases where we have to reverse a word, check
for balanced parenthesis, and in editors where the word you typed the last is
the first to be removed when you use undo operation. Similarly, to implement
back functionality in web browsers .
Primary Stack Operations:
Types of Stacks:
Advantages of Stacks:
1. LIFO (Last-In, First-Out) Order: Stacks allow for elements to be stored and
retrieved in a LIFO order, which is useful for implementing algorithms like
depth-first search.
2. Efficient Operations: Stacks provide efficient push-and-pop operations, as
only the top element needs to be updated.
3. Easy to Implement: Stacks can be easily implemented using arrays or
linked lists, making them a simple data structure to understand and use.
Disadvantages of Stacks:
1. Fixed Size: Stacks have a fixed size, so they can suffer from overflow if too
many elements are added or underflow if too many elements are removed.
2. Limited Operations: Stacks only allow for push, pop, and peek (accessing
the top element) operations, so they are not suitable for implementing
algorithms that require constant-time access to elements or efficient
insertion and deletion operations.
3. Unbalanced Operations: Stacks can become unbalanced if push and pop
operations are performed unevenly, leading to overflow or underflow.
4. In summary, stacks are a good choice for problems where LIFO order and
efficient push and pop operations are important, but their disadvantages
should be considered for problems that require dynamic resizing, constant-
time access to elements, or more complex operations.
Queue
A queue or FIFO (first in, first out) is an abstract data type that serves as a
collection of elements, with two principal operations: enqueue, the process of
adding an element to the collection. (The element is added from the rear side)
and dequeue the process of removing the first element that was added. (The
element is removed from the front side). It can be implemented by using both
array and linked list. A queue is defined as a linear data structure that is open
at both ends.
Insertion : O(1)
Deletion : O(1)
Access Time : O(n) [Worst Case]
Example: Queue as the name says is the data structure built according to the
queues of a bus stop or train where the person who is standing in the front of
the queue(standing for the longest time) is the first one to get the ticket. So any
situation where resources are shared among multiple users and served on a
first come first serve basis. Examples include CPU scheduling, Disk
Scheduling. Another application of queue is when data is transferred
asynchronously (data not necessarily received at the same rate as sent)
between two processes. Examples include IO Buffers, pipes, file IO, etc.
void enqueue(int data): Inserts an element at the end of the queue i.e.
at the rear end.
int dequeue(): This operation removes and returns an element that is at
the front end of the queue.
Types of Queues:
Simple Queue: Simple queue also known as a linear queue is the most
basic version of a queue. Here, insertion of an element i.e. the Enqueue
operation takes place at the rear end and removal of an element i.e. the
Dequeue operation takes place at the front end.
Circular Queue: In a circular queue, the element of the queue act as a
circular ring. The working of a circular queue is similar to the linear queue
except for the fact that the last element is connected to the first element. Its
advantage is that the memory is utilized in a better way. This is because if
there is an empty space i.e. if no element is present a queue with decreasing
order of values. The priority can also be such that the element with the
lowest value gets the highest priority so in turn it creates a queue with
increasing order of values.at a certain position in the queue, then an
element can be easily added at that position.
Priority Queue: This queue is a special type of queue. Its specialty is
that it arranges the elements in a queue based on some priority. The priority
can be something where the element with the highest value has the priority
so it creates
Dequeue: Dequeue is also known as Double Ended Queue. As the name
suggests double ended, it means that an element can be inserted or removed
from both the ends of the queue unlike the other queues in which it can be
done only from one end. Because of this property it may not obey the First
In First Out property.
Advantages of Queues:
1. FIFO (First-In, First-Out) Order: Queues allow for elements to be stored
and retrieved in a FIFO order, which is useful for implementing algorithms
like breadth-first search.
2. Efficient Operations: Queues provide efficient enqueue and dequeue
operations, as only the front and rear of the queue need to be updated.
3. Dynamic Size: Queues can grow dynamically, so they can be used in
situations where the number of elements is unknown or can change over
time.
Disadvantages of Queues:
1. Limited Operations: Queues only allow for enqueue, dequeue, and peek
(accessing the front element) operations, so they are not suitable for
implementing algorithms that require constant-time access to elements or
efficient insertion and deletion operations.
2. Slow Random Access: Queues do not allow for constant-time access to
elements by index, so accessing an element in the middle of the queue can
be slow.
3. Cache Unfriendly: Queues can be cache-unfriendly, as elements are
retrieved in a different order than they are stored, which can lead to poor
cache utilization and performance.
CONCLUSSION:
Linear data structures play a pivotal role in the world of computer
science and data management. These structures, which include arrays, linked
lists, stacks, and queues, offer a foundational way to organize and manipulate
data in a sequential, one-dimensional manner.
The significance of linear data structures lies in their simplicity and efficiency.
They provide predictable access times, making them invaluable for various
computational tasks such as searching, sorting, and managing data. Their
versatility allows them to be used in a wide range of applications, from
implementing data storage systems to designing efficient algorithms.
Understanding the properties, advantages, and limitations of linear data
structures is essential for anyone involved in programming, software
development, or data analysis. These structures serve as the building blocks
upon which more complex data structures and algorithms are built, making
them a fundamental concept in the field of computer science.
In essence, linear data structures are the backbone of efficient data
management and algorithm design, and mastering them is a key step toward
becoming a proficient programmer and problem solver in the world of
technology.
REFERANCES BOOKS:
1.Data Structures & Algorithms in Computer Science
-Joe Oswald
2. Data Structures & Algorithms
- A.A Puntambekar