0% found this document useful (0 votes)
24 views38 pages

Cad 73 A 05354

Linear data structures are arrangements of data elements in a sequential manner, where each element is connected to its adjacent elements. Examples include arrays, stacks, and queues, each with specific operations for insertion, deletion, and traversal. Arrays store multiple values of the same type in contiguous memory, while stacks follow a LIFO principle and queues follow a FIFO principle.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views38 pages

Cad 73 A 05354

Linear data structures are arrangements of data elements in a sequential manner, where each element is connected to its adjacent elements. Examples include arrays, stacks, and queues, each with specific operations for insertion, deletion, and traversal. Arrays store multiple values of the same type in contiguous memory, while stacks follow a LIFO principle and queues follow a FIFO principle.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Unit-2: Linear Data Structure

What is mean by Linear Data Structure?


Data structure where data elements are arranged sequentially or linearly where
each element is connected to its previous element and next adjacent element is
called as linear data structure

Examples of linear data structures are,


• Arrays
• Stack
• Queue
• Linked List
Arrays
• Need of Arrays?
• What is Arrays?
• Arrays are used to store multiple values in a single variable, instead of
declaring separate variables for each value.
• Arrays is a collections of items of same data type stored in contiguous
memory location.
Cond..
Note: Location of next index depends on the datatype we use.
Array Declaration
Syntax: Data-type array_name [size of array]
Eg: int a[5];

Some correct array declaration:


int a[5];
int a[5*2];
int a[2+2];
Int a[10/2];

Some wrong array declaration:


 int a[-5];
 int a[];
Array Initialization
At compile time:
• int a[5]={0,1,2,-1,3}
• int a[]={1,2,3,4,5,6,7}
• int a[5]={1,2,3} //4th and 5th position it fill zeros
• int a[5]={1,2,3,4,5,6,7} //error
• int a[5]; //garbage values
• int a[5];
a[0]=1; a[2]=3; a[4]=5;
a[1]=2; a[3]=4;
int a[5]={};
Cond..

Run-Initialization
int arr[5];
int i;
printf("Enter array elements:");
for(i=0;i< 5;i++)
scanf("%d", &arr[i]);
Memory representation and elements access in Arrays

Array elements representation in memory.

Array is a fixed size data structure.

Formula to access array elements is,

Base+i*(Size of the data type)

 Array follows random access method.

 Time taken to access an array element is constant that is, O(1).


Operations on Arrays
Primary operations on array are,
• Traversal
• Insertion
• Deletion
• Searching
• Sorting

Some other operations on array are,

• Find maximum and minimum element from an array


• Reverse an array
• Find duplicate element of an array
• Find second largest element in the array
Traversal
• Visiting every element one after the other exactly once and printing them is called
as traversal.
• Sample code for traversal,
int a[50],size,i;
printf(“Enter the size of the array\n”);
scanf(“%d”,&size);
printf(“Enter array elements”);
for(i=0;i<size;i++)
{
scanf(“%d”,&a[i]);
}
Printf(“Array elements are\n”);
for(i=0;i<size;i++)
{
printf(“%d”,a[i]);
}
Cond..
Insertion

• Insertion of the element can be done in three different positions,


1. At the beginning
2. At the end
3. At the specific position
Note: While inserting the element we need to check the overflow condition (Exceeds
size of the array)
• Program to check overflow condition.
Deletion
Searching and Sorting
Stack
• Stack is non-primitive linear data structure that follows LIFO. Stack has
one end. It contains one pointer that is top of the stack pointer. Stack can
be defined as a container in which insertion and deletion can be done from
one end, known as top-of-the stack.
• In stack, insertion operation is called as PUSH operation
• Deletion operation is called as POP operation.
Working of stack

Initially let us consider stack is empty, and size of stack is 5.


Operations on stack

Primary operations on stack are,

• PUSH()
• POP()
• DISPLAY()

Some other operations on stack are,

• isEmpty()
• isFull()
• PEEK()
PUSH Operation

The steps involved in the PUSH operation is given below:

• Before inserting an element in a stack, we check whether the stack is


full.

• If we try to insert the element in a stack, and the stack is full, then
the overflow condition occurs.

• When we initialize a stack, we set the value of top as -1 to check that


the stack is empty.

• When the new element is pushed in a stack, first, the value of the top
gets incremented, i.e., top=top+1, and the element will be placed at the
new position of the top.

• The elements will be inserted until we reach the max size of the stack.
Cond..
POP Operation

The steps involved in the POP operation is given below:

• Before deleting the element from the stack, we check whether the stack is
empty.

• If we try to delete the element from the empty stack, then the underflow
condition occurs.

• If the stack is not empty, we first access the element which is pointed
by the top.

• Once the pop operation is performed, the top is decremented by 1, i.e.,


top=top-1.
Cond..
Applications of Stack
• A Stack can be used for evaluating prefix, postfix and infix
expressions consisting of operands and operators.

• Stacks can be used for Backtracking. Suppose we are finding a path


for maze problem. We choose a path and after following it we
realize that it is wrong. Now we need to go back to the beginning
of the path to start new path. This can be done using stack.

• It can also be used to convert one form of expression to another


form.

• It can be used for parenthesis checking, that is to check proper


opening and closing of parenthesis.

• It can be used for string reversal.

• It is used for function call.


Infix, Prefix and Postfix notation
• What is infix, postfix and Prefix notation

• Operator precedence rule (Parenthesis-LA, Exponential-RA, Division and


Multiplication-LA, Addition and Subtraction-LA )

