HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
1. 6 doubly linked list
1. DATA STRUCTURES
Dr. P. Subathra
subathrakishore@yahoo.com
Professor
Dept. of Information Technology
KAMARAJ College of Engineering & Technology
(AUTONOMOUS)
Madurai
Tamil Nadu
India
2. CS8391 – DATA STRUCTURES
ONLINE CLASSES – CLASS NO. 9
08.09.2020
(02:00 PM – 03:00 PM)
8. Motivation
• Doubly linked lists are useful for playing video
and sound files with “rewind” and “instant
replay”
• They are also useful for other linked data
which require “rewind” and “fast forward” of
the data
9. Node of a Doubly Linked List
• NODE
– Data Field
– Link / Pointer Fields
• HEAD
• TAIL
• NULL Pointers
16. SN Operation Description
1 Insertion at beginning Adding the node into the linked list at beginning.
2 Insertion at end Adding the node into the linked list to the end.
3 Insertion after
specified node
Adding the node into the linked list after the specified
node.
4 Deletion at beginning Removing the node from beginning of the list
5 Deletion at the end Removing the node from end of the list.
6 Deletion of the node
having given data
Removing the node which is present just after the node
containing the given data.
7 Searching Comparing each node data with the item to be searched
and return the location of the item in the list if the item
found else return null.
8 Traversing Visiting each node of the list at least once in order to
perform some specific operation like searching, sorting,
display, etc.
Operations on a Doubly Linked List
18. EMPTY LIST
struct node * head = NULL;
struct node * head = NULL;
Operations on a Doubly Linked List
NULL
head tail
NULL
19. CREATING A LIST
• Creating the FIRST Node and Attaching it to
the List
struct node * temp = (node *) malloc (sizeof (struct node));
tempnext = NULL;
temprev=NULL;
Operations on a Doubly Linked List
Data fieldPrev Link
temp
1005
Next Link
NULL NULL
head
NULL
tail
NULL
20. CREATING A LIST
• Creating the FIRST Node and Attaching it to
the List
struct node * temp = (node *) malloc (sizeof (struct node));
tempnext = NULL;
temprev=NULL;
head = temp;
tail = temp;
Operations on a Doubly Linked List
Data fieldPrev Link
temp
1005
Next Link
NULL NULL
1005
head
1005
tail
21. CREATING A LIST
• Creating the FIRST Node and Attaching it to
the List
struct node * temp = (node *) malloc (sizeof (struct node));
tempnext = NULL;
temprev=NULL;
head = temp;
tail = temp;
tempdata=777;
Operations on a Doubly Linked List
Data fieldPrev Link
temp
1005
Next Link
NULL 777 NULL
1005
head
1005
tail
25. struct node
{
int data; // Data
node *prev; // A reference to the
//previous node
node *next; // A reference to the next
//node
};
node *head; // points to first node
node *tail; // points to first last node
head = NULL;
tail = NULL;
OPERATIONS ON A DOUBLY LINKED LIST :
CREATING A LIST (code)
34. void insertLast(int d)
{
// create new node
node *temp;
temp = new node();
temp->data = d;
temp->prev = end;
temp->next = NULL;
// if list is empty
if(end == NULL)
front = temp;
else
end->next = temp;
end = temp;
}
OPERATIONS ON A DOUBLY LINKED LIST :
INSERT LAST
37. • Insert a node New before Cur (not at front
or rear)
10 7020 55
40Head
New
Cur
New->next = Cur;
New->prev = Cur->prev;
Cur->prev = New;
(New->prev)->next = New;
Tail
OPERATIONS ON A DOUBLY LINKED LIST :
INSERT BEFORE
38. • Insert a node New after Cur (not at front or
rear)
10 7020 55
40Head
New
Cur
New->prev = Cur;
New->next = Cur->next;
Cur->next = New;
(New->next)->prev = New;
Tail
OPERATIONS ON A DOUBLY LINKED LIST :
INSERT AFTER
49. Deleting a Node
• Delete a node Cur (not at front or rear)
(Cur->prev)->next = Cur->next;
(Cur->next)->prev = Cur->prev;
delete Cur;
10 7020 5540
Head
Cur
Tail
51. void Doubly_Linked_List :: delete_node(node *n)
{
// if node to be deleted is first node of list
if(n->prev == NULL)
{
front = n->next; //the next node will be front of list
front->prev = NULL;
}
// if node to be deleted is last node of list
else if(n->next == NULL)
{
end = n->prev; // the previous node will be last of list
end->next = NULL;
}
else
{
//previous node's next will point to current node's next
n->prev->next = n->next;
//next node's prev will point to current node's prev
n->next->prev = n->prev;
}
//delete node
delete(n);
}
OPERATIONS ON A DOUBLY LINKED LIST :
DELETE MIDDLE
52. OPERATIONS ON A DOUBLY LINKED LIST :
FORWARD TRAVERSAL
void forwardTraverse()
{
node *trav;
trav = front;
while(trav != NULL)
{
cout<<trav->data<<endl;
trav = trav->next;
}
}
53. OPERATIONS ON A DOUBLY LINKED LIST :
BACKWARD TRAVERSAL
void backwardTraverse()
{
node *trav;
trav = end;
while(trav != NULL)
{
cout<<trav->data<<endl;
trav = trav->prev;
}
}