|
| 1 | +// ConsoleApplication4.cpp : Defines the entry point for the console application. |
| 2 | + |
| 3 | +#include <iostream> |
| 4 | +#include "stdafx.h" |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +// Create Main Node |
| 8 | +class Node{ |
| 9 | +public: |
| 10 | +int data; |
| 11 | +Node*next; |
| 12 | + |
| 13 | +Node(){ |
| 14 | +//Initialize class properties in constractor |
| 15 | +data = 0; |
| 16 | +next = NULL; |
| 17 | +}; |
| 18 | +}; |
| 19 | + |
| 20 | +//Create Linked-List Class |
| 21 | +class LinkedList { |
| 22 | +public: |
| 23 | +Node*head; |
| 24 | +LinkedList(){ |
| 25 | +head = NULL; |
| 26 | +}; |
| 27 | + |
| 28 | + |
| 29 | +//1- Check if List is Empty |
| 30 | +bool isEmpty(){ |
| 31 | +if (head == NULL){ return true; } |
| 32 | +else{ return false; }; |
| 33 | +}; |
| 34 | + |
| 35 | + |
| 36 | +//2-Insertion First of LinkedList |
| 37 | +void insertFirst(int newValue){ |
| 38 | +//If list is empty |
| 39 | +if (isEmpty()){ |
| 40 | +Node* newNode = new Node(); |
| 41 | +newNode->data = newValue; |
| 42 | +newNode->next = NULL; |
| 43 | +head = newNode; |
| 44 | +}else{ |
| 45 | +Node* newNode = new Node(); |
| 46 | +newNode->data = newValue; |
| 47 | +newNode->next = head; |
| 48 | +head = newNode; |
| 49 | +}; |
| 50 | +}; |
| 51 | + |
| 52 | + |
| 53 | +// 3- Insert Before Specific Position |
| 54 | +void insertBefore(int item, int newValue){ |
| 55 | +//Check if the list is empty |
| 56 | +if (isEmpty()){ |
| 57 | +//if the list is empty put the new value as first node |
| 58 | +insertFirst(newValue); |
| 59 | +}; |
| 60 | +//Check if the node already exist or not |
| 61 | +if (isFounded(item)){ |
| 62 | +// new node |
| 63 | +Node* newNode = new Node(); |
| 64 | +newNode->data = newValue; |
| 65 | +// temp pointer equal the head pointer |
| 66 | +Node* temp = head; |
| 67 | +// condition to stop while loop [Traversing] |
| 68 | +while (head != NULL && temp->next->data != item){ |
| 69 | +temp = temp->next; |
| 70 | +}; |
| 71 | +newNode->next = temp->next; |
| 72 | +temp->next = newNode; |
| 73 | +}else{ |
| 74 | +cout << "Item Not Found \n"; |
| 75 | +}; |
| 76 | +}; |
| 77 | + |
| 78 | +//4- Appending new Node at the last of LinkedList |
| 79 | +void append(int newValue){ |
| 80 | +if (isEmpty()){ |
| 81 | +insertFirst(newValue); |
| 82 | +}else{ |
| 83 | +Node* temp = head; |
| 84 | +while (temp->next != NULL){ |
| 85 | +temp = temp->next; |
| 86 | +}; |
| 87 | +Node* newNode = new Node(); |
| 88 | +newNode->data = newValue; |
| 89 | +temp->next = newNode; |
| 90 | +newNode->next = NULL; |
| 91 | +}; |
| 92 | +}; |
| 93 | + |
| 94 | + |
| 95 | +//5- Delete node from List |
| 96 | +int remove(int item){ |
| 97 | +//check if list is empty |
| 98 | +if (isEmpty()){ |
| 99 | +cout << "List is Empty! \n"; |
| 100 | +}; |
| 101 | + |
| 102 | +//to save the deleted value |
| 103 | +int deletedValue; |
| 104 | + |
| 105 | +//check if the deleted item equal the head's data |
| 106 | +if (head->data == item){ |
| 107 | +Node* delPtr = head; |
| 108 | +head = head->next; |
| 109 | +deletedValue = delPtr->data; |
| 110 | +delete delPtr; |
| 111 | +return deletedValue; |
| 112 | +}else{ |
| 113 | +//delete node from list based on send item |
| 114 | +//we have to create two pointers |
| 115 | +//first one for pervious node |
| 116 | +//second one for the specific item wanna delete |
| 117 | +Node* prev = NULL; |
| 118 | +Node* delPtr = head; |
| 119 | +while (delPtr->data != item){ |
| 120 | +prev = delPtr; |
| 121 | +delPtr = delPtr->next; |
| 122 | +}; |
| 123 | +prev->next = delPtr->next; |
| 124 | +deletedValue = delPtr->data; |
| 125 | +delete delPtr; |
| 126 | +return deletedValue; |
| 127 | +}; |
| 128 | +}; |
| 129 | + |
| 130 | + |
| 131 | +//5- Dsiplay & Traversing Operation |
| 132 | +void display(){ |
| 133 | +Node* temp = head; |
| 134 | +//Cause i don't know number of iterations |
| 135 | +while (temp != NULL){ |
| 136 | +cout << temp->data << " "; |
| 137 | +temp = temp->next; |
| 138 | +}; |
| 139 | +}; |
| 140 | + |
| 141 | + |
| 142 | +//6- Counter - Number Of Nodes |
| 143 | +int count(){ |
| 144 | +int count = 0; |
| 145 | +Node* temp = head; |
| 146 | +while (temp != NULL){ |
| 147 | +count++; |
| 148 | +temp = temp->next; |
| 149 | +}; |
| 150 | +return count; |
| 151 | +}; |
| 152 | + |
| 153 | + |
| 154 | +//7- Search for Some item |
| 155 | +bool isFounded(int theKey){ |
| 156 | +bool found = false; |
| 157 | +Node* temp = head; |
| 158 | +while (temp != NULL){ |
| 159 | +if (temp->data == theKey){ |
| 160 | +found = true; |
| 161 | +}; |
| 162 | +temp = temp->next; |
| 163 | +} |
| 164 | +return found; |
| 165 | +}; |
| 166 | +}; |
| 167 | + |
| 168 | + |
| 169 | +int _tmain() |
| 170 | +{ |
| 171 | +// Create Instance From LinkedList Class |
| 172 | +LinkedList list; |
| 173 | +if (list.isEmpty()){ |
| 174 | +cout << "List is Empty \n"; |
| 175 | +}else{ |
| 176 | +cout << "The List Contain " << list.count() << endl; |
| 177 | +}; |
| 178 | +//////////////////////////////////////////////////////////////// |
| 179 | +int nums; |
| 180 | +cout << "how many items you wanna add to list?"; |
| 181 | +cin >> nums; |
| 182 | +int *arr = new int[nums]; |
| 183 | +for (int i = 0; i < nums; i++){ |
| 184 | +cout << "Enter the item number " << i+1 << " to insert in the list \n"; |
| 185 | +cin >> arr[i]; |
| 186 | +list.insertFirst(arr[i]); |
| 187 | +list.display(); |
| 188 | +cout << endl; |
| 189 | +}; |
| 190 | +cout << "The List Contain " << list.count() << " items" << endl; |
| 191 | +//////////////////////////////////////////////////////////////// |
| 192 | +int item; |
| 193 | +cout << "Enter item to search for? \n"; |
| 194 | +cin >> item; |
| 195 | +if (list.isFounded(item)){ |
| 196 | +cout << "Item Found. \n"; |
| 197 | +}else{ |
| 198 | +cout << "Item Not Found \n"; |
| 199 | +}; |
| 200 | +//////////////////////////////////////////////////////////////// |
| 201 | +int newValue; |
| 202 | +cout << "enter the item and new Value to insert in list \n"; |
| 203 | +cin >> item; |
| 204 | +cin >> newValue; |
| 205 | +list.insertBefore(item, newValue); |
| 206 | +list.display(); |
| 207 | +//////////////////////////////////////////////////////////////// |
| 208 | +int newVal; |
| 209 | +cout << "enter item to insert in the tail of list \n"; |
| 210 | +cin >> newVal; |
| 211 | +list.append(newVal); |
| 212 | +list.display(); |
| 213 | +//////////////////////////////////////////////////////////////// |
| 214 | +int delVal; |
| 215 | +cout << "enter value to delete from list \n"; |
| 216 | +cin >> delVal; |
| 217 | +cout << "removed value is " <<list.remove(delVal) << endl; |
| 218 | +list.display(); |
| 219 | +////////////////////////**End of Program**////////////////////// |
| 220 | +system("pause"); |
| 221 | +return 0; |
| 222 | +} |
| 223 | + |
0 commit comments