-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.cpp
More file actions
72 lines (66 loc) · 1.42 KB
/
Copy pathlinkedlist.cpp
File metadata and controls
72 lines (66 loc) · 1.42 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
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
// constructor
Node(int data){
this->data=data;
this->next=NULL;
}
};
void InsertAtHead(Node* &head, int d){
// new node create
Node* temp = new Node(d);
temp->next=head;
head = temp;
}
void print (Node* &head){
Node* temp = head;
while (temp !=NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
}
void InsertAtPosition(Node* &tail, Node* &head, int position,int d ){
//insert at start
if(position==1){
InsertAtHead(head,d);
return;
}
// isert at last
if(temp->next ==Null){
insertAttail(tail,d);
return;
}
Node* temp = head;
int cnt=1;
while(cnt < position-1){
temp = temp->next;
cnt++;
}
// create a node d
Node* nodetoInsert=new Node(d);
nodetoInsert ->next=temp->next;
temp->next=nodetoInsert;
}
int main(){
//create node
Node* node1 =new Node(10);
cout<<node1-> data <<endl;
cout<<node1->next<<endl;
//head poinyed
Node* head =node1;
print(head);
InsertAtHead(head,12);
print(head);
// InsertAtHead(head,22);
// print(head);
InsertAtHead(head,32);
print(head);
InsertAtPosition(head,3,42);
print(head);
return 0;
}