You are on page 1of 12

(ii) Circular Linked List :

The Circular Linked list is the collection


of nodes , each node will consists of the two
parts , data part and the address part , the data
part will store the value and the address part will
store the address of the next node or first node if
there is no next node .

The logical structure of each node is,

So, the Circular linked list containing


the following elements ,

5,10,15 will look like,


(A) Procedure to create the Circular linked list

Procedure
CREATELIST(INFO,LINK,HEAD,VAL)
[ The procedure CREATELIST is used to
create the
Circular linked list.INFO is the array
containing the
information part and the LINK is the
array
containing the address of the next node or
NULL.
HEAD will contain the address of the first
node
or NULL if there is no node . VAL is
the
value for which the node is to be created.
]
Step 1: Set PTR:=HEAD.
Step 2: If PTR=NULL then :
[Linked list is empty,allocate
memory to TEMP]
(a) Set INFO(TEMP):=VAL.
(b) Set LINK(TEMP):=TEMP.
(c) Set HEAD:=TEMP.
Else :
[Linked list already contain some
node,traverse
till the last node is reached.]

(a) Repeat while


LINK(PTR)!=HEAD do:
(b) Set PTR:=LINK(PTR).
[End of the while loop]
[Allocate memory to the TEMP.]
(c) Set INFO(TEMP):=VAL.
(d) Set LINK(TEMP):=HEAD.
(e) Set LINK(PTR):=TEMP.
[End of the If structure]
Step 3: Return.
Output :

1) Linked List is empty .

2) VAL=5

PTR=HEAD=NULL.

PTR=NULL

NULL=NULL (Condition is true)

3) val=10
(B) Procedure to print the Circular linked list

Procedure
PRINTLIST(INFO,LINK,HEAD)
[Procedure to print the Circular linked list.]

Step 1: Set PTR:=HEAD.


Step 2:Repeat Step 3 to 4 while
LINK(PTR)!=HEAD do:
Step 3:Write INFO(PTR).
Step 4: Set PTR:=LINK(PTR).
[End of while loop]
Step 5:Write INFO(PTR).
Step 6:Return.

(C) Procedure to count the number of nodes


present in the linked list.
Procedure
COUNTNODES(INFO,LINK,HEAD)
[Procedure to count the number of nodes
present in the linked list.]

Step 1: Set PTR:=HEAD.


Step 2: Set COUNT:=0.
Step 3:Repeat Steps 4 to 5 while
LINK(PTR)!=HEAD do:
Step 4: Set COUNT:=COUNT+1.
Step 5: Set PTR:=LINK(PTR).
[End of while loop]
Step 6:If PTR!=NULL then :
Set COUNT:=COUNT+1.
[End of If Structure]
Step 8:Write COUNT.
Step 9: Return

Q Procedure to insert the new node before the


first node
Procedure
INSERTFIRST(INFO,LINK,HEAD,VAL)
[The procedure INSERTFIRST is used for
inserting a newnode before the first node]

Step 1: Set PTR:=HEAD.


[Allocate memory to TEMP]
Step 2: Set INFO[TEMP]:=VAL.
Step 3: Set LINK[TEMP]:=PTR.
Step 4: Repeat Step 5 to 6 while
LINK(PTR)!=HEAD do:
Step 5: Set PTR := LINK(PTR).
[End of Loop]
Step 6: Set LINK(PTR) :=TEMP.
Step 7: Set HEAD :=TEMP.
Step 5: Return.

Q. Procedure to insert a new node after the last


node .

Procedure
INSERTLAST(PTR,LINK,HEAD,VAL)
[Procedure INSERTLAST is used to insert a
new node to the last node]
Step 1: Set PTR:=HEAD.
Step 2: Repeat Steps 3 to 3 while
LINK(PTR)!=HEAD do :
Step 3: Set PTR:=LINK(PTR).
[End of the while loop]
[Allocate memory to TEMP]
Step 4: Set INFO(TEMP):=VAL.
Step 5: Set LINK(TEMP):=HEAD.
Step 6: Set LINK(PTR):=TEMP.
Step 7: Return.

Q. Procedure to delete the last node.

Procedure DELETELAST(PTR,LINK,HEAD)
[Procedure DELETELAST is used to to delete
the last node]
Step 1: Set PTR:=HEAD.
Step 2: Repeat Steps 3 to 4 while
LINK(PTR)!=HEAD do :
Step 3: Set PREVIOUS:=PTR.
Step 4: Set PTR:=LINK(PTR).
[End of the while loop]
[Allocate memory to TEMP]
Step 5: Set TEMP:=PTR.
Step 6: Set LINK(PREVIOUS):=HEAD.
Step 7: FREE(TEMP).
Step 8: Return.

Q. Procedure to delete a first node from the


linked list .
Procedure
DELETEFIRST(INFO,LINK,HEAD)
[Procedure to delete the first node from the
linked list]

Step 1: Set PTR:=HEAD.


Step 2: Set TEMP:=PTR.
Step 3: Set TEMP2:=LINK(PTR).
Step 4: Repeat Steps 5 to 5 while
LINK(PTR)!=HEAD do:
Step 5: Set PTR:=LINK(PTR).
[End of while loop]
Step 6: Set LINK(PTR):=TEMP2.
Step 7: Set HEAD:=TEMP2.
Step 8: call FREE(TEMP).
Step 9: Return.

You might also like