-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionInSinglyLL.cpp
More file actions
51 lines (46 loc) · 973 Bytes
/
Copy pathinsertionInSinglyLL.cpp
File metadata and controls
51 lines (46 loc) · 973 Bytes
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
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
// constructor
Node(int data)
{
this->data = data;
this->next = NULL;
}
};
void insertionAtHead(Node *&head, int d)
{
// new temp node
Node *temp = new Node(d);
temp->next = head;
// head pointing back to temp node.
head = temp;
}
void print(Node *&head)
{
// temp node pointing back to the head as it is the starting node and needs to be addressed again in this function as this is a diff scope
Node *temp = head;
while (temp != NULL)
{
cout << temp->data << endl;
temp = temp->next;
}
cout << endl;
}
int main()
{
Node *node1 = new Node(10);
// cout << node1->data << endl;
// cout << node1->next << endl;
// head pointer to node1
Node *head = node1;
insertionAtHead(head, 20);
print(head);
insertionAtHead(head, 15);
print(head);
return 0;
}