---------------------------------------------------- Pointers ---------------------------------------------------- | This uses a pointer to loop through the chars | in a char array (string). | 'text[80]' is the char array. | '*textPtr' is the pointer. | 'textPtr++' is pointer incrementation, which | just means it advances to the next memory location | which in this case will be the next array element | which will be the next char in the string. It does | not increase by byte, but rather by the size of the | data type. ----------------------------------------------------- ***************************************************** #include using namespace std; void main(){ char text[80]; char *textPtr = NULL; cout << "Please enter a string: "; cin >> text; textPtr = text; while (*textPtr){ cout << *textPtr << endl; textPtr++; } } ******************************************************** -------------------------------------------------------- Structures -------------------------------------------------------- | This creates a structure called Point(x/y coord point) | then initializes a new 'Point', sets its values, then | displays the values. -------------------------------------------------------- ******************************************************** #include using namespace std; struct Point { int x; int y; }; void main(){ Point pt1; pt1.x = 1; pt1.y = 2; cout << "(" << pt1.x << ", " << pt1.y << ")" << endl; } ******************************************************** -------------------------------------------------------- Linked Lists -------------------------------------------------------- | This code creates a structure named 'Employee' and | initializes a new instance of it, sets its values. By | setting its '*next' value to NULL is like ending the | list. This code creats a linked list that has only one | item in it. | Use '->' like '.' -------------------------------------------------------- ******************************************************** #include using namespace std; #define STRING_LENGTH 32 struct Employee { char firstName[STRING_LENGTH]; char lastName[STRING_LENGTH]; Employee *next; }; void main(){ Employee *employee = NULL; employee = new Employee; strcpy(employee->firstName, "Jonathan"); strcpy(employee->lastName, "Brenner"); employee->next = NULL; cout << employee->firstName << " " << employee->lastName << endl; } ******************************************************** -------------------------------------------------------- OK the big one -------------------------------------------------------- Alright, this code has a lot to it. First, 'struct Employee': Think of this as an object with properties or a very small class with only variables. I think of the '*next' variable as a 'doorway' to a new room that contains the four values. Van Gorp used the metaphor of a train, as in the structure is a 'boxcar' with the values in it, and '*next' as the 'hook' that two cars are connected by. Whatever works for yuh. Next, the 'insert' function: - The '*empList' parameter, passed as a pointer, is basically the linked list, it points to the location of the first item in the list, so its the 'front door' or the 'engine car'. That is why its the 'current node' because it starts with the first item in the list. (you could call them items instead of nodes as well, Gorp used nodes). - The '*node' parameter is the item or node to be inserted into the list, so think of the function as insert(list_to_add_node_to, node_to_add_to_list) - The nodes are NULL until assigned a value, so in 'if' statments, empty 'items' will = 'false' - The 'if' block after the 'while' loop is just to determine where to insert the item in the list these can be confusion, but know that they are completely optional. They are use in this code to sort the list as items are added to it. - After the item has been entered, the function returns the 'updated' list or rather. Next, the 'findEmployee' function: - The '*empList' parameter is the list to search and the 'parNumber' is the employee number that the user entered to search for. - '*curNode' is, again, the equiv of the first item in the list. So anytime you see that line 'Datatype *someVar = linkedList;' you can think of it as 'starting from the first item..." [if thats confusing completely ignore it] - 'if (!found)' if the current item's 'number' value does not match the value being searched then continue through the list to the next item By setting the current node to the current node's 'next' value, you are changing which item the program is looking at. So that line of code is like: stop looking at this item(curNode) but rather look at the item after(curNode->next) - If the employee is found, then the function returns the curNode, but since its returned as a pointer, its really returning the location of the item in the list, and then the program uses that info to go find the values later. - 'display' just simply starts from the first item in the list and spits out all the values for each item. Now for 'main()': - 'empNumber' will just be the holding variable for the number the user will search. - fstream requires you to include - inFile(file_name, IO_type) - BUF_SIZE can be any value you want - buffer is just an array that will hold each char from the file. (Name it whatever you want) - '*empPtr' is just like '*curNode' - The !inFile just means if the file stream is unavailable (ie: file's not open, no read/write permissions, ect) - The do loop creates a new item, sets the values using the vals from the file, then by setting its next val to NULL, it means that empPtr is only a single item and not a list. Then since insert() returns the updated list, then the empList is set to that list - Loop goes till it can't read a new line - getline(array_to_read_chars_into, num_chars_to_read, char_to_stop_reading_at) - 'display' the now updated list - ask the user to enter a number to search for - The '*foundEmployee' initialization should make scense by now. - Finally, just dispay the vals of the employee that was found. -------------------------------------------------------- ******************************************************** #include #include #include #include using namespace std; struct Employee { string firstName; string lastName; unsigned long number; double salary; Employee *next; }; Employee* insert(Employee *empList, Employee *node){ Employee *curNode = empList; Employee *prevNode = NULL; //Sort by employee number while (curNode && node->number > curNode->number){ prevNode = curNode; curNode = curNode->next; } if (!curNode && !prevNode){ empList = node; } else if (!prevNode){ node->next = curNode; empList = node; } else if (!curNode){ prevNode->next = node; } else { node->next = curNode; prevNode->next = node; } return empList; } Employee* findEmployee(Employee *empList, unsigned int parNumber){ bool found = false; Employee *curNode = empList; while(curNode && !found){ found = (curNode->number == parNumber); if (!found) curNode = curNode->next; } return curNode; } void display(Employee *empList){ Employee *curNode = empList; cout << "***************************************" << endl; while (curNode) { cout << "---" << endl; cout << "First Name: " << curNode->firstName << endl; cout << "Last Name: " << curNode->lastName << endl; cout << "Number: " << curNode->number << endl; cout << "Salary: $" << fixed << setprecision(2) << curNode->salary << endl; cout << "---" << endl; curNode = curNode->next; } cout << "***************************************" << endl; } void main(){ unsigned int empNumber; fstream inFile("employees.dat", ios::in); const unsigned int BUF_SIZE = 128; char buffer[BUF_SIZE]; Employee *empPtr = NULL; Employee *empList = NULL; if (!inFile) { cerr << "Input file not opened." << endl; exit(1); } do { empPtr = new Employee; inFile >> empPtr->firstName; inFile >> empPtr->lastName; inFile >> empPtr->number; inFile >> empPtr->salary; empPtr->next = NULL; empList = insert(empList, empPtr); } while(inFile.getline(buffer, BUF_SIZE, '\n')); display(empList); cout << "Find an employee by number: "; cin >> empNumber; Employee *foundEmployee = findEmployee(empList, empNumber); if (foundEmployee){ cout << endl; cout << "----------(" << foundEmployee->number << ")----------" << endl; cout << " " << foundEmployee->firstName << " " << foundEmployee->lastName << endl; cout << " $" << fixed << setprecision(2) << foundEmployee->salary<< endl; cout << "---------------------------" << endl; } else { cout << "No employee was found." << endl; } } ********************************************************