-
Notifications
You must be signed in to change notification settings - Fork 0
/
addNodeEnd.c++
54 lines (42 loc) · 847 Bytes
/
addNodeEnd.c++
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
#include<iostream>
using namespace std;
class Node{
public:
int val;
Node* next;
Node(int data){
val = data;
next = NULL;
}
};
void insertAtHead(Node* &head, int val){
Node* new_node = new Node(val);
new_node->next = head;
head = new_node;
}
void insertAtTail(Node* &head, int val){
Node* new_node = new Node(val);
Node* temp = head;
while(temp->next != NULL){
temp = temp->next;
}
temp->next = new_node;
}
void display(Node* head){
Node* temp = head;
while(temp!= NULL){
cout<<temp->val<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}
int main(){
Node* head = NULL;
insertAtHead(head,2);
display(head);
insertAtHead(head,1);
display(head);
insertAtTail(head,5);
display(head);
return 0;
}