Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create DeleteALinkedList.cpp #303

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions Cpp/DeleteALinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// C++ program to delete a linked list
#include <bits/stdc++.h>
using namespace std;

/* Link list node */
class Node {
public:
int data;
Node* next;
};

/* Function to delete the entire linked list */
void deleteList(Node** head_ref)
{

/* deref head_ref to get the real head */
Node* current = *head_ref;
Node* next = NULL;

while (current != NULL)
{
next = current->next;
free(current);
current = next;
}

/* deref head_ref to affect the real head back
in the caller. */
*head_ref = NULL;
}

/* Given a reference (pointer to pointer) to the head
of a list and an int, push a new node on the front
of the list. */
void push(Node** head_ref, int new_data)
{
/* allocate node */
Node* new_node = new Node();

/* put in the data */
new_node->data = new_data;

/* link the old list off the new node */
new_node->next = (*head_ref);

/* move the head to point to the new node */
(*head_ref) = new_node;
}

/* Driver code*/
int main()
{
/* Start with the empty list */
Node* head = NULL;

/* Use push() to construct below list
1->12->1->4->1 */
push(&head, 1);
push(&head, 4);
push(&head, 1);
push(&head, 12);
push(&head, 1);

cout << "Deleting linked list";
deleteList(&head);

cout << "\nLinked list deleted";
}

// This is code is contributed by rathbhupendra