Algorithm To Perform Push
Algorithm To Perform Push
Algorithm
Push Operation
Pop Operation
Peek Operation
Algorithm
Algorithm
Algorithm
Algorithm Steps
1. Reverse the Infix Expression: Start by reversing the given infix expression. While
reversing, make sure to swap the parentheses:
a. Replace ( with ) and vice versa.
2. Convert the Reversed Expression to Postfix: Apply the usual infix to postfix
conversion using a stack:
a. If the character is an operand, add it directly to the result.
b. If the character is an operator, pop operators from the stack until an operator
with lower precedence or a parenthesis is at the top of the stack, and then
push the current operator onto the stack.
c. If the character is a parenthesis, handle it similarly to the standard infix-to-
postfix conversion.
3. Reverse the Postfix Expression: After converting the reversed infix to postfix,
reverse the resulting postfix expression to get the prefix expression.
Algorithm create a linked list and perform insertions and deletions of all cases. Write
functions to sort and finally delete the entire list at once.
• Node Structure: We define a Node structure that contains an integer data and a
pointer to the next node (next).
• Insertion Functions:
• insertAtBeginning: Adds a node at the start.
• insertAtEnd: Adds a node at the end by traversing to the last node.
• insertAfter: Adds a node after a specified node (by data).
• Deletion Functions:
• deleteFromBeginning: Deletes the first node.
• deleteFromEnd: Deletes the last node by traversing to the second-last node.
• deleteNode: Deletes a node with a specific value.
• Sorting Function:
• sortList: Implements bubble sort to sort the linked list in ascending order.
• Delete Entire List:
• deleteList: Deletes all nodes in the list by traversing through and freeing each
node.
• Printing the List:
• printList: Traverses and prints all the elements in the list.
Algorithm to implement a linear queue
Algorithm Steps
1. Initialize:
a. Define two pointers, low and high, representing the bounds of the search
range.
b. Initially, low is the first index (0), and high is the last index (n-1, where n is
the size of the array).
2. Iterate:
a. Calculate the middle index mid = low + (high - low) / 2 to avoid
overflow.
b. Compare the middle element (arr[mid]) with the target element (key):
i. If arr[mid] == key, then the element is found and return the mid
index.
ii. If arr[mid] < key, then the target must be in the right half, so set
low = mid + 1.
iii. If arr[mid] > key, then the target must be in the left half, so set
high = mid - 1.
3. Termination:
a. The search continues until low <= high. If low becomes greater than high,
then the element is not present in the array, and return -1.
Function linearSearch:
Takes an array arr[], its size n, and the key (element to search for).
It loops through the array from the first element to the last.
If the element at the current index matches key, it returns the index.
If the loop completes without finding the key, it returns -1, indicating the element is
not present.
Main Function:
Function insertionSort:
Function printArray:
Main Function:
Function selectionSort:
Function printArray:
This function is used to print the array before and after sorting.
Main Function:
Graph Representation:
We represent the graph using an adjacency list. Each vertex points to a list of its
adjacent vertices.
A Node structure is used to represent each vertex and its adjacent vertices in the
adjacency list.
createGraph:
Allocates memory for the graph structure and initializes each adjacency list to
NULL.
addEdge:
Adds an edge to the graph by creating a new node and inserting it into the adjacency
list of both the source and destination vertices (since it's an undirected graph).
BFS:
Main Function:
A graph with 4 vertices is created, and edges are added. The BFS traversal starts
from vertex 0 and explores all reachable vertices.
Graph Representation:
The graph is represented using an adjacency list. Each vertex points to a list of its
adjacent vertices.
A Node structure represents each vertex and its adjacent vertices in the adjacency
list.
createGraph:
Allocates memory for the graph structure and initializes each adjacency list to
NULL.
addEdge:
Adds an edge to the graph by creating a new node and inserting it into the adjacency
list of both the source and destination vertices (since the graph is undirected).
DFS (Recursive):
Main Function: