How to delete an element in a linked list ?
Example
[pastacode lang=”markdown” manual=”Input%3A%20position%20%3D%201%2C%20Linked%20List%20%3D%209-%3E2-%3E3-%3E7-%3E6%0AOutput%3A%20Linked%20List%20%3D%20%209-%3E3-%3E7-%3E6%0A%0AInput%3A%20position%20%3D%200%2C%20Linked%20List%20%3D%209-%3E2-%3E3-%3E7-%3E6%0AOutput%3A%20Linked%20List%20%3D%202-%3E3-%3E7-%3E6″ message=”” highlight=”” provider=”manual”/]
Sample code
[pastacode lang=”java” manual=”%23include%20%3Cstdio.h%3E%20%0A%23include%20%3Cstdlib.h%3E%20%0A%0A%2F%2F%20A%20linked%20list%20node%20%0Astruct%20Node%20%0A%7B%20%0A%09int%20data%3B%20%0A%09struct%20Node%20*next%3B%20%0A%7D%3B%20%0A%0A%2F*%20Given%20a%20reference%20(pointer%20to%20pointer)%20to%20the%20head%20of%20a%20list%20%0Aand%20an%20int%2C%20inserts%20a%20new%20node%20on%20the%20front%20of%20the%20list.%20*%2F%0Avoid%20push(struct%20Node**%20head_ref%2C%20int%20new_data)%20%0A%7B%20%0A%09struct%20Node*%20new_node%20%3D%20(struct%20Node*)%20malloc(sizeof(struct%20Node))%3B%20%0A%09new_node-%3Edata%20%3D%20new_data%3B%20%0A%09new_node-%3Enext%20%3D%20(*head_ref)%3B%20%0A%09(*head_ref)%20%3D%20new_node%3B%20%0A%7D%20%0A%0A%2F*%20Given%20a%20reference%20(pointer%20to%20pointer)%20to%20the%20head%20of%20a%20list%20%0Aand%20a%20position%2C%20deletes%20the%20node%20at%20the%20given%20position%20*%2F%0Avoid%20deleteNode(struct%20Node%20**head_ref%2C%20int%20position)%20%0A%7B%20%0A%2F%2F%20If%20linked%20list%20is%20empty%20%0Aif%20(*head_ref%20%3D%3D%20NULL)%20%0A%09return%3B%20%0A%0A%2F%2F%20Store%20head%20node%20%0Astruct%20Node*%20temp%20%3D%20*head_ref%3B%20%0A%0A%09%2F%2F%20If%20head%20needs%20to%20be%20removed%20%0A%09if%20(position%20%3D%3D%200)%20%0A%09%7B%20%0A%09%09*head_ref%20%3D%20temp-%3Enext%3B%20%2F%2F%20Change%20head%20%0A%09%09free(temp)%3B%09%09%09%20%2F%2F%20free%20old%20head%20%0A%09%09return%3B%20%0A%09%7D%20%0A%0A%09%2F%2F%20Find%20previous%20node%20of%20the%20node%20to%20be%20deleted%20%0A%09for%20(int%20i%3D0%3B%20temp!%3DNULL%20%26%26%20i%3Cposition-1%3B%20i%2B%2B)%20%0A%09%09temp%20%3D%20temp-%3Enext%3B%20%0A%0A%09%2F%2F%20If%20position%20is%20more%20than%20number%20of%20ndoes%20%0A%09if%20(temp%20%3D%3D%20NULL%20%7C%7C%20temp-%3Enext%20%3D%3D%20NULL)%20%0A%09%09return%3B%20%0A%0A%09%2F%2F%20Node%20temp-%3Enext%20is%20the%20node%20to%20be%20deleted%20%0A%09%2F%2F%20Store%20pointer%20to%20the%20next%20of%20node%20to%20be%20deleted%20%0A%09struct%20Node%20*next%20%3D%20temp-%3Enext-%3Enext%3B%20%0A%0A%09%2F%2F%20Unlink%20the%20node%20from%20linked%20list%20%0A%09free(temp-%3Enext)%3B%20%2F%2F%20Free%20memory%20%0A%0A%09temp-%3Enext%20%3D%20next%3B%20%2F%2F%20Unlink%20the%20deleted%20node%20from%20list%20%0A%7D%20%0A%0A%2F%2F%20This%20function%20prints%20contents%20of%20linked%20list%20starting%20from%20%0A%2F%2F%20the%20given%20node%20%0Avoid%20printList(struct%20Node%20*node)%20%0A%7B%20%0A%09while%20(node%20!%3D%20NULL)%20%0A%09%7B%20%0A%09%09printf(%22%20%25d%20%22%2C%20node-%3Edata)%3B%20%0A%09%09node%20%3D%20node-%3Enext%3B%20%0A%09%7D%20%0A%7D%20%0A%0A%2F*%20Drier%20program%20to%20test%20above%20functions*%2F%0Aint%20main()%20%0A%7B%20%0A%09%2F*%20Start%20with%20the%20empty%20list%20*%2F%0A%09struct%20Node*%20head%20%3D%20NULL%3B%20%0A%0A%09push(%26head%2C%206)%3B%20%0A%09push(%26head%2C%207)%3B%20%0A%09push(%26head%2C%203)%3B%20%0A%09push(%26head%2C%202)%3B%20%0A%09push(%26head%2C%209)%3B%20%0A%0A%09puts(%22Created%20Linked%20List%3A%20%22)%3B%20%0A%09printList(head)%3B%20%0A%09deleteNode(%26head%2C%204)%3B%20%0A%09puts(%22%5CnLinked%20List%20after%20Deletion%20at%20position%204%3A%20%22)%3B%20%0A%09printList(head)%3B%20%0A%09return%200%3B%20%0A%7D” message=”” highlight=”” provider=”manual”/]
Output:
[pastacode lang=”markdown” manual=”Created%20Linked%20List%3A%20%0A%209%20%202%20%203%20%207%20%206%20%0ALinked%20List%20after%20Deletion%20at%20position%204%3A%20%0A%209%20%202%20%203%20%207″ message=”” highlight=”” provider=”manual”/]