Linear Data Structures

Ashini Samaranayake
4 min readNov 14, 2023

--

Data structure is an arrangement of data in a computer’s memory (maybe sometimes on a disk).

Data structures used in Java.

data structure types

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.

create an array

Array elements are accessed using an index number in square brackets.The first element is numbered 0.

example-

array indexes

code

output

There are three main operations in array.

  1. Insertion
  2. Deletion
  3. 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.

how the linked list contains

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.

create a linked list

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

  1. Insertion
  2. Deletion
  3. Display
  4. 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.

add elements to the stack
remove elements from stack

Most microprocessors use a stack-based architecture.

Main method of a stack.

  1. push() //put item on top of stack
  2. pop() //take item from top of stack
  3. peek() //peek at top of stack
  4. isEmpty() //true if stack is empty
  5. isFull() //true if stack is full

There are different philosophies about how to handle stack errors.

stack code

Use of stscks

  1. reverse word
  2. 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)

how queue look like
how queue is work

The terms for insertion and removal in a stack are fairly standard; everyone says push and pop.

Queue method :

  1. insert() //assumes that the queue is not full.
  2. remove() // assumes that the queue is not empty.
  3. 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.

queue code

--

--