forked from bishweashwarsukla/hactoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSinglyLinkedListImplementation.cpp
58 lines (47 loc) · 1021 Bytes
/
SinglyLinkedListImplementation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream>
#include<map>
using namespace std;
class Node {
public:
int data;
Node* next;
//constructor
Node(int data) {
this -> data = data;
this -> next = NULL;
}
//destructor
~Node() {
int value = this -> data;
//memory free
if(this->next != NULL) {
delete next;
this->next = NULL;
}
cout << " memory is free for node with data " << value << endl;
}
};
void insertAtHead(Node* &head, int d) {
// new node create
Node* temp = new Node(d);
temp -> next = head;
head = temp;
}
void insertAtTail(Node* &tail, int d) {
// new node create
Node* temp = new Node(d);
tail -> next = temp;
tail = temp;
}
void print(Node* &head) {
if(head == NULL) {
cout << "List is empty "<< endl;
return ;
}
Node* temp = head;
while(temp != NULL ) {
cout << temp -> data << " ";
temp = temp -> next;
}
cout << endl;
}