Presentation is loading. Please wait.

Presentation is loading. Please wait.

1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,

Similar presentations


Presentation on theme: "1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,"— Presentation transcript:

1 1

2

3

4

5

6

7 Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list, the last node contains a pointer to the first node of the list. We can have a circular singly listed list as well as circular doubly linked list. We can have a circular singly listed list as well as circular doubly linked list. While traversing a circular linked list, we can begin at any node and traverse the list in any direction forward or backward until we reach the same node where we had started. While traversing a circular linked list, we can begin at any node and traverse the list in any direction forward or backward until we reach the same node where we had started. Thus, a circular linked list has no beginning and no ending. Thus, a circular linked list has no beginning and no ending. 1234567 FIRST

8 Insert a new node at the beginning of circular linked list Function INSERTBEG (X, FIRST) : Given X, a new element, and FIRST, a pointer to the first element of a circular linked list whose typical node contains INFO and LINK fields, this functions inserts X. AVAIL is a pointer to the top element of the availability stack, NEW and TEMP are temporary pointer variables. It is required that X precedes the node whose address is given by FIRST. 1. [Underflow?] 1. If AVAIL = NULL 2. then Write (‘STACK UNDERFLOW’) 1. Return (FIRST) 2. [Obtain address of next free node] 1. NEXT  AVAIL 3. [Remove free node from availability stack] 1. AVAIL  LINK (AVAIL) 4. [Initialize fields of new node] 1. INFO (NEW)  X 5.[Is list empty?] 1.If FIRST = NULL 2.Then LINK(NEW)  NEW 1.Return (NEW) 6.[Search for the first node] 1.TEMP  FIRST 7.[Search for the end of the list] 1.Repeat while LINK(TEMP) != FIRST 1.TEMP  LINK(TEMP) 8.[Set LINK field of new node] 1.LINK (NEW)  FIRST

9 9. [Set LINK field of last node] 1. LINK (TEMP)  NEW 10. [Return first node pointer] 1. Return (NEW) 9 17342651734265 9173426 5 FIRST, TEMP FIRST TEMP Insert a new node at the beginning of circular linked list

10 Insert a new node at the end of circular linked list Function INSERTEND (X, FIRST) : Given X, a new element, and FIRST, a pointer to the first element of a circular linked list whose typical node contains INFO and LINK fields, this functions inserts X. AVAIL is a pointer to the top element of the availability stack, NEW and TEMP are temporary pointer variables. It is required that X be inserted at the end of the list. 1. [Underflow?] 1. If AVAIL = NULL 2. then Write (‘STACK UNDERFLOW’) 1. Return (FIRST) 2. [Obtain address of next free node] 1. NEXT  AVAIL 3. [Remove free node from availability stack] 1. AVAIL  LINK (AVAIL) 4. [Initialize fields of new node] 1. INFO (NEW)  X 2. LINK (NEW)  FIRST 5.[Is list empty?] 1.If FIRST = NULL 1.Return (NEW) 6.[Search for the last node] 1.TEMP  FIRST 7.[Search for the end of the list] 1.Repeat while LINK(TEMP) != FIRST 1.TEMP  LINK(TEMP) 8.[Set LINK field of new node] 1.LINK (TEMP)  NEW 9.[Return FIRST node pointer] 1.Return (FIRST)

11 Delete a node from the beginning of circular linked list Procedure DELETEBEG (FIRST) : Given FIRST, a pointer to the first element of a circular linked list whose typical node contains INFO and LINK fields, this procedure deletes the node from the beginning of the circular linked list. TEMP is used to find the desired node. 1. [Empty list?] 1. If FIRST = NULL 2. then Write (‘UNDERFLOW’) 1. Return 2. [Initialize search for first node] 1. TEMP  FIRST 3. [Search for the end of list] 1. Repeat while LINK (TEMP) != FIRST 1. TEMP  LINK (TEMP) 4. [Delete the first node] 1. LINK (TEMP)  LINK (FIRST) 5.[Make next node to be first?] 1.If FIRST  LINK ( TEMP) 6.[Return node to availability stack] 1.LINK (FIRST)  AVAIL 2.AVAIL  FIRST 3.Return

12 Delete a node from the end of circular linked list Procedure DELETEEND (FIRST) : Given FIRST, a pointer to the first element of a circular linked list whose typical node contains INFO and LINK fields, this procedure deletes the node from the end of the circular linked list. TEMP is used to find the desired node. PRED keeps track of predecessor of TEMP. 1. [Empty list?] 1. If FIRST = NULL 2. then Write (‘UNDERFLOW’) 1. Return 2. [Initialize search for first node] 1. TEMP  FIRST 3. [Find last node] 1. Repeat thru Step 5 while LINK (TEMP) != FIRST 4. [Update predecessor marker] 1. PRED  TEMP 5.[Move to next node] 1.TEMP  LINK ( TEMP) 6.[Delete last node] 1.LINK (PRED)  FIRST 7.[Return node to availability stack] 1.LINK (TEMP)  AVAIL 2.AVAIL  TEMP 3.Return

13 ANY QUESTIONS?? 13


Download ppt "1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,"

Similar presentations


Ads by Google