Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Data Structure: Arrays, Stack, Link-list

The document provides an overview of data structures including arrays, linked lists, stacks, and queues, along with their characteristics, operations, and applications. It includes code examples for array initialization and manipulation, as well as detailed explanations of linked list types and their functionalities. Additionally, it discusses the real-life applications of these data structures in various scenarios such as scheduling, memory management, and user interface operations.

Uploaded by

Declan Mba
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Data Structure: Arrays, Stack, Link-list

The document provides an overview of data structures including arrays, linked lists, stacks, and queues, along with their characteristics, operations, and applications. It includes code examples for array initialization and manipulation, as well as detailed explanations of linked list types and their functionalities. Additionally, it discusses the real-life applications of these data structures in various scenarios such as scheduling, memory management, and user interface operations.

Uploaded by

Declan Mba
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

CSC 301

Arrays/Linked
DATA STRUCTURES:
List/Stack/Queue
Tuesday,
11/02/25

Uchenna-Declan
Mba
Arrays (codes)
public class Arrays {
public static void main(String[] args) {
int [] numbers = new int[10];
numbers[0] = 10;
numbers[1] = 500;
numbers[2] = 1000;
numbers [3] = 534;
numbers [4] = 702;
numbers [6] = 342;
numbers [9] = 5543;

for (int i = 0; i < numbers.length; i++) {


System.out.println(numbers[i]);
}
}

}
Arrays (codes) Initialization & Declaration

