You are on page 1of 8

DS

1. What is ds?
 Data structure is a storage that is used to store and organize data. It
is a way of arranging data on a computer so that it can be accessed
and updated efficiently. on your requirement and project, it is
important to choose the right data structure for your project. For
example, if you want to store data sequentially in the memory, then
you can go for the Array data structure.
 The first of the two components of a program is a data structure,
which is a specialized and organized “collection and arrangement of
data (or data elements) and its subsequent storage in a computer’s 2
memory, in such a way that it can be easily accessed and retrieved
when needed.” Since a data structure refers to how data is
organized, it also shows the logical relationship between and among
the elements of data
2. Types of data structure?
1. Primitive Data Structures
 Also called “primary data structure”, a primitive data structure is one
that is created from scratch, so to speak, without using other data
structures as support or tool. This is the most basic and simple data
structure, which is designed to operate upon by machine-level
instructions.Briefly, the known types of primitive data structures
include the Integer, Floating Point, Double, Character, Enumerated
Type, and the “true or false” Boolean structure.
2. Derived Data Structures (non-primitive data structures)
 When the users get to use a set of operations in order to define a
data type, we are referring to the Abstract Data Structure. Others
refer to it as “non-primitive data structure” and “secondary data
structure”, a name adapted in reference to the fact that its creation
is dependent on a primary data structure. Further, there are two
subcategories under this classification.

2.1. Static and Dynamic data structures


o This type of structure is formed when it is already known, from
the outset, how many data items will be contained within it. static
memory allocation may be used to create the data structure, and
the size of the structure may be determined early on, so it can be
fixed. This type of structure is ideal when dealing with a definite
or precise number of data items to be stored, making it easier to
add or remove data items accordingly, depending on the available
space within the memory allocation.The main argument against it
is that there is a possibility that the memory may not be used
efficiently. Example: Array
o In dynamic data structure, the number of data items may be
unknown or uncertain at the beginning. This requires a certain
degree of flexibility, which is why creation of the data structure is
facilitated by the use of dynamic memory allocation. It is dynamic
or “variable- 4 size” in the sense that, during execution, the data
structure size may grow or shrink as needed, in order to
accommodate the data to be stored.
o When it comes to memory use, dynamic data structures are much
more efficient than the static data structure. It is possible for the
structure to overflow or underflow. If there is too much data fed
into the structure and it goes beyond the allocated memory
limitation, an overflow takes place. In contrast, an underflow
increases risks of the structure being empty and unutilized.
Example: Linked List
2.2. Linear and Non linear data structures
o As the name implies, these structures clearly demonstrate
adjacent elements stored.
o A classic example of a linear data structure is a linked list, where
one link (called a node) in the list is directly related to the node
next to it, or even on the opposite side, on a one-is-to-one basis.
Array can also be called as linear as the elements are stored one
after the other as a list.
o Non linear data structures are the exact opposite of the linear
structure; here the datas are stored in a hierarchical manner or in
a branched mode.
o A typical representation of this is a tree, which is designed to
point to a single or multiple nodes at one time. Contiguous and
non-contiguous data structures

2.3. Contiguous and non-contiguous data structures


o In contiguous data structure, the elements are stored in
continuous memory locations. So elements can be stored only
when the required space are available as a single block. Even if
the memory space is available in scattered manner in required
size, we can’t store data. Example: array.
o But in non continuous data structures, data can be stored when
the required space is available in scattered locations. So memory
utilization is very efficient in non contiguous data structures.
Example: linked list

3. What is stack?
 Stack: stack is a Last In, First Out (LIFO) data structures which can be
implemented both using static methods and dynamic methods.. the
two basic operations can be performed in a stack are pushing and
popping. Pushing is the process of inserting an element to the top of
the stack where top is the position of last inserted item. Popping is
the process of deleting an element from the stack top.
4. Operation of stack?
 A stack is an Abstract Data Type (ADT), commonly used in most
programming languages.

 Stack is a LIFO data structure in which only two operations are


allowed i.e., pushing and popping. Pushing is the process of adding
an element to the stack top whereas popping is a process of
removing an item from the stack top.

 For easily understanding the concept of stack just imagine a stack as


