Linked List implementation of Queue Data Structure in Java

A Queue is a data structure for storing data similar to Linked List and Stack. In Queue, the order in which data arrives is important. In general, a queue is a line of people or things waiting to be served in sequential order stating at the beginning of the line.

A queue is an ordered list in which insertions are done at one end (rear) and deletion done at other end (front). The first element to be inserted is the first one to be deleted. Hence, it is called as First in First Out(FIFO) or Last in Last Out (LILO).

This is some what similar to Stack. When an element is inserted in a queue, the concept is called EnQueue and when an element is removed from the queue, the concept is called DeQueue.Trying to DeQueue an empty Queue is called as underflow and trying to EnQueue an full queue is called overflow. Generally, we treat them as exceptions.

Queue Operations
Main Operations

  • enQueue(int data) : Insert an element at the end of the queue
  • deQueue() : Removes and element an element at the front of the queue
Auxiliary Operations
  • int Front() : Returns the element at the front without removing it.
  • int QueueSize() : Returns the queue size.
  • int isEmpty() : Indicates whether element are stored.

Click here to read more

Advertisements

Implementation of Two Stacks with one Array in Java

This is the continuation of previous post on Array implementation of stack in java. In this post, we implements 2 stacks using one array.

While implementing stack using array we took one index which first initially point to -1, In this case of 2 stacks we take two index one point to -1 and other point to the end of the array i.e, size of the array.

Algorithms :

  • Start with two indexes one at the left and other at the right end of the array.
  • Left index simulate the first stack and second index simulate the right stack.
  • If we want to put the element in the first stack then put the element at the left index. Similarly, if we want to put the element in the second stackthen put the element at the right index.
  • First stack grow towards left and second stack grow towards left.

Click here to read more

Linked List implementation of Stack Data Structure in Java

stack is a simple data structure for storing data. In stack, the order in which the data arrives is important. A pile of books is a good example of stack. Astack is an ordered list in which insertion and deletion are done at one end, where end is called top. The last element inserted is the first one to be deleted. Hence, it is called Last in First Out (LIFO) or First in Last Out (FILO) list

In this post, we’ll see the other way of implementing Stack is by Linked List. This is continuation of the previous post about Array implementation of Stack.
In this implementation, push operation is implemented by inserting element at the beginning of the list. Pop operation is implemented by deleting the node from the beginning.
Even you can throw exception instead of printing message in case of pop operation when stack is empty.

Click here to read more

Array implementation of Stack Data structure in Java

A stack is a simple data structure for storing data. In stack, the order in which the data arrives is important. A pile of books is a good example of stack.
A stack is an ordered list in which insertion and deletion are done at one end, where end is called top. The last element inserted is the first one to be deleted. Hence, it is called Last in First Out (LIFO) or First in Last Out (FILO) list

In computer science, a stack is a particular kind of abstract data type or collection in which the principal (or only) operations on the collection are the addition of an entity to the collection, known as push and removal of an entity, known as pop. Often a peek or top operation is also implemented, returning the value ofthe top element without removing it.

Stack Operations
Main operations

  • void push(int data) : Insert data onto stack.
  • int pop() : Removes and returned last inserted element from the stack
Auxiliary operations
  • int top() or peek() : Returns the last inserted elements without removing it.
  • int size() :  Returns the number of elements stored in stack.
  • boolean isStackFull() : Check whether stack is full or not.
  • boolean isEmpty() : Check whether stack is empty or not.

Click here to read more

Linked List implementation in Java

In this post, we’ll see the basic of Linked List, its advantages and implementation in Java. 

Linked List is a data structure used for storing collection of data. It has the following properties:

  • Successive element are connected by pointers, in Java we means references.
  • Last element of Linked List point to NULL
  • Can grow or shrink in size during the execution of a program.
  • Can me long as long as required.
  • It does not waste memory space.

Click here to read more

 

Given an integer array, find all pairs that sum up to a specific value k

This is one of the basic interview question about algorithm about finding the two numbers from array such that there sum is equal to K.
In this post, we’ll look into different approaches for finding all the pair of integer such that there sum is equal to K.

Approach 1 Brute-Force
In this approach, for each input element check whether there is any element exist whose sum is K. This can be solved with just two loops.

Time Complexity : O(n^2) for nested loops, Space Complexity : O(1)

Click here to read more

Different approaches for checking whether Array has duplicate elements or not

This is one of basic searching algorithm question.
In this post, we’ll look into different approaches of checking whether array has duplicate elements or not. In addition to this, we’ll also see how to findall duplicate elements from array in one pass.

Approach 1 Brute-Force approach
In this method, for each input elements check whether there is any element with the same value. This we can solve with two loops.

Time Complexity : O(n^2) for nested loops, Space Complexity : O(1)

Click here to read more