0% found this document useful (0 votes)
7 views23 pages

2 STACKwithkey

The document provides an overview of stack data structures, including their definition, implementation methods (static and dynamic), and basic operations (push and pop). It discusses applications of stacks, such as managing function calls and evaluating arithmetic expressions, and includes examples of recursion and its types. Additionally, it covers various algorithms and problems related to stacks, including postfix and prefix notation, and provides pseudo code for recursive functions.

Uploaded by

goyalkanak91
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views23 pages

2 STACKwithkey

The document provides an overview of stack data structures, including their definition, implementation methods (static and dynamic), and basic operations (push and pop). It discusses applications of stacks, such as managing function calls and evaluating arithmetic expressions, and includes examples of recursion and its types. Additionally, it covers various algorithms and problems related to stacks, including postfix and prefix notation, and provides pseudo code for recursive functions.

Uploaded by

goyalkanak91
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

STACK

• A stack is a non-primitive linear data structure. it is an ordered list in which addition of a


new data item and deletion of already existing data item is done from only one end known
as top of stack (TOS).
• The element which is added in last will be first to be removed and the element which is
inserted first will be removed in last.
• That is why it is called last in first out (LIFO) or first in last out (FILO) type of list.
• Most frequently accessible element in the stack is the top most element, whereas the least
accessible element is the bottom of the stack.
Stack Implementation
• Stack is generally implemented in two ways.
• Static Implementation: - Here array is used to create stack. it is a simple technique but is
not a flexible way of creation, as the size of stack has to be declared during program design,
after that size implementation is not efficient with respect to memory utilization.
• Dynamic implementation: - It is also called linked list representation and uses pointer to
implement the stack type of data structure.

Q Which of the following is true about linked list implementation of stack?


(A) In push operation, if new nodes are inserted at the beginning of linked list, then in pop
operation, nodes must be removed from end.
(B) In push operation, if new nodes are inserted at the end, then in pop operation, nodes must
be removed from the beginning.
(C) Both of the above
(D) None of the above
Answer: (D)

• Basics operations on stack


o Push
o Pop

Q Which data structure is used for balancing of symbols?


(A) Stack (B) Queue (C) Tree (D) Graph
Answer: (A)

Q The best data structure to check whether an arithmetic expression has balanced parentheses is
a (GATE - 2004) (1 Marks)
(A) queue (B) stack (C) tree (D) list
Answer: (B)
Push operation: - The process of adding new element to the top of stack is called push
operation. the new element will be inserted at the top after every push operation the top is
incremented by one. in the case the array is full and no new element can be accommodated it
is called over-flow condition.

PUSH (S, N, TOP, x)


{
if (TOP=N)
Print stack overflow and exit

TOP = TOP + 1

S[TOP] = x

exit
}
Pop: - The process of deleting an element. from the top of stack is called POP operation,
after every POP operation the stack is decremented by one if there is no element in the stack
and the POP operation is requested then this will result into a stack underflow condition.

POP (S, N, TOP)


{
if (TOP==-1)
print underflow and exit
y = S[TOP]
TOP=TOP-1
return(y) and exit
}

Q A single array A[1...MAXSIZE] is used to implement two stacks. The two stacks grow from
opposite ends of the array. Variables top1 and top2 (top 1< top 2) point to the location of the
topmost element in each of the stacks. If the space is to be used efficiently, the condition for
“stack full” is (GATE - 2004) (2 Marks)
(A) (top1 = MAXSIZE/2) and (top2 = MAXSIZE/2+1)
(B) top1 + top2 = MAXSIZE
(C) (top1= MAXSIZE/2) or (top2 = MAXSIZE)
(D) top1= top2 -1
Answer: (D)

Q Let S be a stack of size n >= 1. Starting with the empty stack, suppose we push the first n
natural numbers in sequence, and then perform n pop operations. Assume that Push and Pop
operation take X seconds each, and Y seconds elapse between the end of one such stack
operation and the start of the next operation. For m >= 1, define the stack-life of m as the time
elapsed from the end of Push(m) to the start of the pop operation that removes m from S. The
average stack-life of an element of this stack is (GATE - 2003) (2 Marks)
(A) n(X+ Y) (B) 3Y + 2X (C) n(X + Y)-X (D) Y + 2X
Answer: (C)
Application of Stack
Q Which one of the following is an application of Stack Data Structure?
(A) Managing function call (B) recursion
(C) Arithmetic expression evaluation (D) All of the above
Answer: (D)

Stack Permutation

Q if the input sequence is 1, 2, 3, 4, 5 then identify the wrong stack permutation (possible pop
sequence)?
a) 3, 5, 4, 2, 1 b) 2, 4, 3, 5, 1 c) 4, 3, 5, 2, 1 d) 5, 4, 3, 1, 2

