0% found this document useful (0 votes)
190 views

Unit - 3 Linked List: Linear Data Structure

The document discusses linked lists and their representation in memory. A linked list is a linear data structure where each element consists of the data and a pointer to the next element. The memory for a linked list contains the data values and pointers between nodes. When a new node is added, memory is allocated from the free pool and the pointer is updated. When a node is deleted, the memory is returned to the free pool for reuse.

Uploaded by

Manan kansara
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
190 views

Unit - 3 Linked List: Linear Data Structure

The document discusses linked lists and their representation in memory. A linked list is a linear data structure where each element consists of the data and a pointer to the next element. The memory for a linked list contains the data values and pointers between nodes. When a new node is added, memory is allocated from the free pool and the pointer is updated. When a node is deleted, the memory is returned to the free pool for reuse.

Uploaded by

Manan kansara
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 59

A next B next C next D

NULL
FIRST

Unit - 3
Linked List
Linear Data Structure

Prepared By:
Ms. Purvi Tandel, Chandni Naik
Topics of Unit 3
Queue:
 Representation Of Queue
 Operations On Queue
 Circular Queue
 Priority Queue
 Array representation of Priority Queue
 Double Ended Queue
 Applications of Queue
Linked List:
 Singly Linked List
 Doubly Linked list
 Circular linked list
 Linked implementation of Stack
 Linked implementation of Queue
 Applications of linked list
Classification of Data Structure
Data
Structure

Non-Linear
Linear Data
Data
Structure
Structure

Sequential Linked
Hierarchical Network
Organizatio Organizatio
Relationship Relationship
n n

Examples: Array Linked List Trees Graphs


Stack
Queue
Advantage and Disadvantage of Arrays

Advantages: Disadvantages:
• Array is very simple to use and easy • Array is static in nature. Memory is
to create. allocated at compile time.

• No memory management needed. • Most likely, either it has not enough


or too much memory.

• We can access any element with • Insertion and deletion of element is


the help of index directly. more complicated compared to
linked list.
• Arrays are quick to loop.
Linked list
• In simple terms, Linked list is a linear collection of data elements.
• A list is a collection of items in which each item is linked with other & order is
given by specified link from one item to other.
• Each item is denoted by node which consists of 2 fields.
1) The value of the node
2) Other containing the address of the next node in the list. The last node
in the list contain NULL pointer to indicate that it is the end of the list.
A 1000 B 2050 C 3335 D
5000 1000 2050 3335
Last Node
A linked List
• It is possible for a list to have no nodes at all, such a list is called empty list.
Advantage and Disadvantage of Linked list

Advantages: Disadvantages:
• It is a dynamic data structure therefore • Lots of overhead in code (lots of
the size of the linked list can grow or malloc calls & assigning pointers).
shrink in size during execution of the
programme.
• Must traverse entire list to go to the
nth node.
• Programmer need not to worry about
how many element will be stored in the
linked list. • It consume extra space to contain
the address of the next item in the
list, it is time consuming also.
• Each element is allocated with memory
as it is added to the list.
Pros & Cons of Linked Allocation (operation-wise)
Pros
 Insertion Operation
• we have an n elements in list and it is required to insert a new element between the
first and second element, what to do with sequential allocation & linked allocation?
• Insertion operation is more efficient in Linked allocation.

A 1000 B 2050 C 3335 D


5000 1000 2050 3335

A 2100 B 2050 C 3335 D


5000 1000 2050 3335

X 1000
2100
Continue..
 Deletion Operation
• Deletion operation is more efficient in Linked Allocation

A 1000 B 2050 C 3335 D


5000 1000 2050 3335

A 2050 B 2050 C 3335 D


5000 1000 2050 3335
 Join Operation
• Join operation is more efficient in Linked Allocation.

A 1000 B 2050 C 3335 D 5050


5000 1000 2050 3335

12 580 52 5096 15 5145 100


5050 580 5096 5145
 Split Operation
• Split operation is more efficient in Linked Allocation

A 1000 B 2050 C 3335 D


5000 1000 2050 3335

A 1000 B 2050 C 3335 D


5000 1000 2050 3335
Cons
 Search Operation
