Linked Lists Chapter 3 1 Objectives You will be able to: Describe an abstract data type for lists. Understand and use an implementation of a List ADT. 2 Linked Lists An alternative to arrays for storing an ordered collection of data items of the same type. Normally, but not always, nodes are dynamically allocated and deleted. List can grow indefinitely. “Linked” is an implementation issue. The ADT is simply a “List”. 3 List Operations Typical operations for a List ADT: Create list Insert item at head Insert item at tail Insert item at specified position Insert item in order Retrieve item at specified position Retrieve item with specified value or key Delete item Traverse Determine if list is empty Determine number of items in the list Destroy list Many more 4 Linked Lists vs. Arrays Advantages of lists Efficient insertion and deletion at any position. No specific limit on size. Disadvantages of lists Lack of direct access to all nodes 5 List as an ADT Use copy semantics. Don’t give clients access to internals. Insertion adds a copy of client's data to the list. Retrieval gives client a copy of the data. All data is stored internally in the list. No pointers to data outside The list encapsulates the data. 6 Singly Linked Lists Nodes consist of data + pointer. List object has pointers to first node and last node. Null if list is empty Each node has a pointer to the next node. Last node has null pointer. 7 Singly Linked List Example Code in Figure 3.2 on page 79 and following A singly linked list of integers. Source files available from author’s web site: http://www.mathcs.duq.edu/drozdek/DSinCpp/ intSLLst.h intSLLst.cpp To look at the example, create an empty Visual Studio C++ project and download the files into the project directory. Or copy the files into an empty directory on Circe. 8 Creating a Project 9 Creating a Project 10 Creating a Project 11 Creating a Project 12 Creating a Project 13 Project Directory 14 Project Directory Download here. 15 After Download The source files intSLLst.h and intSLLst.cpp are in the project directory, but are not yet in the project. We have to add them to the project. 16 Add Files to Project 17 Add Files to Project 18 Source Files in the Project 19 Still can’t compile We don’t have a program yet. Just a class definition. Need a “main” in order to compile and run. 20 Adding a “main” 21 Adding a “main” 22 Project with main.cpp 23 Start with a stub 24 Compile and Run 25 Compile Error Evidently the author used an older compiler. 26 Fixing the Errors 27 Ready to Run Click here to run. 28 Program Running We have a working program! Now we need to make it do something. Start by looking at the class definition. 29 The Node Class //************************ intSLLst.h *************** // singly-linked list class to store integers #ifndef INT_LINKED_LIST #define INT_LINKED_LIST class IntSLLNode { public: int info; class IntSLLNode *next; IntSLLNode(int el, IntSLLNode *ptr = 0) { info = el; next = ptr; } }; 30 The List Class class IntSLList { public: IntSLList() { head = tail = 0; } ~IntSLList(); int isEmpty() { return head == 0; } void addToHead(int); void addToTail(int); int deleteFromHead(); // delete the head and return its info; int deleteFromTail(); // delete the tail and return its info; void deleteNode(int); bool isInList(int) const; void printAll() const; private: IntSLLNode *head, *tail; }; 31 main.cpp #include <iostream> #include "intSLLst.h" using namespace std; int main() { IntSLList myList; cout << "Testing addToHead();\n"; cout << "Calling addtoHead for 3 2 1 0\n"; myList.addToHead(3); myList.addToHead(2); myList.addToHead(1); myList.addToHead(0); cout << "Initial List:\n"; myList.printAll(); cin.get(); } // Hold window open. 32 Program Running 33 Test the Delete Method cout << "\nTesting deleteFromHead\n"; while (!myList.isEmpty()) { cout << "Deleting head node\n"; myList.deleteFromHead(); cout << "Current List:\n"; myList.printAll(); } cin.get(); // Hold window open. } 34 Program Running 35 Assignment Before next class Read Chapter 3, through section 3.1.3. Download and read the author’s singly linked list class files. Do the example from today’s class for yourself if you didn’t do it in class. Read the .cpp file. Understand every line! Expect a quiz on this code next week. Project 1 36