Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Module 2 - Linear Data Structure (1)

The document provides an overview of linear data structures, focusing on arrays, stacks, and queues, including their definitions, operations, and implementations. It discusses the complexity of arrays, the Last In First Out (LIFO) principle of stacks, and the First In First Out (FIFO) principle of queues, along with their respective operations. Additionally, it covers linked lists, their types, and basic operations such as insertion, deletion, and traversal.

Uploaded by

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

Module 2 - Linear Data Structure (1)

The document provides an overview of linear data structures, focusing on arrays, stacks, and queues, including their definitions, operations, and implementations. It discusses the complexity of arrays, the Last In First Out (LIFO) principle of stacks, and the First In First Out (FIFO) principle of queues, along with their respective operations. Additionally, it covers linked lists, their types, and basic operations such as insertion, deletion, and traversal.

Uploaded by

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

CSE2001 – Data Structure and Algorithm

Module – 2 – Linear Data Structure

Dr. Mohamed Sirajudeen Yoosuf


Assistant Professor/ Registered Patent Agent (IN/PA: 3876)
School of Computer Science and Engineering
VIT-AP University, Amaravati
09.July.2022 12:00 to 12:50PM
Declaring an Array
Data Structure
Arrays
A linear data structure has data elements connected to
each other so that elements are arranged in a
sequential manner and each element is connected to
the element in front of it and behind it. This way, the
structure can be traversed in a single run.
Operations on an Array
• Initializing or Creating an Array
• Inserting a Value on Array
• Deleting a Value from Array
• Traversing the Array
• Searching a particular value in a Array
• Accessing a Value
Complexity of Array
Complexity of 2-D Array
Linear Data Structure - Stack
Linear Data Structure - Stack
Definition of Stack
• A stack is a linear Data Structure in which insertions
and deletions are allowed only at the end, called the
top of the stack.
• Last In First Out (LIFO)
Operations on a Stack
Push () ---> Pushing the data into the stack
Pop() ---> Taking the Data Out

Top() ---> Return the last inserted value


Size() ---> Returns the size of the Stack
isEmpty() ---> returns TRUE if stack is empty
isFull() ---> returns TRUE if Stack is FULL
Push ()
Pop ()
Top ()
Implementing Stack Using an Array

• Top represent the last inserted value, if the stack is


empty, top will be -1
How to implement Array in
Programming Language

• Stack can be implemented with Array or Linked List in


Programming Language.
Let us implement Push(1)

• Value 1 is pushed in Stack and top is been incremented


by 1.
Let us implement Push(1)

• Value, 2, 3, 5, 10 and 11 are pushed one after other to


the stack
Implementing multiple push

• Value, 2, 3, 5, 10 and 11 are pushed one after other to


the stack
Pseudo Code of Stack (Push
Operation)
Pseudo Code of Stack (Pop
Operation)
Application of Stack

• String Reversal
• Balanced Parenthesis
• UNDO/REDO
• Infix, Prefix and Postfix Evaluation
• Depth First Search (Module 5)
String Reversal
Balanced Parenthesis
Undo/ Redo
Infix/Postfix/Prefix Evaluation
Infix Evaluation
Infix to Postfix

Infix to Prefix

Postfix to Infix

Prefix to Infix
Infix to Prefix

1.(A+B)*(C/D)

2. A*(B*C+D*E)+F

3. (A+B)*C+(D-E)/F+G

4. + + A * B C D
Postfix to Infix

1. abc+de/*-

2. a = 2 3 + 4 5 6 – – * //find ‘a’
value

3. b = 5 3 * 9 + 6 / 8 4 / + //find ‘b’
value
Prefix to Infix

1. a = - + 5 / 10 5 5 //find ‘a’
value

2. b = + 9 * 3 / 8 4 //find ‘b’
value

3. b = 5 3 * 9 + 6 / 8 4 / + //find ‘b’
value
Queues
Stack vs Queue
Stack vs Queue
Queue

35
Queues
• Queue: a collection whose elements are added
at one end (the rear or tail of the queue) and
removed from the other end (the front or
head of the queue)
• A queue is a FIFO (first in, first out) data
structure
• Any waiting line is a queue:
– The check-out line at a grocery store
– The cars at a stop light
– An assembly line
6-36
Conceptual View of a Queue
Adding an element

Front of queue

New element is added


to the rear of the
queue

6-37
Conceptual View of a Queue
Removing an element

New front element of queue

Element is removed
from the front of the
queue
6-38
Operations on a Queue
Operation Description

dequeue Removes an element from the front of the


queue
enqueue Adds an element to the rear of the queue

first Examines the element at the front of the


queue
isEmpty Determines whether the queue is empty

size Determines the number of elements in the


queue
toString Returns a string representation of the queue
Queue (Linear Queue)
• It is a linear data structure consisting of list of items.
• In queue, data elements are added at one end, called the rear and removed from another
end, called the front of the list.
• Two basic operations are associated with queue:
7
1. “Insert” operation is used to insert an element into a queue.
2. “Delete” operation is used to delete an element from a queue. 6

FIFO list
• Example: EEE 5
Queue: AAA, BBB, CCC, DDD, EEE
1 2 3 4 5 6 7 DDD 4

0 0 CCC DDD EEE FFF GGG CCC 3

Front BBB 2
Rear
Rear AAA 1
Front

09/10/08 40
Algorithms for Insert and Delete Operations in Linear Queue

For Insert Operation


Insert-Queue(Queue, Rear, Front, N, Item)
Here, Queue is the place where to store data. Rear represents the
location in which the data element is to be inserted and Front represents
the location from which the data element is to be removed. Here N is the
maximum size of the Queue and finally, Item is the new item to be added.

1. If Rear = N then Print: Overflow and Return. /*…Queue already

filled..*/

2. Set Rear := Rear +1

3. Set Queue[Rear] := Item

4.09/10/08
Return. 41
Queues
class Queue {
int front, rear, capacity;
int queue[];
Queue(int c)
{
front = rear = 0;
capacity = c;
queue = new int[capacity];
} 6-42
Queues
// function to insert an element // at the
rear of the queue
static void queueEnqueue(int data)
{ // check queue is full or not
if (capacity == rear) {
System.out.printf("\nQueue is
full\n");
return; }
// insert element at the rear
else {
queue[rear] = data; 6-43
// function to delete an element
// from the front of the queue
static void queueDequeue()
{
// if queue is empty
if (front == rear) {
System.out.printf("\nQueue is
empty\n");
return;
}
09/10/08 Shaily Kabir, Dept. of CSE, DU
// shift all the elements from index 2 till rear
// to the right by one
else {
for (int i = 0; i < rear - 1; i++)
{
queue[i] = queue[i + 1];
}
// store 0 at rear indicating there's no
element
if (rear < capacity)
queue[rear] = 0;
// print queue elements
static void queueDisplay()
{
int i;
if (front == rear) {
System.out.printf("\nQueue
is Empty\n"); return; }
// traverse front to rear and print elements
for (i = front; i < rear; i++) {
System.out.printf(" %d <-- ",
queue[i]);
} return; }
// print front of queue
static void queueFront()
{
if (front == rear) {
System.out.printf("\nQueue
is Empty\n");
return;
}
System.out.printf("\nFront
Element is: %d", queue[front]);
return;
}}
public class StaticQueueinjava {

// Driver code
public static void main(String[] args)
{
// Create a queue of capacity 4
Queue q = new Queue(4);

09/10/08 Shaily Kabir, Dept. of CSE, DU


q.queueEnqueue(20);
q.queueEnqueue(30);
q.queueEnqueue(40);
q.queueEnqueue(50);
q.queueDequeue();
q.queueDequeue();
q.display();
}}
09/10/08 Shaily Kabir, Dept. of CSE, DU
Example: Consider the following queue (linear queue).
Rear = 4 and Front = 1 and N = 7
10 50 30 40
1 2 3 4 5 6 7
(1) Insert 20. Now Rear = 5 and Front = 1

10 50 30 40 20
1 2 3 4 5 6 7
(2) Delete Front Element. Now Rear = 5 and Front = 2

x 50 30 40 20
1 2 3 4 5 6 7
(3) Delete Front Element. Now Rear = 5 and Front = 3

x x 30 40 20
1 2 3 4 5 6 7
(4) Insert 60. Now Rear = 6 and Front = 3

x x x x 20 60 70
1 2 3 4 5 6 7
09/10/08 50
Linked lists

1. Concepts of linked lists


2. Simple linked lists
3. Circular linked lists
4. Double linked lists
5. Circular double linked lists
1. Concepts of linked list
• A linked list is a linear collection of data elements
called nodes in which linear representation is given by
links from one node to the next node.
• Similar to array, it is a linear collection of data
elements of the same type.
• Different from array, data elements of linked list are
generally not lined in consecutive memory space;
instead they are dispersed in various locations
Concepts of linked list
• Linked list is a data structure which in turn can be
used to implement other data structures. Thus, it acts
as building block to implement data structures like
stacks, queues and their variations.
• A linked list can be perceived as a train or a sequence
of nodes in which each node contains one or more
data fields and a pointer to the next node.
Element of linked list
• Linked list element (node) is user defined structure
data type, typically contains two parts
 data variables
 pointers to next elements, hold the addresses of
next elements

Example:

struct node {
int data; // data
struct node *next; // pointer to next element
};
Simple Linked List
START

1 2 3 4 5 6 7 X

• In the above linked list, every node contains two parts - one
integer and the other a pointer to the next node.
• The data part of the node which contains data may include a
simple data type, an array or a structure.
• The pointer part of the node contains a pointer to the next node
(or address of the next node in sequence).
• The last node will have no next node connected to it, so it will
store a NULL value.
• A linked list is defined by the pointer pointing to the first node,
e.g START
Linked List Operations
1. Create a node and linked list
2. Traversal, e.g. display all elements
3. Search node
4. Insert node
at beginning, at end, after/before a node
5. Delete node
at beginning, at end, after/before a node
6. Sort
How to create nodes
A node is a structure data type, can be created in
two methods, statically and dynamically.
• Static method
– use array of structures
– declared as globally outside functions
– declared locally within a function

• Dynamic method (mostly used for linked list)


– use stdlib function malloc(size) get memory space
struct node *np = (struct node*) malloc(sizeof(struct node));
How to create nodes
struct node *np = (struct node*) malloc(sizeof(struct node));

At run time, OS allocates consecutive sizeof(struct node)


bytes in the heap region,
return the address of the address of the first memory cell,

store the address to struct node type pointer np.


Need (struct node*) to cast the return address to struct
node pointer value.

Need use free(np) to release when deleting the node!!!


Otherwise cause memory leaking
Traversing Linked Lists
• We can traverse the entire linked list using a
single pointer variable called START.

• The START node contains the address of the first


node; the next part of the first node in turn
stores the address of its succeeding node.

• Using this technique the individual nodes of the


list will form a chain of nodes.

• If START = NULL, this means that the linked list is


empty and contains no nodes.
2. Singly Linked Lists
• A singly linked list is the simplest type of linked list in which
every node contains some data and a pointer to the next node
of the same data type.
START

1 2 3 4 5 6 7 X

Example:

struct node {
int data;
struct node *next;
};
Traversal Singly Linked Lists
ALGORITHM FOR TRAVERSING A LINKED LIST

Step 1:
[INITIALIZE] SET PTR = START
Step 2:
Repeat Steps 3 and 4 while PTR != NULL
Step 3:
Apply Process to PTR->DATA
Step 4:
SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: EXIT

void display(struct node *ptr) {


while(ptr != NULL) {
printf("%d ", ptr->data); // process
ptr = ptr->next;
}
}
Call as display(START);
Searching for Val 4 in Linked List

1 7 3 4 2 6 5 X

PTR

1 7 3 4 2 6 5 X

PTR

1 7 3 4 2 6 5 X

PTR

1 7 3 4 2 6 5 X

PTR
Searching a Linked List
ALGORITHM TO SEARCH A LINKED LIST
Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Step 3 while PTR != NULL
Step 3: IF VAL = PTR->DATA
SET POS = PTR
Go To Step 5
ELSE
SET PTR = PTR->NEXT
[END OF IF]
[END OF LOOP]
Step 4: SET POS = NULL // not found
Step 5: EXIT // found, output POS

struct node* search(struct node *ptr, int num) {


while((ptr != NULL) && (ptr->data != num)) {
ptr = ptr->next;
}
return ptr;
}
// call example, search(START, 4)
Inserting a Node at the Beginning
1 7 3 4 2 6 5 X

START

9 1 7 3 4 2 6 5 X

START

ALGORITHM: INSERT A NEW NODE IN THE BEGINNING OF THE LINKED LIST


Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = START
Step 6: SET START = New_Node
Step 7: EXIT

See example
Inserting a Node at the End
1 7 3 4 2 6 5 X

START, PTR

1 7 3 4 2 6 5 9 X

START

ALGORITHM TO INSERT A NEW NODE AT THE END OF THE LINKED LIST


Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 10
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = NULL
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR->NEXT != NULL
Step 8: SET PTR = PTR ->NEXT
[END OF LOOP]
Step 9: SET PTR->NEXT = New_Node
Step 10: EXIT
Inserting a Node after Node that has Value NUM
ALGORITHM TO INSERT A NEW NODE AFTER A NODE THAT HAS VALUE
NUM

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: SET PREPTR = PTR
Step 7: Repeat Steps 8 and 9 while PTR->DATA != NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR->NEXT
[END OF LOOP]
Step 10: SET New_Node->NEXT = PTR->NEXT
Step 11: SET PTR->NEXT = New_Node
Step 12: EXIT
Deleting the First Node
Algorithm to delete the first node from the linked
list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 5
[END OF IF]
Step 2: SET PTR = START
Step 3: SET START = START->NEXT
Step 4: FREE PTR
Step 5: EXIT
1 7 3 4 2 6 5 X

START

7 3 4 2 6 5 X

START
Deleting the Last Node
1 7 3 4 2 6 5 X

START, PREPTR, PTR

1 7 3 4 2 6 X 5 X

PREPTR PTR
START

ALGORITHM TO DELETE THE LAST NODE OF THE LINKED LIST


Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Steps 4 and 5 while PTR->NEXT != NULL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR->NEXT
[END OF LOOP]
Step 6: SET PREPTR->NEXT = NULL
Step 7: FREE PTR
Step 8: EXIT
Deleting the Node After a Given Node

1 7 3 4 2 6 5 X

START, PREPTR, PTR

1 7 3 4 2 6 5 X

START PREPTR PTR

1 7 3 4 2 6 5 X

START

1 7 3 4 6 5 X
Deleting the Node After a Given Node
ALGORITHM TO DELETE THE NODE AFTER A GIVEN NODE FROM THE
LINKED LIST

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 10
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PREPTR = PTR
Step 4: Repeat Step 5 and 6 while PTR->DATA != NUM
Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR->NEXT
[END OF LOOP]
Step7: SET TEMP = PTR->NEXT
Step 8: SET PTR->NEXT = TEMP->NEXT
Step 9: FREE TEMP
Step 10: EXIT
3. 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. Tail ->next= Start;

START

1 2 3 4 5 6 7
Circular Linked List
Algorithm to insert a new node in the beginning of
circular the linked list

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: Repeat Step 7 while PTR->NEXT != START
Step 7: PTR = PTR->NEXT
Step 8: SET New_Node->Next = START
Step 9: SET PTR->NEXT = New_Node
Step 10: SET START = New_Node
Step 11: EXIT
Circular Linked List

1 7 3 4 2 6 5

START, PTR

1 7 3 4 2 6 5

PTR
START

9 1 7 3 4 2 6 5

START
Circular Linked List
Algorithm to insert a new node at the end of the circular
linked list

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 10
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: Repeat Step 8 while PTR->NEXT != START
Step 7: SET PTR = PTR ->NEXT
[END OF LOOP]
Step 8: SET PTR ->NEXT = New_Node
Step 9: SET New_Node->Next = START
Step 10: EXIT
Circular Linked List

1 7 3 4 2 6 5

START, PTR

1 7 3 4 2 6 5 9

START PTR
Circular Linked List
Algorithm to insert a new node after a node that has
value NUM
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: SET PREPTR = PTR
Step 7: Repeat Step 8 and 9 while PREPTR->DATA != NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR->NEXT
[END OF LOOP]

Step 10: PREPTR->NEXT = New_Node


Step 11: SET New_Node->NEXT = PTR
Step 12: EXIT
Circular Linked List
Algorithm to delete the first node from the circular linked list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR->NEXT != START
Step 4: SET PTR = PTR->NEXT
[END OF IF]
Step 5: SET PTR->NEXT = START->NEXT
Step 6: FREE START
Step 7: SET START = PTR->NEXT
Step 8: EXIT
Circular Linked List

1 7 3 4 2 6 5
START, PTR

1 7 3 4 2 6 5

PTR
START

7 3 4 2 6 5

START
Circular Linked List
Algorithm to delete the last node of the circular linked
list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR->NEXT != START
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR->NEXT
[END OF LOOP]
Step 6: SET PREPTR->NEXT = START
Step 7: FREE PTR
Step 8: EXIT
Circular Linked List

1 7 3 4 2 6 5

START, PREPTR, PTR

1 7 3 4 2 6 5
PREPTR
PTR
START

1 7 3 4 2 6

START
Circular Linked List
Algorithm to delete the node after a given node from the circular linked list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PREPTR = PTR
Step 4: Repeat Step 5 and 6 while PREPTR->DATA != NUM
Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR->NEXT
[END OF LOOP]
Step 7: SET PREPTR->NEXT = PTR->NEXT
Step 8: FREE PTR
Step 9: EXIT
Circular Linked List

1 7 3 4 2 6 5

START, PREPTR, PTR

1 7 3 4 2 6 5

START PREPTR PTR

1 7 3 4 6 5

START
Doubly Linked List
 A doubly linked list or a two way linked list is a more complex
type of linked list which contains a pointer to the next as well
as previous node in the sequence. Therefore, it consists of
three parts and not just two. The three parts are data, a
pointer to the next node and a pointer to the previous node

START

X 3 4 5 X
1 2
Doubly Linked List
• In C language, the structure of a doubly linked list is given as,
struct node
{ struct node *prev;
int data;
struct node *next;
};

• The prev field of the first node and the next field of the last
node will contain NULL. The prev field is used to store the
address of the preceding node. This would enable to
traverse the list in the backward direction as well.
Doubly Linked List
Algorithm to insert a new node in the beginning of the
doubly linked list

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 8
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->PREV = NULL
Step 6: SET New_Node->Next = START
Step 7: SET START = New_Node
Step 8: SET New_Node->next->Prev=START
X
Step 9: EXIT X 1 7 3 4 2
START

X 9 1 7 3 4 2 X

START
Doubly Linked List
Algorithm to insert a new node after a node that has
value NUM

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: Repeat Step 8 while PTR->DATA != NUM
Step 7: SET PTR = PTR->NEXT
[END OF LOOP]
Step 8: New_Node->NEXT = PTR->NEXT
Step 9: PTR->NEXT->PREV=New_Node
Step 10: SET New_Node->PREV = PTR
Step 11: SET PTR->NEXT = New_Node
Step 12: EXIT
Doubly Linked List

X 1 7 3 4 2 X

START, PTR

X 1 7 3 4 2 X

START PTR

X 1 7 3 9 4 2 X

START
Doubly Linked List
Algorithm to delete the first node from the doubly linked
list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 6
[END OF IF]
Step 2: SET PTR = START
Step 3: SET START = START->NEXT
Step 4: SET START->PREV = NULL
Step 5: FREE PTR
Step 6: EXIT

X 1 7 3 4 2 X

START, PTR

7 3 4 2 X
Doubly Linked List
Algorithm to delete the last node of the doubly linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 7
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 and 5 while PTR->NEXT != NULL
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: SET PTR->PREV->NEXT = NULL
Step 6: FREE PTR
Step 7: EXIT
X 1 3 5 7 8 9 X
1
START, PTR

X 1 3 5 7 8 9 X
1
START PTR

X 1 3 5 7 8 X

START
Doubly Linked List
Algorithm to delete the node after a given node from the
doubly linked list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR->DATA != NUM
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: SET TEMP = PTR->NEXT
Step 6: SET PTR->NEXT = TEMP->NEXT
Step 7: SET TEMP->NEXT->PREV = PTR
Step 8: FREE TEMP
Step 9: EXIT
Doubly Linked List

X 1 3 4 7 8 9 X
1
START, PTR

X 1 3 4 7 8 9 X
1
START
PTR

X 1 3 4 8 9 X

START
Circular Doubly Linked List
• A circular doubly linked list or a circular two way linked list is a
more complex type of linked list which contains a pointer to
the next as well as previous node in the sequence.

• The difference between a doubly linked and a circular doubly


linked list is same as that exists between a singly linked list
and a circular linked list. The circular doubly linked list does
not contain NULL in the previous field of the first node and
the next field of the last node. Rather, the next field of the last
node stores the address of the first node of the list, i.e; START.
Similarly, the previous field of the first field stores the address
of the last node.
Circular Doubly Linked List
• Since a circular doubly linked list contains three parts in its
structure, it calls for more space per node and for more
expensive basic operations. However, it provides the ease to
manipulate the elements of the list as it maintains pointers to
nodes in both the directions . The main advantage of using a
circular doubly linked list is that it makes searches twice as
efficient.

START

1 1 2 3 4
Circular Doubly Linked List
Algorithm to insert a new node in the beginning of the circular doubly linked list

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 10
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET START->PREV->NEXT = new_node;
Step 6: SET New_Node->PREV = START->PREV;
Step 7: SET START->PREV= new_Node;
Step 8: SET new_node->next = START;
Step 9: SET START = New_Node
Step 10: EXIT
Circular Doubly Linked List

1 7 3 4 2

START

9 1 7 3 4 2

START
Circular Doubly Linked List
Algorithm to insert a new node at the end of the circular
doubly linked list

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = START
Step 6: SET New_Node->PREV = START->PREV
Step 6 a:SET START->PREV->NEXT=New_Node ;
6 b:START->PREV=New_Node
START
Step 7: EXIT
Circular Doubly Linked List
Algorithm to insert a new node after a node that has
value NUM

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: Repeat Step 8 while PTR->DATA != NUM
Step 7: SET PTR = PTR->NEXT
[END OF LOOP]
Step 8: New_Node->NEXT = PTR->NEXT
Step 9: SET PTR->NEXT->PREV = New_Node
Step 9: SET New_Node->PREV = PTR
Step 10: SET PTR->NEXT = New_Node
Step 11: EXIT
Circular Doubly Linked List

1 7 3 4 2
START, PTR

1 7 3 4 2

PTR

START 9
1 7 3 9 4 2

START
Circular Doubly Linked List
Algorithm to delete the first node from the circular doubly
linked list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 7
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PTR->PREV=>NEXT= PTR->NEXT
Step 4: SET PTR->NEXT->PREV = PTR->PREV
Step 5: SET START = START->NEXT
Step 6: FREE PTR
Step 7: EXIT
1 3 5 7 8 9
1
START, PTR

3 5 7 8 9
1
START
Circular Doubly Linked List
Algorithm to delete the last node of the circular doubly
linked list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START->PREV
Step 5: SET PTR->PREV->NEXT = START
Step 6: SET START->PREV = PTR->PREV
Step 7: FREE PTR
Step 8: EXIT1 3 5 7 8 9
1
START PTR

X 1 3 5 7 8

START
Circular Doubly Linked List
Algorithm to delete the node after a given node from the
circular doubly linked list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR->DATA != NUM
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: SET TEMP = PTR->NEXT
Step 6: SET PTR->NEXT = TEMP->NEXT
Step 7: SET TEMP->NEXT->PREV = PTR
Step 8: FREE TEMP
Step 9: EXIT
Circular Doubly Linked List

1 3 5 7 8 9
1
START

1 3 5 7 8 9
1
START PTR

1 3 4 8 9
START
Circular Doubly Linked List
 A header linked list is a special type of linked list which
contains a header node at the beginning of the list. So, in a
header linked list START will not point to the first node of the
list but START will contain the address of the header node.
There are basically two variants of a header linked list-

 Grounded header linked list which stores NULL in the next


field of the last node

 Circular header linked list which stores the address of the


header node in the next field of the last node. Here, the
header node will denote the end of the list.
Circular Doubly Linked List

Header Node

1 2 3 4 5 6 X

START
Header Node
1 2 3 4 5 6

START
Circular Doubly Linked List
Algorithm to traverse a Circular Header Linked List

Step 1: SET PTR = START->NEXT


Step 2: Repeat Steps 3 and 4 while PTR != START
Step 3: Apply PROCESS to PTR->DATA
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: EXIT
Circular Doubly Linked List
Algorithm to insert a new node after a given node

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 10
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET PTR = START->NEXT
Step 5: SET New_Node->DATA = VAL
Step 6: Repeat step 4 while PTR->DATA != NUM
Step 7: SET PTR = PTR->NEXT
[END OF LOOP]
Step 8: New_Node->NEXT = PTR->NEXT
Step 9: SET PTR->NEXT = New_Node
Step 10: EXIT

You might also like