• If particular node in the list is required, it is necessary to follow links
from the first node onwards until the desired node is found, in this
situation it is more time consuming to go through linked list than a
sequential list.
• Search operation is more time consuming in Linked Allocation.
• Linked list require more memory compared to array because along with
value it stores pointer to next node.
Linked lists are among the simplest and most common data structures. They can
be used to implement other data structures like stacks, queues, and symbolic
expressions, etc…
Representation of Linked List in Memory
S1 200 S2 300 S3 500 S4 700 S5 800 S6 1000 S7 NULL
100 200 300 500 700 800 1000
FIRST Memory Representation
• When we delete a node from a linked list, then operating system Address Roll No Next
changes the status of the memory occupied by it from memory FIRST 100 S1 200
available.
200 S2 300
• The computer maintain a list of all the free memory cells.
• The list of available space is called the free pool. 300 S3 500
• Linked List has pointer variable FIRST which store the address of the first AVAIL 400
node of the list. Likewise, for the free pool (which is a linked list of all 500 S4 700
the free memory cells), we have a pointer variable AVAIL which store
600 900
the address of the first free space.
• When new student’s record has to be added, the memory address 700 S5 800
pointed by AVAIL will be taken and used to store information. After the 800 S6 1000
insertion next available free space address will be stored in AVAIL. 900 -1(NULL)
• If we delete node or whole linked list, Space occupied by it must be
given back to the free pool so memory can be reused by some other 1000 S7 -1(NULL)
program.
S1 200 S2 300 S3 500 S4 700 S5 800 S6 1000 S7 NULL
400
100 200 300 500 700 800 1000

FIRST S8 NULL
Example: Suppose, I want to store S8 then memory address Memory Representation 400
with 400 is used and AVAIL points to next free block cell. Address Roll No Next
• The OS does this task of adding the free memory to the
FIRST 100 S1 200
free pool.
200 S2 300
• This task performed by OS when it finds the CPU idle or
the program are falling short of memory space. 300 S3 500
• The OS scans through all the memory cells & marks those AVAIL 400 S8 -1(NULL)
600
cells that are being used by some other program. 500 S4 700
• Then, it collects all the cells which are not being used & AVAIL 600 900
adds their address to free pool, this process is called 700 S5 800
garbage collection. 800 S6 1000
900 -1(NULL)
1000 S7 400
-1(NULL)
Operations & Type of Linked List
Operations on Linked List Types of Linked List
 Insert  Singly Linked List
• Insert at first position  Circular Linked List
• Insert at last position  Doubly Linked List
• Insert into ordered list
 Delete
 Traverse list (Print list)
 Copy linked list
Singly Linked List

A next B next C next D


NULL
FIRST

• It is basic type of linked list.


• Each node contains data and pointer to next node.
• Last node’s pointer is null.
• First node address is available with pointer variable FIRST.
• Limitation of singly linked list is we can traverse only in one
direction, forward direction.
Node Structure of Singly List
Info Link
Typical Accessing Part

Node
of Node
Node Info (Node)
Link (Node)
Data Pointer to
Next Node

struct node
C Structure {
to represent int info;
struct node *link;
a node };
Algorithms for singly linked list
1. Insert at first position
2. Insert at last position
3. Insert in Ordered Linked list
4. Delete Element
5. Copy Linked List
Availability Stack
• A pool or list of free nodes, which we refer to as the availability stack is maintained in
conjunction with linked allocation.
• Whenever a node is to be inserted in a list, a free node is taken from the availability
stack and linked to the new list.
• On other end, the deleted node from the list is added to the availability stack.

NEW Check for free node in Availability Stack


AVAIL IF AVAIL is NULL
AVAIL
THEN Write(‘Availability Stack Underflow’)
Return

Obtain Address of next free node


NEW  AVAIL

Remove free node from Availability Stack


AVAIL  LINK(AVAIL)
Function: INSERT( X,First)
• This function inserts a new node at the first position of Singly linked
list.
• This function returns address of FIRST node.
• X is a new element to be inserted.
• FIRST is a pointer to the first element of a Singly linked linear list.
Initially , FIRST= NULL (if Linked List is empty) .
• Typical node contains INFO and LINK fields.
• AVAIL is a pointer to the top element of the availability stack.
• NEW is a temporary pointer variable.
Function: INSERT(X,FIRST) 1. [Underflow?]
IF AVAIL = NULL
Availability Stack Then Write
FIRST=INSERT(X, FIRST) AVAIL (“Availability Stack
200 100 Underflow”)
INSERT(10, NULL) NEW
INSERT(20, 100) Return(FIRST)
AVAIL 300 200 2. [Obtain address of next
INSERT(30, 200) NEW
INSERT(40, 300) free Node]
INSERT(50, 400) AVAIL 300
NEW  AVAIL
400 3. [Remove free node from
NEW
NEW 50 400 availability Stack]
AVAIL
500 NEW
500 400 AVAIL  LINK(AVAIL)
4. [Initialize fields of
AVAIL Null 500
new node and
NEW 40 300 NEW its link to the list]
INFO(NEW)  X
400 LINK (NEW)  FIRST
5. [Return address of new
NEW 30 200 NEW 20 100 NEW 10 NULL node]
Return (NEW)
300 200 100
Function: INSEND(X, FIRST)
• This function inserts a new node at the last position of linked list.
• This function returns address of FIRST node.
• X is a new element to be inserted.
• FIRST is a pointer to the first element of a Singly linked linear list.
• Typical node contains INFO and LINK fields.
• AVAIL is a pointer to the top element of the availability stack.
• NEW is a temporary pointer variable.
Function: INSEND(X, First) Availability Stack

