Solution: Struct Nodetype (Itemtype Info Nodetype Next )
Solution: Struct Nodetype (Itemtype Info Nodetype Next )
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?
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.