C Programming and Data Structures: Unit III: b. Linear Data Structures Stacks and Queues

Two Marks Questions with Answers

Linear Data Structures Queues | C Programming and Data Structures

C Programming and Data Structures: Unit III: b. Linear Data Structures Stacks and Queues : Two Marks Questions with Answers

Two Marks Questions with Answers


Q. 1Give an example that shows how a stack is used by a computer system.

Ans. : In computer system the memory model consists of stack memory stores types of variables that have a fixed lifetime based on how C programs run. It is a section of RAM that grows and shrinks as the program runs. When you call a function, its parameters and any variables you have defined in that function (which are not static) are stored on the stack.

 

Q. 2 What do you understand by polish notation? Explain.

Ans. : This is also called as prefix notation. In this type of notation the operator followed by two operands. For example if (a+b)*c is a given expression then its polish notation will be *+ abc.

 

Q. 3 What is a top pointer of a stack?

Ans. : The top denotes the only one end of the stack from which the element can be inserted or deleted. When we push the element onto the stack, the top is incremented. When we pop the element from the stack the top is decremented.

 

Q. 4 Write the postfix notation for following expression. (A+B)*C-(D-E)^F

Ans.: Following steps can be followed for computing the postfix expression -

Step 1: (AB+)*C-(D-E)^F

Step 2: (AB+C*)-(D-E)^F

Step 3: (AB+C*)-(DE-)^F

Step 4: (AB+C*)-(DE-F^)

Step 5: AB+C*DE-F^-

 

Q. 5 Write any two applications of stack. AU: Dec.-14, 18, 19

Ans. : The stack can be used for

1. Conversion of expression from one form to another. Various forms of expression are infix, prefix and postfix.

2. Evaluation of postfix expression.

3. Evaluation of recursive functions.

4. Reversing the string.

5. For checking the well formedness of parenthesis.

 

Q. 6 List the characteristics of stacks.

Ans. :

1. Insertion and deletion can be made by one end only. Hence the element inserted last will be first one to come out. Hence sometimes it is called LIFO.

2. Stacks are useful for evaluating an expression.

3. Stacks can store the functions calls.

 

Q. 7 Write the role of stack in function call.

Ans. : The stack is an useful data structure for handling the recursive function calls. When a recursive call is encountered, the status of call is pushed onto the stack. And at the return of the call the stack is popped off. Thus execution of recursive statements is done with the help of stack.

 

Q. 8 What is Last-In-First-Out strategy? Which data structure follows this strategy?

Ans. : In the Last In First Out strategy we insert the element in the data structure lastly and while removing the elements from this data structure, the element which is inserted lastly will get removed first.

The stack data structure makes use of Last In First Out(LIFO) data structure.

 

Q. 9 Write the steps to reverse the contents of the list with the help of stack data structure.

Ans. :

Step 1: Read each element from the list and push it onto the stack.

Step 2: Pop the element from the stack and store the popped element in a separate

Step 3: Read the array completely from left to write. This will be the reversed list of the elements.

 

Q. 10 Which data structure is used in handling the recursive function?

Ans. : The stack is used to handle the recursive function call.

 

Q. 11 Given the prefix for an expression write its postfix. AU: May-15

 -*-+abc/ef-g/hi

Ans. :

-*-+abc/ef-g/hi

- * - T1 cef-g/hi T1 = ab+

-T2/ef-g/hi T2 = T1c-

- T2 T3 g/hi T3=ef /

- T4g-/hi T4 = T2T3*

- T4 g T5  T5 = hi/

-T4 T6 T6=g T5-

T7 T7= T4 T6

By backward substitution,

T7

T4 T6-

T2 T3 T6 -

T1 c T3 * T6 -

ab + c - T3 * T6 -

ab + c –ef/*t6-

ab + c -ef/* g T5--

ab + c - ef /* ghi/ -- is postfix expression

 

Q. 12 Give the infix for an expression, write its prefix a* b/c+d?

Ans. : The prefix expression is /* ab + cd.

 

Q. 13 Convert the following infix expression to postfix expression using stack.

AU: May - 19

Ans. :


The required postfix expression is abc*def++g/+.

 

Q. 14 State the rules to be followed during infix to postfix conversion. AU: Dec.-19

Ans. : Refer section 4.6.

 

Q. 15 What do the terms LIFO and FIFO means? Explain.

Ans. : The LIFO stands for Last In First Out. This property belongs to stack. That means the element inserted lastly will be the element to be popped off first from the stack.

The FIFO stands for First In First Out. This property belongs to queue. That means the element inserted first in the queue will be the first to get deleted.

 

Q. 16 What are the front and rear pointers of queue.

Ans. : The front end of the queue from which the element gets deleted is called front and the end from which the element is inserted in the queue is called rear. is called rear.roA For example: Refer Fig. 4..10.1.

 

Q. 17 Write any four applications of queues. AU: May-14

Ans. :

1. In operating system for scheduling jobs, priority queues are used.

2. In Local Area Network (LAN) multiple computers share few printers. Then these printers accumulate jobs in a queue. Thus printers can process multiple print commands with the help of queues.

3. For categorizing data, queues are used.

4. In simulation and modeling queues are used.

5. In computer networks, while broadcasting message, the message packets are accumulated in queue. And then these packets are forwarded.

 

Q. 18 What is a dequeue? AU: May-14,16 Dec.-14

Ans. : The dequeue is a data structure in which the element can be inserted from both the ends i.e. front and rear. Similarly, the element can be deleted from both the front and rear end.

 

Q. 19 How do you test for an empty queue?

Ans. : Following is a C code which is used to test an empty queue -

if((Q.front==-1) | | (Q.front>Q.rear))

printf("\n The Queue is Empty!!");

else

printf("\n The Queue is not Empty!!");

 

Q. 20 What is the use of queue in operating system ?

Ans. :

The queue is used for scheduling the jobs in operating system.

 

Q. 21 What is stack and queue ?

AU: Dec.-15

Ans. :

Stack is linear data structure in which insertion and deletion of element is from one end called top.

Queue is a linear data structure in which insertion of element is from one end called rear and deletion of element is from other end called front.

 

Q. 22 What are priority queues? What are the ways to implement priority queue?

AU: Dec.-18

Ans. :

• Definition: The priority queue is a data structure having a collection of elements which are associated with specific ordering.

• Ways to implement priority queue:

There are two ways to implement priority queue.

1. Ascending priority queue - It is a collection of items in which the items can be inserted arbitarily but only smallest element can be removed.

2. Descending priority queue - It is a collection of items in which insertion of items can be in any order but only largest element can be removed.

 

C Programming and Data Structures: Unit III: b. Linear Data Structures Stacks and Queues : Tag: : Linear Data Structures Queues | C Programming and Data Structures - Two Marks Questions with Answers