1. [Underflow?] 7. [Search for end of list] AVAIL 100


200
IF AVAIL = NULL Repeat while LINK (SAVE) ≠ NEW
Then Write (“Availability NULL
Stack Underflow”) SAVE  LINK (SAVE) AVAIL 300 200
Return(FIRST) NEW
8. [Set link field of last node
2. [Obtain address of to NEW] AVAIL 300
400
next free Node] LINK (SAVE)  NEW NEW
NEW  AVAIL 9. [Return first node pointer]
Return (FIRST) AVAIL 400
500
3. [Remove free node from NEW
availability Stack] FIRST=INSEND(10, NULL)
AVAIL  LINK(AVAIL) FIRST=INSEND(20, 100) AVAIL Null 500
FIRST=INSEND(30, 100) NEW
4. [Initialize fields of FIRST=INSEND(40, 100)
new node] FIRST=INSEND(50, 100)
INFO(NEW)  X FIRST=INSEND(60, 100)
LINK (NEW)  NULL NEW NEW NEW NEW
5. [Is the list empty?]
If FIRST = NULL
Then Return (NEW)
6. [Initialize search for NEW 10 200
NULL 20 NULL
300 400
30 NULL 40 NULL
500 50 NULL
a last node] 100 200 300 400 500
SAVE FIRST
SAVE SAVE SAVE SAVE
Function: INSORD(X, FIRST)
• This function inserts a new node such that linked list preserves the
ordering of the terms in increasing order of their INFO field.
• This function returns address of FIRST node.
• X is a new element to be inserted.
• FIRST is a pointer to the first element of a Singly linked linear list.
• Typical node contains INFO and LINK fields.
• AVAIL is a pointer to the top element of the availability stack.
• NEW is a temporary pointer variable.
Predecessor
5 10 15 20 25 30

FIRST
22
NEW
Function: INSORD(X, FIRST)
1. [Underflow?] 6. [Does the new node precede
IF AVAIL = NULL all other node in the list?]
THEN Write (“Availability IF INFO(NEW) ≤ INFO (FIRST)
Stack Underflow”) THEN LINK (NEW)  FIRST
Return(FIRST) Return (NEW)
2. [Obtain address of 7. [Initialize temporary pointer]
next free Node] SAVE  FIRST
NEW  AVAIL 8. [Search for predecessor of
3. [Remove free node from new node]
availability Stack] Repeat while LINK (SAVE) ≠ NULL
AVAIL  LINK(AVAIL) & INFO(NEW) ≥ INFO(LINK(SAVE))
4. [Initialize fields of SAVE  LINK (SAVE)
new node] 9. [Set link field of NEW node
INFO(NEW)  X and its predecessor]
5. [Is the list empty?] LINK (NEW)  LINK (SAVE)
IF FIRST = NULL LINK (SAVE)  NEW
THEN LINK(NEW)  NULL 10. [Return first node pointer]
Return (NEW) Return (FIRST)
Function: INSORD(X, FIRST) 1. [Underflow?]
IF AVAIL = NULL
6. [Does the new node precede
all other node in the list?]
FIRST = INSORD(X, FIRST) THEN Write (“Availability IF INFO(NEW) ≤ INFO (FIRST)
FIRST=INSORD(3, 100) Availability Stack Stack Underflow”) THEN LINK (NEW)  FIRST
AVAIL Return(FIRST) Return (NEW)
200 800 2. [Obtain address of 7. [Initialize temporary pointer]
NEW
next free Node] SAVE  FIRST
AVAIL NEW  AVAIL 8. [Search for predecessor of
300 200 3. [Remove free node from new node]
availability Stack] Repeat while LINK (SAVE) ≠ NULL
AVAIL  LINK(AVAIL) & INFO(NEW) ≥ INFO(LINK(SAVE))
Null 300 4. [Initialize fields of SAVE  LINK (SAVE)
new node] 9. [Set link field of NEW node
INFO(NEW)  X and its predecessor]
5. [Is the list empty?] LINK (NEW)  LINK (SAVE)
IF FIRST = NULL LINK (SAVE)  NEW
THEN LINK(NEW)  NULL 10. [Return first node pointer]
Return (NEW) Return (FIRST)

