-
Notifications
You must be signed in to change notification settings - Fork 1
/
linklist.cpp
162 lines (152 loc) · 2.66 KB
/
linklist.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
#include<iostream>
using namespace std;
class node{
public:
node *next;
int value;
};
class linklist{
node *head;
node *tail;
public:
linklist(){ // constructor
head=NULL;
tail=NULL;
}
void insert(int data){ // insert value in linklist
node *ptr=new node;
ptr->value=data;
ptr->next=NULL;
if(head==NULL){
head=ptr;
tail=ptr;
}
else{
tail->next=ptr;
tail=ptr;
}
}
void search(int dat){
node *ptr;
int counter=1;
ptr=head;
while(ptr!=NULL){
if(ptr->value==dat){
cout<<"The searched value is found at node "<<counter<<endl;
}
else{
counter++;
}
ptr=ptr->next;
}
}
void del(char val){
node *prev,*forward;
prev=head;
forward=head->next;
if(prev->value==val){
head=forward;
delete(prev);
}
else{
while(forward->value!=val){
prev=prev->next;
forward=forward->next;
}
if(forward->next==NULL){
prev->next=NULL;
delete(forward);
}
else{
prev->next=forward->next;
delete(forward);
}
}
}
void display(){
node *ptr;
ptr=head;
while(ptr!=NULL){
cout<<ptr->value<<endl;
ptr=ptr->next;
}
}
bool edit(char d,char v){
node *ptr;
ptr=head;
while(ptr!=NULL){
if(ptr->value==d){
ptr->value=v;
return true;
}
ptr=ptr->next;
}
return false;
}
void sort(){
node *i,*j;
for(i=head;i!=NULL;i=i->next){
for(j=i->next;j!=NULL;j=j->next){
if(i->value>j->value){
swap(i->value,j->value);
}
}
}
}
};
//good
int main(){
int data,i=0,x,input,insertvalue,deletevalue,editvalue,searchvalue,toedit;
linklist ll;
while(true){
cout<<"following are Functions \n 1.Insert \n2.search \n3.edit \n4.delete \n5.display"<<endl;
cin>>input;
switch(input){
case 1:{
cout<<"Enter the value to insert"<<endl;
cin>>insertvalue;
ll.insert(insertvalue);
ll.sort();
cout<<"value has been inserted"<<endl;
break;
}
case 2:{
cout<<"Enter the value to search"<<endl;
cin>>searchvalue;
ll.search(searchvalue);
break;
}
case 3:{
cout<<"enter the value to edit"<<endl;
cin>>editvalue;
cout<<"enter the value to enter"<<endl;
cin>>toedit;
x=ll.edit(editvalue,toedit);
ll.sort();
if(x==1){
cout<<"value has been edited "<<endl;
}
else{
cout<<"given value is not found "<<endl;
}
break;
}
case 4:{
cout<<"enter the value to delete"<<endl;
cin>>deletevalue;
ll.del(deletevalue);
cout<<"value has been deleted"<<endl;
break;
}
case 5:{
ll.display();
break;
}
default:{
cout<<"wrong input"<<endl;
break;
}
}
}
return 0;
}