Docsity

DATA STRUCTURE AND ALGORITHM, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
UNIT 2 โ€” Circular linked list: implementation, Double linked list implementation, insertion,
deletion and searching operations.
STACKS and QUEUES: Operations, array and linked representations of stack, stack applications,
infix to postfix conversion, postfix expression evaluation
2.1 CIRCULAR LINKED LIST
A circular linked list is a linked list in which the last node points back to the first node instead of NULL.
Normal linked list Circular linked list
Last node โ†’ NULL Last node โ†’ first node
Traversal stops at NULL Traversal stops when current returns to HEAD
Simple one-way traversal Useful for cyclic/round-robin processing
2.1.1 Structure and Implementation
HEAD โ†’ [10|โ€ข] โ†’ [20|โ€ข] โ†’ [30|โ€ข]
โ†‘____________________|
๏‚ทEach node contains DATA and NEXT.
๏‚ทHEAD points to the first node.
๏‚ทThe last nodeโ€™s NEXT points to HEAD.
๏‚ทDuring traversal, stop when current == HEAD again.
current = HEAD
if HEAD != NULL:
do:
visit(current.data)
current = current.next
while current != HEAD
Traversal time: O(n).
Circular List Creation
Frame 1 โ€” Start
HEAD โ†’ NULL
The list is empty.
Frame 2 โ€” Insert 10
HEAD โ†’ [10|โ€ข] โ†บ
The first node points to itself.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download DATA STRUCTURE AND ALGORITHM and more Study notes Data Structures and Algorithms in PDF only on Docsity!

UNIT 2 โ€” Circular linked list: implementation, Double linked list implementation, insertion, deletion and searching operations. STACKS and QUEUES: Operations, array and linked representations of stack, stack applications, infix to postfix conversion, postfix expression evaluation 2.1 CIRCULAR LINKED LIST A circular linked list is a linked list in which the last node points back to the first node instead of NULL. Normal linked list Circular linked list Last node โ†’ NULL Last node โ†’ first node Traversal stops at NULL Traversal stops when current returns to HEAD Simple one-way traversal Useful for cyclic/round-robin processing 2.1.1 Structure and Implementation HEAD โ†’ [10|โ€ข] โ†’ [20|โ€ข] โ†’ [30|โ€ข] โ†‘____________________| ๏‚ท Each node contains DATA and NEXT. ๏‚ท HEAD points to the first node. ๏‚ท The last nodeโ€™s NEXT points to HEAD. ๏‚ท During traversal, stop when current == HEAD again. current = HEAD if HEAD != NULL: do: visit(current.data) current = current.next while current != HEAD Traversal time: O(n). Circular List Creation Frame 1 โ€” Start HEAD โ†’ NULL The list is empty. Frame 2 โ€” Insert 10 HEAD โ†’ [10|โ€ข] โ†บ The first node points to itself.

Frame 3 โ€” Insert 20 HEAD โ†’ [10] โ†’ [20] โ”€โ” โ†‘_______________โ”˜ The last node points back to HEAD. Frame 4 โ€” Insert 30 HEAD โ†’ [10] โ†’ [20] โ†’ [30] โ”€โ” โ†‘_______________________โ”˜ The circle is complete. 2.1.2 Insertion Insert at beginning ๏‚ท Create a node. If empty: new.next = new; HEAD = new. ๏‚ท Otherwise find the last node; new.next = HEAD; last.next = new; HEAD = new. ๏‚ท With a REAR pointer, this can be O(1). Insert at end ๏‚ท Create a node. ๏‚ท Set new.next = HEAD. ๏‚ท Link the old last node to new and make new the last node. ๏‚ท With only HEAD, finding the last node is O(n); with REAR, it can be O(1). Insert after a given node new.next = current.next current.next = new Once the target is known, insertion is O(1). Searching for the target may take O(n). Insert 40 after 20 Frame 1 โ€” Before

Delete last node ๏‚ท Find the node before the last node. ๏‚ท Set its NEXT to HEAD. ๏‚ท Remove the old last node. ๏‚ท For a one-node list, make the list empty. Delete a specific node ๏‚ท Search while keeping PREV and CUR. ๏‚ท Set prev.next = cur.next. ๏‚ท Update HEAD if the first node is deleted. Delete 20 Frame 1 โ€” Before 10 โ†’ 20 โ†’ 30 โ†’ back to 10 Target = 20. Frame 2 โ€” Locate PREV=10, CUR= Keep both pointers. Frame 3 โ€” Bypass 10.next = 30 10 skips 20. Frame 4 โ€” Remove 10 โ†’ 30 โ†’ back to 10 20 is detached.

2.1.4 Searching current = HEAD do: if current.data == key: FOUND current = current.next while current != HEAD NOT FOUND Time: O(n), extra space: O(1). 2.2 DOUBLY LINKED LIST A doubly linked list has PREV, DATA and NEXT in each node, so traversal can move in both directions.

Prepare the new node. Frame 3 โ€” New links 20 โ‡„ 25 โ‡„ 30 Set 25.prev and 25.next. Frame 4 โ€” Repair links 20.next=25; 30.prev= Update both neighbors. Frame 5 โ€” Result 10 โ‡„ 20 โ‡„ 25 โ‡„ 30 Both directions work. 2.2.2 Deletion Delete first/last Delete first: HEAD = HEAD.next; if HEAD != NULL: HEAD.prev = NULL; else TAIL = NULL Delete last: TAIL = TAIL.prev; if TAIL != NULL: TAIL.next = NULL; else HEAD = NULL Delete known node P if P.prev != NULL: P.prev.next = P.next else: HEAD = P.next

if P.next != NULL: P.next.prev = P.prev else: TAIL = P.prev 2.2.3 Searching Start at HEAD and follow NEXT until the key is found or NULL is reached. Search is O(n). Delete 20 from 10 โ‡„ 20 โ‡„ 30 Frame 1 โ€” Before 10 โ‡„ 20 โ‡„ 30 Target = 20. Frame 2 โ€” Left repair 10.next = 30 10 skips 20. Frame 3 โ€” Right repair 30.prev = 10 30 points back to 10. Frame 4 โ€” Result 10 โ‡„ 30 20 is detached.

isFull() Check fixed array capacity O(1) Overflow: PUSH on a full fixed-size array stack. Underflow: POP on an empty stack. PUSH 10, 20, 30 Frame 1 โ€” Empty TOP = - No elements. Frame 2 โ€” PUSH 10 TOP โ†’ 10 TOP becomes 0. Frame 3 โ€” PUSH 20 TOP โ†’ 20 10 TOP becomes 1. Frame 4 โ€” PUSH 30 TOP โ†’ 30 20 10 TOP becomes 2. POP Frame 1 โ€” Before TOP โ†’ 30 20 10 30 is at TOP. Frame 2 โ€” Read item = 30 Save/return the top item. Frame 3 โ€” Move TOP TOP โ†’ 20 10

TOP decreases. Frame 4 โ€” Result 30 removed 20 is now the top. 2.3.1 Array Representation int stack[MAX]; int top = -1; PUSH(x): if top == MAX-1 โ†’ OVERFLOW; else stack[++top] = x POP(): if top == -1 โ†’ UNDERFLOW; else return stack[top--] ๏‚ท Simple and fast; contiguous memory. ๏‚ท Basic array implementation has fixed capacity. 2.3.2 Linked Representation TOP โ†’ [30|โ€ข] โ†’ [20|โ€ข] โ†’ [10|NULL] ๏‚ท PUSH: new.next = TOP; TOP = new. ๏‚ท POP: save TOP data; TOP = TOP.next; delete old node. ๏‚ท Dynamic size, but each node needs a link. 2.3.3 Stack Applications ๏‚ท Function calls and recursion

A + (B * C - D) / E

Frame 1 โ€” 1 Output A | Stack โ€” A is an operand. Frame 2 โ€” 2 Output A | Stack + Push +. Frame 3 โ€” 3 Output A | Stack + ( Push (. Frame 4 โ€” 4 Output AB | Stack + ( B goes to output. Frame 5 โ€” 5 Output AB | Stack + ( * Push . Frame 6 โ€” 6 Output ABC | Stack + ( * C goes to output. Frame 7 โ€” 7 Output ABC | Stack + ( - Pop * before pushing -. Frame 8 โ€” 8 Output ABCD | Stack + ( - D goes to output. Frame 9 โ€” 9 Output ABCD- | Stack +

Pop until (and discard it. Frame 10 โ€” 10 Output ABCD- | Stack + / Push /. Frame 11 โ€” 11 Output ABCD-E | Stack + / E goes to output. Frame 12 โ€” 12 Output ABCD-E/+ | Stack โ€” Pop /, then +. Final postfix: ABCD-E/+ 2.5 โ€” POSTFIX EXPRESSION EVALUATION Scan left to right. Push operands. When an operator appears, pop b first, pop a second, calculate a operator b, and push the result. for each token: if operand: PUSH(token) else: b = POP(); a = POP()

2.6 โ€” REVISION

Remember Meaning Circular LAST โ†’ FIRST Doubly PREV โ‡„ NEXT Stack LIFO PUSH Insert at TOP POP Delete at TOP Infix โ†’ Postfix Operands โ†’ output; operators โ†’ stack Postfix evaluation Operand โ†’ push; operator โ†’ pop, calculate, push Common Mistakes ๏‚ท Circular last node must point to HEAD, not NULL. ๏‚ท Doubly linked list insertion/deletion requires repairing both directions. ๏‚ท POP on empty stack = underflow; PUSH on full fixed array = overflow. ๏‚ท In infix conversion, pop remaining operators at the end. ๏‚ท In postfix evaluation, the first popped operand is the RIGHT operand.