Home Misc
Category:

Misc

View Miscellaneous problems

  • Misc

    How will you reverse a queue?

    by nikoo28
    1 minutes read

    Question: Give an algorithm to reverse a queue. You can only use generic functions of the Queue ADT. Input: 4, 8, 15, 16, 23, 42 Output: 42,23,16,15,8,4 To solve this question will take the help of an auxiliary stack. The steps involved will be:- Create an auxiliary stack S. Until the queue Q is not empty, push the elements of the queue, in the stack S. Now we have a stack in which the last element of the Queue is at the TOP. Until the stack is empty POP(S) and…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    Implementing a Queue

    by nikoo28
    2 minutes read

    Before implementing a queue, we must know the basic operations that should be available for a queue. Main Queue Operations:- enQueue(int data) :- Inserts an element at the end of the queue. int deQueue():- Removes and returns the element at the front of the queue. Auxiliary Queue Operations:- int front():- Returns the first element at the queue, without removing it. int queueSize():- Returns the size of the queue. int isEmptyQueue():- Indicates whether no elements are present. We can implement a queue using an ADT (Abstract Data Type). To implement a…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Suppose we want to find the minimum element available in a stack at any point of time. The most trivial and common method would be to traverse the entire stack and keep a record of the least element encountered. At the end, we can return this element. This method no doubt is easy, but it consists of traversing the entire stack again and again, which can lead to time complexities. Thus, we need to optimize it, and it can be done with a very neat little trick:- ALGORITHM:- Take an…

    0 FacebookTwitterLinkedinWhatsappEmail
  • In infix expressions, the operator precedence is implicit unless we use parentheses. Therefore, for the infix to postfix conversion algorithm, we have to define the operator precedence inside the algorithm. We did this in the post covering infix, postfix and prefix expressions. What are infix, prefix and postfix expressions? Important properties:- Let us consider the infix expression 2 + 3 * 4 and its postfix will be 2 3 4 * +. Notice that between infix and postfix the order of the numbers(or operands) is unchanged. It is 2 3…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Stacks can be used to check if the given expression has balanced symbols or not. The algorithm is very much useful in compilers. Each time parser reads one character at a time. If the character is an opening delimiter like ‘(‘ , ‘{‘ or ‘[‘ then it is PUSHED in to the stack. When a closing delimiter is encountered like ‘)’ , ‘}’ or ‘]’ is encountered, the stack is popped. The opening and closing delimiter are then compared. If they match, the parsing of the string continues. If they…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Linked ListMisc

    Implementation of a stack

    by nikoo28
    2 minutes read

    An implementation of stack is similar to that of a Linked List. We will try to discuss the basic operations in a stack discussed in this post. MAIN OPERATIONS ON A STACK:- push( int ) :- Inserts a data into the stack pop :- Returns the topmost data from the stack AUXILIARY OPERATIONS ON A STACK int Top( ) :- returns the topmost element on the stack int Size( ) :- returns the total size of the stack int isStackEmpty( ) :- returns true if the stack is empty The…

    0 FacebookTwitterLinkedinWhatsappEmail
  • MiscTheory

    What is Segmentation Fault?

    by nikoo28
    2 minutes read

    From Wikipedia: A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system). Segmentation is one approach to memory management and protection in the operating system. It has been superseded by paging for most purposes, but much of the terminology of segmentation is still used, “segmentation fault” being an example.…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Many of us must have heard of the clause “Real Programmers code in C” and it is faster than any other language because “it is close to the machine“. What is so special about this language that makes it different from any other language. Let us try to explore this answer to some extent. There isn’t much that’s special about C. That’s one of the reasons why it’s fast. Newer languages which have support for garbage collection, dynamic typing and other facilities which make it easier for the programmer to…

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More