Queues and linked lists are common data structures. Queues follow FIFO ordering and allow insertion at the rear and removal from the front. Linked lists provide efficient insertion and removal by using nodes connected by pointers. Doubly linked lists allow efficient insertion and removal from both ends, enabling implementations of double-ended queues. Positions abstract the concept of location in a data structure and allow node-based operations on linked lists. Iterators encapsulate traversal of a data structure.
Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first.
a. Concept and Definition✓
b. Inserting and Deleting nodes ✓
c. Linked implementation of a stack (PUSH/POP) ✓
d. Linked implementation of a queue (Insert/Remove) ✓
e. Circular List
• Stack as a circular list (PUSH/POP) ✓
• Queue as a circular list (Insert/Remove) ✓
f. Doubly Linked List (Insert/Remove) ✓
For more course related material:
https://github.com/ashim888/dataStructureAndAlgorithm/
Personal blog
www.ashimlamichhane.com.np
Queues
a. Concept and Definition
b. Queue as an ADT
c. Implementation of Insert and Delete operation of:
• Linear Queue
• Circular Queue
For More:
https://github.com/ashim888/dataStructureAndAlgorithm
http://www.ashimlamichhane.com.np/
As multimedia are being used to facilitate and enhance teaching and creative learning, making rich media including images and videos searchable are critical in today and future elearning platform and management system. Traditionally, it's a difficult challenge. Some of the team members have invented an interdisciplinary framework which integrates the state-of-the-art image recognition technology and brain science research from MIT, intelligent cluster algorithm and patentable digital “tagging” technology
Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first.
a. Concept and Definition✓
b. Inserting and Deleting nodes ✓
c. Linked implementation of a stack (PUSH/POP) ✓
d. Linked implementation of a queue (Insert/Remove) ✓
e. Circular List
• Stack as a circular list (PUSH/POP) ✓
• Queue as a circular list (Insert/Remove) ✓
f. Doubly Linked List (Insert/Remove) ✓
For more course related material:
https://github.com/ashim888/dataStructureAndAlgorithm/
Personal blog
www.ashimlamichhane.com.np
Queues
a. Concept and Definition
b. Queue as an ADT
c. Implementation of Insert and Delete operation of:
• Linear Queue
• Circular Queue
For More:
https://github.com/ashim888/dataStructureAndAlgorithm
http://www.ashimlamichhane.com.np/
As multimedia are being used to facilitate and enhance teaching and creative learning, making rich media including images and videos searchable are critical in today and future elearning platform and management system. Traditionally, it's a difficult challenge. Some of the team members have invented an interdisciplinary framework which integrates the state-of-the-art image recognition technology and brain science research from MIT, intelligent cluster algorithm and patentable digital “tagging” technology
I prepared this document for a rum brand in 2012. Nothing ever came of the meeting, but I thought my followers might enjoy seeing a discussion document that highlights how to make a brand super premium. It was meant for a presentation-style meeting so some slides may not be 100% clear, but it should still be an enjoyable read.
In java , I want you to implement a Data Structure known as a Doubly.pdfaromalcom
In java , I want you to implement a Data Structure known as a Doubly-Ended-Queue. it is a
“fair” data structure in that it implements a FIFO (First In First Out ) behavior. As such, it is
often used to implement various wait lists in computer systems. For example, jobs waiting to use
the CPU, jobs waiting for a printer, jobs waiting to be placed into RAM for execution. In short,
whenever we want a fair strategy for waiting we use queues.
A DEQUE (Doubly-ended-queue) is a related data structure. Although similar to a Queue, it
differs in that it allows for insertions AND deletions from either end of the list (both the front
and the rear).
Your implementation MUST use a doubly-linked-list implementation. You may not use a static
array implementation.
Thus, a Deque is a List but it is one which only concerns itself with the first and last positions for
any insertion or deletion. The 6 operations supported are :
public void insertFront( int item ) - insert the given item (as a node) into the first position of the
Deque.
public void insertRear( int item ) - insert the given item (as a node) into the last position of the
Deque.
public int deleteFront( ) - delete and return the element stored in the first node of the Deque.
public int deletRear( ) – delete and return the element stored in the last node of the Deque.
public boolean isempty( ) - returns true if the Deque is currently empty or false if it is not.
public void printDeque( ) - print the integers from the list, one per line, from the first element
through to the last in order.
Classes
Your program must implement the following 3 classes. public class dequeDriver
This class will contain your program’s main method. It will need to declare a deque object and
process input as indicated below.
Your program should prompt the user for the path of an input file. It should open the file for
input and process it line by line. Each line of the input file will have one of the following forms.
PR
IF
IR
DF
DR
The meanings of each input is as follows:
PR - print the current contents of the deque from front to rear using the printDeque( ) method of
the deque object.
IF - insert the given int value into the front of the deque.
IR - insert the given int value into the rear of the deque.
DF - delete the front value from the deque.
DR – delete the rear element of the deque.
Below is an example input file that your program should be able to process.
PR
IF 4
IF 5
IF 6
IR 7
PR
DR
PR
DF
PR
The output for the input file shown above is :
EMPTY DEQUE
----- Front -----
6
5
4
7
----- Rear -----
----- Front -----
6
5
4
----- Rear -----
----- Front -----
5
4
----- Rear -----
public class dequeNode
This class will implement the linked nodes that will be used to implement the deque itself.
It should have the following protected data members.
protected dequeNode next; // next pointer to next node
protected dequeNode prev; // previous pointer to previous node
protected int val; // the integer value stored within the dequeNod.
2. Queues
A queue differs from a stack in that its insertion
and removal routines follows the first-in-first-out
(FIFO) principle.
Elements may be inserted at any time, but only
the element which has been in the queue the
longest may be removed.
Elements are inserted at the rear (enqueued) and
removed from the front (dequeued)
Front Queue Rear
3. Queues (2)
The queue supports three fundamental
methods:
New():ADT – Creates an empty queue
Enqueue(S:ADT, o:element):ADT - Inserts object o
at the rear of the queue
Dequeue(S:ADT):ADT - Removes the object from the
front of the queue; an error occurs if the queue is
empty
Front(S:ADT):element - Returns, but does not
remove, the front element; an error occurs if the
queue is empty
4. Queues (3)
These support methods should also be
defined:
Size(S:ADT):integer
IsEmpty(S:ADT):boolean
Axioms:
Front(Enqueue(New(), v)) = v
Dequeque(Enqueue(New(), v)) = New()
Front(Enqueue(Enqueue(Q, w), v)) =
Front(Enqueue(Q, w))
Dequeue(Enqueue(Enqueue(Q, w), v)) =
Enqueue(Dequeue(Enqueue(Q, w)), v)
5. An Array Implementation
Create a queue using an array in a circular
fashion
A maximum size N is specified.
The queue consists of an N-element array
Q and two integer variables:
f, index of the front element (head – for
dequeue)
r, index of the element after the rear one (tail
– for enqueue)
7. An Array Implementation (3)
Pseudo code
Algorithm size() Algorithm dequeue()
return (N-f+r) mod N if isEmpty() then
return Queueemptyexception
Algorithm isEmpty()
Q[f] null
return (f=r)
f (f+1)modN
Algorithm front()
if isEmpty() then
Algorithm enqueue(o)
return Queueemptyexception
if size = N - 1 then
return Q[f]
return Queuefullexception
Q[r] o
r (r +1)modN
8. Implementing Queue with Linked List
Nodes (data, pointer) connected in a chain by
links
The head of the list is the front of the queue, the
tail of the list is the rear of the queue. Why not
the opposite?
10. Linked List Implementation (2)
Enqueue - create a new node at the tail
chain it and move the tail reference
How about removing at the tail?
11. Double-Ended Queue
A double-ended queue, or deque, supports
insertion and deletion from the front and back
The deque supports six fundamental methods
InsertFirst(S:ADT, o:element):ADT - Inserts e at the
beginning of deque
InsertLast(S:ADT, o:element):ADT - Inserts e at end of
deque
RemoveFirst(S:ADT):ADT – Removes the first element
RemoveLast(S:ADT):ADT – Removes the last element
First(S:ADT):element and Last(S:ADT):element –
Returns the first and the last elements
12. Doubly Linked Lists
Deletions at the tail of a singly linked list cannot be
done in constant time
To implement a deque, we use a doubly linked
list
A node of a doubly linked list has a next and a
prev link
Then, all the methods of a deque have a constant
(that is, O(1)) running time.
13. Doubly Linked Lists (2)
When implementing a doubly linked lists, we add
two special nodes to the ends of the lists: the
header and trailer nodes
The header node goes before the first list element. It
has a valid next link but a null prev link.
The trailer node goes after the last element. It has a
valid prev reference but a null next reference.
The header and trailer nodes are sentinel or
“dummy” nodes because they do not store
elements
14.
15. Stacks with Deques
Implementing ADTs using implementations
of other ADTs as building blocks
Stack Method Deque
Implementation
size() size()
isEmpty() isEmpty()
top() last()
push(o) insertLast(o)
pop() removeLast()
17. The Adaptor Pattern
Using a deque to implement a stack or queue is an
example of the adaptor pattern. Adaptor patterns
implement a class by using methods of another class
In general, adaptor classes specialize general
classes
Two such applications
Specialize a general class by changing some methods eg
implementing a stack with a deque.
Specialize the types of objects used by a general class eg
defining an IntegerArrayStack class that adapts ArrayStack
to only store integers.
18. Circular Lists
No end and no beginning of the list, only one
pointer as an entry point
Circular doubly linked list with a sentinel is an
elegant implementation of a stack or a queue
20. The Vector ADT
A sequence S (with n elements) that supports the following
methods:
elemAtRank(r): Return the element of S with rank r; an
error occurs if r < 0 or r > n -1
replaceAtRank(r,e): Replace the element at rank r with e
and return the old element; an error condition occurs if
r < 0 or r > n - 1
insertAtRank(r,e): Insert a new element into S which will
have rank r; an error occurs if r< 0 or r > n
removeAtRank(r): Remove from S the element at rank r;
an error occurs if r < 0 or r > n - 1
23. Implem. with a Doubly Linked List
the list before insertion
creating a new node for
insertion:
the list after insertion
24. Java Implementation
public void insertAtRank (int rank, Object element)
throws BoundaryViolationException {
if (rank < 0 || rank > size())
throw new BoundaryViolationException(“invalid rank”);
DLNode next = nodeAtRank(rank);
// the new node will be right before this
DLNode prev = next.getPrev();
// the new node will be right after this
DLNode node = new DLNode(element, prev, next);
// new node knows about its next & prev.
// Now we tell next & prev about the new node.
next.setPrev(node);
prev.setNext(node);
size++;
}
25. Implementation with a Doubly Linked
List
the list before
deletion
deleting a node
after deletion
26. Java Implementation
public Object removeAtRank (int rank)
throws BoundaryViolationException {
if (rank < 0 || rank > size()-1)
throw new BoundaryViolationException(“Invalid rank.”);
DLNode node = nodeAtRank(rank); // node to be removed
DLNode next = node.getNext(); // node before it
DLNode prev = node.getPrev(); // node after it
prev.setNext(next);
next.setPrev(prev);
size--;
return node.getElement();
// returns the element of the deleted node
}
27. Java Implementation (contd.)
private DLNode nodeAtRank (int rank) {
//auxiliary method to find node of element with given rank
DLNode node;
if (rank <= size()/2) { //scan forward from head
node = header.getNext();
for (int i=0; i < rank; i++)
node = node.getNext();
} else { //scan backward from the tail
node = trailer.getPrev();
for (int i=0; i < size()-rank-1 ; i++)
node = node.getPrev();
}
return node;
}
28. Nodes
Linked lists support the efficient execution of
node-based operations:
removeAtNode(Node v) and
insertAfterNode(Node v, Object e), would be
O(1).
However, node-based operations are not
meaningful in an array-based implementation
because there are no nodes in an array.
Nodes are implementation-specific.
Dilemma:
If we do not define node based operations, we are not
taking full advantage of doubly-linked lists.
If we do define them, we violate the generality of ADTs.
29. From Nodes to Positions
We introduce the Position ADT
Intuitive notion of “place” of an element.
Positions have only one method:
element(): Returns the element at this
position
Positions are defined relative to other
positions (before/after relation)
Positions are not tied to an element or
rank
30. The List ADT
ADT with position-based methods
generic methods: size(), isEmpty()
query methods: isFirst(p), isLast(p)
accessor methods: first(), last(), before(p), after(p)
update methods
swapElements(p,q), replaceElement(p,e)
insertFirst(e), insertLast(e)
insertBefore(p,e), insertAfter(p,e)
remove(p)
each method takes O(1) time if implemented with
a doubly linked list
31. The Sequence ADT
Combines the Vector
and List ADT (multiple
inheritance)
Adds methods that
bridge between ranks
and positions
atRank(r) returns a
position
rankOf(p) returns an
integer rank
An array-based implementation needs to use
objects to represent the positions
33. Iterators
Abstraction of the process of scanning through a
collection of elements
Encapsulates the notions of “place” and “next”
Extends the position ADT
Generic and specialized iterators
ObjectIterator: hasNext(), nextObject(), object()
PositionIterator: nextPosition()
Useful methods that return iterators:
elements(), positions()