ALGORITHM
ALGORITHM
AIM:
To Write a program to implement the List ADT using arrays and linked lists.
ALGORITHM:
i) Algorithm List ADT using arrays.
AIM:
To Write a programs to implement the following using a singly linked list. Stack ADT
Queue ADT.
ALGORITHM:
i)Stack
1. Define a Stack class with the following operations:
a. is_empty(): Return True if the stack is empty, False otherwise.
b. push(item): Add an item to the top of the stack.
c. pop(): Remove and return the item from the top of the stack.
d. peek(): Return the item at the top of the stack without removing it.
e. size(): Return the number of elements in the stack.
f. clear(): Remove all elements from the stack
ii)Queue
1. Define a Queue class with the following operations:
a. is_empty(): Return True if the queue is empty, False otherwise.
b. enqueue(item): Add an item to the rear of the queue.
c. dequeue(): Remove and return the item from the front of the queue.
d. front(): Return the item at the front of the queue without removing it.
e. rear(): Return the item at the rear of the queue without removing it.
f. size(): Return the number of elements in the queue.
g. clear(): Remove all elements from the queue.
EX:NO:
AIM:
To Write a program that reads an infix expression, converts the expression to postfix
form and then evaluates the postfix expression (use stack ADT).
ALGORITHM:
1. Initialize an empty stack and an empty string for the output postfix expression.
2. Scan the infix expression from left to right.
3. For each character in the infix expression:
a. If it's an operand, append it to the output string.
b. If it's an operator:
i. Pop and append operators from the stack to the output until the stack is
empty, or the top of the stack has lower precedence than the current operator, or the
top of the stack is an opening parenthesis "(".
ii. Push the current operator onto the stack.
c. If it's an opening parenthesis "(", push it onto the stack.
d. If it's a closing parenthesis ")", pop operators from the stack and append them to
the output until an opening parenthesis "(" is encountered. Discard the opening
parenthesis.
4. After scanning the entire infix expression, pop any remaining operators from the
stack and append them to the output string.
5. The resulting output string is the postfix expression.
EX:NO:
AIM:
To Write a program to implement priority queue ADT.
ALGORITHM:
1. Define a Priority Queue class with the following operations:
is_empty()`: Check if the priority queue is empty.
enqueue(item, priority)`: Add an item with its associated priority to the priority
queue.
dequeue()`: Remove and return the item with the highest priority.
peek()`: Return the item with the highest priority without removing it.
size()`: Return the number of elements in the priority queue.
clear()`: Remove all elements from the priority queue.
2. Implement the priority queue using an appropriate data structure that efficiently
supports insertion and removal operations based on priority.
3. Ensure that elements with equal priority are dequeued in the order they were
enqueued.
EX:NO:
AIM:
To Write a program to perform the following operations: Insert an
element into a binary search tree. Delete an element from a binary search tree.
Search for a key element in a binary search tree.
ALGORITHM:
1. Define a Node class to represent each node in the binary search tree. Each node
should have data, a left child reference, and a right child reference.
AVL TREE
DATE:
AIM:
To Write a program to perform the following operations Insertion into an
AVL-tree, Deletion from an AVL-tree.
ALGORITHM:
AIM:
To Write a programs for the implementation of BFS and DFS for a given
graph.
ALGORITHM:
i)BFS
Step 2: Enqueue the starting node A and set its STATUS = 2 (waiting state)
Step 4: Dequeue a node N. Process it and set its STATUS = 3 (processed state).
Step 5: Enqueue all the neighbours of N that are in the ready state (whose STATUS
= 1) and set
their STATUS = 2
[END OF LOOP]
Step 6: EXIT
ii) DFS
Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state)
Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state)
Step 5: Push on the stack all the neighbors of N that are in the ready state (whose
STATUS = 1) and set their STATUS = 2 (waiting state)
[END OF LOOP]
Step 6: EXIT
EX:NO:
AIM:
To Write a programs for implementing the following searching methods: Linear
search Binary search.
ALGORITHM:
i) Linear Search
Step 1: Set i to 1.
Step 2: if i > n, then jump to step 7.
Step 3: if A[i] = x then jump to step 6.
Step 4: Set i to i + 1.
Step 5: Go to step 2.
Step 6: Print element x found at index i and jump to step 8.
Step 7: Print element not found.
Step 8: Exit.
ii) Binary Search
AIM:
ALGORITHM:
i) Bubblle sort
1. The largest element is placed in its correct position, i.e., the end of the array.
2. Place the second largest element at correct position.
Step 1 - If the element is the first element, assume that it is already sorted.
Return
Step 3 - Now, compare the key with all elements in the sorted array.
Step 4 - If the element in the sorted array is smaller than the current element,
then move to the next element. Else, shift greater elements in the array towards
the right.