You are on page 1of 6

HARSHIT GUPTA

41051202717
CSE-EVE

POPL ASSIGNMENT

Q1.WHAT DO YOU UNDERSTAND BY LIFO


AND FIFO?

ANS-

1.Last-In-First-Out
In a LIFO data structure, the newest element added to the queue will be processed first.
Sometimes this will be called a stack and is usually pictured as below. The new element
is always added at the end of the stack. There are two basic operations that a LIFO
needs to allow:
 push – append an element to the end of the container
 pop – remove the most recent element from the container

The LIFO data structure can be thought of as analogous to a deck of cards that you add
to the top of and draw from the top of.

A Basic LIFO Data Structure


The following code should outline a basic LIFO as we have discussed so far.
class BasicLIFO
{
private:
/*use a vector to store the data...*/
vector<float> containerLIFO;
bool isEmpty()
{
/*checks if the container is empty or not...
we need this function as our container is private.*/
return containerLIFO.empty();
}

public:

void push(float x)
{
/*append value to vector...*/
containerLIFO.push_back(x);
}

bool pop()
{
/* check whether the queue is empty or not... */
if (isEmpty())
{
return false;
}
/* delete an element from the queue...*/
containerLIFO.pop_back();
/*return true if the operation is successful...*/
return true;
}
};
There are more advanced LIFO structures that are generic, multi-channeled, can handle
multiple users, multiple readers or can still have methods that handle internal sorting
and searching.
LIFOs are useful when a program needs to access the most recent information entered.
Stacks are important in algorithms involving backtracking. While this is not fundamental
to DSP it is common to use this in other parts of a program such as File Management or
parameter parsing. I highly recommend you come to grips with std::stack for solving
interview related LIFO questions.

1.First-In-First-Out
In a FIFO data structures the first element added to the container will be processed first.
There are two basic operations that a FIFO needs to allow:
 enqueue – append an element to the end of the container
 dequeue – remove the first element from the container

A Basic FIFO Data Structure


The following code should outline a basic FIFO as we have discussed so far.
class BasicFIFO
{
private:
/*use a vector to store the data...
pretend this is an audio buffer....*/
vector<float> containerFIFO;
/*a read index to indicate the start position...*/
int readIndex;

bool isEmpty()
{
/*checks whether the container is empty or not...*/
return readIndex >= containerFIFO.size();
}
public:
BasicFIFO()
{
/*initialize readIndex position...*/
readIndex = 0;
}

bool enqueueValue(float x)
{
/*insert an element into the container...
return true if the operation is successful...*/
containerFIFO.push_back(x);
return true;
}

bool dequeueValue()
{
/*increment readIndex...
return true if the operation is successful...*/
if (isEmpty())
{
return false;
}
readIndex++;
return true;
}
};

This class and the behavior designated by the functions ensures that our data structure
has this First In First Out behavior that the name implies. However, we can
do much better as this class is highly inefficient.
Q2.WHAT IS THE DIFFERENCE BETWEEN LINEAR AND NON
LINEAR DATA STRUCTURE?
ANS-
Linear Data Structures
A Linear data structure have data elements arranged in sequential
manner and each member element is connected to its previous and
next element. This connection helps to traverse a linear data structure
in a single level and in single run. Such data structures are easy to
implement as computer memory is also sequential. Examples of linear
data structures are List, Queue, Stack, Array etc.
Non-linear Data Structures
A non-linear data structure has no set sequence of connecting all its
elements and each element can have multiple paths to connect to
other elements. Such data structures supports multi-level storage and
often cannot be traversed in single run. Such data structures are not
easy to implement but are more efficient in utilizing computer memory.
Examples of non-linear data structures are Tree, BST, Graphs etc.

DIFFERENCE BETWEEN LINEAR AND NON LINEAR DATA STRUCTURE

Sr. Key Linear Data Structures Non-linear Data Structures


No.

Data Element In linear data structure, data In non-linear data structure, data
Arrangement elements are sequentially elements are hierarchically
1
connected and each element is connected and are present at
traversable through a single run. various levels.

2 Levels In linear data structure, all data In non-linear data structure, data
elements are present at a single elements are present at multiple
Sr. Key Linear Data Structures Non-linear Data Structures
No.

level. levels.

Implementation Linear data structures are easier to Non-linear data structures are
complexity implement. difficult to understand and
3
implement as compared to linear
data structures.

Traversal Linear data structures can be Non-linear data structures are


traversed completely in a single not easy to traverse and needs
4
run. multiple runs to be traversed
completely.

Memory Linear data structures are not very Non-linear data structures uses
5 utilization memory friendly and are not memory very efficiently.
utilizing memory efficiently.

Time Time complexity of linear data Time complexity of non-linear


6 Complexity structure often increases with data structure often remain with
increase in size. increase in size.

7 Examples Array, List, Queue, Stack. Graph, Map, Tree.

You might also like