-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathlink list insertion operations
More file actions
117 lines (99 loc) · 2.16 KB
/
Copy pathlink list insertion operations
File metadata and controls
117 lines (99 loc) · 2.16 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node
{
int data;
struct node* next;
};
struct node*create(int data)
{
struct node *a;
a=(struct node*)malloc(sizeof(struct node));
a->next=NULL;
a->data=data;
return a;
}
struct node* insertf(struct node *p,int data)
{
struct node *a;
a=(struct node*)malloc(sizeof(struct node));
a->next=p;
a->data=data;
return a;
}
struct node*insertlast(struct node *p,int data)
{
struct node *a;
a=(struct node*)malloc(sizeof(struct node));
while(p->next!=NULL)
{
p=p->next;
}
p->next=a;
a->next=NULL;
a->data=data;
return a;
}
struct node*insertb(struct node *p,int data,int index)
{
struct node *a;
a=(struct node*)malloc(sizeof(struct node));
int i=0;
while(i!=index-1)
{
p=p->next;
i++;
}
a->next=p->next;
p->next=a;
a->data=data;
return a;
}
struct node*insertbserch(struct node *p,int data,int sea)
{
struct node *a;
a=(struct node*)malloc(sizeof(struct node));
while(p->data!=sea)
{
p=p->next;
}
a->next=p->next;
p->next=a;
a->data=data;
return a;
}
void display(struct node* p)
{
while(p!=NULL)
{
cout<<p->data<<endl;
p=p->next;
}
}
int main()
{
struct node *p1=create(12);
struct node *p2=create(13);
struct node *p3=create(14);
struct node *p4=create(15);
p1->next=p2;
p2->next=p3;
p3->next=p4;
p4->next=NULL;
cout<<"--------------- create node---------------------"<<endl;
display(p1);
cout<<"---------------insert at first ---------------------"<<endl;
struct node*p=insertf(p1,11);
display(p);
cout<<"----------------insert at last--------------------"<<endl;
struct node*p5=insertlast(p,16);
display(p);
cout<<"---------------insert at particular index---------------------"<<endl;
struct node*p6=insertb(p,17,2);
display(p);
cout<<"----------------insert at element--------------------"<<endl;
struct node*p7=insertbserch(p,19,12);
display(p);
return 0;
}