Professional Documents
Culture Documents
Data Structures and Algorithm Analysis 3rd Exam
Data Structures and Algorithm Analysis 3rd Exam
3rd Exam
Q: Write steps to insert a new node at the left of a node containing information (X) in an
existing D.L.L.L
Answer:
1. [Obtain a new node from the availability pool]
newNode ← avail
avail ← link(avail)
2. [Check if info is in the L node]
If (info(L) = X) then
LPTR(L) ← newNode
RPTR(newNode) ← L
L ← newNode
LPTR(L) ← NULL
Exit
3. [Loop through the D.L.L.L searching for X]
Let P be a pointer
P ← L
While (P ≠ R and info(P) ≠ X)
{
P ← RPTR(P)
}
If (info(P) = X) then
{
LPTR(newNode) ← LPTR(P)
RPTR(newNode) ← P
RPTR(LPTR(P)) ← newNode
LPTR(P) ← newNode
}
Else output “Element X not found” and exit
4. [Finished] exit
1
Q: Write steps to insert a new node at the left of a node containing information (X) in an
existing D.L.L.L
Answer:
1. [Obtain a new node from the availability pool]
newNode ← avail
avail ← link(avail)
2. [Loop through D.L.L.L searching for X]
Let P be a pointer
P ← L
While (P ≠ R and info(P) ≠ X)
{
P ← RPTR(P)
}
If (info(P) = X) then
{
If (P = R) then
{
LPTR(newNode) ← R
RPTR(R) ← newNode
R ← newNode
RPTR(R) ← NULL
}
Else
{
LPTR(newNode) ← P
RPTR(newNode) ← RPTR(P)
LPTR(RPTR(P) ← newNode
RPTR(P) ← newNode
}
}
Else output “Element X not found” and exit
3. [Finished] exit
2
Q: State the difference between the following:
I. Stack and Linked Stack
II. Indexed structure and Linked structure
III. Queue and Linked Queue
IV. Linked address and Computed address
Answer:
I. A stack is implemented using an array which means that it is ordered and the
elements are accessed randomly(directly) and they occupy consecutive memory
locations, this means that their physical order and logical management are the
same and the access time is also the same, overflow happens when the top pointer
is equal to the size of the array, while underflow can happens when the top pointer
points to zero.
A linked stack on the other hand is implemented using a linked list, which means
that the elements are accessed sequentially and the do NOT occupy consecutive
memory locations, which means that their physical order and logical management
are not the same and thus the access time is different, overflow can happen when
there is no memory left, When the available memory points to NULL
(i.e avail ← NULL), while underflow happens when the top pointer points to NULL
(i.e top ← NULL).
II. Indexed Structures are structures which occupy consecutive memory locations,
and their elements can be accessed randomly(directly), and the access time is the
same for its elements. Those structure have their size defined in the beginning of
the program and the cannot be changed, which means they are constant in their
size and they cannot shrink or expand. An array is an indexed structure
Linked Structure on the other hand, are structure which do NOT occupy consecutive
memory locations, which means that access to elements is sequential(starting
with the element which has a pointer pointing to it) and the access time is NOT the
same for all elements, their size can shrink and expand while executing the
program and thus it need NOT to be defined in the beginning of the program.
A linked list is a Linked Structure.
3
III. A queue is implemented using an array which means that it is ordered and the
elements are accessed randomly(directly) and they occupy consecutive memory
locations, this means that their physical order and logical management are the
same and the access time is also the same, overflow happens when the rear
pointer is equal to the size of the array, while underflow can happens when the front
pointer points to zero and also when rear = front = 0 and we try to delete an
element.
A linked queue on the other hand is implemented using a linked list which means it
is NOT ordered and elements are accessed sequentially(starting from the from the
Front pointer) and the do NOT occupy consecutive memory locations, this means
that their physical order and logical management are NOT the same and the access
time is different. Overflow can happen when there is no space left in the memory
(i.e avail ← NULL), while underflow can happen when front pointer points to NULL
(i.e Front ← NULL).
IV. Linked address is the pointer which points to our required node in a linked
structure, it can be found by searching the linked structure starting from the first
node, and thus it cannot be found directly since linked structures do NOT occupy
consecutive memory locations.we also need to know what we are searching for(i.e
we need the info node) to find the element we are looking for.
Computed address on the other hand is the memory location which points to an
element in an indexed structure, it can be computed directly by knowing the base
address(the address of the first element) and the location from the first element
from the first element.
Q: Choose a suitable answer for the following(Answers are in bold)
★ Which of the following data structure is a non linear type
a) Circular Queue
b) Queue
c) None of the above
★ The memory address of the first element in an array is called
a) The first address
b) The floor address
c) Base address
★ Which of the following data structure is an indexed data structure
a) Stack
b) List
c) Matrix
★ Which of the following data structures store homogeneous data elements
a) Pointer
b) Record
c) Array
4
★ The situation when a linked list head = NULL
a) Underflow
b) Empty list
c) Overflow
★ Primitive data permits two operations only(assignments and comparison) is
a) Integer
b) Link
c) List
★ Circularly linked list is used to
a) Change the head node
b) Reduce storage
c) Increase storage
★ When new data are to be inserted into a data structure, but there is no available
space, this situation is usually called
a) Underflow
b) Avail Flow
c) Over ow
★ Which of the following data structures can not store homogeneous data elements
a) Pointer
b) Array
c) Record
★ The index structure satisfy
a) Random access
b) Sequential access
c) Quick search
★ Overflow occurs in queue when
a) Front > Rear
b) Rear >= size of the queue
c) Front < Rear
★ D.L.L.L is better than S.L.L.L in
a) Reducing storage
b) Insertion
c) Deletion
★ To insert a new element to D.L.L.L
a) Two cases
b) Four cases
c) Three cases
★ Dequeue permits
a) Insertion from one end
b) Deletion from one end
c) Insertion from one end and deletion from another end
★ To insert a new element to a D.L.L.L, we must change
a) Three pointers
b) Four pointers
c) Five pointers
5
★ The suffix expression K B C * + H ^ F 4 / is equivalent to
a) (K + B * C) ^ H / F ^ 4
b) (K + B * C) ^ H ^ F / 4
c) (K + B) * C ^ H / F ^ 4
★ The expression 6 4 5 * + 36 4 / - equal
a) 27
b) 17
c) 25
★ The expression * 15 + 2 / 40 - 8 3 equal to
a) 250
b) 150
c) 51
★ The prefix expression ^ * + A B G / C H is equivalent to
a) ((A + B) * G ) / C ^ H
b) ((A + B) * G) ^ (C / H)
c) (((A ^ B) * G) + C) / H
★ The dynamic variables consist of two types
a) Link and pointer
b) Record and reference
c) Link and reference
★ The suffix expression A B C * + H ^ F 2 ^ /
a) (A + B + C) ^ H / F ^ 2
b) (A + B * C) ^ H ^ F / 2
c) (A + B) * C ^ H / F ^ 2