Pesudocode For 1 To 9
Pesudocode For 1 To 9
SEARCHING
(A) LINEAR SEARCH
AIM:
To implement a C program to search for a specific element (key) in a given list of elements using the Linear
Search technique.
Pseudocode :
function linear_search(array, n, target):
for i from 0 to n-1:
if array[i] == target:
return i
return -1
(B) BINARY SEARCH
AIM:
To implement the Binary Search algorithm in C to efficiently search for a target element in a sorted array.
Pseudocode :
function binary_search(array, n, target):
left = 0 right = n – 1
while left <= right:
mid = (left + right) / 2
if array[mid] == target:
return mid
else if array[mid] < target:
left = mid + 1
else:
right = mid – 1
return -1
2. SORTING
(A)BUBBLE SORT
AIM:
To write a C program to sort an array of elements in ascending order using the Bubble Sort technique.
Pseudocode :
function bubble_sort(array, n):
for i from 0 to n-1:
for j from 0 to n-i-2:
if array[j] > array[j+1]:
swap(array[j], array[j+1])
(B)INSERTION SORT
AIM:
To implement the Insertion Sort algorithm in C to sort an array of integers in ascending order.
PESUDOCODE:
function insertion_sort(array, n):
for i from 1 to n-1:
key = array[i]
j=i–1
while j >= 0 and array[j] > key:
array[j+1] = array[j]
j=j-1
array[j+1] = key
(C) SELECTION SORT
AIM:
To implement the Selection Sort array of elements in ascending order, the algorithm selects the minimum element from
the unsorted portion of the array and swaps it with the first unsorted element, repeatedly doing so until the entire
array is sorted.
PESUDOCODE:
function selection_sort(array, n):
for i from 0 to n-1:
min_index = i
for j from i+1 to n-1:
if array[j] < array[min_index]:
min_index = j
swap(array[i], array[min_index])
3. LINKED LIST
(A) IMPLEMENTATION OF SINGLY LINKED LIST
AIM
To implement a Singly Linked List in C and perform all basic operations, including insertion, deletion, traversal,
searching.
PESUDOCODE:
1. Define the Node structure
struct Node {
int data;
struct Node* next;
};
2. Create a new node
function createNode(value)
node = allocate memory for Node
if node is NULL
print "Memory allocation failed"
return NULL
end if
node.data = value
node.next = NULL
return node
end function
3. Insert a node at the beginning
function insertAtBeginning(head, value)
newNode = createNode(value)
if newNode is NULL
return head
end if
newNode.next = head
head = newNode
return head
end function
4. Insert a node at the end
function insertAtEnd(head, value)
newNode = createNode(value)
if newNode is NULL
return head
end if
if head is NULL
head = newNode
return head
end if
temp = head
while temp.next is not NULL
temp = temp.next
end while
temp.next = newNode
return head
end function
5. Insert a node at a specific position (1-based index)
function insertAtPosition(head, value, position)
if position == 1
return insertAtBeginning(head, value)
end if
newNode = createNode(value)
if newNode is NULL
return head
end if
temp = head
count = 1
while count < position - 1 and temp is not NULL
temp = temp.next
count = count + 1
end while
if temp is NULL
print "Position out of bounds"
return head
end if
newNode.next = temp.next
temp.next = newNode
return head
end function
6. Delete a node from the beginning
function deleteFromBeginning(head)
if head is NULL
print "List is empty"
return head
end if
temp = head
head = head.next
free temp
return head
end function
7. Delete a node from the end
function deleteFromEnd(head)
if head is NULL
print "List is empty"
return head
end if
if head.next is NULL
free head
head = NULL
return head
end if
prev = NULL
temp = head
while temp.next is not NULL
prev = temp
temp = temp.next
end while
prev.next = NULL
free temp
return head
end function
8. Delete a node at a specific position (1-based index)
function deleteAtPosition(head, position)
if position == 1
return deleteFromBeginning(head)
end if
temp = head
prev = NULL
count = 1
while count < position and temp is not NULL
prev = temp
temp = temp.next
count = count + 1
end while
if temp is NULL
print "Position out of bounds"
return head
end if
prev.next = temp.next
free temp
return head
end function
9. Display the linked list
function displayList(head)
if head is NULL
print "List is empty"
return
end if
temp = head
while temp is not NULL
print temp.data, "->"
temp = temp.next
end while
print "NULL"
end function
10. Search for a node by value
function searchNode(head, value)
position = 1
temp = head
while temp is not NULL
if temp.data == value
print "Value found at position:", position
return position
end if
temp = temp.next
position = position + 1
end while
print "Value not found in the list"
return -1
end function
(B) IMPLEMENTATION OF DOUBLY LINKED LIST
AIM:
To implement a Doubly Linked List in C for performing operations such as creation, insertion,deletion, traversal (forward
and backward), searching.
PESUDOCODE:
Doubly Linked List Structure in C:
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
1. Create a Node
function createNode(value)
node = allocate memory for Node
if node is NULL
print "Memory allocation failed"
return NULL
end if
node.data = value
node.next = NULL
node.prev = NULL
return node
end function
2. Insert at the Beginning
function insertAtBeginning(head, value)
newNode = createNode(value)
if newNode is NULL
return head
end if
newNode.next = head
if head is not NULL
head.prev = newNode
end if
head = newNode
return head
end function
3. Insert at the End
function insertAtEnd(head, value)
newNode = createNode(value)
if newNode is NULL
return head
end if
if head is NULL
head = newNode
return head
end if
temp = head
while temp.next is not NULL
temp = temp.next
end while
temp.next = newNode
newNode.prev = temp
return head
end function
4. Insert at a Specific Position
function insertAtPosition(head, value, position)
if position == 1
return insertAtBeginning(head, value)
end if
newNode = createNode(value)
if newNode is NULL
return head
end if
temp = head
count = 1
while count < position - 1 and temp is not NULL
temp = temp.next
count = count + 1
end while
if temp is NULL
print "Position out of bounds"
return head
end if
newNode.next = temp.next
if temp.next is not NULL
temp.next.prev = newNode
end if
temp.next = newNode
newNode.prev = temp
return head
end function
5. Delete from the Beginning
function deleteFromBeginning(head)
if head is NULL
print "List is empty"
return head
end if
temp = head
head = head.next
if head is not NULL
head.prev = NULL
end if
free temp
return head
end function
6. Delete from the End
function deleteFromEnd(head)
if head is NULL
print "List is empty"
return head
end if
if head.next is NULL
free head
head = NULL
return head
end if
temp = head
while temp.next is not NULL
temp = temp.next
end while
temp.prev.next = NULL
free temp
return head
end function
7. Delete from a Specific Position
function deleteAtPosition(head, position)
if position == 1
return deleteFromBeginning(head)
end if
temp = head
count = 1
while count < position and temp is not NULL
temp = temp.next
count = count + 1
end while
if temp is NULL
print "Position out of bounds"
return head
end if
temp.prev.next = temp.next
if temp.next is not NULL
temp.next.prev = temp.prev
end if
free temp
return head
end function
8. Display list from beginning
function DisplayListFromBeginning (head)
if head is NULL
print "List is empty"
return
end if
temp = head
while temp is not NULL
print temp.data
temp = temp.next
end while
end function
9. Display list from end
function DisplayListFromEnd (head)
if head is NULL
print "List is empty"
return
end if
temp = head
while temp.next is not NULL
temp = temp.next
end while
while temp is not NULL
print temp.data
temp = temp.prev
end while
end function
10. Search for an Element
function searchNode(head, value)
temp = head
position = 1
while temp is not NULL
if temp.data == value
print "Found at position", position
return position
end if
temp = temp.next
position = position + 1
end while
print "Element not found"
return -1
end function
(C) IMPLEMENTATION OF CIRCULAR LINKED LIST
AIM:
To implement a Circular Linked List in C that allows the user to perform various operations such as creation, insertion,
deletion, traversal, searching, display.
struct Node {
int data;
struct Node* next;
};
1. Create a Node
function createNode(value)
node = allocate memory for Node
if node is NULL
print "Memory allocation failed"
return NULL
end if
node.data = value
node.next = NULL
return node
end function
2. Insert at the Beginning
function insertAtBeginning(head, value)
newNode = createNode(value)
if newNode is NULL
return head
end if
if head is NULL // If list is empty, newNode points to itself
newNode.next = newNode
head = newNode
else
temp = head
while temp.next != head // Traverse to the last node
temp = temp.next
end while
temp.next = newNode // Last node points to the new node
newNode.next = head // New node points to the head
head = newNode // Update head to the new node
end if
return head
end function
3. Insert at the End
function insertAtEnd(head, value)
newNode = createNode(value)
if newNode is NULL
return head
end if
if head is NULL // If list is empty, newNode points to itself
newNode.next = newNode
head = newNode
else
temp = head
while temp.next != head // Traverse to the last node
temp = temp.next
end while
temp.next = newNode // Last node points to the new node
newNode.next = head // New node points to the head
end if
return head
end function
4. Insert at a Specific Position
function insertAtPosition(head, value, position)
newNode = createNode(value)
if newNode is NULL
return head
end if
if position == 1
return insertAtBeginning(head, value)
end if
temp = head
count = 1
while count < position - 1 and temp.next != head
temp = temp.next
count = count + 1
end while
if temp.next == head // Position out of bounds
print "Position out of bounds"
return head
end if
newNode.next = temp.next
temp.next = newNode
return head
end function
5. Delete from the Beginning
function deleteFromBeginning(head)
if head is NULL
print "List is empty"
return head
end if
if head.next == head // Only one node in the list
free head
head = NULL
else
temp = head
while temp.next != head // Traverse to the last node
temp = temp.next
end while
temp.next = head.next // Last node points to the second node
free head
head = temp.next // Update head to the second node
end if
return head
end function
6. Delete from the End
function deleteFromEnd(head)
if head is NULL
print "List is empty"
return head
end if
if head.next == head // Only one node in the list
free head
head = NULL
else
temp = head
while temp.next.next != head // Traverse to the second last node
temp = temp.next
end while
free temp.next // Delete the last node
temp.next = head // Second last node points to the head
end if
return head
end function
7. Delete from a Specific Position
function deleteAtPosition(head, position)
if head is NULL
print "List is empty"
return head
end if
if position == 1
return deleteFromBeginning(head)
end if
temp = head
count = 1
while count < position - 1 and temp.next != head
temp = temp.next
count = count + 1
end while
if temp.next == head // Position out of bounds
print "Position out of bounds"
return head
end if
toDelete = temp.next
temp.next = toDelete.next
free toDelete
return head
end function
8. Traverse the List
function displayList(head)
if head is NULL
print "List is empty"
return
end if
temp = head
do
print temp.data, "->"
temp = temp.next
while temp != head
print "Head"
end function
9. Search for a Node
function searchNode(head, value)
position = 1
temp = head
while temp != head
if temp.data == value
print "Value found at position", position
return position
end if
temp = temp.next
position = position + 1
end while
print "Value not found in the list"
return -1
end function
(D) POLYNOMIAL ADDITION USING LINKED LIST
AIM:
To implement the addition of polynomials using linked lists in C. where each node contains a term with the coefficient
and exponent.
Pseudocode
1. Define Node Structure
structure Node {
int coefficient
int exponent
Node* next
}
2. Function to Create a New Node
function createNode(coefficient, exponent):
newNode = allocate memory for Node
if newNode is NULL:
print "Memory allocation failed"
return NULL
end if
newNode.coefficient = coefficient
newNode.exponent = exponent
newNode.next = NULL
return newNode
end function
3. Function to Insert a Term in the Polynomial
function insertTerm(head, coefficient, exponent):
newNode = createNode(coefficient, exponent)
if newNode is NULL:
return head
end if
if head is NULL or exponent > head.exponent:
newNode.next = head
head = newNode
return head
end if
temp = head
while temp.next is not NULL and temp.next.exponent > exponent:
temp = temp.next
end while
if temp.next is not NULL and temp.next.exponent == exponent:
temp.next.coefficient += coefficient
else:
newNode.next = temp.next
temp.next = newNode
end if
return head
end function
4. Function to Add Two Polynomials
function addPolynomials(poly1, poly2):
result = NULL
while poly1 is not NULL and poly2 is not NULL:
if poly1.exponent > poly2.exponent:
result = insertTerm(result, poly1.coefficient, poly1.exponent)
poly1 = poly1.next
else if poly1.exponent < poly2.exponent:
result = insertTerm(result, poly2.coefficient, poly2.exponent)
poly2 = poly2.next
else:
sum = poly1.coefficient + poly2.coefficient
if sum != 0:
result = insertTerm(result, sum, poly1.exponent)
end if
poly1 = poly1.next
poly2 = poly2.next
end if
end while
// If there are remaining terms in poly1
while poly1 is not NULL:
result = insertTerm(result, poly1.coefficient, poly1.exponent)
poly1 = poly1.next
end while
// If there are remaining terms in poly2
while poly2 is not NULL:
result = insertTerm(result, poly2.coefficient, poly2.exponent)
poly2 = poly2.next
end while
return result
end function
5. Function to Display the Polynomial
function displayPolynomial(head):
if head is NULL:
print "Polynomial is empty"
return
end if
temp = head
while temp is not NULL:
print temp.coefficient, "x^", temp.exponent
if temp.next is not NULL:
print " + "
end if
temp = temp.next
end while
print ""
end function
6. Function to Evaluate Polynomial at x
function evaluatePolynomial(head, x):
result = 0
temp = head
while temp is not NULL:
result += temp.coefficient * (x ^ temp.exponent)
temp = temp.next
end while
return result
end function