-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinklist insert middil.cpp
74 lines (61 loc) · 1.38 KB
/
linklist insert middil.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
//constructor
Node(int data){
this->data=data;
this->next=NULL;
}
};
//refrence(orignal link list bane copy not)
void insertAtHead(Node* &head, int d ) {
//new node create
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 =tail-> next;
}
void Print(Node* &head){
Node* temp =head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp = temp -> next;
}
cout<<endl;
}
void insertAtPosition(Node* & head ,int position, int d){
Node* temp = head;
int cnt =1;
while (cnt<position-1){
temp = temp->next;
cnt++;
}
// creating a note for d
Node* nodeToinsert =new Node(d);
nodeToinsert -> next =temp ->next;
temp ->next =nodeToinsert;
}
int main(){
//created a new node
Node*node1=new Node(10);
//cout<<node1->data<<endl;
//cout<<node1->next<<endl;
//head to pointed to node1
Node* head =node1;
Node* tail =node1;
Print(head);
insertAtTail(tail,12);
Print(head);
insertAtTail(tail,15);
Print(head);
insertAtPosition(head, 3, 22);
Print(head);
return 0;
}