a vertical cylinder which is closed at the bottom. So just imagine that
we have to put some coins into the cylinder. What will we do? We
will put the coins from the top of the cylinder. Each time when you
put a cylinder it will. So, we can place a coin only on the top of the
last inserted coin and also, we can remove the coin only from the top
of the cylinder. That is, the coin which was inserted last will be
removed from the search.

 Then we have to know what a stack top is. stack top is nothing but
the position of the last inserted item in a stack.
Stack can be implemented either statically or dynamically. Static
implementation of a stack is possible only with the help of an array
and the dynamic implementation of the stack is performed with the
help of a linked list.
 Another two concepts related with the stack are stack overflow and
stack underflow. Stack overflow is a condition in which there is no
further space in the stack for the insertion. And the stack underflow
is a condition in which the stack is empty.
5. What is push and pop with algorithm?
5.1. Push operation
 Adding an element into the top of the stack is referred to as push
operation. Push operation involves following two steps
o Increment the variable Top so that it can now refer to the next
memory location.
o Add an element at the position of the incremented top. This is
referred to as adding a new element at the top of the stack.

 Add an element at the position of the incremented top. This is


referred to as adding a new element at the top of the stack.

Algorithm

push (STACK,ITEM )

Description: Here STACK is an array with MAX locations.

TOP points to the top most element and ITEM is the value to be
inserted.

1. If TOP == MAX-1 Then [Check for overflow]

Print: Overflow

Else

Set TOP = TOP + 1 [Increment TOP by 1]

Set STACK[TOP] = ITEM [Assign ITEM to top of STACK]

Print: ITEM inserted

End of If
2.Exit

5.2. Pop operation


 Deletion of an element from the top of the stack is called pop
operation. The value of the variable top will be decremented by 1
whenever an item is deleted from the stack. The top most element of
the stack is stored in another variable and then the top is
decremented by 1. the operation returns the deleted value that was
stored in another variable as the result.
 The underflow condition occurs when we try to delete an element
from an already empty stack.

Algorithm

pop (STACK )

Description: Here STACK is an array with MAX locations.

TOP points to the top most element and ITEM contains the value
popped from STACK.

1. If TOP == -1 Then [Check for underflow]

Print: Underflow

Else

Set ITEM = STACK[TOP] [Assign top of STACK to ITEM]

Set TOP = TOP - 1 [Decrement TOP by 1]

Print ITEM “removed”

End If

2. Return
6. what is infix postfix and prefix operation?
 We know that a mathematical expression is a combination of
operands and operators And depending upon the position of the
operands and operators expression they can be represented in three
different ways. Prefix expression postfix expressions and infix
expressions

6.1. Infix notation: X + Y


o Operators are written in-between their operands. This is the usual
way we write expressions. An expression such as A * ( B + C ) / D is
usually taken to mean something like: "First add B and C together,
then multiply the result by A, then divide by D to give the final
answer."
6.2. Postfix notation (also known as "Reverse Polish
notation"): X Y +
o Operators are written after their operands. The infix expression
given above is equivalent to A B C + * D /

6.3. Prefix notation (also known as "Polish notation"): + X Y


o Operators are written before their operands. The expressions
given above are equivalent to / * A + B C D

7. Postfix evaluation algorithm?

Infix to postfix(infix)
Description: Here infix is an infix expression and we have to obtain a
corresponding postfix expression and a stack STK is used.
Initially we have to scan the infix expression from left to right until the
end of the infix expression until the end of expression is encountered.
While scanning from left to right we have to perform the required
operations depending on whether it is an operand and operator.
1) start
2) Add closing braces to the end of the infix expression and
corresponding opening braces to the top of the stack.
3) Scan infix from left to right until the end of the expression is
encountered.
a) If an operand is encountered simply push the operand on to STK
after incrementing the top
b) If an operand is encountered just add it to the postfix expression.
c) When a closing parenthesis is encountered
i) Pop and add to the postfix expression each operators until an
opening bracket is encountered
ii) Then remove the opening bracket from STK
d) If an operator is encountered
i) repeatedly pop and add to the postfix expression all the
operators having equal or higher precedence than the
operator encountered.
ii) Then add the operator on to STK
4) Stop

You might also like