NEW

3 100 5 400 10 500 15 600 20 700 25 900 30


800 100 400 500 600 700 900
FIRST
Function: INSORD(X, FIRST) 1. [Underflow?]
IF AVAIL = NULL
6. [Does the new node precede
all other node in the list?]
FIRST = INSORD(X, FIRST) THEN Write (“Availability IF INFO(NEW) ≤ INFO (FIRST)
INSORD(22, 800) Availability Stack Stack Underflow”) THEN LINK (NEW)  FIRST
AVAIL Return(FIRST) Return (NEW)
300 200 2. [Obtain address of 7. [Initialize temporary pointer]
NEW
next free Node] SAVE  FIRST
NEW  AVAIL 8. [Search for predecessor of
AVAIL Null 300 3. [Remove free node from new node]
availability Stack] Repeat while LINK (SAVE) ≠ NULL
AVAIL  LINK(AVAIL) & INFO(NEW) ≥ INFO(LINK(SAVE))
4. [Initialize fields of SAVE  LINK (SAVE)
new node] 9. [Set link field of NEW node
INFO(NEW)  X and its predecessor]
5. [Is the list empty?] LINK (NEW)  LINK (SAVE)
IF FIRST = NULL LINK (SAVE)  NEW
THEN LINK(NEW)  NULL 10. [Return first node pointer]
Return (NEW) Return (FIRST)
SAVE SAVE SAVE SAVE SAVE

3 100 5 400 10 500 15 600 200


20 700 25 900 30
800 100 400 500 600 700 900
FIRST
22 700
NEW 200
Function: INSORD(X, FIRST) 1. [Underflow?]
IF AVAIL = NULL
6. [Does the new node precede
all other node in the list?]
FIRST = INSORD(X, FIRST) THEN Write (“Availability IF INFO(NEW) ≤ INFO (FIRST)
INSORD(32, 800) Availability Stack Stack Underflow”) THEN LINK (NEW)  FIRST
AVAIL Return(FIRST) Return (NEW)
Null 300 2. [Obtain address of 7. [Initialize temporary pointer]
NEW
next free Node] SAVE  FIRST
NEW  AVAIL 8. [Search for predecessor of
AVAIL = NULL 3. [Remove free node from new node]
availability Stack] Repeat while LINK (SAVE) ≠ NULL
AVAIL  LINK(AVAIL) & INFO(NEW) ≥ INFO(LINK(SAVE))
4. [Initialize fields of SAVE  LINK (SAVE)
new node] 9. [Set link field of NEW node
INFO(NEW)  X and its predecessor]
5. [Is the list empty?] LINK (NEW)  LINK (SAVE)
IF FIRST = NULL LINK (SAVE)  NEW
THEN LINK(NEW)  NULL 10. [Return first node pointer]
Return (NEW) Return (FIRST)
SAVE SAVE SAVE SAVE SAVE SAVE SAVE SAVE

3 100 5 400 10 500 15 600 20 200 22 700 25 900 30 300


800 100 400 500 600 200 700 900
FIRST
32
NEW 300
Procedure: DELETE( X, FIRST)
• This algorithm delete a node whose address is given by variable X.
• FIRST is a pointer to the first element of a Singly linked linear list.
• Typical node contains INFO and LINK fields.
• SAVE & PRED are temporary pointer variable.
PRED SAVE

5 10 15 20 25 30

FIRST
Procedure: DELETE( X, FIRST)
1. [Is Empty list?] 6. [End of the list?]
IF FIRST = NULL If SAVE ≠ X
THEN write (‘Underflow’) THEN write (‘Node not found’)
Return Return

2. [Initialize search for X] 7. [Delete X]


SAVE  FIRST If X = FIRST
THEN FIRST  LINK(FIRST)
3. [Find X] ELSE LINK (PRED)  LINK (X)
Repeat thru step-5
while SAVE ≠ X and 8. [Free Deleted Node]
LINK (SAVE) ≠ NULL Free (X)

4. [Update predecessor marker]


PRED  SAVE
5. [Move to next node]
SAVE  LINK(SAVE)
Function: DELETE(X,FIRST) 1. [Is Empty list?]
IF FIRST = NULL
4. [Update predecessor marker]
PRED  SAVE
FIRST=DELETE(X, FIRST) THENwrite (‘Underflow’) 5. [Move to next node]
Return SAVE  LINK(SAVE)
DELETE(800, 800)
2. [Initialize search for X] 6. [End of the list?]
SAVE  FIRST If SAVE ≠ X
Availability Stack
THENwrite (‘Node not found’)
3. [Find X] Return
Repeat thru step-5
100 800 while SAVE ≠ X and 7. [Delete X]
LINK (SAVE) ≠ NULL If X = FIRST
THENFIRST  LINK(FIRST)
AVAIL 100 ELSELINK (PRED)  LINK (X)
200
8. [Free Deleted Node]
Null 200 Free (X)

SAVE

3 100 5 400 10 500 15 600 20 200 22 700 25 900 30


800 100 400 500 600 200 700 900
FIRST
Function: DELETE(X,FIRST) 1. [Is Empty list?]
IF FIRST = NULL
4. [Update predecessor marker]
PRED  SAVE
FIRST=DELETE(X, FIRST) THENwrite (‘Underflow’) 5. [Move to next node]
Return SAVE  LINK(SAVE)
DELETE(800, 800)
Availability Stack 2. [Initialize search for X] 6. [End of the list?]
DELETE(500, 100) SAVE  FIRST If SAVE ≠ X
800 500 THENwrite (‘Node not found’)
3. [Find X] Return
Repeat thru step-5
AVAIL 100 800 while SAVE ≠ X and 7. [Delete X]
LINK (SAVE) ≠ NULL If X = FIRST
THENFIRST  LINK(FIRST)
200 100 ELSELINK (PRED)  LINK (X)

8. [Free Deleted Node]


Null 200 Free (X)

SAVE PRED SAVE PRED SAVE

5 400 10 600
500 15 600 20 200 22 700 25 900 30
100 400 500 600 200 700 900
FIRST
Function: TRAVERSE(FIRST) 1. [Empty List?]
IF FIRST = NULL
Then Write (“LINKED
LIST EMPTY”)
Return
10 500 15 600 20 700 25 900 30 2. [store the address of
400 500 600 700 900 first Node]
FIRST temp temp temp temp  FIRST
temp temp
3. [Repeat while loop ]
Repeat while temp!=NULL
print(INFO(temp))
10 15 20 25 30
temp  LINK (temp)

NULL
Function: COUNT_NODES(FIRST)
• This function counts number of nodes of the linked list and returns
COUNT.
• FIRST is a pointer to the first element of a Singly linked linear list.
• Typical node contains INFO and LINK fields.
• SAVE is a Temporary pointer variable.
Function: COUNT_NODES(FIRST)
1. [Is list Empty?]
IF FIRST = NULL
Then COUNT  0
Return(COUNT)

2. [Initialize loop for a last node to update count]


SAVE FIRST

3. [Go for end of list]


Repeat while LINK (SAVE) ≠ NULL
SAVE  LINK (SAVE)
COUNT  COUNT + 1

4. [Return Count]
Return (COUNT)
Function: COPY (FIRST)
• This function Copy a Link List and creates new Linked List
• This function returns address of first node of newly created linked list.
• The new list is to contain nodes whose information and pointer fields
are denoted by FIELD and PTR, respectively.
• The address of the first node in the newly created list is to be placed
in BEGIN
• FIRST is a pointer to the first element of a Singly linked linear list.
• Typical node contains INFO and LINK fields.
• AVAIL is a pointer to the top element of the availability stack.
• NEW, SAVE and PRED are temporary pointer variables.
Function: COPY (FIRST) 6. [Copy Node]
IF AVAIL = NULL
1. [Is Empty list?]
IF FIRST = NULL
FIRST SAVE SAVE SAVE SAVE THEN write THEN Return(NULL)
(‘Underflow’) 2. [Copy first node]
5 600 10 700 15 800 30 Return (NULL) IF AVAIL = NULL
ELSE NEW  AVAIL THEN write (‘Underflow’)
500 600 700 800
AVAIL  Return (NULL)
BEGIN PRED LINK(AVAIL) ELSE NEWAVAIL
PRED PRED
AVAILLINK(AVAIL)
FIELD(NEW)INFO(SAVE)
5 300 15 500 30 NULL PTR(PRED)NEW FIELD(NEW)INFO(FIRST)
10 400
BEGIN NEW
NEW 200 NEW 300 NEW 400 NEW 500 7. [Set link of last3. [Initialize Traversal]
node and SAVE  FIRST
return] 4. [Move the next node if
PTR(NEW)  NULL not at
AVAIL Return(BEGIN) the end if list]
300 200
NEW Repeat thru step 6
while LINK(SAVE) ≠ NULL
AVAIL 300 5. [Update predecessor and
400 save pointer]
NEW
Availability Stack PREDNEW
AVAIL 400 SAVELINK(SAVE)
500
NEW

AVAIL Null 500


NEW
Circularly Linked Linear List
• Circular linked list is a sequence of element in which every element
has link to its next element in the sequence & the last element has a
link to the first element in the sequence.
LAST

5 10 15 20 25 30

FIRST

Advantages of Circular List


• In circular list, every node is accessible from given node. From given node all
nodes can be reached by chaining through the list.
• It saves time when we have to go to the last node from the first node. It can be
done in single step because there is no need to traverse the in between nodes.
Circularly Linked Linear List
• If we replace NULL pointer of the last node of Singly Linked Linear List with the
address of its first node, that list becomes circularly linked linear list or Circular List.
• FIRST is the address of first node of Circular List
• LAST is the address of the last node of Circular List
• Disadvantages of Circular List
• It is not easy to reverse the linked list
• If proper care is not taken, then the problem of infinite loop can occur
• If we at a node and go back to the previous node, then we can not do it in single
step. Instead we have to complete the entire circle by going through the in
between nodes and then we will reach the required node.
LAST

5 10 15 20 25 30

FIRST
Circularly Linked Linear List Cont…
• Applications of Circular List
• When we are surfing the net, we can use the back button & the forward button
to move to the previous pages that we have already visited. A circular linked list
is used to maintain the sequence of web page visited. Traversing this circular
linked list either in forward or backward direction helps to revisit the pages again
using back & forward buttons.
Operations on Circular List
• Insert at First
• Insert at Last
• Delete a node from front
Procedure: CINSERT( X,FIRST)
• This procedure inserts a new node at the first position of Circular linked
list.
• X is a new element to be inserted.
• FIRST is a pointer to the first elements of a Circular linked linear list.
• Typical node contains INFO and LINK fields.
• NEW and PTR is a temporary pointer variable.
1. [UnderFlow?]
CINSERT( X,FIRST) IF AVAIL = NULL
THEN write (‘ Availability stack
FIRST=INSERT(X, FIRST) AVAIL 300 200 Underflow’)
NEW Return(FIRST)
INSERT(10, NULL) 2. [Obtain address of next free node ]
INSERT(20, 200) AVAIL 300 NEWAVAIL
400
INSERT(30, 300) NEW 3. [Remove free node from availability
Availability Stack
stack]
AVAIL 400 AVAILLINK(AVAIL)
500
NEW 4. [Initialize Fields of new node]
INFO(NEW) X
AVAIL Null 500 5. [Check for the empty list]
if FIRST = NULL
THEN
FIRST PTR FIRST PTR FIRST NEW
FIRST
LINK(NEW) FIRST
Return NEW
6. [Set PTR]
30 300 20 200 10 300
200
400 PTRFIRST
7. [Check for the last node]
400 300 200 While LINK(PTR)!= FIRST
NEW NEW NEW PTR  LINK(PTR)
8. [Add new node at front]
LINK(NEW) FIRST
LINK(PTR)  NEW
FIRST  NEW
return FIRST
Procedure: C_END_INSERT( X, FIRST)
• This procedure inserts a new node at the last position of Circular
linked list.
• X is a new element to be inserted.
• FIRST is a pointer to the first element of a Circular linked linear list.
• Typical node contains INFO and LINK fields.
• NEW & PTR are temporary pointer variable.
1. [Underflow?]
C_END_INSERT( X, FIRST) Availability Stack
If AVAIL = NULL
Then write(“Availability stack
FIRST=C_END_INSERT(X, FIRST) AVAIL 300 200 underflow”)
INSERT(10, NULL) NEW Return(FIRST)
INSERT(20, 200) 2. [Obtain address of next free node]
AVAIL NEW  AVAIL
INSERT(30, 300) 400 300
NEW 3. [Remove free node from availability
stack]
AVAIL 400 AVAIL  LINK(AVAIL)
500
NEW 4. [Initialize fields of new node]
INFO(NEW)  X
AVAIL Null 500 5. [Check for the empty list]
If FIRST = NULL

