Skip to content

Commit 31c7dfd

Browse files
authoredJan 27, 2023
Insertion in Linked List
1 parent 0a669ff commit 31c7dfd

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
 

‎linkedList.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Node{
5+
public:
6+
int data;
7+
Node* next;
8+
9+
Node(int data){
10+
this -> data = data;
11+
this -> next = NULL;
12+
}
13+
};
14+
15+
void insertAtHead(Node* &head, int d){
16+
//new node
17+
Node* temp = new Node(d);
18+
temp -> next = head;
19+
head = temp;
20+
}
21+
22+
void insertAtTail(Node* &tail, int d){
23+
Node* temp = new Node(d);
24+
tail -> next = temp;
25+
tail = temp;
26+
}
27+
28+
void print(Node* &head){
29+
if(head == NULL){
30+
cout << "List is empty" << endl;
31+
return ;
32+
}
33+
Node* temp = head;
34+
35+
while(temp != NULL){
36+
cout << temp ->data << " ";
37+
temp = temp ->next;
38+
}
39+
cout << endl;
40+
}
41+
42+
int main(){
43+
//creating node
44+
Node* node1 = new Node(10);
45+
//cout << node1 -> data << endl;
46+
Node* head = node1;
47+
Node* tail = node1;
48+
49+
insertAtHead(head, 12);
50+
insertAtTail(tail, 20);
51+
52+
print(head);
53+
54+
return 0;
55+
}

0 commit comments

Comments
 (0)