-
Notifications
You must be signed in to change notification settings - Fork 0
/
link list using class in c++.cpp
169 lines (168 loc) · 2.04 KB
/
link list using class in c++.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include<iostream>
using namespace std;
class link
{
public:
void insert();
void menu();
void in_beg();
void del();
void show();
void in_any();
};
struct student
{
int mark;
struct student *next;
};
struct student *start=NULL;
int main()
{
link p;
while (1)
p.menu();
}
void link::menu()
{
int ch;
cout<<"\n----------- menu---------\n1. Isert\n2.In_beg\n3.Delete\n4.Show\n5.in_any\n6.Exit\n"<<endl;
cin>>ch;
switch(ch)
{
link p;
case 1:p.insert();
break;
case 2:p.in_beg();
break;
case 3:p.del();
break;
case 4:p.show();
break;
case 5:p.in_any();
break;
case 6:exit(1);
break;
default: cout<<"\n wrong input"<<endl;
}
}
void link::insert()
{
struct student *temp,*last;
temp=new student;
cin>>temp->mark;
temp->next=NULL;
if(start==NULL)
{
start=temp;
}
else
{
last=start;
while(last->next!=NULL)
{
last=last->next;
}
last->next=temp;
}
}
void link::in_beg()
{
struct student *temp,*s;
s=start;
temp=new student;
scanf("%d",&temp->mark);
temp->next=NULL;
temp->next=s;
start=temp;
}
void link::del()
{
struct student *s,*t;
s=start;
int i,pos;
if(s==NULL)
{
cout<<"\n link is empty"<<endl;
}
else
{
cout<<"\nenter the position"<<endl;
cin>>pos;
if(pos==1)
{
struct student *s,*t;
t=start->next;
s=start;
s=NULL;
start=t;
}
else
{
for(i=1;i=pos-2;i++)
{
s=s->next;
}
t=s->next;
s->next=t->next;
t=NULL;
}
}
}
void link::show()
{
struct student *st;
st=start;
if (st==NULL)
{
cout<<"\n link is empty"<<endl;
}
else
{
while(st!=NULL)
{
cout<<st->mark<<endl;
st=st->next;
}
}
}
void link::in_any()
{
int pos,i;
struct student *temp,*s,*t=NULL;
s=start;
if(start==NULL)
{
cout<<"\nlink is empty"<<endl;
}
else
{
cout<<"\nenter the postion"<<endl;
cin>>pos;
temp=new student;
cin>>temp->mark;
temp->next=NULL;
if(pos==1)
{
temp->next=s;
s=temp;
start=s;
}
else
{
for(i=1;i<=pos-2;i++)
{
while(s->next!=NULL)
{
s=s->next;
}
}
t=s->next;
s->next=temp;
temp->next=t;
if(t==NULL)
{
cout<<"\n invalid opt"<<endl;
}
}
}
}