Linked Lists | |
|
|
The Beginning of a Linked List |
If you create an array-based list, you can start by declaring an array member variable that would hold the items and each item can be located by an index that is assigned to it when the item is added to the list. When you do this, you must provide an estimate of the maximum number of items that will be allowed in the list. Without good planning, the dimension you specify could be too high or too low but C++ doesn't allow you to declare an array without a dimension if you are not initializing the array. This means that, when you create an array-based list, you must also specify the maximum number of items that the list can hold. A linked list is a list that can grow or shrink as the user wishes. This means that, when creating the list, you don't need to predict the maximum number of items that will be added to the list. To use this flexibility, the items must be managed through pointers. Because the list would use a member that defines its item, you can declare a member variable that is conform to the intended items. When a list starts, it is empty or at least it must be considered like that, before any item is added to it. To specify this, you should declare a primary member variable. Although you can call it anything, it is usually called Head. This member can be made private if you don't intend to access it outside of the class. If you want clients of the class to access it, you can make it public. Although this member is declared as a pointer and it marks the beginning of the list, you should not allocate memory for it in the constructor. Its memory would be managed when it is accessed. Therefore, you can simply initialize is as NULL. Here is an example: |
Header File: ListOfParts.h |
#pragma once//---------------------------------------------------------------------------struct CarPart{ long PartNumber; char PartName[40]; double UnitPrice;};//---------------------------------------------------------------------------class ListOfParts{private: int size;public: ListOfParts(); int Count(); CarPart* Head;};//--------------------------------------------------------------------------- |
Source File: ListOfParts.cpp |
#include |
The Linking of a List |
As mentioned already, each member of an array-based list can be accessed using its index, stored in an item member variable declared as an array. Because the items of a linked list are not indexed, you must provide another mechanism to locate an item in the list. The easiest way to do this is to first locate an item, then ask that item to provide you with a pointer to the item that succeeds it. If the list is empty, this information would be provided as null. If the item you first access is the last in the list, you would also provide nothing. Any other item in the middle would give you access to the next item in the list. To support this theory, you must provide a pointer to the next item in the list as a member variable to the class that holds the item. As a tradition, this member variable is called Next but you can give it any name you want. Because this item actually is a complete one, it must be declared as a self-referencing member that has the same name as its class: |
Header File: ListOfParts.h |
#pragma once//---------------------------------------------------------------------------struct CarPart{ long PartNumber; char PartName[40]; double UnitPrice; CarPart* Next;};//---------------------------------------------------------------------------class ListOfParts{private: int size;public: ListOfParts(); int Count(); CarPart* Head;};//--------------------------------------------------------------------------- |
Operations of a Linked List |
Item Addition |
The primary operation you can perform on a list is to add a new item to it, since a list is fundamentally empty when it starts. In order to indicate that you want to add an item to the list, you can declare a method that receives an item as argument. For the return type, you have two main options. Because the main job of this method is to add a new item, which it hardly fails to do if you implement it right, it can be defined as void. Alternatively, you can make it return the position of the new item in the list. Here is an example: |
Header File: ListOfParts.h |
#pragma once//---------------------------------------------------------------------------struct CarPart{ long PartNumber; char PartName[40]; double UnitPrice; CarPart* Next;};//---------------------------------------------------------------------------class ListOfParts{private: int size;// List Builderpublic: ListOfParts(); int Count(); CarPart* Head;// List Operationspublic: int Add(CarPart* Item);};//--------------------------------------------------------------------------- |
Source File: ListOfParts.cpp |
#include |
Source File: Exercise.cpp |
#include |
Item Retrieval |
Once a list exists, the user can explorer it. One of the operations performed on items is to locate and retrieve one. To do this, you can declare a method that takes as argument as index. The method would examine the argument with regards to the number of items in the list to make sure the argument's value is in the range of current items of the list. If the number is too low or too high, the method can return null or 0. If the number is in the range, the method can return the item at that position. Here is an example: |
Header File: ListOfParts.h |
#pragma once//---------------------------------------------------------------------------struct CarPart{ long PartNumber; char PartName[40]; double UnitPrice; CarPart* Next;};//---------------------------------------------------------------------------class ListOfParts{private: int size;// List Builderpublic: ListOfParts(); int Count(); CarPart* Head;// List Operationspublic: int Add(CarPart* Item); CarPart *Retrieve(int pos);};//--------------------------------------------------------------------------- |
Source File: ListOfParts.cpp |
#include |
Source File: Exercise.cpp |
#include |
This would produce: Number of Parts: 4Car Part InformationPart #: 27644Description: A/C Filter DrierUnit Price: $55.55Car Part InformationPart #: 87873Description: Brake DiskUnit Price: $35.15Car Part InformationPart #: 27487Description: Clutch DiskUnit Price: $47.15Car Part InformationPart #: 9743Description: Air FilterUnit Price: $8.75 |
Item Deletion |
Deleting an item consists of removing it from the list. There are two main types of deletion you can perform on a list. You can simply ask the class to delete an item. In this case, it is usually the item at the end that gets deleted. If you do this, make sure you perform other routines operations such as decrementing the count of items in the list. Here is an example: |
Header File: ListOfParts.h |
#pragma once//---------------------------------------------------------------------------struct CarPart{ long PartNumber; char PartName[40]; double UnitPrice; CarPart* Next;};//---------------------------------------------------------------------------class ListOfParts{private: int size;// List Builderpublic: ListOfParts(); int Count(); CarPart* Head;// List Operationspublic: int Add(CarPart* Item); CarPart *Retrieve(int pos); bool Delete();};//--------------------------------------------------------------------------- |
Source File: ListOfParts.cpp |
#include |
Source File: Exercise.cpp |
#include |
This would produce: Number of Parts: 4-=- List of Parts -=-Car Part InformationPart #: 27644Description: A/C Filter DrierUnit Price: $55.55Car Part InformationPart #: 87873Description: Brake DiskUnit Price: $35.15Car Part InformationPart #: 27487Description: Clutch DiskUnit Price: $47.15Car Part InformationPart #: 9743Description: Air FilterUnit Price: $8.75Number of Parts: 3-=- List of Parts -=-Car Part InformationPart #: 27644Description: A/C Filter DrierUnit Price: $55.55Car Part InformationPart #: 27487Description: Clutch DiskUnit Price: $47.15Car Part InformationPart #: 9743Description: Air FilterUnit Price: $8.75Press any key to continue Another technique used to delete an item consists of specifying the position of the item to be deleted. To do this, you can pass an argument as the desired position. The method would check the range of values of the current list. If the specified position is beyond the appropriate range, the method can return false, 0, or null, depending on how you create it. Here is an example: |
Header File: ListOfParts.h |
#pragma once//---------------------------------------------------------------------------struct CarPart{ long PartNumber; char PartName[40]; double UnitPrice; CarPart* Next;};//---------------------------------------------------------------------------class ListOfParts{private: int size;// List Builderpublic: ListOfParts(); int Count(); CarPart* Head;// List Operationspublic: int Add(CarPart* Item); CarPart *Retrieve(int pos); bool Delete(); bool Delete(int pos);};//--------------------------------------------------------------------------- |
Source File: ListOfParts.cpp |
#include |
Source File: Exercise.cpp |
#include |
This would produce: Number of Parts: 4-=- List of Parts -=-Car Part InformationPart #: 27644Description: A/C Filter DrierUnit Price: $55.55Car Part InformationPart #: 87873Description: Brake DiskUnit Price: $35.15Car Part InformationPart #: 27487Description: Clutch DiskUnit Price: $47.15Car Part InformationPart #: 9743Description: Air FilterUnit Price: $8.75Number of Parts: 3-=- List of Parts -=-Car Part InformationPart #: 27644Description: A/C Filter DrierUnit Price: $55.55Car Part InformationPart #: 87873Description: Brake DiskUnit Price: $35.15Car Part InformationPart #: 9743Description: Air FilterUnit Price: $8.75Press any key to continue |
Item Location |
One of the operations hardly performed on a list is to find one. This is because if you ask a list to locate a particular item, you must provide as much information as possible. Probably the most expedient way you can do this is to completely define an item and pass it to the list. Only if the item is found in the list would it be recognized. In the source code, we implement this method as Find(). |
There are different ways you can try locating an item in a list. For example, you can provide partial information and try locating all items that match such an entry. Here is an example: |
Linked List Source Code |
Header File: ListOfParts.h |
#pragma once//---------------------------------------------------------------------------struct CarPart{ long PartNumber; char PartName[40]; double UnitPrice; CarPart* Next;};//---------------------------------------------------------------------------class ListOfParts{private: int size;// List Builderpublic: ListOfParts(); int Count(); CarPart* Head;// List Operationspublic: int Add(CarPart* Item); CarPart *Retrieve(int pos); bool Delete(); bool Delete(int pos); bool Find(CarPart* Item);};//--------------------------------------------------------------------------- |
Source File: ListOfParts.cpp |
#include |
Source File: Exercise.cpp |
#include |
No comments:
Post a Comment