-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedList.cpp
55 lines (45 loc) · 975 Bytes
/
linkedList.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
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(int data){
this -> data = data;
this -> next = NULL;
}
};
void insertAtHead(Node* &head, int d){
//new node
Node* temp = new Node(d);
temp -> next = head;
head = temp;
}
void insertAtTail(Node* &tail, int d){
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;
}
int main(){
//creating node
Node* node1 = new Node(10);
//cout << node1 -> data << endl;
Node* head = node1;
Node* tail = node1;
insertAtHead(head, 12);
insertAtTail(tail, 20);
print(head);
return 0;
}