FIRST PTR PTR Then FIRST  NEW


LINK(NEW)  FIRST
Return NEW
10 200
300 20 200
400 6. [Set link pointer to new node]
30 200 LINK(NEW)  FIRST
200 300 400 7. [Set PTR]
PTR  FIRST
NEW NEW NEW 8. [Check for last node]
while LINK(PTR) != FIRST
PTR  LINK(PTR)
9. [Add new node at the end]
LINK(PTR) = NEW
Return(FIRST)
Procedure: CDELETE(FIRST)
• This algorithm delete a node from the front.
• FIRST is pointer to the First element of a Circular linked list.
• Typical node contains INFO and LINK fields.
• NEW & PTR are temporary pointer variable.
C_DELETE(FIRST) 1. [Check for empty list]
If FIRST = NULL
FIRST=C_DELETE(FIRST) Then write(“List is empty”)
DELETE(100) Return(FIRST)
2. [Check for only one node in
list]
If FIRST = LINK(FIRST)
Then Return NULL
3. [Set temp variable]
temp  FIRST
S1 200 S2 300 S3 500 S4 200
100 4. [Check for last node]
100 while LINK(temp) != FIRST
200 300 500
Then temp  LINK(temp)
temp FIRST temp FIRST temp temp 5. [Set pointer]
FIRST  LINK(FIRST)
LINK(temp)  FIRST
6. [Return the first node
pointer]
Return(FIRST)
Doubly Linked Linear List
• In certain applications, it is very desirable that a list be traversed in
either forward or reverse direction.
• This property implies that each node must contain two link fields
instead of usual one.
• The links are used to denote Predecessor and Successor of node.
• The link denoting its predecessor is called Left Link.
• The link denoting its successor is called Right Link.
• A list containing this type of node is called doubly linked list or two
way chain.
Doubly Linked Linear List
• Typical node of doubly linked linear list contains INFO, LPTR RPTR Fields
• LPTR is pointer variable pointing to Predecessor of a node
• RPTR is pointer variable pointing to Successor of a node
• Left most node of doubly linked linear list is called L, LPTR of node L is always
NULL
• Right most node of doubly linked linear list is called R, RPTR of node R is
always NULL

LPTR INFO RPTR

Typical node of L R
Doubly Linked List Doubly linked linear list
Doubly Linked Linear List
• Doubly linked list is a sequence of elements in which every element has
links to its previous and next element in the sequence.

Advantages:
• We can traverse in both direction i.e. from starting to end & from end to
starting as well.
• It is easy to reverse the linked list
• If we are at a node, then we can go to any node (which is not possible to
reach the previous node in singly linked list)
Disadvantages:
• It requires more space per node because one extra field is require for
pointer for previous node.
• Still elements are accessed sequentially
Doubly Linked Linear List
Applications:
• To implement tree
• Browser cache which allows you to hit the back button
• Undo/redo functionality in word
• A music player which has next & previous buttons
• Represent deck of cards in a game
• In many OS, the thread scheduler maintains a doubly linked list
Procedure: DOUBINS (L,R,M,X)
• This algorithm inserts a new node in doubly linked linear list.
• The insertion is to be performed to the left of a specific node with
its address given by the pointer variable M
• Typical node of doubly linked list contains following fields LPTR,
RPTR and INFO
• LPTR is pointer variable pointing to Predecessor of a node
• RPTR is pointer variable pointing to Successor of a node
• L & R are pointer variables pointing for Leftmost and Rightmost
node of Linked List.
• NEW is the address of New Node
• X is value to be inserted
Procedure: DOUBINS (L,R,M,X)
4. [Left-most insertion?]
1. [Obtain new node from availability
If M = L
stack]
Then LPTR(NEW)  NULL
NEW  NODE
RPTR(NEW)  M
LPTR(M)  NEW
2. [Copy information field]
L  NEW
INFO(NEW)  X
Return
3. [Insertion into an empty list?]
5. [Insert in middle]
If R = NULL
LPTR(NEW)  LPTR(M)
then LPTR(NEW)  RPTR(NEW)  NULL
RPTR(NEW)  M
L  R  NEW
LPTR(M)  NEW
Return
RPTR(LPTR(NEW))  NEW
Return
1. [Obtain new node from availability
DOUBINS (L,R,M,X) stack]
NEW  NODE
Insertion into an empty list: Function call
2. [Copy information field]
DOUBINS(NULL,NULL,NULL,10) INFO(NEW)  X

3. [Insertion into an empty list?]


