// LinkList.cpp // Implementation of Linked List Class #include #include #include "LinkList.h" #include "Student.h" using namespace std; // First, Implementation of the Node Class // Constructors // Node not in use. Next will be NULL Node::Node() {Next=NULL; } // Node gets init value. Next is NULL Node::Node(const Student &Elt) {Info=Elt; Next=NULL;} // Now, implement the LinkedList class // The Constructor // Pre: The argument should ba a value that will always be minimal LinkedList::LinkedList() {First=NULL; } // The Destructor LinkedList::~LinkedList() {Node *Hold; while (First) { Hold=First; First=First->Next; delete Hold; } } // Pre: First is a pointer to type Node. It may be undefined // Post: First is NULL pointer void LinkedList::Init() {First=NULL;} // Pre:None // Post: Current points at first node of list, unless list Empty // Returns: 0 ==> Empty List 1 ==> Success int LinkedList::StartOfList() {if (First->Next) { Current=First; return(1); } return(0); } // Pre: Current points to a Node // Post: None // Returns: Ptr to copy of Info Member BEFORE Current pointer is moved of // Node or NULL if End of List Student LinkedList::GetListElt() {return(Current->Info); } // Pre: Current points to a Node or NULL // Post: Current moves to next Node in List (or NULL if List End) // Returns:0 ==> Current is NULL 1 ==> Success int LinkedList::NextInList() {if (Current->Next) { Current=Current->Next; return(1); } return(0); } // Pre: P is defined. Node has two fields: Info and Next // Post: All Nodes in the list pointed at by P are visited, with // the contents output using the function declared to overload // the << operator std::ostream &LinkedList::PrintList(std::ostream &Dest) {Node *P; for(P=First;P;P=P->Next) Dest << P->Info << endl; return(Dest); } // Pre: Info contains the proper type that is the Info member of a node // > has been overloaded for Student // Post: *First points to non-null list containing at least one node void LinkedList::InsertInOrder(Student &Elt) {Node *P,*NN; // Place Data into Node NN=new Node; NN->Info=Elt; // Case: Empty List if (!(First)) First=NN; // Case: Insert at Head else if (NN->InfoInfo) { NN->Next=(First); First=NN; } // Case: Insert after Head else { // Find Spot for (P=First;P->Next && NN->Info > (P->Next)->Info;P=P->Next); // Adjust Links NN->Next=(P->Next); P->Next=(NN); } } // Pre: First points to a list of 0 or more nodes // Key contains a value to be used for comparison // Post: Returns a pointer to the info field of the node containing the // desired value contained in Key, or NULL if not found. Student *LinkedList::SearchList(Student Key) {Node *P; for (P=First;P && !(P->Info==Key);P=P->Next); if (!P) return(NULL); return(&(P->Info)); // return(&Result); } // Pre: First is a pointer to a non-null Linked List that contains the // data value in Key. CompFn knows how to make a correct comparison // based on that value // Post: First points to a possibly NULL list that has been reduced // by one node, the first node containing the Key. void LinkedList::DeleteInOrder(Student &Key) {Node *P,*Hold; if (Key==First->Info) { Hold=First; First=First->Next; } else { for (P=First;Key>(P->Next)->Info;P=P->Next); Hold=P->Next; P->Next=(Hold->Next); } delete Hold; }