13
LINKED LIST 1

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,

Embed Size (px)

Citation preview

Page 1: 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,

1

LINKED LIST

Page 2: 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,
Page 3: 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,
Page 4: 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,
Page 5: 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,
Page 6: 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,
Page 7: 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,

Circular Linked 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.

• 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. 1 2 3 4 5 6 7

FIRST

Page 8: 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,

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 = NULL2. 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 = NULL2. 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

Page 9: 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,

9

9. [Set LINK field of last node]1. LINK (TEMP) NEW

10.[Return first node pointer]1. Return (NEW) 1 7 3 4 2 6 5

1 7 3 4 2 6 5

9 1 7 3 4 2 6 5

FIRST, TEMP

FIRST

FIRST

TEMP

Insert a new node at the beginning of circular linked list

Page 10: 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,

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 = NULL2. 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) X2. 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)

Page 11: 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,

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 = NULL2. 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) AVAIL2. AVAIL FIRST3. Return

Page 12: 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,

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 = NULL2. 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) AVAIL2. AVAIL TEMP3. Return

Page 13: 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,

THANK YOU!!ANY QUESTIONS??

13