0% found this document useful (0 votes)
65 views

DSA notes python

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

DSA notes python

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

24 November 2024 19:22

Q1. What is Stack data structure?


Ans: A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle .It contains only one pointer top
pointer pointing to the topmost element of the stack. a stack can be defined as a container in which insertion and deletion
can be done from the one end known as the top of the stack.

Q2. What are the different operation possible with stack data structures?
Ans:
The following are some common operations implemented on the stack:

A. Push: When we insert an element in a stack then the operation is known as a push. If the stack is full then
the overflow condition occurs.

The steps involved in the PUSH operation is given below:

1.Before inserting an element in a stack, we check whether the stack is full.

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

3.When we initialize a stack, we set the value of top as -1 to check that the stack is empty.

4.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.

5.The elements will be inserted until we reach the max size of the stack.

B. Pop: When we delete an element from the stack, the operation is known as a pop. If the stack is empty
means that no element exists in the stack, this state is known as an underflow state.
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

New Section 1 Page 1


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

Q3. What are the different applications of stack data structure?


Ans:
• Balancing of symbols: Stack is used for balancing a symbol. For example, we have the following program:

• Recursion: The recursion means that the function is calling itself again. To maintain the previous states, the
compiler creates a system stack in which all the previous records of the function are maintained.

• Expression conversion: Stack can also be used for expression conversion. This is one of the most important
applications of stack. The list of the expression conversion is given below:

Infix to prefix
Infix to postfix
Prefix to infix
Prefix to postfix
Postfix to infix

Memory management: The stack manages the memory. The memory is assigned in the contiguous memory blocks.
The memory is known as stack memory as all the variables are assigned in a function call stack memory.

Q4. What is Linked list data structure?


Ans:
Linked List can be defined as collection of objects called nodes that are randomly stored in the memory .A
node contains two fields i.e. data stored at that particular address and the pointer which contains the address
of the next node in the memory .The last node of the list contains pointer to the null.

New Section 1 Page 2


Q5. What are the different operation possible with Linked list data structures?
Ans:
1.Insertion

The insertion into a singly linked list can be performed at different positions. Based on the position of the new node
being inserted, the insertion is categorized into the following categories.

2. Deletion and Traversing


The Deletion of a node from a singly linked list can be performed at different positions. Based on the position of the
node being deleted, the operation is categorized into the following categories.

New Section 1 Page 3


Q6. What is array data structure?
Ans:
Arrays are defined as the collection of similar types of data items stored at contiguous memory locations. It is one of the
simplest data structures where each data element can be randomly accessed by using its index number.
• Index starts with 0.
• The array's length is 10, which means we can store 10 elements.
• Each element in the array can be accessed via its index.

Q7.What are the different operation possible with Arrays data structures?
Ans:
• Insertion - It is used to add an element at a particular index.
Insertion operation
This operation is performed to insert one or more elements into the array. As per the requirements, an element can be
added at the beginning, end, or at any index of the array. Now, let's see the implementation of inserting an element into the
array.

New Section 1 Page 4


array.

• Deletion - It is used to delete an element from a particular index.


Deletion operation
As the name implies, this operation removes an element from the array and then reorganizes all of the array elements.

New Section 1 Page 5


Q8. What are the different applications of Arrays data structure?
Ans:
Arrays are useful because -
• Sorting and searching a value in an array is easier.
• Arrays are best to process multiple values quickly and easily.
Q9. What is Queue data structure?
Ans:Queue is the data structure that is similar to the queue in the real world. 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. Queue can also be defined as the list or collection in which the insertion is done from one end
known as the rear end or the tail of the queue, whereas the deletion is done from another end
known as the front end or the head of the queue.

New Section 1 Page 6


Q10.What are the different operation possible with Queue data structures?
Ans:
• Enqueue: The Enqueue operation is used to insert the element at the rear end of the queue. It returns void.

• Dequeue: It performs the deletion from the front-end of the queue. It also returns the element which has been
removed from the front-end. It returns an integer value.

Q11. What are the different applications of Arrays data structure?


New Section 1 Page 7
Q11. What are the different applications of Arrays data structure?
Ans:
1. Queues are widely used as waiting lists for a single shared resource like printer, disk, CPU.

2. Queues are used in asynchronous transfer of data (where data is not being transferred at the same rate
between two processes) for eg. pipes, file IO, sockets.

3. Queues are used as buffers in most of the applications like MP3 media player, CD player, etc.

Q12. draw the graphical representation of Big-OH ,Big Omega and Theta notation.
Ans:
1. Big O notation (O):
It is defined as upper bound and upper bound on an algorithm is the most amount of time required ( the worst case performance).
Big O notation is used to describe the asymptotic upper bound.
Mathematically, if f(n) describes the running time of an algorithm; f(n) is O(g(n)) if there exist positive constant C and n0 such that

Mathematically, if f(n) describes the running time of an algorithm; f(n) is O(g(n)) if there exist positive constant C and n0 such that,
0 <= f(n) <= Cg(n) for all n >= n0

2. Big Omega notation (Ω) :


It is define as lower bound and lower bound on an algorithm is the least amount of time required ( the most efficient way possible, in
other words best case).
Just like O notation provide an asymptotic upper bound, Ω notation provides asymptotic lower bound.

Let f(n) define running time of an algorithm;


f(n) is said to be Ω(g (n)) if there exists positive constant C and (n0) such that
0 <= Cg(n) <= f(n) for all n >= n0
n = used to given lower bound on a function
If a function is Ω(n-square) it is automatically Ω(n) as well.

New Section 1 Page 8


3. Big Theta notation (Θ) :
It is define as tightest bound and tightest bound is the best of all the worst case times that the algorithm can take.
Let f(n) define running time of an algorithm.
f(n) is said to be Θ(g(n)) if f(n) is O(g(n)) and f(n) is Ω(g(n)).
Mathematically,
0 <= f(n) <= C1g(n) for n >= n0
0 <= C2g(n) <= f(n) for n >= n0
Merging both the equation, we get :
0 <= C2g(n) <= f(n) <= C1g(n) for n >= n0
The equation simply means there exist positive constants C1 and C2 such that f(n) is sandwich between C2 g(n) and C1g(n).

New Section 1 Page 9

You might also like