-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData Structure - Linear (LinkedList).cpp
More file actions
223 lines (199 loc) · 4.73 KB
/
Data Structure - Linear (LinkedList).cpp
File metadata and controls
223 lines (199 loc) · 4.73 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// ConsoleApplication4.cpp : Defines the entry point for the console application.
#include <iostream>
#include "stdafx.h"
using namespace std;
// Create Main Node
class Node{
public:
int data;
Node*next;
Node(){
//Initialize class properties in constractor
data = 0;
next = NULL;
};
};
//Create Linked-List Class
class LinkedList {
public:
Node*head;
LinkedList(){
head = NULL;
};
//1- Check if List is Empty
bool isEmpty(){
if (head == NULL){ return true; }
else{ return false; };
};
//2-Insertion First of LinkedList
void insertFirst(int newValue){
//If list is empty
if (isEmpty()){
Node* newNode = new Node();
newNode->data = newValue;
newNode->next = NULL;
head = newNode;
}else{
Node* newNode = new Node();
newNode->data = newValue;
newNode->next = head;
head = newNode;
};
};
// 3- Insert Before Specific Position
void insertBefore(int item, int newValue){
//Check if the list is empty
if (isEmpty()){
//if the list is empty put the new value as first node
insertFirst(newValue);
};
//Check if the node already exist or not
if (isFounded(item)){
// new node
Node* newNode = new Node();
newNode->data = newValue;
// temp pointer equal the head pointer
Node* temp = head;
// condition to stop while loop [Traversing]
while (head != NULL && temp->next->data != item){
temp = temp->next;
};
newNode->next = temp->next;
temp->next = newNode;
}else{
cout << "Item Not Found \n";
};
};
//4- Appending new Node at the last of LinkedList
void append(int newValue){
if (isEmpty()){
insertFirst(newValue);
}else{
Node* temp = head;
while (temp->next != NULL){
temp = temp->next;
};
Node* newNode = new Node();
newNode->data = newValue;
temp->next = newNode;
newNode->next = NULL;
};
};
//5- Delete node from List
int remove(int item){
//check if list is empty
if (isEmpty()){
cout << "List is Empty! \n";
};
//to save the deleted value
int deletedValue;
//check if the deleted item equal the head's data
if (head->data == item){
Node* delPtr = head;
head = head->next;
deletedValue = delPtr->data;
delete delPtr;
return deletedValue;
}else{
//delete node from list based on send item
//we have to create two pointers
//first one for pervious node
//second one for the specific item wanna delete
Node* prev = NULL;
Node* delPtr = head;
while (delPtr->data != item){
prev = delPtr;
delPtr = delPtr->next;
};
prev->next = delPtr->next;
deletedValue = delPtr->data;
delete delPtr;
return deletedValue;
};
};
//5- Dsiplay & Traversing Operation
void display(){
Node* temp = head;
//Cause i don't know number of iterations
while (temp != NULL){
cout << temp->data << " ";
temp = temp->next;
};
};
//6- Counter - Number Of Nodes
int count(){
int count = 0;
Node* temp = head;
while (temp != NULL){
count++;
temp = temp->next;
};
return count;
};
//7- Search for Some item
bool isFounded(int theKey){
bool found = false;
Node* temp = head;
while (temp != NULL){
if (temp->data == theKey){
found = true;
};
temp = temp->next;
}
return found;
};
};
int _tmain()
{
// Create Instance From LinkedList Class
LinkedList list;
if (list.isEmpty()){
cout << "List is Empty \n";
}else{
cout << "The List Contain " << list.count() << endl;
};
////////////////////////////////////////////////////////////////
int nums;
cout << "how many items you wanna add to list?";
cin >> nums;
int *arr = new int[nums];
for (int i = 0; i < nums; i++){
cout << "Enter the item number " << i+1 << " to insert in the list \n";
cin >> arr[i];
list.insertFirst(arr[i]);
list.display();
cout << endl;
};
cout << "The List Contain " << list.count() << " items" << endl;
////////////////////////////////////////////////////////////////
int item;
cout << "Enter item to search for? \n";
cin >> item;
if (list.isFounded(item)){
cout << "Item Found. \n";
}else{
cout << "Item Not Found \n";
};
////////////////////////////////////////////////////////////////
int newValue;
cout << "enter the item and new Value to insert in list \n";
cin >> item;
cin >> newValue;
list.insertBefore(item, newValue);
list.display();
////////////////////////////////////////////////////////////////
int newVal;
cout << "enter item to insert in the tail of list \n";
cin >> newVal;
list.append(newVal);
list.display();
////////////////////////////////////////////////////////////////
int delVal;
cout << "enter value to delete from list \n";
cin >> delVal;
cout << "removed value is " <<list.remove(delVal) << endl;
list.display();
////////////////////////**End of Program**//////////////////////
system("pause");
return 0;
}