• Associativity rule
Associative Rule Chart
Infix to Postfix conversion-Rules and Example
• Priorities of operator:
1. highest priority-Exponential operator
2. Next highest priority-Division and Multiplication
3. Least priority- Addition and Subtraction
• No two operators of same priority can stay together in stack column
• Lowest priority operator cannot be placed before highest priority.
• If any operator is between opening and closing parenthesis then pop
entire ( operator )
Example: Convert below infix expression and postfix expression.
1. (A+B/C*(D+E)-F)
2. A+(B*C-(D/E^F)*G)*H
Queue
• Queue is a non-primitive, linear data structure. Which is also called as abstract
data type (ADT). It follows first in first out (FIFO) method. That is, the element
which is inserted first will be deleted first.

• Unlike in stack where insertion and deletion of the element is done from only one
end which is called as top of the stack. Here in queue insertion and deletion is
done from two different ends. Insertion is done from rear (tail) end and deletion
is done from front(head) end.

• Insertion operation is called as enqueue and deletion operation is called as


dequeue
Operations on Queue
Primary operations

• enqueue(x)
• dequeue()
• display()

Some other operations

• isfull()
• isempty()
• front()/peek()
Limitations of Linear Queue

• In normal queue once an element


is deleted from the queue that
space becomes non-usable unless
you delete all the elements. If
all the elements are deleted the
queue size is automatically
reset to given size with front
and rear at -1 index.
Circular Queue
• The circular queue solves the problem of normal/linear queue. In normal queue once
an element is deleted from the queue that space becomes non-usable unless you
delete all the elements. If elements are deleted the queue size is automatically
reset to given size.
Priority Queue
• A priority queue is a special type of queue in which each element is associated
with a priority value. And, elements are served on the basis of their priority.
That is, higher priority elements are served first.
• However, if elements with the same priority occur, they are served according to
their order in the queue.

• Assigning Priority Value

• Generally, the value of the element itself is considered for assigning the
priority. For example,
• The element with the highest value is considered the highest priority element.
However, in other cases, we can assume the element with the lowest value as the
highest priority element.
Difference between Priority Queue and Normal Queue

In a queue, the first-in-first-out rule is implemented whereas, in a priority queue,


the values are removed on the basis of priority. The element with the highest priority
is removed first.
Dequeue

What is a queue?
A queue is a data structure in which whatever comes first will go out first, and it
follows the FIFO (First-In-First-Out) policy. Insertion in the queue is done from one
end known as the rear end or the tail, whereas the deletion is done from another end
known as the front end or the head of the queue.

The real-world example of a queue is the ticket queue outside a cinema hall, where the
person who enters first in the queue gets the ticket first, and the person enters last
in the queue gets the ticket at last.

What is a Dequeue (or double-ended queue) ?


The dequeue stands for Double Ended Queue. Dequeue is a linear data structure where
the insertion and deletion operations are performed from both ends. We can say that
dequeue is a generalized version of the queue.
Advantages of Array

• In arrays, the elements can be accessed randomly by using the index number.

• Using arrays, other data structures like stack, queue, linked list, trees,
graphs etc. can be implemented.

• Two dimensional arrays are used to represent matrices.


Dis-advantages of Array

• The number of elements to be stored in an array should be known in advance

• Allocating more memory than the requirement leads to wastage of memory


space and less allocation of memory also leads to problem.

• An array is a static structure. Once declared the size of the array cannot
be modified

• Insertion and deletion are quite difficult.

• Elements must be stored on consecutive memory location.


Linked List

• Linked List is a linear data structure

• Elements are stored in consecutive memory location.

• Linear order maintained using pointer(link)


Linked list
• Linked List is a linear data structure, in which elements are not stored at a
contiguous location, rather they are linked using pointers. Linked List forms a series
of connected nodes, where each node stores the data and the address of the next node.

• Node Structure: A node in a linked list typically consists of two components:


Data: It holds the actual value or data associated with the node.
Next Pointer: It stores the memory address (reference) of the next node in the
sequence.
Head and Tail: The linked list is accessed through the head node, which points to the
first node in the list. The last node in the list points to NULL or nullptr, indicating
the end of the list. This node is known as the tail node.
Why linked list?
• Dynamic Data structure: The size of memory can be allocated or de-allocated at run time
based on the operation insertion or deletion.
• Ease of Insertion/Deletion: The insertion and deletion of elements are simpler than
arrays since no elements need to be shifted after insertion and deletion, Just the
address needed to be updated.
• Efficient Memory Utilization: As we know Linked List is a dynamic data structure the
size increases or decreases as per the requirement so this avoids the wastage of
memory.
• Implementation: Various advanced data structures can be implemented using a linked list
like a stack, queue, graph, hash maps, etc.
Types of Linked List
There are mainly three types of linked lists:

• Singly-linked list

• Doubly linked list

• Circular linked list


Singly Linked List
In a singly linked list, each node contains a reference to the next node in the sequence.
Traversing a singly linked list is done in a forward direction.
Doubly Linked List
In a doubly linked list, each node contains references to both the next and previous
nodes. This allows for traversal in both forward and backward directions, but it requires
additional memory for the backward reference.
Circular Linked List
In a circular linked list, the last node points back to the head node, creating a
circular structure. It can be either singly or doubly linked.
Operations on Linked List
• Traversing: To traverse all nodes one by one.

• Insertion: To insert new nodes at specific positions.

• Deletion: To delete nodes from specific positions.

• Searching: To search for an element from the linked list.

You might also like