Algorithm Design Unit 1
Algorithm Design Unit 1
UNIT 1
The user of data type does not need to know how that data type is
implemented, for example, we have been using Primitive values like
int, float, char data types only with the knowledge that these data
type can operate and be performed on without any idea of how they
are implemented.
So a user only needs to know what a data type can do, but not how
it will be implemented. Think of ADT as a black box which hides the
inner structure and design of the data type. Now we’ll define three
ADTs namely List ADT, Stack ADT, Queue ADT.
1. List ADT
Vies of list
View of stack
View of Queue
Stack
Push:
Adds an item to the stack. If the stack is full, then it is said to be
an Overflow condition.
Algorithm for push:
begin
if stack is full
return
endif
else
increment top
stack[top] assign value
end else
end procedure
Pop:
Removes an item from the stack. The items are popped in the
reversed order in which they are pushed. If the stack is empty, then
it is said to be an Underflow condition.
Algorithm for pop:
begin
if stack is empty
return
endif
else
store value of stack[top]
decrement top
return value
end else
end procedure
Top:
Returns the top element of the stack.
Algorithm for Top:
begin
return stack[top]
end procedure
isEmpty:
Returns true if the stack is empty, else false.
Types of Stacks:
Fixed Size Stack: As the name suggests, a fixed size
stack has a fixed size and cannot grow or shrink
dynamically. If the stack is full and an attempt is made to
add an element to it, an overflow error occurs. If the stack
is empty and an attempt is made to remove an element
from it, an underflow error occurs.
Dynamic Size Stack: A dynamic size stack can grow or
shrink dynamically. When the stack is full, it automatically
increases its size to accommodate the new element, and
when the stack is empty, it decreases its size. This type of
stack is implemented using a linked list, as it allows for
easy resizing of the stack.
ntroduction to Queue – Data Structure and
Algorithm Tutorials
Read
Discuss
Courses
Practice
What is Queue?
A queue is a linear data structure that is open at both ends and the
operations are performed in First In First Out (FIFO) order.
We define a queue to be a list in which all additions to the list are
made at one end, and all deletions from the list are made at the
other end. The element which is first pushed into the order, the
delete operation is first performed on that.
FIFO Principle of Queue:
A Queue is like a line waiting to purchase tickets, where the
first person in line is the first person served. (i.e. First come
first serve).
Position of the entry in a queue ready to be served, that is,
the first entry that will be removed from the queue, is called
the front of the queue(sometimes, head of the queue),
similarly, the position of the last entry in the queue, that is,
the one most recently added, is called the rear (or the tail)
of the queue. See the below figure.
Characteristics of Queue:
Queue can handle multiple data.
We can access both ends.
They are fast and flexible.
Queue Representation:
One way chain or singly linked list can be traversed only in one direction.
In other words, we can say that each node contains only next pointer,
therefore we can not traverse the list in the reverse direction.
In the above figure, the arrow represents the links. The data part of every
node contains the marks obtained by the student in the different subject.
The last node in the list is identified by the null pointer which is present in
the address part of the last node. We can have as many elements we
require, in the data part of the list.
Linked-List
o The first part contains the value of the coefficient of the term.
o The second part contains the value of the exponent.
o The third part, LINK points to the next term (next node).
Observe that the number of nodes equals the number of terms in the
polynomial. So we have 4 nodes. Moreover, the terms are stored to
decrease exponents in the linked list. Such representation of polynomial
using linked lists makes the operations like subtraction, addition,
multiplication, etc., on polynomial very easy.
Addition of Polynomials:
To generate a new linked list for the resulting polynomials that is formed
on the addition of given polynomials P(x) and Q(x), we perform the
following steps,
1. Traverse the two lists P and Q and examine all the nodes.
2. We compare the exponents of the corresponding terms of two
polynomials. The first term of polynomials P and Q contain
exponents 4 and 3, respectively. Since the exponent of the first
term of the polynomial P is greater than the other polynomial Q, the
term having a larger exponent is inserted into the new list. The new
list initially looks as shown below:
3. We then compare the exponent of the next term of the list P with
the exponents of the present term of list Q. Since the two exponents
are equal, so their coefficients are added and appended to the new
list as follows:
4. Then we move to the next term of P and Q lists and compare their
exponents. Since exponents of both these terms are equal and after
addition of their coefficients, we get 0, so the term is dropped, and
no node is appended to the new list after this,