Linear Data Structures
Data structure is an arrangement of data in a computer’s memory (maybe sometimes on a disk).
Data structures used in Java.
Array
Arrays are the most commonly used data storage structure. Array is an object in Java that contains similar types of data. There are two types of data in Java: Primitive ( int and double) and objects. In most of the programming languages arrays are considered as primitive types. However, in Java, we get it as objects.
Array elements are accessed using an index number in square brackets.The first element is numbered 0.
example-
code
output
There are three main operations in array.
- Insertion
- Deletion
- Searching
Linked List
Linked lists are probably the second most commonly used general-purpose storage structures after arrays. The linked list is a versatile mechanism suitable for use in many kinds of general-purpose databases. Conceptually simpler than some other popular structures such as trees.
A link is an object of a class called something like Link. A field in the list itself contains a reference to the first link.
This kind of class definition is sometimes called self-referential because it contains a field — called next in this case — of the same type as itself. We show only two data items in the link: an int and a double.
The difference between array and linked list:
In an array each item occupies a particular position.In a list the only way to find a particular element is to follow along the chain of elements.
Operations of linked list
- Insertion
- Deletion
- Display
- Search
Stack
A stack allows access to only one data item: the last item inserted.If you remove this item, you can access the next-to-last item inserted, and so on. We call it as Last In First Out(LIFO). It works as below.
Most microprocessors use a stack-based architecture.
Main method of a stack.
- push() //put item on top of stack
- pop() //take item from top of stack
- peek() //peek at top of stack
- isEmpty() //true if stack is empty
- isFull() //true if stack is full
There are different philosophies about how to handle stack errors.
Use of stscks
- reverse word
- Delimiter Matching
Queue
Queue is a data structure that is somewhat like a stack, except that in a queue the first item inserted is the first to be removed (First-In-First-Out, FIFO)
The terms for insertion and removal in a stack are fairly standard; everyone says push and pop.
Queue method :
- insert() //assumes that the queue is not full.
- remove() // assumes that the queue is not empty.
- peek() //returns the value at front.
Deques: A deque is a double-ended queue
Priority Queues: a more specialized data structure than a stack or a queue.