Skip to content

Commit

Permalink
Create Deleting Node in Linked List
Browse files Browse the repository at this point in the history
  • Loading branch information
Naveenkumarsahu9795 authored Oct 3, 2023
1 parent 6621625 commit 533c0e4
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions Deleting Node in Linked List
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <bits/stdc++.h>
using namespace std;

struct Node {
int number;
Node* next;
};

void Push(Node** head, int A)
{
Node* n = (Node*)malloc(sizeof(Node));
n->number = A;
n->next = *head;
*head = n;
}

void deleteN(Node** head, int position)
{
Node* temp;
Node* prev;
temp = *head;
prev = *head;
for (int i = 0; i < position; i++) {
if (i == 0 && position == 1) {
*head = (*head)->next;
free(temp);
}
else {
if (i == position - 1 && temp) {
prev->next = temp->next;
free(temp);
}
else {
prev = temp;

// Position was greater than
// number of nodes in the list
if (prev == NULL)
break;
temp = temp->next;
}
}
}
}

void printList(Node* head)
{
while (head) {
if (head->next == NULL)
cout << "[" << head->number << "] "
<< "[" << head << "]->"
<< "(nil)" << endl;
else
cout << "[" << head->number << "] "
<< "[" << head << "]->" << head->next
<< endl;
head = head->next;
}
cout << endl << endl;
}

// Driver code
int main()
{
Node* list = (Node*)malloc(sizeof(Node));
list->next = NULL;
Push(&list, 1);
Push(&list, 2);
Push(&list, 3);

printList(list);

// Delete any position from list
deleteN(&list, 1);
printList(list);
return 0;
}

0 comments on commit 533c0e4

Please sign in to comment.