370 likes | 509 Views
Linked Lists. Chapter 3. Objectives. You will be able to: Describe an abstract data type for lists. Understand and use an implementation of a List ADT. Linked Lists. An alternative to arrays for storing an ordered collection of data items of the same type.
E N D
Linked Lists Chapter 3
Objectives You will be able to: • Describe an abstract data type for lists. • Understand and use an implementation of a List ADT.
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”.
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
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
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.
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.
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.
Project Directory Download here.
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.
Still can’t compile • We don’t have a program yet. • Just a class definition. • Need a “main” in order to compile and run.
Compile Error Evidently the author used an older compiler.
Ready to Run Click here to run.
Program Running We have a working program! Now we need to make it do something. Start by looking at the class definition.
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; } };
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; };
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. }
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. }
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