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

Solution: Struct Nodetype (Itemtype Info Nodetype Next )

A queue is a first-in, first-out (FIFO) linear structure used to organize items where the item added first is processed first. A queue can be implemented using two stacks, with one stack used for enqueue operations and the other for dequeue operations, making one of the operations more efficient than the other. Stacks can be used to check if a string of parentheses is balanced by pushing opening parentheses and popping matching closing parentheses onto a stack.

Uploaded by

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

Solution: Struct Nodetype (Itemtype Info Nodetype Next )

A queue is a first-in, first-out (FIFO) linear structure used to organize items where the item added first is processed first. A queue can be implemented using two stacks, with one stack used for enqueue operations and the other for dequeue operations, making one of the operations more efficient than the other. Stacks can be used to check if a string of parentheses is balanced by pushing opening parentheses and popping matching closing parentheses onto a stack.

Uploaded by

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

1. What is a Queue?

A Queue is a linear structure that follows a particular order in which the operations are performed.
The order is First in First Out (FIFO). An excellent example of a queue is any consumer queue for
a resource where the consumer that came first is served first. The difference between stacks and
queues is in removing. We remove the item the most recently added; in a queue, we remove the
item the least recently added.
2. write the c++ code for creating a node structure of the linked list.

Solution:
struct NodeType
{
ItemType info;
NodeType* next;
};

3. Solution:
bool StackType<ItemType>::IsEmpty()
{
return (top == -1);
}
4. what the difference in the implementation of array-based sorted and unsorted
lists?
Answer: In short, searching in an unsorted array takes O(n) time: you potentially have to
look at every item to determine if what you're looking for is there. A sorted array lets you
speed up the search. Instead of examining every item, you only have to determine at most
log2(n) items. That makes a huge difference when numbers get large. For example, if your
list contains a million items, binary search only has to examine 20 of them. Sequential
search has to look at all million.
5. How can a queue be implemented using a stack?

Solution: A queue can be implemented using two stacks. Let


queue to be implemented be q, and stacks used to implement q be
stack1 and stack2. q can be implemented in two ways:
Method 1 (By making the enQueue operation costly) This method makes sure that the oldest
entered element is always at the top of stack one so that the deQueue operation pops from stack1.
To put the element at the top of stack1, stack2 is used.
enQueue(q, x):
a) While stack1 is not empty, push everything from stack1 to stack2.
b) Push x to stack1 (assuming the size of stacks is unlimited).
c) Push everything back to stack1.
Here time complexity will be O(n)
deQueue(q):
a) If stack1 is empty, then an error
b) Pop an item from stack1 and return it
Here time complexity will be O(1)
Method 2 (By making the deQueue operation costly)In this method, in the en-queue
operation, the new element is entered at the top of stack1. In de-queue operation, if
stack2 is empty, all the elements are moved to stack2, and finally, the top of stack2 is
returned.
enQueue(q, x)
1) Push x to stack1 (assuming the size of stacks is unlimited).
Here time complexity will be O(1)

deQueue(q)
1) If both stacks are empty, then error.
2) If stack2 is empty
While stack1 is not empty, push everything from stack1 to stack2.
3) Pop the element from stack2 and return it.
Here time complexity will be O(n)

6. answer: Queue.
7. answer: stacks can be used to solve the problem. A stack is the appropriate data structure
for keeping the parentheses. The statement of the algorithm is straightforward. Starting with an
empty stack, process the parenthesis strings from left to right. If a symbol is an opening parenthesis,
push it on the stack to signal that a corresponding closing symbol needs to appear later. If, on the
other hand, a symbol is a closing parenthesis, pop the stack. As long as it is possible to pop the
stack to match every closing symbol, the parentheses remain balanced. If there is no opening
symbol on the stack to match a closing symbol, the string is not balanced correctly. When all
symbols have been processed at the end of the string, the stack should be empty.

You might also like