-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingly-LL.cpp
More file actions
101 lines (75 loc) · 1.78 KB
/
Copy pathSingly-LL.cpp
File metadata and controls
101 lines (75 loc) · 1.78 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#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){
//create a new node
Node* temp = new Node(d);
temp -> next = head;
head = temp;
}
void insertAtEnd(Node* &end, int d){
Node* temp = new Node(d);
end -> next = temp;
end = end ->next;
}
void insertAtposition(Node* &end,Node* &head,int posi ,int d){
Node* temp = head;
//inserting at start
if(posi ==1){
insertAtHead(head,d);
return;
}
//inserting at other posi
int cnt=1;
while(cnt < posi-1){
temp = temp -> next;
cnt++;
}
//inserting end
if(temp -> next == NULL){
insertAtEnd(end,d);
return;
}
Node* nodeToInsert = new Node(d);
nodeToInsert -> next = temp -> next;
temp->next = nodeToInsert;
}
void printLL(Node* &head){
Node* temp = head;
while(temp !=NULL){
cout<<temp -> data<<" ";
temp = temp -> next;
}
cout<<endl;
}
int main(){
// int n ;
// cin>>n;
Node* node1 = new Node(10);
//cout<< node1 -> data<<endl;
//cout<< node1 -> next<<endl;
Node* head = node1;
Node* end = node1;
printLL(head);
// insertAtHead(head,12);
// printLL(head);
// insertAtHead(head,15);
// printLL(head);
insertAtEnd(end,12);
printLL(head);
insertAtEnd(end,15);
printLL(head);
insertAtposition(end,head,4,17);
printLL(head);
cout<<"Head "<<head -> data<<endl;
cout<<"End "<<end -> data<<endl;
return 0;
}