-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinklist_insert.cpp
More file actions
70 lines (53 loc) · 1021 Bytes
/
Copy pathlinklist_insert.cpp
File metadata and controls
70 lines (53 loc) · 1021 Bytes
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
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
};
void lltrav(Node *n){
while(n!=NULL){
cout<< n->data << " ";
n=n->next;
}
cout<<endl;
}
Node *insertatfirst(Node *head,int val){
Node *ptr=new Node();
ptr->data=val;
ptr->next=head;
return ptr;
}
Node *insertafternode(Node *head,int val,Node *prev){
Node *p=new Node();
p->next=prev->next;
prev->next = p;
p->data = val;
return head;
}
Node *insertatend(Node *head,int val){
Node *ptr=head;
Node *q=new Node();
while(ptr->next!=NULL){
ptr=ptr->next;
}
ptr->next=q;
q->next = NULL;
q->data=val;
return head;
}
int main(){
Node *head = new Node();
Node *sec = new Node();
Node *thi = new Node();
head->data=1;
head->next = sec;
sec->data=3;
sec->next=thi;
thi->data = 8;
thi->next = NULL;
lltrav(head);
head = insertatend(head,7);
lltrav(head);
return 0;
}