Please answer all parts of the question Suppose that a stack which contains a se
ID: 3827279 • Letter: P
Question
Please answer all parts of the question
Suppose that a stack which contains a set of integers is implemented using an array. Write an algorithm that returns the number of positive integers in the stack. Ensure that the stack holds the original set of data upon completion of the algorithm. You may use additional stacks in your algorithm. An unique integer is stored in each node of a doubly-linked list which has a reference, start, that points to the first node of the list. Write an algorithm to delete the node with the largest integer from the list. Design a recursive algorithm which finds the node with the largest value in a singly-linked list.Explanation / Answer
a) Since the stack is reperesented using an array, we can loop through the array to find out the number of positive numbers.
Let items be the array containing the stack items
Let size be the number of items in the stack array
Let counter = 0 be the variable for counting the number of positive integers
for int i in rage of 0 to size:
if items[i] >0:
counter = counter + 1;
return counter;
b) Finding the largest number in the list
Let start be the front of the double linked list
Let largest be the integer holding the largets number
largest = start.data;
WHILE start is not null:
if largest < start.data
largest = start.data;
start = start.next;
Removing the largets number
if(start.next == null)
start = null;
else:
while(start.next.next != null):
if start.data== largest:
break;
else start = start.next ;
start.data = start.next.data;
start.next.prev = start;
start.next = start.next.next;
c) int largestNumber(list, largest)
{
if(list == null)
return largest
if(list.data > largest)
largest = list.data;
largest = largestNumber(list.next, largest);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.