Chapter 2 - Linear Data Structures
Chapter 2 - Linear Data Structures
Efficiency: Efficiency of a program depends upon the choice of data structures. There
are better data structures which can make the search process efficient like ordered
array, binary search tree or hash tables.
Reusability: Data structures are reusable, i.e. once we have implemented a particular
data structure, we can use it at any other place. Implementation of data structures
can be compiled into libraries which can be used by different clients.
Abstraction: Data structure is specified by the ADT which provides a level of
abstraction. The client program uses the data structure through interface only,
without getting into the implementation details.
Traversing: Traversing the data structure means visiting each element of the data structure in order to
perform some specific operation like searching or sorting.
Insertion: Insertion can be defined as the process of adding the elements to the data structure at any
location.
Deletion:The process of removing an element from the data structure is called Deletion.
Searching: The process of finding the location of an element within the data structure is called
Searching.
Sorting:The process of arranging the data structure in a specific order is known as Sorting.
Merging: When two lists List A and List B of size M and N respectively, of similar type of elements,
clubbed or joined to produce the third list, List C of size (M+N), then this process is called merging.
Arrays are defined as the collection of similar type of data items stored at contiguous
memory locations.
Arrays are the derived data type in C programming language which can store the
primitive type of data such as int, char, double, float, etc.
Array is the simplest data structure where each data element can be randomly
accessed by using its index number.
Each element is of same data type and carries a same size i.e. int = 4 bytes.
Elements of the array are stored at contiguous memory locations where the first
element is stored at the smallest memory location.
Elements of the array can be randomly accessed since we can calculate the address
of each element of the array with the given base address and the size of data
element.
Array provides the single name for the group of variables of the same type therefore,
it is easy to remember the name of all the elements of an array.
Traversing an array is a very simple process, we just need to increment the base
address of the array in order to visit each element one by one.
Any element in the array can be directly accessed by using the index.
Address of any element of a 1D array can be calculated by using the following formula:
− A[i] = α + (i) * esize
where:
α - base or starting address
i – element
esize – element size in bytes
A linked list is also a collection of elements, but the elements are not stored in a
consecutive location. Suppose a programmer made a request for storing the integer
value then size of 4-byte memory block is assigned to the integer value.
A linked list can also be defined as the collection of the nodes in which one node is
connected to another node, and node consists of two parts, i.e., one is the data part
and the second one is the address part, as shown in the below figure:
The declaration of an array is very simple as it is of single type. But the linked list
contains two parts, which are of two different types, i.e., one is a simple variable, and
the second one is a pointer variable. We can declare the linked list by using the user-
defined data type known as structure.
The structure of a linked list can be defined as:
struct node
{
int data;
struct node *next;
}
CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 15
BASIC OPERATIONS
Adding a new node in linked list is a more than one step activity. We shall learn this
with diagrams here. First, create a node using the same structure and find the
location where it has to be inserted. Imagine that we are inserting a node B
(NewNode), between A (LeftNode) and C (RightNode).Then point B.next to C −.
Deletion is also a more than one step process. We shall learn with pictorial
representation. First, locate the target node to be removed, by using searching
algorithms. The left (previous) node of the target node now should point to the next
node of the target node −
Except the node (first node) pointed by the head node, all
nodes should point to their predecessor, making them their
new successor.The first node will point to NULL.
We'll make the head node point to the new first node by using
the temp node.
A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is
named stack as it behaves like a real-world stack, for example – a deck of cards or a pile of
plates, etc.
A real-world stack allows operations at one end only. For example, we can place or remove
a card or plate from the top of the stack only. Likewise, Stack ADT allows all data operations
at one end only.At any given time, we can only access the top element of a stack.
A stack can be implemented by means of Array, Structure, Pointer, and Linked List.
Stack can either be a fixed size one or it may have a sense of dynamic resizing. (Refer
to Laboratory manual for the array and linked list implementation).
Stack operations may involve initializing the stack, using it and then de-initializing it. Apart
from these basic stuffs, a stack is used for the following two primary operations −
− push() − Pushing (storing) an element on the stack.
− pop() − Removing (accessing) an element from the stack.
To use a stack efficiently, we need to check the status of stack as well. For the same purpose,
the following functionality is added to stacks −
− peek() − get the top data element of the stack, without removing it.
− isFull() − check if stack is full.
− isEmpty() − check if stack is empty.
The order of the operands in both forms is the same whether or not parentheses
are present in the infix expression.
If the infix expression contains no parentheses, then the order of the operators in
the postfix expression is according to their priority.
If the infix expression contains parenthesized sub expressions, rule 2 applies for such
sub expression.
And the following are the priority numbers: 1) icp(x) - priority number when token x
is an incoming symbol (incoming priority) and 2) isp(x) - priority number when token
x is in the stack (in-stack priority)
CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 28
THE ALGORITHM: INFIX TO POSTFIX
CONVERSION
Convert a + ( b * c + d ) - f / g ^ h into
its postfix form using stack
Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both
its ends. One end is always used to insert data (enqueue) and the other is used to remove data
(dequeue). Queue follows First-In-First-Out methodology, i.e., the data item stored first will be
accessed first.
A real-world example of queue can be a single-lane one-way road, where the vehicle enters first, exits
first. More real-world examples can be seen as queues at the ticket windows and bus-stops.
CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 32
QUEUE REPRESENTATION
As we now understand that in queue, we access both ends for different reasons. The
following diagram given below tries to explain queue representation as data structure −
As in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers and
Structures. For the sake of simplicity, we shall implement queues using one-dimensional array.
(Refer to the Laboratory Manual for the array and linked list implementation of a Queue)
CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 33
BASIC OPERATIONS
Queue operations may involve initializing or defining the queue, utilizing it, and then
completely erasing it from the memory. Here we shall try to understand the basic operations
associated with queues −
− enqueue() − add (store) an item to the queue.
− dequeue() − remove (access) an item from the queue.
Few more functions are required to make the above-mentioned queue operation efficient.
These are −
− peek() − Gets the element at the front of the queue without removing it.
− isfull() − Checks if the queue is full.
− isempty() − Checks if the queue is empty.
Linear Queue. In Linear Queue, an insertion takes place from one end while the deletion
occurs from another end.
Circular Queue. In Circular Queue, all the nodes are represented as circular. It is similar to
the linear Queue except that the last element of the queue is connected to the first element.
Priority Queue. A priority queue is another special type of Queue data structure in which
each element has some priority associated with it.
Double Ended Queue (Dequeue). Both the Linear Queue and Deque are different as the
linear queue follows the FIFO principle whereas, dequeue does not follow the FIFO principle.
In Dequeue, the insertion and deletion can occur from both ends.
CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 35
QUEUE APPLICATION: TOPOLOGICAL
SORTING
Topological sorting is problem involving activity networks. It uses both sequential and
link allocation techniques in which the linked queue is embedded in a sequential
vector.
It is a process applied to partially ordered elements. The input is a set of pairs of
partial ordering and the output is the list of elements, in which there is no element
listed with its predecessor not yet in the output.
Input. A set of number pairs of the form (i, j) for each relation i ≼ j could represent
the partial ordering of elements.The input pairs could be in any order.
Output. The algorithm will come up with a linear sequence of items such that no
item appears in the sequence before its direct predecessor.
A requirement in topological sorting is not to output the items with which the
predecessors are not yet in the output. To do this, there is a need to keep track of
the number of predecessors for every item. A vector could be used for this purpose.
Let's call this vector COUNT. When an item is placed in the output, the count of
every successor of the item is decremented. If the count of an item is zero, or it
becomes zero as a result of putting in the output all of its predecessors, that would
be the time it is considered ready for output. To keep track of the successors, a
linked list named SUC, with structure (INFO, LINK), will be used. INFO contains the
label of the direct successor while LINK points to the next successor, if any.
CHAPTER 2: LINEAR DATA STRUCTURES 19 February 2023 38
EXAMPLE Draw the Graph:
Given the partial ordering of seven elements, how can Following the procedures of the algorithm, we have below:
they be arranged such that no element appears in the
sequence before its direct predecessor?
(0,1), (0,3), (0,5), (1,2), (1,5), (2,4), (3,2), (3,4), (5,4), (6,5),
(6,7), (7,1), (7,5)
Following are the basic operations supported by a list: Insertion, Deletion, Display, Search and Delete.
Stacks are sometimes called as Last-In-First-Out (LIFO) lists i.e. the element which is inserted first in
the stack, will be deleted last from the stack.
Evaluation of Expressions includes transforming infix, prefix and postfix expression with each other
One application of Stack is converting infix to postfix.
A queue is a data structure in which whatever comes first will go out first. It follows the FIFO (First-
In-First-Out) policy.
A queue can be a linear queue, circular queue, priority queue and double ended queue
One application of queue is topological sorting.