-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink_list.cpp
124 lines (109 loc) · 1.83 KB
/
link_list.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
//linked list is sorted....,.
#include<iostream>
using namespace std;
//class node{
//
// public :
// int data;
// node *next;
// node(int data){
// this->data=data;
// this->next=NULL;
// }
//};
//this code works efficiently.....
struct node{
int data;
node *next;
node(int data){
this->data=data;
this->next=NULL;
}
};
class linked_list{
node *head;
public:
linked_list(){
head=NULL;
}
void insertion(int item){
if(head==NULL){
head=new node(item);
return;
}
node *np = new node(item);
if(item<head->data){
np->next=head;
head=np;
return;
}
node * temp=head->next;
node * prev=head;
while(temp!=NULL){
if(item<temp->data){
np->next=temp;
prev->next=np;
return;
}
prev=temp;
temp=temp->next;
}
prev->next=np;
return;
}
void deletion(int item){
if(head==NULL){
cout<<"\nunderflow condition.";
return;
}
node* prev=NULL;
node *current=head;
while(current!=NULL){
if(item==current->data && prev==NULL){
head=head->next;
return;
}
if(item<current->data){
break;
}
if(item==current->data){
node* temp = current;
prev->next=current->next;
delete temp;
return;
}
prev=current;
current=current->next;
}
cout<<"\nitem not found..";
return;
}
void display(){
node *temp=head;
if(temp==NULL){
cout<<"\nlist is empty ....";
return;
}
while(temp!=NULL){
cout<<temp->data;
temp=temp->next;
}
return;
}
};
int main(){
linked_list l1;
l1.display();
l1.insertion(2);
l1.insertion(6);
l1.insertion(1);
l1.insertion(5);
l1.insertion(4);
l1.display();
l1.deletion(1);
l1.deletion(5);
l1.deletion(3);
l1.deletion(0);
l1.display();
return 0;
}