Vous êtes sur la page 1sur 5

LOVELY PROFESSIONAL UNIVERSITY

HOMEWORK: #3

Course No: CAP205 Course Title: Data Structures Batch: 2009-12


Class: MCA Term: 210111 Section: TB903
Note: Answers will be graded on correctness, efficiency, clarity, elegance & other normal criteria that determine quality.

Part-A
1. Write an algorithm to delete a given node in a two-way link list.
Ans:- Prev holds the previous element and next hold address of the next element. Head
and tail address of left and right node.
1. Loc=search(item)
Loc=loc->link
2. If(loc==NULL)
Write(“item is not found”)
Else
*pploc,*ploc,*loc
pploc=ploc=NULL
3. While(loc!=NULL)
If(loc->next==item
Break;
Else
pploc=ploc
ploc=loc
loc=loc->link
(end of if statement)
4. exit
2. Discuss the concept to insert an element as a first node in Link List And also
implement the same to insert ITEM=75 in the list where the list is as follow:
33, 39, 45, 49, 58
Ans:- Inserting an element as a first node in the list is the easiest way . To insert a new
node as a first node the following steps are as follow :-
a) create a new node from the free storage lists.
b) point the link part of the new node to the first node of the linked list.
Link(new)<-head
c)Set the HEAD pointer to point to the first new node because the new node
must precede all the nodes and can be represented as HEAD<-New.
start

33 39 45 49 58 X

start

new 33 39 45 49 58 X

75

Discuss the concept to insert an element in Link List at a given position. And
also implement the same to insert ITEM=38 in the list at the location 3 where the
list is as follow:
46, 39, 48, 49, 58
Ans:- for inserting the element at a given position in the linked. First enter the position
where user want to inserted an item . If loc==1 means no node exist insert the item at
first location. Newnode->next=start and start= newnode. And then we assign the link of
new node to the next node of the location. And the link of the node of the before location
to the new.

start
3
hold position (increment until position=3)

46 39 48 49 58 X

Start
Loc=3 38 .

46 39 48 49 58 X
Part-B
3. Discuss the concept to insert an element in Link List where the list is sorted. And
also implement that concept to insert ITEM=45 where the list is as follow:
33, 44, 55, 66, 88, 99
Ans:- In the sorted linked list new data is inserted in such a way that the ordering of the
list is to be maintained . To maintain the order the new data node may be inserted either
at the beginning or at the end. For creating or inserting a new node we need first to create
the space and then assign the data item into the INFO part. The space for a new node is
checked from the AVAIL list.
Suppose ITEM is to be inserted into a sorted list. Then ITEM must be
inserted between node lik INFO (a)<item<=INFO(b)
This find the location of node ‘a’. Traverse the list using pointer variable PTR and
comparing the ITEM with INFO[ptr]. The traversing continues as long as
INFO[ptr]>item.

Start new
45 .

33 44 55 66 88 99 X

(preptr)Save ptr
Start new
45 .

33 44 55 66 88 99 X

(preptr)Save ptr

4. Suppose a list is a linked list in memory consisting of numerical values. Write a


procedure which find maximum value MAX in the list.
Ans:- 1. Initialize MAX=0 to info part of the first
MAX<-INFO(head)
2. PTR point to second node of the linked list
PTR<-LINK(head)
3.repeat steps 4 and 5 while PTR!=NULL
4.while (INFO(PTR)>MAX) then
MAX<-INFO(ptr)
(end of loop)
5.PTR<-LINK(PTR)
(End of loop)
6.Return.

5. Suppose a list is linked list in memory consisting of numerical values. Write the
procedure for finding the average of the values in the list.
Ans1.start
2.if begin=NULL then

write "list is empty"


exit
end if
3.set pointer=begin
count=0

4.repeat while pointer!=NULL

a.process pointer->info
b. sum=sum+pointer->info
c.assign pointer=pointer->next
d. count++
end loop
5.average=sum/count
6. return average
7.end

Vous aimerez peut-être aussi