Notes: A resource file entitled "Creating a Simple Microsoft Visual C++ Project" has been added to the website. DO NOT specify a path when opening a file. The executable will look for the file in the current path. Linked Lists Arrays work well when you know exactly how many items you want to store. However, if the number of items is unknown or variable, then you need a more flexible structure. A linked list provides the answer. A linked list grows and shrinks as needed, item by item, storing only exactly what you need at the moment you need it. A linked list is a collection of nodes linked together. There are two basic parts to each node: Content - this is where the information is stored Pointer - this tells us where to find the next piece of information The most difficult part of a linked list is working with the pointer. A pointer is simply a memory address - it tells you where to go to find the next piece of information. Three types of simple linked lists are: Single - each link has one pointer pointing to the next item in the list Double - each link has a second pointer pointing to the previous item in the list Circular - the last link in the list points to the first link The first step is to define a node. We'll start with a simple node that stores one piece of information: struct node { char StockNbr[10]; node* Next; }; Now we need a datatype of node: typedef node* ptrType; NOTE: The asterisk indicates that this a pointer to a memory location, not an actual datatype. A few variables of type node that will come in handy for a single-linked list: ptrType ptrHead = NULL; // always points to the first item in the list ptrType ptrPre = NULL; // always points to the previous item we looked at ptrType ptrCur = NULL; // always points to the current item we are looking at ptrType ptrNew = NULL; // used for creating new nodes. To get an idea of how these are used, let's take a look at some simple functions: Searching a linked list: for ( ptrPre=NULL, ptrCur=ptrHead; (ptrCur!=NULL) && strcmp(strInput, ptrCur->StockNbr) > 0; ptrPre=ptrCur, ptrCur=ptrCur->Next ); When this loop is completed, ptrCur is pointing to the item which is larger than the one we want to insert, and ptrPre is pointing to the item which is smaller than the one we want to insert. Adding a node to an ordered, single linked list This is a simple insertion algorithm. We search the list until we find a node whose content is larger than the one we are inserting. Then, all values from that point on are moved up the chain to make space for the new value. With pointers, it's not necessary to move all the values. All we need to do is re-adjust the pointers. Before insertion, ptrPre points to ptrCur. We want ptrNew to be in the middle of these two. So, ptrPre now has to point to ptrNew instead of ptrCur. ptrNew has to point to ptrCur. void AddItem(string strInput) { ptrNew = new node; strcpy(ptrNew->StockNbr, strInput); if ( ptrHead == NULL ) { ptrNew->Next = ptrHead; ptrHead = ptrNew; } else { for ( ptrPre=NULL, ptrCur=ptrHead; (ptrCur!=NULL) && strcmp(strInput, ptrCur->StockNbr) > 0; ptrPre=ptrCur, ptrCur=ptrCur->Next ); ptrNew->Next = ptrCur; ptrPre->Next = ptrNew; } } Deleting a node from an ordered, single linked list This is slightly more complicated, because you have to take into account whether or not: ptrPre is null (deleting from the beginning of the list, no pointer to re-adjust) ptrCur->Next is null (deleting from the end of the list, no pointer to re-adjust) void DelItem(string strInput) { if ( ptrHead != NULL ) for ( ptrPre=NULL, ptrCur=ptrHead; (ptrCur!=NULL) && strcmp(strInput, ptrCur->StockNbr) > 0; ptrPre=ptrCur, ptrCur=ptrCur->Next ); if ( strcmp(strInput, ptrCur->StockNbr) == 0 ) { if ( ptrPre == NULL ) { ptrHead = ptrHead->Next; ptrCur->Next = NULL; delete ptrCur; ptrCur = NULL; } else if (ptrCur->Next == NULL ) { ptrPre->Next = NULL; ptrCur->Next = NULL; delete ptrCur; ptrCur = NULL; } else { ptrPre->Next = ptrCur->Next; ptrCur->Next = NULL; delete ptrCur; ptrCur = NULL; } } } }