0% found this document useful (0 votes)
15 views

Explain different types of queues in data structures

The document outlines various data structures, focusing on stacks and queues, including their implementations using arrays. It provides algorithms for stack operations (push, pop, peek, isEmpty, isFull) and queue operations (enqueue, dequeue, check if empty/full). Additionally, it discusses double-ended queues and recursion, along with their advantages and disadvantages.

Uploaded by

mehtaanand311
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Explain different types of queues in data structures

The document outlines various data structures, focusing on stacks and queues, including their implementations using arrays. It provides algorithms for stack operations (push, pop, peek, isEmpty, isFull) and queue operations (enqueue, dequeue, check if empty/full). Additionally, it discusses double-ended queues and recursion, along with their advantages and disadvantages.

Uploaded by

mehtaanand311
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Explain different types of queues in data structures.

2 write a algorithm for implemention of stck using array

Stack Implementation Using Array

1. Initialize the Stack:

 Create an array to hold the stack elements.

 Initialize a variable (let's call it top) to keep track of the index of the top element of the
stack. Set top to -1 initially, indicating that the stack is empty.

2. Define Operations:

Push Operation (Add Element):

 Input: An element to be added to the stack.

 Steps:
1. Check if the stack is full (i.e., if top is equal to the maximum size of the array minus
1). If it is, handle overflow (e.g., throw an error or resize the array if dynamic
resizing is supported).

2. Increment the top index by 1.

3. Place the new element at the top index of the array.

Pop Operation (Remove Element):

 Output: The element removed from the stack.

 Steps:

1. Check if the stack is empty (i.e., if top is -1). If it is, handle underflow (e.g., throw
an error or return an indication that the stack is empty).

2. Retrieve the element at the top index of the array.

3. Decrement the top index by 1.

4. Return the retrieved element.

Peek Operation (View Top Element):

 Output: The element at the top of the stack without removing it.

 Steps:

1. Check if the stack is empty (i.e., if top is -1). If it is, handle the condition (e.g.,
throw an error or return a special value indicating that the stack is empty).

2. Return the element at the top index of the array.

IsEmpty Operation (Check if Stack is Empty):

 Output: A boolean indicating whether the stack is empty.

 Steps:

1. Check if top is -1.

2. Return true if top is -1; otherwise, return false.

IsFull Operation (Check if Stack is Full):

 Output: A boolean indicating whether the stack is full.

 Steps:

1. Check if top is equal to the maximum size of the array minus 1.

2. Return true if top equals the maximum size minus 1; otherwise, return false.

3 define double ended queue . find the variants of double ended queue
4 same as question no 3rd

5 What is Recursion? State its advantages and disadvantages.


6 Write an algorithm for implementing queues using array.

Implementing a queue using an array involves creating a data structure that supports the basic
operations of a queue: enqueue (adding an element), dequeue (removing an element), and
checking if the queue is empty. Here’s a step-by-step algorithm for implementing a queue using an
array:

 Create an array of size Size to hold the queue elements.

 Initialize:

 Front to 0 (index of the front of the queue).

 Rear to -1 (index of the rear of the queue).

 CurrentSize to 0 (number of elements in the queue).

Enqueue operation :- : Insert an element in the queue

1.Increment the rear by 1

2. Put the new element at index specified by rear.

Updated Algorithm

1. If front equal to -1 then increment front by 1

2.Increment the rear by 1

3. Put the new element at index specified by rear.

Dequeue operation :-

• If front = -1 and rear = -1 OR front = rear +1 then queue is empty.

• If front == rear , then it indicates that there is one element in the queue expect the initial
empty queue indicated by front = -1 and rear =-1.

• If rear = MAX -1 , then it indicates that the queue is full.

Check if Queue is Empty (if(front == -1 || front == rear +1)

1. Check Condition:

o Return True if CurrentSize == 0, otherwise False.

Check if Queue is Full if(rear == MAX -1)

1. Check Condition:
o Return True if CurrentSize == Size, otherwise False.

7 What are the applications of stack?

• Reversing a list

• Undo/Redo

• Cascaded function calls

• Parentheses checker

• Conversion of an infix expression into a postfix expression

• Evaluation of a postfix expression

• Conversion of an infix expression into a prefix expression

• Evaluation of a prefix expression

• Tower of Hanoi

8 Write an algorithm to implement a Priority queue?

\
8 Explain the advantage of circular queue over linear queue. Write a function in C language to insert
an element in circular queue

You might also like