Q if the input sequence is 5, 4, 3, 2, 1 then identify the wrong stack permutation (possible pop
sequence)?
a) 4, 2, 1, 3, 5 b) 5, 2, 3, 4, 1 c) 4, 5, 1, 2, 3 d) 3, 4, 5, 2, 1
b
Evaluation of arithmetic expression
• An expression is defined as a number of operands or data items combined using several
operators. There are basically three of notation to represent an expression.
• Infix notation: the operator is written in between the operands. e.g. A+B. the reason why
this notation is called infix is the place of operator in the expression.
• Prefix notation: In which the operator is written before the operands it is also called as
polish notation. e.g. +AB
• Postfix: In the postfix notation the operator are written after the operands, so it is called
the postfix notation. It is also known as suffix notation or recursive polish notation. AB+
• Postfix notation is type of notation which is most suitable for a computer to calculate any
expression. It is universally accepted notation for designing arithmetic and logical unit
(ALU) of the CPU.
• Any expression entered into the computer is first converted into postfix notation, stored in
stack and then calculated.

Q Consider an expression a + b * c / d ^ e ^ f * d – c + b, convert it into both prefix and post fix


notation?

Q Assume that the operators +, -, × are left associative and ^ is right associative. The order of
precedence (from highest to lowest) is ^, x, +, -. The postfix expression corresponding to the
infix expression a + b × c – d ^ e ^ f is (GATE - 2004) (2 Marks)
(A) abc × + def ^ ^ – (B) abc × + de ^ f ^ –
(C) ab + c × d – e ^ f ^ (D) – + a × bc ^ ^ def
Answer: (A)
Q Consider an expression log(x!), convert it into both prefix and post fix notation?

Q The result evaluating the postfix expression 8 2 3 * 1 / + 4 1 * 2 / +

Q The result evaluating the prefix expression + + 8 / * 2 3 1 / * 4 1 2

Q The result evaluating the postfix expression 10 5 + 60 6 / * 8 – is (GATE - 2015) (1 Marks)


(A) 284 (B) 213 (C) 142 (D) 71
Ans: c

Q The following postfix expression with single digit operands is evaluated using a stack 8 2 3 ^
/23*+51*-
Note that ^ is the exponentiation operator. The top two elements of the stack after the first *
is evaluated are: (GATE - 2007) (2 Marks)
(A) 6, 1 (B) 5, 7 (C) 3, 2 (D) 1, 5
Answer: (A)

Q To evaluate an expression without any embedded function calls: (GATE - 2002) (1 Marks)
(A) One stack is enough
(B) Two stacks are needed
(C) As many stacks as the height of the expression tree are needed
(D) A Turing machine is needed in the general case
Answer: (A)

Q if -, * and $ are used as subtraction, multiplication and exponential.


i) – is highest precedence, then * and the $
ii) all are L to R associative
3–2*4$1*2$3
2 * 2 – 1 $ 1 $ 4 – 2 (all are R to L)
Recursion
• Recursion is one of the most powerful tools in a programming language all it requires is to
specify a reasonable condition and instruction that from right logic to solve a problem.
• Recursion is defined as defining anything in terms of itself.
• A function is called recursive if a statement with in the body of a function calls the same
function
• Recursion is used to solve problems involving iterations (multiple execution) in reverse
order.
• To make a complete function executed repeatedly in terms of itself. Recursion is an
alternative to iteration in making a function execute repeatedly.
• Types of recursion: Recursion is of two types depending on weather a function call itself
from with in itself than called direct recursion.
• when two function call one another mutually indirect recursion.
• Direct recursion is further of two types tail recursion and head recursion
o tail when the fun is called in the last, all the other are head recursion

Q Find the output of the following pseudo code?


Void main()
{
fun(4);
}

Void fun(int x)
{
if (x > 0)
{
Printf(“%d”, x);
fun(x - 1);
}
}
Q Find the output of the following pseudo code?
Void main()
{
salman(3);
}

Void salman(int x)
{
if (x > 0)
{
Salman(x-1);
Printf(“%d”, x);
Salman(x - 1);
}
}

Q Find the output of the following pseudo code on x = 6?


int x(int n)
{
if (n < 3)
return 1;
Else
return x(n-1) + x(n-1) + 1;
}

Q Find the output of the following pseudo code?


void Print_array(a, i, j)
{
if (i = = j)
{
printf(‘’%d’, a[i]);
return;
}
Else
{
printf(‘’%d’, a[i]);
print_array(a, i+1, j)
return x(n-1) + x(n-1) + 1;
}
}
Q Find the output of the following pseudo code?
void Print_somthing(a, i, j)
{
if (i = = j)
{
printf(‘’%d’, a[i]);
return;
}
Else
{
if(a[i] < a[j])
Print_somthing (a, i+1, j);
Else
Print_somthing(a, i, j-1);
}
}

