-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink_list.cpp
More file actions
83 lines (83 loc) · 1.04 KB
/
Copy pathlink_list.cpp
File metadata and controls
83 lines (83 loc) · 1.04 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
#include<iostream>
using namespace std;
struct node
{
int info;
node *link;
}*n,*m,*g,*save,*ptr;
int item,choice;
void insert();
void delete_element();
void traverse();
main()
{
cout<<"Press 1 to insert element in link list\n Press 2 to delete element from link list\n press 3 to traverse the link list"<<endl;
cout<<"enter your choice:";
cin>>choice;
switch(choice)
{
case 1:insert();
break;
case 2:delete_element();
break;
case 3:traverse();
break;
}
}
void insert()
{
cout<<"Enter element you want to insert";
cin>>item;
if(n=='\0')
{
n=new node;
n->info=item;
n->link='\0';
cout<<"Element inserted:"<<n->info<<endl;
}
else
{
m=n;
g=new node;
g->info=item;
g->link='\0';
cout<<"element inserted"<<endl;
}
while(m->link!='\0')
{
m=m->link;
m->link=g;
}
}
void delete_element()
{
cout<<"Enter item you want to delete";
cin>>item;
if(item==n->info)
{
save=n;
ptr=n->link;
}
else
{
while(ptr!='\0')
{
if(item==ptr->info)
{
save->link=ptr->link;
}
else
{
save=ptr;
ptr=ptr->link;
}
}
}
}
void traverse()
{
for(m=n;m->link!='\0';m=m->link)
{
cout<<m->info<<endl;
}
}