CS 250 Computer Programming and Problem Solving - FALL 1998 |
---|
[ Home | Syllabus | Notes | Glossary | CS250 WEB Version Home Page ]
Linked Lists Review
Initialize current to first node while (list is not done) { Display the data in the current position move to next in list }
struct node; typedef struct node* ptrType; struct node { int Data; ptrType Next; };
void DisplayList(ptrType Head) // -------------------------------------------------------------- // Displays the data in a linked list. // Precondition: Head is a pointer variable that points to // the linked list. // Postcondition: Displays the data in the linked list. // Both Head and the list are unchanged. // -------------------------------------------------------------- { // Loop invariant: Cur points to the next node to be displayed for (ptrType Cur = Head; Cur != NULL; Cur = Cur->Next) cout << Cur->Data << "\n"; } // end DisplayList
NewPtr = new node;
NewPtr->Item = 8;
NewPtr->Next = Cur;
Prev->Next = NewPtr;
// Are these statements order dependent?
// What are the special cases?
Prev->Next = Cur->Next; // splices out current node
Cur->Next = NULL; // Defensive programming
delete Cur;
Cur = NULL; // Defensive programming
Does this work for special cases?
What are the special cases?