Q Find what this function is doing?


void what (struct Bnode *t)
{
if (t)
{
what(t → LC);
printf(‘’%d’, t→data);
what(t → LC);
}
}

Q Find what this function is doing?


void what(struct Bnode *t)
{
if (t)
{
printf(‘’%d’, t→data);
what(t → LC);
printf(‘’%d’, t→data);
what(t → LC);
}
}
Q Find what this function is doing?
void what(struct Bnode *t)
{
if (t)
{
printf(‘’%d’, t→data);
what(t → LC);
printf(‘’%d’, t→data);
what(t → LC);
printf(‘’%d’, t→data);
}
}

Q Find what this function is doing?


void A(struct Bnode *t)
{
if (t)
{
B(t → LC);
printf(‘’%d’, t→data);
B(t → LC);
}
}

void B(struct Bnode *t)


{
if (t)
{
printf(‘’%d’, t→data);
A(t → LC);
A(t → LC);
}
}

Excessive recursion: - Does not remember previously estimated value.


fibonacci series
if n==0, then f(n) = 0
if n==1, then f(n) = 1
if n > 1, then f(n-1) + f(n-2)
no of invocation = 2f(n+1) - 1
no of addition = f(n+1) – 1

Tower of hanoi: suppose there are three pegs lablled as A, B and C and suppose on peg A
there are placed a finite number n of disks with decreasing size. the object of the game is to
move the disks from peg A to peg C using peg B as an auxiliary.

Rules:
only one disk may be moved at a time specially only the top disk on any peg may be involved
to any other peg
at no time can a larger disk be placed on smaller disk.
Tower(N, B, A, E)
{
if(n = 1)
{
B→E
return
}
tower(n-1, B, E, A)
B→E
tower(n-1, A, B, E)
Return
}

total disk moves = 2n -1


total number of function call = 2n+1 -1
how many invocation are required for the first disk to move = n

Ackerman function A(m, n)


if(m = 0), the A(m, n) = n+1
if ((m != 0) && (n=0)), the A(m, n) = A(m-1, 1)
if ((m != 0) && (n != 0)), the A(m, n) = A(m-1, A(m, n-1))

Q Following is C like pseudo code of a function that takes a number as an argument, and uses
a stack S to do processing.
void fun (int n)
{
Stack S; // Say it creates an empty stack S
while (n > 0)
{
// This line pushes the value of n%2 to stack S
push (&S, n%2);
n = n/2;
}
// Run while Stack S is not empty
while (!isEmpty(&S))
printf("%d ", pop(&S)); // pop an element from S and print it

}
What does the above function do in general?
(A) Prints binary representation of n in reverse order
(B) Prints binary representation of n
(C) Prints the value of Logn
(D) Prints the value of Logn in reverse order
Answer: (B)

Q What does the following function print for n = 25?


void fun(int n)
{

if (n == 0)
return;
printf("%d", n%2);

fun(n/2);
}
(A) 11001 (B) 10011 (C) 11111 (D) 00000
Answer: (B)

Q Consider the following recursive function fun (x, y). What is the value of fun (4, 3)

int fun(int x, int y)


{
if (x == 0)
return y;
return fun(x - 1, x + y);
}
(A) 13 (B) 12 (C) 9 (D) 10
Answer: (A)

Q The output of following program

#include <stdio.h>
int fun(int n)
{
if (n == 4)
return n;
else return 2*fun(n+1);
}

int main()
{
printf("%d ", fun(2));
return 0;

}
(A) 4 (B) 8 (C) 16 (D) Runtime Error
Answer: (C)

Q What does the following function do?


int fun(int x, int y)
{
if (y == 0)
return 0;
return (x + fun(x, y-1));
}
(A) x + y (B) x + x*y (C) x*y (D) xy
Answer: (C)

Q What does the following function do?


int fun(unsigned int n)
{
if (n == 0 || n == 1)
return n;
if (n%3 != 0)
return 0;
return fun(n/3);
}
(A) It returns 1 when n is a multiple of 3, otherwise returns 0
(B) It returns 1 when n is a power of 3, otherwise returns 0
(C) It returns 0 when n is a multiple of 3, otherwise returns 1
(D) It returns 0 when n is a power of 3, otherwise returns 1
Answer: (B)

Q Output of following program?

#include<stdio.h>
void print(int n)
{
if (n > 4000)
return;
printf("%d ", n);
print(2*n);
printf("%d ", n);
}

int main()
{
print(1000);
getchar();
return 0;
}
(A) 1000 2000 4000 (B) 1000 2000 4000 4000 2000 1000
(C) 1000 2000 4000 2000 1000 (D) 1000 2000 2000 1000
Answer: (B)

Q Predict the output of following program


