(a)
Describe thenumber of disk accesses and CPU time for n stack operations in worse-case by using the simple implementation.
(a)
Explanation of Solution
In worse case for every stack operation it requires disk access for implementation sothe number of disk accesses for n stack operations using this simple implementation is asymptotically equals to
The CPU time in worse case for n -stack operations is
(b)
Describethe number of diskaccesses and the CPU time required for n -PUSH operations in worse-case.
(b)
Explanation of Solution
The new page starts only when everymthpushes therefore the number of disk operations is n/m. The diskaccess requires time of asymptotically
Hence,the number of disk accesses and CPU time for n - PUSH operations in worse-case is
(c)
Describe the disk accesses and the CPU time required for n -stack operations in worse-case.
(c)
Explanation of Solution
Stack operations include PUSH and POP. The PUSH and POP operations requires alternate disk accesses that means mdisk accesses for performing stack operation so the number of disk accesses asymptotically is
The CPU time for performing stack operations is equal to
(d)
Describe that the amortized number of disk accesses is O (1 /m ) and the amortized CPU time is O (1) for any stack operation .
(d)
Explanation of Solution
At the initial function called as Potential Function of stack defined by difference of size of stack and recent pass which is multiple of m. The value of potential function is 0 at initial state.
When any stack operation is performed the value is decreased and increased according to the operation by one.Loading new pages requires the position which is at leastm position away from recent position to cross the page boundary therefore loading and storing of new stack page requires CPU time of O ( m ).
By dropping appropriate Potential function of order O ( m ) and selecting the suitable value for potential value results to managed the stack pages so that the amortized number of disk accesses for any stack operation is O (1 /m ) and the amortized CPU time for any stack operation is O (1).
Want to see more full solutions like this?
- Code in C One of the applications of a stack is to backtrack - that is, to retrace its steps. As an example, imagine we want to read a list of items, and each time we read a negative number we must backtrack and print the five numbers that come before the negative number and then discard the negative number. Use a stack to solve this problem. Use a stack and push them into the stack (without printing them) until a negative number is read. At this time, stop reading and pop five items from the stack and print them. If there are fewer than five items in the stack, print an error message and stop the program. After printing the five items, resume reading data and placing them in the stack. When the end of the file is detected, print the message and the items remaining in the stack. Sample Output 1 2 3 4 5 6 7 -1 7 6 5 4 3 9 6 4 3 -2 3 4 6 9 2 -3 Number of items left is less than five! Program is terminating! Project name : Backtrack Filenames: backtrack.h, backtrack.c,…arrow_forwardImagine a (literal) stack of plates. If the stack gets too high, it might topple.Therefore, in real life, we would likely start a new stack when the previous stack exceeds somethreshold. Implement a data structure SetOfStacks that mimics this. SetOfStacks should becomposed of several stacks and should create a new stack once the previous one exceeds capacity.SetOfStacks. push() and SetOfStacks. pop() should behave identically to a single stack(that is, pop () should return the same values as it would if there were just a single stack).FOLLOW UPImplement a function popAt(int index) which performs a pop operation on a specific substack.arrow_forwardWrite a function that uses a stack to test whether a given string (as a character array) is a palindrome. Exercise 14 asked you to write an algorithm for such a function. Although this solution can be solved with simple looping (and it is with the isPlaindromIterative function) - we want to demonstrate an understanding of stack structures to check for a palindrome. Input accept a string (as a character array) add characters to the stack Processing push pop peek (top) Output use stack methods to check for palindromes Efficient Correctarrow_forward
- Passing Arguments to a FunctionThe sample program in Example 5.4 shows how the stack frame is used to passarguments to a function.The code simply creates some local stack variables, fillsthem with values, and passes them to a function called callex().The callex() function takes the supplied arguments and prints them to the screen.Example 5.4 Stack and Passing Parameters to a Function/* stack2.c */#include <stdlib.h>#include <stdio.h>int callex(char *buffer, int int1, int int2){/* This prints the input variables to the screen:*/printf("%s %d %d\n",buffer,int1, int2);return 1;}int main(int argc, char **argv){Stack Overflows • Chapter 5 173 char buffer[15]="Hello Buffer"; /* a 15-byte character bufferwith12 characters filled/*int int1=1, int2=2; /* two four-byte integers */callex(buffer,int1,int2); /*call our function*/return 1; /*leaves the main function*/}You need to compile this example in MSVC in a console application inRelease mode or in GCC without optimizations. Example…arrow_forwardWrite C++ code to implement Stack using object orientation. Write... Write C++ code to implement Stack using object orientation. Write C++ code to convert the Stack implemented in [1] to a Dynamic Stack with necessary modifications and extensions.arrow_forwardTranscribed Image Text НOMEWORK do it by C# languages. o If we think of compilers, they check whether the parentheses in the expression we wrote are the same. o For example: 2 * (i + 5 * (7-j / (4 * k)) has a lack of parentheses. ")" o Write the program that checks if the number of parentheses in the expression is equal, using the stack. interface design Rough code of the assignment o Check brackets using stack: 1) Create an empty stack and start reading the symbols 2) If the symbol is the start symbol ( 'CCOPut in the stack 3) If the symbol is the closing symbol((V' T. Y). 1. Return error report if stack is empty I. If not ENTER THE MATHEMATICAL EXPRESSION YOU WANT TEXTBOX CHECK THE EXPRESSION WITH BUTTON THE STRUCTURE OF THE PILE Receive from stack Send error if received symbol is not the same as start symbol 4) If the statement is over and the stack is full, return an error.arrow_forward
- A data structure known as a drop-out stack functions exactly like a stack, with the exception that if the stack size is n, the first element is lost when the n + 1 element is pushed. Use an array to implement a drop-out stack. (Hint: It would make sense to implement a circular array.)arrow_forwardProject Overview: This project is for testing the use and understanding of stacks. In this assignment, you will be writing a program that reads in a stream of text and tests for mismatched delimiters. First, you will create a stack class that stores, in each node, a character (char), a line number (int) and a character count (int). This can either be based on a dynamic stack or a static stack from the book, modified according to these requirements. I suggest using a stack of structs that have the char, line number and character count as members, but you can do this separately if you wish.Second, your program should start reading in lines of text from the user. This reading in of lines of text (using getline) should only end when it receives the line “DONE”.While the text is being read, you will use this stack to test for matching “blocks”. That is, the text coming in will have the delimiters to bracket information into blocks, the braces {}, parentheses (), and brackets [ ]. A string…arrow_forward(2) Answer the following questions concerning lists, stacks, and queues: Please be thorough with explanation! (e) Suppose you are implementing a list of stacks. Let's say you decide to implement both the general list (the list of stacks) and each individual stack using a vector. Assume that there are currently N stacks in the list, and each stack has a maximum of K items, where N and K are both large. You are going to iterate through the general list and apply a single operation to each stack as you encounter it. Now answer the following four questions, using big-Theta: What is the average-case time complexity of pushing one additional item into each stack? What is the worst-case time complexity of pushing one additional item into each stack? What is the average-case time complexity of popping one item from each stack? What is the worst-case time complexity of popping one additional item from each stack? (For pops, if the stack is empty, you can assume that the pop itself is a…arrow_forward
- Given the following sequence of characters: DA&T!A ST*R&U!CT!&UR*ES. Consider the stack data structure, supporting two operations push and pop. Suppose that for the above sequence, each letter (such as DATA STRUCTURES) corresponds to a push of that letter onto the stack, each exclamation mark(!) corresponds one pop operation on the stack, each ampersand (&) corresponds one peep operation on the stack and each asterisk (*) corresponds two pop operations on the stack. Show the sequence of values returned by the pop operations. Also, display the remaining elements in the stack after all the push and pop operations.arrow_forward(2) Answer the following questions concerning lists, stacks, and queues Please be thorough with explanatio! (d) Suppose you are implementing a list of stacks. Let's say you decide to implement both the general list (the list of stacks) and each individual stack using a double linked list. Assume that there are currently N stacks in the list, and each stack has a maximum of K items, where N and K are both large. You are going to iterate through the general list and apply a single operation to each stack as you encounter it. Now answer the following four questions, using big-Theta notation: What is the average-case time complexity of pushing one additional item into each stack? What is the worst-case time complexity of pushing one additional item into each stack? What is the average-case time complexity of popping one item from each stack? What is the worst-case time complexity of popping one additional item from each stack? (For pops, if the stack is empty, you can assume that the pop…arrow_forwardWrite a program to implement a Stack using array. In order to implement the stack, programfor following stack operations has to be written: • void push(int): to insert data onto the stack.• Int pop(): to remove and return the last inserted element from the stack.• Int top(): Returns the last inserted element without removing it.• int Size(): Returns the number of elements stored in the stack.• int IsEmptyStack(): Indicates whether any elements are stored in the stack or not.• int IsFullStack(): Indicates whether the stack is full or not.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning