-
-
Save AahanSingh/5c09881bde29fd48d3cd1873936d2733 to your computer and use it in GitHub Desktop.
CList DeleteAtP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func DeleteAtP(head **Node, p int) { | |
if p < 1 { | |
fmt.Println("\nPositions start from 1. Cannot delete.") | |
return | |
} | |
if *head == nil { | |
fmt.Println("\nList is empty") | |
return | |
} | |
if p == 1 { | |
DeleteFirst(head) | |
return | |
} | |
i := 1 | |
var previous *Node = nil | |
current := *head | |
for i < p && current.Next != *head { | |
previous = current | |
current = current.Next | |
i++ | |
} | |
if i < p { | |
fmt.Println("Invalid position.") | |
return | |
} | |
fmt.Println("Deleting", current, "at position", p) | |
previous.Next = current.Next | |
current = nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment