You are on page 1of 7

HOMEWORK NO:3

CAP204: Fundamentals of Data Structures

Part-A

1. Suppose a list is a linked list in memory consisting of


numerical values Write a procedure which find
maximum value MAX in the list.

Ans1
Procedure: max (start, ptr, mx)
Step1: ptr=start, max=[ptr]info
[initializing pointer with the address of the first node
and mx with the value in the first node]
Step2: ptr=[ptr]link
[increasing the pointer to the next node]

Step3: repeat step 4 and step5(while ptr!=NULL)


Step4: check (if [ptr]info>mx)
Then set mx=[ptr]info
Step5: ptr=[ptr]link [end of the step 3 loop]
Step6: exit
6) EXIT
2. Suppose list is linked list in a memory consisting of
numerical values .write the procedure for finding the
average mean of the values in the list.

Ans 2

3. Differentiate between link list and arrays

Ans 3

Linked list consists of data nodes, each pointing to the


next in the list. An array consists of contiguous chunks
memory of predetermined size.
Linked lists are quite flexible. They can grow to any size --
up to resource limits -- and each node can be a different
size. Memory allocation for a node is done as needed,
usually when a new node is added.

Linked lists can be circular, or doubly-linked (pointers


forward and backward), and new nodes can be inserted
anywhere in the chain.

ARRAY

Arrays are fixed in size, so resources must be anticipated


and consumed in advance. Each element is the same size
and must contain the same data type. But access to a
particular element is very fast, because its location in
memory can be determined mathematically and accessed
directly (offset from start of array = subscript * element
size).
4. Write various ways to implement stack data structure.

The operations needed for stack are mainly determined


by the operations provided by an abstract stack, namely:
¬ Stack Push ()
¬ Stack Pop ()
¬ Stack Is Empty ()
Stack push (): This is the function which is for
insertion(pushing)of an element into stack
It is similar to the insertion of an element at the end of a
single linked list see the function insert end() in the
program for operations of single linked list

1)Stack pop(): This is the function which is for


deletion(popping up) of an element from the stack. It is
similar to the deletion of an element at the end of a single
linked list
2)delete_end() in the program for operations of single
linked list

3)stack display():This is the function which is for


displaying the elements of a stack

Part-B

5. Write prefix and postfix for the following expression:


(i) ( A – B ) / ( ( D + E ) * F)

(ii) (A+B*D)+(E/F–G)
Ans 5

ANS:- postfix

{i) (we use brackets [] to denote a partial


translation)

(A-B)/((D+E) * F)= [AB-] / ([DE+] *F)

[AB-]/ [[DEF+*]]

AB- DEF +*/

(ii) (A+B*D) +(E/F-G)

= ( A+ [BD*]) +( E/ [FG-])

= [ABD*+] +(E/[FG-])

= [ABD*+]+ [EFG-/]

= ABD*+EFG-/+

PREFIX

(i) = [-AB]/ ([+DE] *F)

= [ - AB ] [*+ DEF]
= / -AB * + DEF

(ii) = ( A+ [* BD]) + ( E / [- FG])

= [+A*BD ]+ [\E-FG]

= + + A * BD \ E – FG

6. How various types of queues are represented in


memory

Ans 6

there are two types of queues .

(i) DEQUES

(ii) PRIORITY

(i) A Deque is a linear list in which elements can be


added or removed at either end but not in middle. The
Deque is a contraction of the name double ended
queue.

{ii) A priority queue is a collection of elements such


that each element has assigned a priority and such that
the order in which element are deleted and processed
comes from the following rules :

1) Higher priority is processed before any element of


lower priority.

2) Two elements with the same priority are processed


according to the order in which they were added to the
queue.
7. Write algorithm to convert infix expression to prefix
expression

Ans 7

prefix(A,B)

Suppose A is an arithmetic expression written


in infix notation. Find the equivalent prefix expression
B.

1) push “( “ on to the stack and add “ ) “ to the end


of A.

2) scan A from left to write and repeat step 3 to 6 for


each element of A until the STACK is empty:

3) if an operator is encountered then:

(a) Repeatedly pop from STACK and add to B each


operator which have same precedence as or higher
precedence than

(b) add to stack.

4) if an operand is encountered , add it to B.

5) if a left parenthesis is encountered, push it on to


stack.

[ end of if structure

6. if a right parenthsis is encountered, then:


(a) Repeatedly pop from STACK and add B each
operator until a left parenthesis is encounterd .

(b) Remove the left parenthesis.

[end of if structure]

[end of step 2 loop]

7. Exit

Infix Postfix Prefix


( (A * B) + (C / ( (A B *) (C D /) (+ (* A B) (/ C D)
D) ) +) )
((A * (B + C) ) / ( (A (B C +) *) (/ (* A (+ B C) )
D) D /) D)
(A * (B + (C / D) (A (B (C D /) +) (* A (+ B (/ C
)) *) D) ) )

8. Write algorithm to delete nth element from queue

Ans 8

1) have two ptrs. F_Ptr & S_Ptr.


2) Let the ptrs point to the START.
3) Take n as the offset.
4) Start traversing the S_Ptr after the F_Ptr after n.

suppose u want to find 5 element from the last of the list.

In the above case.

1) n = 5 // 5th element from the last


2) check atleast this many elements are there in the list
before beginging
// traverse the F_Ptr offset
for(;n!=0;n–,F_Ptr->next);
// now set the S_Ptr to trail the F_Ptr
S_Ptr = F_Ptr;
for(;F_Ptr!=NULL; S_Ptr=F_Ptr; F_Ptr=F_Ptr->next)

Now whe F_Ptr = Null, S_Ptr is just trailing behind right! it will
be pointing to the n’th element from the last.

When F_Ptr reaches end (i.e., F_Ptr is NULL)


S_Ptr points to the 5th element from the last.

You might also like