-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list_main.cpp
156 lines (130 loc) · 3.27 KB
/
linked_list_main.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
#include <iostream>
#include <string>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(int val){
this->data = val;
this->next= nullptr;
}
};
class Linkedlist{
public:
Node* head;
Linkedlist(){
head= NULL;
}
bool isempty(){
return(head== nullptr);
}
void insertfirst(int value){
Node *newnode = new Node(value);
newnode->next =head;
head = newnode;
}
void display(){
Node* temp = head;
cout<< "the content is"<<endl;
while(temp!= nullptr){
cout<< temp->data<<" ";
temp = temp->next;
}
cout<<"\n";
}
bool isfound(int item){
Node* temp = head;
while(temp != nullptr){
if (temp->data == item){
return true;
break;
}
else{
temp=temp->next;
}
}
return false;
}
int countnodes(){
Node* temp= head;
int counter=0;
while(temp != nullptr){
counter++;
temp = temp->next;
}
return counter;
}
void insertlast(int value){
Node* newnode = new Node(value);
Node* temp = head;
while (temp->next != nullptr){
temp = temp->next;
}
temp->next = newnode;
}
void insertbefore(int item, int newvalue){
Node* newnode = new Node(newvalue);
Node* temp= head;
while(temp->next->data != item && temp != nullptr){
temp = temp->next;
}
newnode->next = temp->next;
temp ->next = newnode;
}
void deletitem(int item){
Node* delptr = head;
delptr = delptr->next;
Node* preptr = head;
while(delptr!= nullptr && delptr->data != item){
delptr= delptr->next;
preptr = preptr->next;
}
preptr->next = delptr->next;
delete delptr;
}
void replacenodes(int item1, int item2){
Node* temp1 = head;
Node* temp2 =head;
while (temp1->next->data != item1 && temp1!= nullptr){
temp1= temp1->next;
}
while (temp2->next->data != item2 && temp2!= nullptr){
temp2 = temp2->next;
}
temp1->next = temp2->next;
temp2->next = temp2->next->next;
}
};
int main()
{
int no_items;
int itema;
Linkedlist lst;
cout<<lst.isempty()<<endl;
cout<< "enter the no of values"<<endl;
cin>> no_items;
for (int i =0 ; i < no_items;i++){
cout<< "the item no "<< i+1 <<endl;
cin>>itema;
lst.insertfirst(itema);
}
lst.display();
cout<< "value to add at the end of list"<<endl;
cin>>itema;
lst.insertlast(itema);
lst.display();
cout<< "the value to insert in the list"<<endl;
cin>>itema;
int pos;
cout<< "the item should be after this value "<<endl;
cin>>pos;
lst.insertbefore(pos,itema);
lst.display();
int delitem;
cout<< " the item you want to delete \n";
cin>>delitem;
lst.deletitem(delitem);
lst.display();
return 0;
}