0% found this document useful (0 votes)
11 views38 pages

Chapter 2

Chapter 2 discusses linear data structures, which are essential for data organization in computing, characterized by sequential arrangements of elements. It covers various types of linear data structures such as arrays, linked lists, stacks, and queues, detailing their properties, operations, and implementations in Java. The chapter emphasizes the importance of these structures in efficient data storage, retrieval, and manipulation across different applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views38 pages

Chapter 2

Chapter 2 discusses linear data structures, which are essential for data organization in computing, characterized by sequential arrangements of elements. It covers various types of linear data structures such as arrays, linked lists, stacks, and queues, detailing their properties, operations, and implementations in Java. The chapter emphasizes the importance of these structures in efficient data storage, retrieval, and manipulation across different applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

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.

• As per above shown illustration, following are the


important points to be considered.
– Index starts with 0.
– Array length is 4 which means it can store 4 elements.
– Each element can be accessed via its index. For example,
we can fetch element at index 1 as 0.
Basic Operations

• Following are the basic operations supported by


an array.
– Traverse − print all the array elements one by one.
– Insertion − add an element at given index.
– Deletion − delete an element at given index.
– Search − search an element using given index or by
value.
– Update − update an element at given index.
Cont..
• Traversal
– Explanation of traversing an array in Java.
– Demonstration of loop-based traversal.
– Example code snippet for traversal:
Cont..
• Insertion
– Explanation of inserting elements into an array.
– Demonstration of direct assignment for insertion.
– Example code snippet for insertion:
Cont..
• Deletion
– Explanation of deleting elements from an array.
– Discussion on shifting elements to fill the gap.
– Example code snippet for deletion:
Cont..
• Search
– Introduction to searching algorithms for arrays.
– Explanation of linear search and binary search.
– Example code snippet for linear search:
Update

• 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

• Since Stack extends the Vector class, it inherits


all the methods Vector.
• Besides these methods, the Stack class
includes 5 more methods that distinguish it
from Vector.
empty() Method
• To check whether a stack is empty or not, we use
the empty() method. For example,
import java.util.Stack;
class Main
{
public static void main(String[] args)
{
Stack<String> animals= new Stack<>();
// Add elements to Stack
animals.push("Dog");
animals.push("Horse");
animals.push("Cat");
System.out.println("Stack: " + animals);
// Check if stack is empty
boolean result = animals.empty();
System.out.println("Is the stack empty? " +
result); } }
push() Method
 To add an element to the top of the stack, we use
the push() method. For example
import java.util.Stack;
class Main { public static void main(String[] args)
{ Stack<String> animals= new Stack<>();
// Add elements to Stack animals.push("Dog");
animals.push("Horse");
animals.push("Cat");
System.out.println("Stack: " + animals);
}
pop() Method
• pop method removes and returns the element
from the top of the stack.
• Check if the stack is empty before popping.
import java.util.Stack;
class Main
{
public static void main(String[] args)
{ Stack<String> animals= new Stack<>();
// Add elements to Stack
animals.push("Dog");
animals.push("Horse");
animals.push("Cat");
System.out.println("Initial Stack: " + animals);
// Remove element stacks
String element = animals.pop();
System.out.println("Removed Element: " + element); } }
search() Method
• to search an element in the stack, we use
the search() method. It returns the position of the
element from the top of the stack. For example,
import java.util.Stack;
class Main
{
public static void main(String[] args)
{
Stack<String> animals= new Stack<>();
// Add elements to Stack
animals.push("Dog");
animals.push("Horse");
animals.push("Cat");
System.out.println("Stack: " + animals);
// Search an elemen
t int position = animals.search("Horse");
System.out.println("Position of Horse: " + position); } }
empty() Method

• To check whether a stack is empty or not, we use


the empty() method. For example,
import java.util.Stack;
class Main
{
public static void main(String[] args)
{
Stack<String> animals= new Stack<>();
// Add elements to Stack
animals.push("Dog");
animals.push("Horse");
animals.push("Cat");
System.out.println("Stack: " + animals);
// Check if stack is empty
boolean result = animals.empty();
System.out.println("Is the stack empty? " + result); } }
Queues
Queues
• Definition:
– Queues are a linear data structure.
– Follows the First In, First Out (FIFO) principle.
– Elements are inserted at the rear and removed
from the front.
Queues
 Applications of Queues:
1. Job Scheduling
– Queues are used in job scheduling systems, such as CPU scheduling in
operating systems.
– Processes are placed in a queue and executed in the order they arrive.
2. Printer Queues
– Print jobs sent to a printer are stored in a queue.
– The printer serves each job in the order they were received.
3. Message Queues
– In distributed systems, message queues are used for communication
between different components.
– Messages are sent to a queue and processed by consumers in the
order they are received.
Queues
 Applications of Queues:
4. Call Center Systems
– Queues are used in call center systems to manage
incoming calls.
– Calls are placed in a queue and serviced by available
agents based on their order.
5. Traffic Management
– Traffic lights at intersections often use queues to
manage the flow of vehicles.
– Each lane has its own queue, and vehicles are
allowed to proceed based on their order.
Queues
• Problem :
Design a printer queue system where print jobs
are stored in a queue and processed by a printer.
Implement a Java program to simulate this
system.
• The program should allow adding print jobs to
the queue and printing them in the order they
were added.

You might also like