#include <stdio.h>
int f(int n)
{
if(n <= 1)
return 1;
if(n%2 == 0)
return f(n/2);
return f(n/2) + f(n/2+1);
}

int main()
{
printf("%d", f(11));
return 0;
}
(A) Stack Overflow (B) 3 (C) 4 (D) 5
Answer: (D)

Q Consider the following recursive C function. If get(6) function is being called in main() then
how many times will the get() function be invoked before returning to the main()? (GATE -
2015) (2 Marks)
void get (int n)
{

if (n < 1) return;
get(n-1);
get(n-3);
printf("%d", n);

}
(A) 15 (B) 25 (C) 35 (D) 45
Answer: (B)

Q Consider the following recursive C function that takes two arguments


unsigned int foo(unsigned int n, unsigned int r)
{
if (n > 0)
return (n%r + foo (n/r, r ));
else
return 0;
}
What is the return value of the function foo when it is called as foo(345, 10) ? (GATE - 2011) (2
Marks)
(A) 345 (B) 12 (C) 5 (D) 3
Answer: (B)
Q Consider the same recursive C function that takes two arguments
unsigned int foo(unsigned int n, unsigned int r)
{
if (n > 0)
return (n%r + foo (n/r, r ));
else
return 0;

}
What is the return value of the function foo when it is called as foo(513, 2)? (GATE - 2011) (2
Marks)
(A) 9 (B) 8 (C) 5 (D) 2
Answer: (D)

Q What does fun2() do in general?


int fun(int x, int y)

{
if (y == 0)
return 0;
return (x + fun(x, y-1));
}

int fun2(int a, int b)


{
if (b == 0)
return 1;
return fun(a, fun2(a, b-1));
}
(A) x*y (B) x+x*y (C) xy (D) yx
Answer: (C)
c

Q What is the output of following program?


#include <stdio.h>
void print(int n, int j)
{
if (j >= n)
return;
if (n-j > 0 && n-j >= j)
printf("%d %d\n", j, n-j);
print(n, j+1);
}

int main()
{
int n = 8;
print(n, 1);

(A) 1 7 2 6 3 5 4 4 4 4 (B) 1 7 2 6 3 5 4 4
(C) 1 7 2 6 3 5 (D) 1 2 3 4 5 6 7 8
Answer: (B)
Explanation: For a given number n, the program prints all distinct pairs of positive integers
with sum equal to n.

Q
#include<stdio.h>
void crazy(int n, int a, int b)
{
if (n <= 0)
return;
crazy(n-1, a, b+n);
printf("%d %d %d\n",n,a,b);
crazy(n-1, b, a+n);
}

int main()
{
crazy(3,4,5);
return 0;
}

(A) (B) (C) (D)


1 4 10 345 1 4 10 345
248 1 4 10 248 159
186 248 186 257
345 186 345 177
159 159
257 257
177 177
Answer: (A)

Q Consider the following C function. (GATE - 2015) (2 Marks)

int fun (int n)


{
int x=1, k;
if (n==1) return x;
for (k=1; k<n; ++k)
x = x + fun(k) * fun(n – k);
return x;
}
The return value of fun(5) is __________.
(A) 0 (B) 26 (C) 51 (D) 71
Answer: (C)

Q What is the return value of following function for 484? What does it to in general?
bool fun(int n)
{
int sum = 0;
for (int odd = 1; n > sum; odd = odd+2)
sum = sum + odd;
return (n == sum);

}
(A) False, it checks whether a given number is power of 3
(B) False, it checks whether a given number is even or not
(C) False, it checks whether a given number is odd or not
(D) True, it checks whether a given number is perfect square.
Answer: (D)
Explanation: The given function adds all odd numbers 1, 3, 5, 7, 9, 11…. till the sum is smaller
than n. If the sum becomes equal to n, then it returns true. This is basically a test for perfect
square numbers. All perfect square numbers can be written as sum of odd numbers.
4=1+3
9 = 1 + 3 + 50
16 = 1 + 3 + 5 + 7
36 = 1 + 3 + 5 + 7 + 9
49 = 1 + 3 + 5 + 7 + 9 + 11

Q Following is an incorrect pseudocode for the algorithm which is supposed to determine


whether a sequence of parentheses is balanced:

declare a character stack


while (more input is available)
{
read a character
if (the character is a '(‘)
push it on the stack
else if (the character is a ')' and the stack is not empty)
pop a character off the stack
else
print "unbalanced" and exit
}
print "balanced"
Which of these unbalanced sequences does the above code think is balanced?
(A) ((()) (B) ())(() (C) (()())) (D) (()))()

Q Which of the following is not a backtracking algorithm?


(A) Knight tour problem (B) N queen problem
(C) Tower of Hanoi (D) M coloring problem
Answer: (C)

You might also like