CSC313-Data Structures Essay MidTerm:
1. Algorithms:
1- Array:
1.. Problem: Find maximum of a, b, c
Algorithm: input = a, b, c , Output = max
Process:
Let max = a
If b > max then
max = b
If c > max then
max = c
Display max
2.. Even/ Odd Numbers:
Input range
for num←0; num<=range; num←num+1 do
if num % 2 = 0 then
print num is even
else
print num is odd
endif
endfor
Write an algorithm to find the largest of a set of numbers. You do not know the number of
numbers:
largest = -∞
while true:
input_number = getInput()
if input_number < 0:
break
if input_number > largest:
largest = input_number
print "The largest number is: ", largest
Write an algorithm only in pseudocode that finds the average of (n) numbers.
For example numbers are [4,5,14,20,3,6]s:
FUNCTION CalculateAverage(numbers: ARRAY OF INTEGER) RETURNS FLOAT
SET sum TO 0
SET count TO LENGTH(numbers)
IF count EQUALS 0 THEN
RETURN 0 // To handle case where no numbers are provided
FOR each number IN numbers DO
sum = sum + number
SET average TO sum / count
RETURN average
END FUNCTION
Example usage
SET numbers TO [4, 5, 14, 20, 3, 6]
SET result TO CalculateAverage(numbers)
PRINT result
2- Queue:
ENQUEUE and DEQUEUE operations:
ENQUEUE(Q,x)
1. Q[[Link]] = x
2. if Q. tail == [Link]
3. [Link] = 1
4. else [Link] = [Link] + 1
DEQUEUE(Q)
1. x = Q[[Link]]
2. if [Link] == Q. size
3. [Link] = 1
4. else [Link] = [Link] +1
5. return x
3- Stack:
STACK-EMPTY(S)
1. if [Link] == 0
2. return TRUE
3. else return FALSE
PUSH(S, x)
1. if [Link] == S. size
2. error "overflow"
3. else [Link] = [Link] +1
4. S[[Link]] = x
POP(S)
1. if STACK-EMPTY(S)
2. error "underflow"
3. else [Link] [Link]-1
4. return S[S. top + 1]
4- Linked list:
[Link]-START-INS (L, x)
[Link] = [Link]
[Link] = x
2. LIST-ENDINS (L, x)
y = [Link]
if [Link] != NIL
y=[Link]
[Link]= x
[Link] = NIL
3. LIST-END-INS (x, y)
1 [Link] = [Link]
2 [Link] = x
2. Queue:
A queue represents a waiting list and dynamic sets
The queue uses FIFO(First In First Out) approach
Has front and rear (or head and tail in other books)
all insertions are made at one end called “rear” or “tail”
all deletions are made at the other end called “front” or “head”
3. Stack:
Is a container which provides exactly one method, push, for putting objects into the
container, and one method, pop, for taking objects out of the container.
Is a container of objects that are inserted and removed according to the last-in first-out
(LIFO) principle.
Objects which are stored in a stack are kept in a pile.
The last item put into the stack is on the top.
When an item is pushed into a stack, it is placed at the top of the pile.
When an item is popped, it is always the top item which is removed.
The action of adding a data element to a stack is called pushing.
The action of removing a data element from a stack is called popping.