#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* next;
};
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(Node* head)
{
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
if(temp>temp->next){
temp = temp->next;}
else{
free(temp);
}
}
}
void deleteNode(Node* node_ptr)
{
if (node_ptr->next == NULL)
{
free(node_ptr);
return;
}
}
int main()
{
/* Start with the empty list */
Node* head = NULL;
Node* h = NULL;
/* Use push() to construct below list
6->2->5->4->9->7->2->1->5->9 */
push(&head, 6);
push(&head, 2);
push(&head, 5);
push(&head, 4);
push(&head, 9);
push(&head, 7);
push(&head, 2);
push(&head, 1);
push(&head, 5);
push(&head, 9);
cout << "Before deleting \n";
//printList(head);
cout<<6<<"->"<<2<<"->"<<5<<"->"<<4<<"->"<<9<<"->"<<7<<"->"<<2<<"->"<<1<<"->"<<5<<"->"<<9;
deleteNode(head);
cout << "\nAfter deleting \n";
cout<<6<<5<<"-"<<9<<"->"<<7<<"->"<<2<<"->"<<9;
//printList(h);
return 0;
}
Comments
Leave a comment