Professional Documents
Culture Documents
Albaath university
Faculty of Mechanical and Electrical Engineering
Department of automatic control and computers
Department of Electronic and Communication
Fourth lecture
• The prev part of the first node and the next part of the last node will always
contain null or (-1) indicating end in each direction.
• Node creation
struct node
{
struct node *prev;
int data;
struct node *next;
};
struct node *head;
• We can Allocate the space for the new node using malloc or new
instruction.
• First method : ptr = (struct node *) malloc(sizeof(struct node *));
• Second method: node* newNode = new node();
• Many operations
• Insertion.
• Deletion.
• Traversing.
• Searching.
• Insertion
• The insertion into a doubly linked list can be performed at different positions.
• Based on the position of the new node being inserted, the insertion is
categorized into the following categories.
• Insertion at the bigging of the linked list.
• Insertion at the end of the linked list.
• Insertion after specific node.
• Algorithm
• Step 1: Start
• Step 2: Create NEW_NODE = PTR
• Step 3: If PTR = NULL, Write OVERFLOW, Goto Step 10
• Step 4: Set PTR -> DATA = ITEM
• Step 5: If HEAD = NULL, Set PTR -> NEXT = NULL, Set PTR -> PREV =
NULL, Set HEAD = PTR, Goto Step 10
• Step 6: Set PTR -> PREV = NULL
• Step 7: Set PTR -> NEXT = HEAD
• Step 8: Set HEAD -> PREV = PTR
• Step 9: Set HEAD = PTR
• Step 10: End
return 0;
}
return 0;
}
• Algorithm
• Step 1: Start
• Step 2: Create NEW_NODE = PTR
• Step 3: If PTR = NULL, Write OVERFLOW, Goto Step 14.
• Step 4: Set PTR -> DATA = ITEM, Set I = 0
• Step 5: If LOC < 0, Write INVALED, TRY AGAIN, Goto Step 14.
• Step 6: Set TEMP = HEAD
• Step 7: Repeat 8 and 9 While I < LOC
• Step 8: Set TEMP = TEMP -> NEXT, Set I = I+1.
• Step 9: If TEMP = NULL, Write "LESS THAN DESIRED NO. OF ELEMENTS”, Goto Step 14
• Step 10: Set TEMP-> NEXT -> PREV = PTR
• Step 11: Set PTR-> NEXT = TEMP -> NEXT
• Step 12: Set PTR-> PREV = TEMP
• Step 13 : Set TEMP -> NEXT = PTR
• Step 14: End
• Deletion at beginning
• Deletion in doubly linked list at the beginning is the simplest operation. We
just need to copy the head pointer to pointer ptr and shift the head pointer to
its next.
• Algorithm
• Step 1: Start
• Step 2: If HEAD = NULL, Write UNDERFLOW, Goto Step 7
• Step 3: Set PTR = HEAD
• Step 4: Set HEAD = HEAD -> NEXT
• Step 5: Set HEAD -> PREV = NULL
• Step 6: Free PTR
• Step 7: End
• Deletion at beginning
• Algorithm
• Step 1: Start
• Step 2: If HEAD = NULL, Write UNDERFLOW, Goto Step 9.
• Step 3: If HEAD - > NEXT = NULL, Set HEAD = NULL, Free HEAD ,
Goto Step 9.
• Step 4: Set PTR = HEAD
• Step 5: Repeat Step 4 While PTR ->NEXT != NULL
• Step 6: Set PTR = PTR ->NEXT
• Step 7: Set PTR ->PREV-> NEXT = NULL
• Step 8: Free PTR
• Step 9: End
• For the Traversal operation the worst case time complexity is O(n).
• For the Insertion operation in general (Not the first element) the worst
case time complexity is O(n).
• For the Deletion operation the worst case time complexity is O(n).
• For the Search operation the worst case time complexity is O(n).