Chapter 2
Chapter 2
Linear Data
Structure
Introduction to Linear Data Structures
• Linear data structures form the backbone of data
organization in computing.
• They are characterized by the sequential
arrangement of data elements, where each element
is linked to its preceding and succeeding elements,
forming a linear order.
• Importance: Linear data structures play a
fundamental role in various computational tasks,
facilitating efficient storage, retrieval, and
manipulation of data in a wide range of applications
Introduction to Linear Data Structures
• Key Characteristics:
– Sequential Order: Elements are arranged one after the other,
allowing for easy traversal.
– Connectivity: Each element is connected to its adjacent
elements, enabling efficient access and modification.
– Common Operations: Linear data structures support essential
operations such as accessing elements, inserting new elements,
deleting existing elements, and traversing the entire structure.
• Examples: Several commonly used linear data structures
include arrays, linked lists, stacks, and queues, each
offering distinct advantages and trade-offs suited for
specific use cases.
Array
• Array is a container which can hold fix number of
items and these items should be of same type.
• Most of the data structure make use of array to
implement their algorithms.
• Following are important terms to understand the
concepts of Array.
– Element − Each item stored in an array is called an
element.
– Index − Each location of an element in an array has a
numerical index which is used to identify the element.
Array Representation
• Arrays can be declared in various ways in different
languages.
• For illustration, let's take Java array declaration.
• Update
– Explanation of updating elements in an array.
– Demonstration of direct assignment for updating.
– Example code snippet for updating:
Example
Linked Lists
Linked List Basics
• A linked list is a linear data structure which can
store a collection of "nodes" connected together
via links i.e. pointers.
• Linked lists nodes are not stored at a contiguous
location, rather they are linked using pointers to
the different memory locations.
• A node consists of the data value and a pointer
to the address of the next node within the linked
list.
Types of Linked List
• Singly Linked List: Each node contains data and
a single link to the next node.
• Doubly Linked List: Each node contains data
and two links: one to the previous node and
one to the next node.
• Circular Linked List: Last node points back to
the first node, forming a circle.
Types of Linked List
• Singly Linked List
– It is the simplest type of linked list in which every node
contains some data and a pointer to the next node of
the same data type.
– The node contains a pointer to the next node means
that the node stores the address of the next node in the
sequence.
– A single linked list allows the traversal of data only in
one way. Below is the image for the same:
Types of Linked List
• Singly Linked List
Types of Linked List
• Doubly Linked List
– A doubly linked list or a two-way linked list is a more
complex type of linked list that contains a pointer to
the next as well as the previous node in sequence.
– Therefore, it contains three parts of data, a pointer
to the next node, and a pointer to the previous node.
– This would enable us to traverse the list in the
backward direction as well. Below is the image for
the same:
Types of Linked List
• Doubly Linked List
Types of Linked List
• Circular Linked List
– A circular linked list is that in which the last node
contains the pointer to the first node of the list.
Stack
Stack
• Definition:
A stack is a data structure that follows the Last
In, First Out (LIFO) principle, meaning the last
element added to the stack is the first one to
be removed.
Stack Implementation
• In stack, elements are stored and accessed in Last In
First Out manner. That is, elements are added to the
top of the stack and removed from the top of the
stack.
Creating a Stack Java
• In order to create a stack, we must import
the java.util.Stack package first.
• Once we import the package, here is how we
can create a stack in Java.
Stack<Type> stacks = new Stack<>();
Here, Type indicates the stack's type. For example,
// Create Integer type stack
Stack<Integer> stacks = new Stack<>();
// Create String type stack
Stack<String> stacks = new Stack<>();
Stack Methods