Algorithms (Data Structure)
Algorithms (Data Structure)
Stack Operations
Push (S, Top, X)
This procedure inserts an element X to the top of a stack which is represented by a vector S
containing N elements with a pointer Top denoting the top element in the stack.
2. [Increment Top]
Top ← Top + 1
3. [Insert Element]
S[Top] ← X
4. [Finished]
Return
2. [Decrement Pointer]
Top ← Top-1
3. [Finished]
Return
2
UNPARENTHESIZED_SUFFIX.
Given an input string INFIX representing an infix expression whose single character symbols have
precedence values and ranks as given in Table-1, a vector S representing a stack and a string
function NEXTCHAR which, when invoked, returns the next character of the input string, this
algorithm converts the string INFIX to its reverse Polish string equivalent, POLISH. RANK contains
the value of each head of the reverse Polish string. NEXT contains the symbol being examined, and
TEMP is a temporary variable which contains the unstacked element. We assume that the given
input string is padded on the right with the special symbol “#”.
Table – 1
3
6. [Push current symbol onto stack and obtain next input symbol]
Call Push (S, Top, NEXT)
NEXT ← NEXTCHAR (INFIX)
4
REVPOL
Given an input string INFIX containing an infix expression which has been padded on the right with
‘)’ and whose symbols have precedence values given by Table-2, a vector S, used as a stack and a
function NEXTCHAR, which when invoked, returns the next character of its argument, this algorithm
converts INFIX into reverse Polish and places the result in the string POLISH. The integer variable
Top denotes the top of the stack. Algorithm Push and Pop are used for stack manipulation. The
integer variable RANK accumulates the rank of the expression. Finally, the string variable TEMP is
used for temporary storage purpose.
Table – 2
1. [Initialize stack]
Top ← 1
S[Top] ← ‘(‘
5
5. [Remove symbols with greater precedence from stack]
If Top < 1
then Write (‘INVALID’)
Exit
Repeat while f(NEXT) < g(S[TOP])
TEMP ← Pop (S, Top)
POLISH ← POLISH O TEMP
RANK ← RANK + r(TEMP)
If RANK < 1
then Write (‘INVALID’)
Exit
6
Queue Operations
QINSERT (Q, F, R, N, Y)
Given F and R, pointers to the front and rear elements of a queue, a queue Q consisting of N
elements, and an element Y, this procedure inserts Y at the rear of the queue. Prior to the first
invocation of the procedure, F and R have been set to zero.
1. [Overflow?]
If R ≥ N
then Write (‘Overflow’)
Return
2. [Increment rear pointer]
R←R+1
3. [Insert element]
Q[R] ← Y
4. [Is front pointer properly set?]
If F = 0
then F ← 1
Return
QDELETE (Q, F, R)
Given F and R, the pointers to the front and rear elements of a queue, respectively, and the queue
Q to which they correspond, this function deletes and returns the last element of the queue. Y is a
temporary variable.
1. [Underflow?]
If F = 0
then Write (‘Underflow’)
Return (0) (‘0’ denotes an empty queue)
2. [Delete element]
Y ← Q[F]
3. [Queue empty?]
If F = R
then F ← R ← 0
else F ← F + 1 (Increment front pointer)
4. [Return element]
Return (Y)
7
CQINSERT (F, R, Q, N, Y)
Given pointers to the front and rear of a circular queue, F and R, a vector Q consisting of N elements,
and an element Y, this procedure inserts Y at the rear of the queue. Initially F and R set to zero.
CQDELETE (F, R, Q, N)
Given F and R, pointers to the front and rear of a circular queue, respectively, and a vector Q
consisting of N elements, this function deletes and returns the last element of the queue. Y is a
temporary variable.
1. [Underflow?]
If F = 0
then Write (‘Underflow’)
Return (0)
2. [Delete element]
Y ← Q[F]
3. [Queue empty?]
If F = R
then F ← R ← 0
Return (Y)
4. [Increment front pointer]
If F = N
then F ← 1
else F ← F + 1
Return (Y)
8
Linked List
INSERT (X, FIRST)
Given X, a new element, and FIRST, a pointer to the first element of a linked linear list whose typical
node contains INFO and LINK fields as previous described, this function inserts X. AVAIL is a pointer
to the top element of the availability stack; NEW is a temporary pointer variable. It is required that X
precede the node whose address is given by FIRST.
1. [Underflow?]
If AVAIL = NULL
then Write (‘Availability Stack Underflow’)
Return (FIRST)
9
INSEND (X, FIRST)
Given X, a new element, and FIRST, a pointer to the first element of a linked linear list whose typical
node contains INFO and LINK fields as previously described, this function inserts X. AVAIL is a
pointer to the top element of the availability stack; NEW and SAVE are temporary pointer variables.
It is required that X be inserted at the end of the list.
1. [Underflow?]
If AVAIL = NULL
then Write (‘Availability Stack Underflow’)
Return (FIRST)
10
INSORD (X, FIRST)
Given a new term X, this function inserts it into a linked linear list whose typical node contains INFO
and LINK fields as previously described. AVAIL is a pointer to the top element of the availability
stack; NEW and SAVE are temporary pointer variables. It is required that X be inserted so that it
preserves the ordering of the terms in increasing order of their INFO fields.
1. [Underflow?]
If AVAIL = NULL
then Write (‘Availability Stack Underflow’)
Return (FIRST)
1. [Empty list?]
If FIRST = NULL
then Write (‘Underflow’)
Return
2. [Initialize search of X]
TEMP ← FIRST
3. [Find X]
Repeat thru step 5 while TEMP ≠ X and LINK (TEMP) ≠ NULL
6. [End of list?]
If TEMP ≠ X
then Write (‘Node not Found’)
Return
7. [Delete X]
If X = FIRST (Is X the first node?)
then FIRST ← LINK (FIRST)
else LINK (PRED) ← LINK (X)
12
COPY (FIRST)
Given FIRST, a pointer to the first node in a linked list, this function makes a copy of this list. A
typical node in the given list consists of INFO and LINK fields; the new list is to contain nodes
whose information and pointer fields are denoted by FIELD and PTR respectively. The address of
the first node in the newly created list is to be placed in BEGIN. NEW, SAVE and PRED are pointer
variables.
1. [Empty list?]
If FIRST = NULL
then Return (NULL)
3. [Initialize traversal]
SAVE ← FIRST
6. [Copy node]
If AVAIL = NULL
then Write (‘Availability Stack Underflow’)
Return (0)
else NEW ← AVAIL
AVAIL ← LINK (AVAIL)
FIELD (NEW) ← INFO (SAVE)
PTR (PRED) ← NEW
13
DOUBINS (L, R, M, X)
Given a doubly linked linear list whose left-most and right-most node addresses are given by the
pointer variables L and R, respectively, it is required to insert a node whose address is given by the
pointer variable NEW. The left and right links of a node are denoted by LPTR and RPTR,
respectively. The information field of a node is denoted by the variable INFO. The name of an
element of the list is NODE. The insertion is to be performed to the left of a specified node with its
address given by the pointer variable M. The information to be entered in the node is contained in
X.
4. [Left-most insertion?]
If M = L
then LPTR (NEW) ← NULL
RPTR (NEW) ← M
LPTR (M) ← NEW
L ← NEW
Return
5. [Insert in middle]
LPTR (NEW) ← LPTR (M)
RPTR (NEW) ← M
LPTR (M) ← NEW
RPTR (LPTR (NEW)) ← NEW
Return
14
DOUBDEL (L, R, OLD)
Given a doubly linked list with the addresses of the left-most and right-most nodes given by the
pointer variables L and R, respectively, it is required to delete the node whose address is contained
in the variable OLD. Nodes contain left and right links with names LPTR and RPTR, respectively.
1. [Underflow?]
If R = NULL
then Write (‘Underflow’)
Return
2. [Delete node]
If L = R (Single node in list)
then L ← R ← NULL
else If OLD = L (Left-most node being deleted)
then L ← RPTR (L)
LPTR (L) ← NULL
else if OLD = R (Right-most node being deleted)
then R ← LPTR (R)
RPTR (R) ← NULL
else RPTR (LPTR (OLD)) ← RPTR (OLD)
LPTR (RPTR (OLD)) ← LPTR (OLD)
15