NULL 10 NULL If R = NULL
then LPTR(NEW)  RPTR(NEW)  NULL
500 L  R  NEW
Return
NEW L R
4. [Left-most insertion?]
If M = L
[Left-most insertion]: Function Call Then LPTR(NEW)  NULL
DOUBINS(500,700,500,50) RPTR(NEW)  M
LPTR(M)  NEW
L  NEW
500 Return
NULL 50 200 10 600 500 20 700 600 30
200 500 600 5. [Insert in middle]
700
LPTR(NEW)  LPTR(M)
NEW L RPTR(NEW)  M
L M R LPTR(M)  NEW
RPTR(LPTR(NEW))  NEW
Return
1. [Obtain new node from availability
DOUBINS (L,R,M,X) stack]
NEW  NODE
[Insert in middle]: Function Call
2. [Copy information field]
DOUBINS(500,700,600,35) INFO(NEW)  X

3. [Insertion into an empty list?]


10 600
100 500
100 20 700 600 30 If R = NULL
500 600 then LPTR(NEW)  RPTR(NEW)  NULL
700 L  R  NEW
M Return
L R 4. [Left-most insertion?]
If M = L
Then LPTR(NEW)  NULL
500 35 600 RPTR(NEW)  M
LPTR(M)  NEW
100 L  NEW
NEW Return

5. [Insert in middle]
LPTR(NEW)  LPTR(M)
RPTR(NEW)  M
LPTR(M)  NEW
RPTR(LPTR(NEW))  NEW
Return
PROCEDURE: DOUBDEL (L, R,
OLD)
• This algorithm deletes the node whose address is contained in the
variable OLD
• Typical node of doubly linked list contains following fields LPTR, RPTR
and INFO
• LPTR is pointer variable pointing to Predecessor of a node
• RPTR is pointer variable pointing to Successor of a node
• L & R are pointer variables pointing for Leftmost and Rightmost node
of Linked List.
Procedure: DOUBDEL (L, R,
OLD)
1. [Underflow ?]
IF R = NULL
THEN write (‘UNDERFLOW’)
return
2. [Delete node]
IF L = R (single node in list)
THEN L  R  NULL
ELSE IF OLD = L (left most node)
THEN L  RPTR(L)
LPTR (L)  NULL
ELSE IF OLD = R (right most)
THEN R  LPTR (R)
RPTR (R)  NULL
ELSE RPTR(LPTR (OLD))  RPTR (OLD)
LPTR(RPTR (OLD))  LPTR (OLD)
3. [Return deleted node]
Restore(OLD)
Return
1. [Underflow ?]
DOUBDEL (L, R, OLD) IF R = NULL
THEN write (‘UNDERFLOW’)
DOUBDEL (L, R, OLD) return
Case 1: Single node in the list 2. [Delete node]
IF L = R (single node in list)
DOUBDEL(500,500,500) THEN L  R  NULL
ELSE IF OLD = L (left most node)
THEN L  RPTR(L)
LPTR (L)  NULL
NULL 10 NULL ELSE IF OLD = R (right most)
L = R = 500 THEN R  LPTR (R)
500
RPTR (R)  NULL
L R L = R = NULL ELSE RPTR(LPTR (OLD))  RPTR (OLD)
LPTR(RPTR (OLD))  LPTR (OLD)
Case 2: Leftmost node in the list 3. [Return deleted node]
Restore(OLD)
DOUBDEL(200,700,200) Return

NULL 50 500 200 10 600 500 20 700 600 30


200 500 600 700
L
R
DOUBDEL (L, R, OLD)
DOUBDEL (L, R, OLD)
Case 3: Rightmost node in the list
DOUBDEL(200,700,700)
NULL 50 500 200 10 600 500 20 700 600 30
200 500 600 1. [Underflow ?]
700
IF R = NULL
L THEN write (‘UNDERFLOW’)
R return
2. [Delete node]
IF L = R (single node in list)
Case 4: Middle node in the list THEN L  R  NULL
DOUBDEL(500,700,600) ELSE IF OLD = L (left most node)
THEN L  RPTR(L)
LPTR (L)  NULL
10 ELSE IF OLD = R (right most)
600
700 500 20 700 500 30
600
THEN R  LPTR (R)
500 600 700 RPTR (R)  NULL
ELSE RPTR(LPTR (OLD))  RPTR (OLD)
L LPTR(RPTR (OLD))  LPTR (OLD)
R 3. [Return deleted node]
Restore(OLD)
Return
Thank you

You might also like