Ijirt101357 Paper
Ijirt101357 Paper
space for parameters and local variables is created operations are allowed enqueue and dequeue.
internally using a stack. Enqueue means to insert an item into the back of
compiler's syntax check for matching braces is the queue, dequeue means removing the front item.
implemented by using stack.
The picture demonstrates the FIFO access.
support for recursion
The difference between stacks and queues is in
Implementation removing. In a stack we remove the item the most
In the standard library of classes, the data type recently added; in a queue, we remove the item the
stack is an adapter class, meaning that a stack is least recently added
built on top of other data structures. The underlying Implementation
structure for a stack could be an array, a vector, an In the standard library of classes, the data type
ArrayList, a linked list, or any other collection. queue is an adapter class, meaning that a queue is
Regardless of the type of the underlying data built on top of other data structures. The underlying
structure, a Stack must implement the same structure for a queue could be an array, a Vector, an
functionality. This is achieved by providing a ArrayList, a LinkedList, or any other collection.
unique interface: This isachieved by providing a unique interface.
public interface StackInterface<AnyType> interfaceQueueInterface‹AnyType>
{ {
public void push(AnyType e); publicbooleanisEmpty();
publicAnyType pop(); publicAnyTypegetFront();
publicAnyType peek(); publicAnyTypedequeue();
publicbooleanisEmpty();
public void enqueue(AnyType e);
}
The following picture demonstrates the idea of public void clear();
implementation by composition. }
Another implementation requirement (in addition Each of the above basic operations must run at
to the above interface) is that all stack operations constant time O(1). The following picture
must run in constant time O(1). Constant time demonstrates the idea of implementation by
means that there is some constant k such that an composition.
operation takes k nanoseconds of computational
time regardless of the stack size. IV. ALGORITHM ADDING AN ELEMET
IN A STACK
Array-based implementation
In an array-based implementation we maintain the procedure add(item : items); {add item to the
following fields: an array A of a default size (≥ 1), global stack stack; top is the current top of stack
and n is its maximum size} begin if top = n
the variable top that refers to the top element in the
thenstackfull; top := top+1; stack(top) := item;
stack and the capacity that refers to the array size. end: {of add}
The variable top changes from -1 to capacity - 1.
We say that a stack is empty when top = -1, and the V. DELETION IN STACK
stack is full when top = capacity-1. procedure delete(var item : items); {remove top
In a dynamic stack abstraction when top reaches element from the stack stack and put it in the item}
capacity, we double up the stack size. begin if top = 0 thenstackempty; item :=
stack(top); top := top-1; end; {of delete}
Linked List-based implementation These two procedures are so simple that they
Linked List-based implementation provides the perhaps need no more explanation. Procedure
best (from the efficiency point of view) dynamic delete actually combines the functions TOP and
stack implementation. DELETE, stackfull and stackempty are procedures
which are left unspecified since they will depend
III. QUEUE ITS APPLICATION AND
upon the particular application. Often a stackfull
IMPLEMENTIONS
condition will signal that more storage needs to be
A queue is a container of objects (a linear allocated and the program re-run. Stackempty is
collection) that are inserted and removed according often a meaningful condition.
to the first-in first-out (FIFO) principle. An VI. ADDITION IN QUEUE
excellent example of a queue is a line of students in
procedureaddq (item : items); {add item to the
the food court of the UC. New additions to a line
queue q} begin if rear=n thenqueuefullelse begin
made to the back of the queue, while removal (or
rear :=rear+1; q[rear]:=item; end;end;{of addq}
serving) happens in the front. In the queue only two