Wijnand
/* Linked List version 1.1 by Wijnand Dalmijn 20017368 Changes: Cleaned up code a little bitfixed a loop bug TODO: 1.Figure out how to stop memory leaks*/#include <iostream>#include <string>using namespace std;//making a structure where the initial information will be put.struct LinkedList{string firstName, lastName, residence, adress, telephone; //standard variables for inputLinkedList* nextList ; // the pointer to the next structure};LinkedList* nextlist = NULL; // pointer where the next list is, this is a global pointer.void makeSelection()// gives a list of the options someone can choose when he starts the program{ cout << "Please make a selection: \n"; cout << "1: add a new linkedlist object \n"; cout << "2: List entire contents of List \n"; cout << "3: Display the third element \n"; cout << "4: Remove the third element from the list \n"; cout << "5: What is the adress of element 3? \n"; cout << "6: remove the first element \n"; cout << "7: What is the memory size of 1 Element? \n"; cout << "8: Display the names and adres of everyone \n"; cout << "0: Exit \n";}LinkedList* makeList(LinkedList* startlist){ LinkedList* newList = new LinkedList; //Create a new structure list // fill up the list with information cout << "What is your name? \n"; cin >> newList->firstName; cout << "What is your last name? \n"; cin >> newList->lastName; cout << "Where do you live? \n"; cin >> newList->residence; cout << "What is your address? \n"; cin >> newList->adress; cout << ("What is your phone number? \n"); cin >> newList->telephone;if (startlist == NULL) // check if there is an existing pointer. { newList->nextList = NULL; //set the pointer in the struct to zero. startlist = newList; //pointer where the list starts. nextlist = newList; //pointer where the list starts, to continue in the next step. return startlist;// return this value to the main() } else { //fill the correct pointer in first nextlist->nextList = newList; // set the pointer in the struct to the next struct. newList->nextList = NULL; // make sure this is set to NULL or else your memory gets gulped. nextlist = newList; return startlist;// return this value to main (); }}// the mainint main(){ LinkedList* startlist = NULL; //pointer to where the first variable goes to char input; bool mainloop = true; // boolean that stops the while. while(mainloop) //loop so the program does not shutdown after first selection { makeSelection(); // display the input possible. cin >> input; // get the input from the screen. switch(input) { case '0': { mainloop = false; //exit the program }break; case '1': { startlist = makeList(startlist); }break; case '2': { //Display everything that has been put in the list. bool loop = true; LinkedList* showList = new LinkedList; // maak een nieuwe list aan. showList = startlist; while(loop) { cout << showList->firstName <<endl; cout << showList->lastName <<endl; cout << showList->adress <<endl; cout << showList->residence <<endl; cout << showList->telephone <<endl; if(showList->nextList == NULL) // check if you reahed the end of the list { loop = false; cout << "The Complete listing has been shown! \n"; } else { nextlist = showList->nextList; //fill the information showList = nextlist; // fill the struct so that the rest can be displayed } } delete showList; // kill the list, it is no longer needed. }break; case '3': { // display the third element only LinkedList* showList = new LinkedList; showList = startlist; for(int i = 0; i<2; i++) { nextlist = showList->nextList; showList = nextlist; } //Display the third Element cout << "Displaying the Third element! \n"; cout << showList->firstName <<endl; cout << showList->lastName <<endl; cout << showList->adress <<endl; cout << showList->residence <<endl; cout << showList->telephone <<endl; delete showList; //remove from the memory. }break; case '4': { LinkedList* showList = new LinkedList; LinkedList* removeList = new LinkedList; showList = startlist; removeList = startlist; // find the address for the third pointer for(int i = 0; i<2; i++) { removeList = removeList->nextList; } // find the second pointer for (int i = 0; i<1; i++) { showList = showList->nextList; } showList->nextList = removeList->nextList; delete removeList; }break; case '5': { // display the adress of the third element LinkedList* showList = new LinkedList; showList = startlist; for(int i = 0; i<2; i++) { nextlist = showList->nextList; showList = nextlist; } cout << "Showing the adres of the third element"; cout << showList->adress <<endl; delete showList; // remove from the memory }break; case '6': { // remove the first Element LinkedList* showList = new LinkedList; LinkedList* removeList = new LinkedList; showList = startlist; removeList = startlist; showList->nextList = removeList->nextList; delete removeList; }break; case '7': { //the memory size of one element LinkedList* showList = new LinkedList; showList = startlist; cout << "the Memory size of 1 element is" << sizeof(showList->firstName); delete showList; // remove from memeory }break; case '8': { //Display The names and phone numbers of every list. bool loop = true; LinkedList* showList = new LinkedList; showList = startlist; while(loop) { cout << showList->firstName <<endl; cout << showList->lastName <<endl; cout << showList->telephone <<endl; if(showList->nextList == NULL) { loop = false; cout << "The Complete listing has been shown! \n"; } else { nextlist = showList->nextList; showList = nextlist; } } delete showList; }break; default: // the default setting if something else is added. { cout << "Please select one of the numbers selected on the screen" << endl; }break; } }}
Reason for the post - ref your last post there is an excellent article by Oluseyi right here about wrapping the Win32 API window stuff in a class. Hope this is of some interest. It is a very good article.