public class Arrays {


public static void main(String[] args)
{
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

for (int i = 0; i < numbers.length; i++) {


System.out.println("The list of element at
index " + i + " : " +numbers[i]);
}

}
Arrays (codes) DT.

Strings
public class Arrays {

public static void main(String[] args) {

String[] nameList = {"Tom", "Mary", "Peter",


"John", "Adam", "Justin"};

for (String aName : nameList) {


System.out.println(“type your text”+ i + " :
" +aName);
}

}
}
Arrays cont…
Besides the fundamental operations like getting and setting, Java
provides various functions to manipulate arrays in the Arrays class.
The Arrays is a utility class which can be found in
the java.util package.
 asList(): returns a fixed-size list backed by an array.
 binarySearch(): searches for a specific value in an array. Returns the
index of the element if found, or -1 if not found. Note that the array
must be sorted first.
 copyOf(): copies a portion of the specified array to a new one.
 copyOfRange(): copies a specified range of an array to a new one.
 equals(): compares two arrays to determine if they are equal or not.
 fill(): fills same values to all or some elements in an array.
 sort(): sorts an array into ascending order.
 And other methods you can find in the Array Class Javadoc.
 Main disadvantages of array: fixed length, not appropriate if a
dynamic container is required.
Linked List
 A linked list is a linear data structure in which
elements are not stored at contiguous
memory locations. The elements in a linked
list are linked using pointers.

They are;
 Singly-linked list

 Doubly linked list

 Circular linked list

 Doubly circular linked list


Diagram of Link list

Used to maintain a list-like structure in the computer


memory.
Each node of the list is linked to its adjacent node
with the help of pointers
Characteristics
 A linked list uses extra memory to store links.
 During the initialization of the linked list, there is no
need to know the size of the elements.
 Linked lists are used to implement stacks, queues,
graphs, etc.
 The first node of the linked list is called the Head.
 The next pointer of the last node always points to
NULL.
 In a linked list, insertion and deletion are possible
easily.
 Each node of the linked list consists of a pointer/link
which is the address of the next node.
 Linked lists can shrink or grow at any point in time
easily.
Operations performed
 Initialization: Initialization is done by creating a head
node with a reference to the first node.
 Inserting elements: Elements can be inserted at the
head, tail, or at a specific position in the linked list.
 Deleting elements: Elements can be deleted from
the linked list by updating the reference of the previous
node to point to the next node, effectively removing
the current node from the list.
 Searching for elements: Linked lists can be searched
for a specific element by starting from the head node
and following the references to the next nodes until the
desired element is found.
Operations performed cont…
 Updating elements: Elements in a linked list
can be updated by modifying the value of a
specific node.
 Traversing elements: The elements in a
linked list can be traversed by starting from
the head node and following the references to
the next nodes until the end of the list is
reached.
 Reversing a linked list: The linked list can
be reversed by updating the references of
each node so that they point to the previous
node instead of the next node.
Applications
 Used to implement stacks, queues, graphs,
etc.
 Used to perform arithmetic operations on long
integers.
 It is used for the representation of sparse
matrices.
 It is used in the linked allocation of files.
 It helps in memory management.
 It is used in the representation of Polynomial
Manipulation where each polynomial term
represents a node in the linked list.
 Used to display image containers. Users can
Applications cont…
 They are used to store the history of
the visited page.
 They are used to perform undo
operations.
 Used in software development where
they indicate the correct syntax of a
tag.
 Linked lists are used to display social
media feeds.
Real-Life Applications
 A linked list is used in Round-Robin
scheduling to keep track of the turn in
multiplayer games.
 It is used in image viewer. The previous
and next images are linked, and hence
can be accessed by the previous and
next buttons.
 In a music playlist, songs are linked to
the previous and next songs.
Stack
 Stack is a linear data structure that
follows a particular order in which the
operations are performed.
The order is LIFO (Last in first out).
Entering and retrieving data is possible
from only one end. The entering and
retrieving of data is also called push
and pop operation in a stack.
Diagram of Stack
Characteristics
 Stack is deployed in many algorithms like
Tower of Hanoi, tree traversal, recursion, etc.
 Stack is implemented through an array or
linked list.
 It follows the Last In First Out operation i.e.,
an element that is inserted first will pop in
last and vice versa.
 The insertion and deletion are performed at
one end i.e. from the top of the stack.
 In stack, if the allocated space for the stack is
full, and still anyone attempts to add more
elements, it will lead to stack overflow.
Applications
 Used in the evaluation and conversion of
arithmetic expressions.
 It is used for parenthesis checking.
 While reversing a string, the stack is used as
well.
 Stack is used in memory management.
 It is also used for processing function calls.
 The stack is used to convert expressions from
infix to postfix.
 Used to perform undo as well as redo
operations in word processors.
Applications cont…
 The stack is used in virtual
machines like JVM.
 The stack is used in the media
players. Useful to play the next
and previous song.
 The stack is used in recursion
operations.
Operations
 Push: Elements can be pushed onto the top of the
stack, adding a new element to the top of the stack.
 Pop: The top element can be removed from the stack
by performing a pop operation, effectively removing
the last element that was pushed onto the stack.
 Peek: The top element can be inspected without
removing it from the stack using a peek operation.
 IsEmpty: A check can be made to determine if the
stack is empty.
 Size: The number of elements in the stack can be
determined using a size operation.
Real-Life Applications
 Real life example of a stack is the layer of eating
plates arranged one above the other. When you
remove a plate from the pile, you can take the plate to
the top of the pile. But this is exactly the plate that
was added most recently to the pile. If you want the
plate at the bottom of the pile, you must remove all
the plates on top of it to reach it.
 Browsers use stack data structures to keep track of
previously visited sites.
 Call log in mobile also uses stack data structure.
Queue
 Queue is a linear data structure that
follows a particular order in which the
operations are performed.
 The order is First In First Out(FIFO) i.e.
the data item stored first will be
accessed first. In this, entering and
retrieving data is not done from only
one end.
Diagram of Queue Data
Structure
Characteristics
 The queue is a FIFO (First In First Out) structure.
 To remove the last element of the Queue, all the
elements inserted before the new element in the
queue must be removed.
 A queue is an ordered list of elements of similar
data types.
Applications
 Used for handling website traffic.
 It helps to maintain the playlist in media players.
 Queue is used in operating systems for handling interrupts.
 It helps in serving requests on a single shared resource, like
a printer, CPU task scheduling, etc.
 It is used in the asynchronous transfer of data e.g. pipes, file
IO, and sockets.
 Queues are used for job scheduling in the operating system.
 In social media to upload multiple photos or videos queue is
used.
 To send an e-mail queue data structure is used.
 In Windows operating system, to switch multiple
applications.
Operations
 Enqueue: Elements can be added to the back of the
queue, adding a new element to the end of the
queue.
 Dequeue: The front element can be removed from
the queue by performing a dequeue operation,
effectively removing the first element that was
added to the queue.
 Peek: The front element can be inspected without
removing it from the queue using a peek operation.
 IsEmpty: A check can be made to determine if the
queue is empty.
 Size: The number of elements in the queue can be
Real-Life Applications
 Single-lane one-way road, where the vehicle
that enters first will exit first.
 Ticket windows.
 A cashier line in a store is also an example of a
queue.
 People on an escalator
Announcement
 Next class on Friday (21/02/25), 10am-
12pm.
Reason: Departmental burial

You might also like