Data S

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Data structure

• A named location that can be used to store and organize data

• Like arrays
An Algorithm

• A collection of steps to solve a problem

• Like linear search or binary serach


Stack

• A LIFO data structure


• Uses push() to insert values, uses pop() to remove last added value
• Order to access bottom values you have at the pop the values form
the top
• Sort objects into a sort of a vertical tower
Stack methods

• empty() : Boolean
• Tests if this stack is empty.

• peek () : E
• Looks at the object at the top of this stack without removing it from the stack.
• pop : E
• Removes the object at the top of this stack and returns that object as the
value of this function.
• push (E item) : E
• Pushes an item onto the top of this stack.
• Search (Object o) : Object
• Returns the 1-based position where an object is on this stack from the top.
Uses of the stack

• Undo/Redo features in text editors


• Moving back/forward through browser history
• Backtracking algorithm (maze, file directories)
• Calling functions (call stack)
Queue

• FIFO data structure


• A collection designed for holding elements prior to processing
linear data structure

• Adding an object  enqueue


• Removing an object  dequeue

• Linked list & priority queue implements the Queue interface


Queue methods

• add (E e) : Boolean
• Inserts the specified element into this queue if it is possible
• returning true upon success and throwing an IllegalStateException if no space is
currently available.
• element(): E
• Retrieves, but does not remove, the head of this queue.
• Will return an expectation if the queue is empty
• offer (E e) : Boolean
• Inserts the specified element into this queue if it is possible
• Return true of or false
• peek ()
• Retrieves, but does not remove, the head of this queue, or returns null if this
queue is empty.
Queue methods

• poll(): E
• Retrieves and removes the head of this queue or returns null if this queue is
empty.
• remove()
• Retrieves and removes the head of this queue.
• Will return an expectation if the queue is empty
Uses of the queues

• Keyboard buffer (making the letters appear in the order they were
inserted in)
• Printer queue
• Used in linkedLists, PriortyQeueus, Breadth-first search
Priority Queue

• A FIFO data structure that serves element with the highest


priorities first before other elements with lower priority

• Upon entering the elements, the priority queue will arrange them

• To reverse the order use the default comparator method 


Collections.reverseOrder();
Priority Queue

• Upon the polling the will print the


elements in an ascending order
Priority Queue

• Upon the polling they will print the


elements in an ascending order
Priority Queue

• Upon the polling they will print the


elements in descending order because
we are using the comparator
single Linked Lists

• Is made up of long chain of nodes and each node contain two


nodes (data , address(pointer)  points at the next node)
• The nodes are non-continuous
• No shifting is required
• Are bad at searching
• The address of the last node points at null
• Upon inserting a new node, you take the address of the new node
and makes point at the next node in line and make the address of
the previous node to point at the new node
• Upon deleting just change the pointer of the previous node
Linked Lists Methods
Doubly linked lists

• They have to address unlike the single linked lists


• You can traverse from head to tail of from tail to head unlike
linked list
• Use more memory

You might also like