1 / 36

Linked Lists

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.

ledell
Download Presentation

Linked Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked Lists Chapter 3

  2. Objectives You will be able to: • Describe an abstract data type for lists. • Understand and use an implementation of a List ADT.

  3. 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”.

  4. 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

  5. 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

  6. 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.

  7. 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.

  8. 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.

  9. Creating a Project

  10. Creating a Project

  11. Creating a Project

  12. Creating a Project

  13. Creating a Project

  14. Project Directory

  15. Project Directory Download here.

  16. 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.

  17. Add Files to Project

  18. Add Files to Project

  19. Source Files in the Project

  20. Still can’t compile • We don’t have a program yet. • Just a class definition. • Need a “main” in order to compile and run.

  21. Adding a “main”

  22. Adding a “main”

  23. Project with main.cpp

  24. Start with a stub

  25. Compile and Run

  26. Compile Error Evidently the author used an older compiler.

  27. Fixing the Errors

  28. Ready to Run Click here to run.

  29. Program Running We have a working program! Now we need to make it do something. Start by looking at the class definition.

  30. 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; } };

  31. 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; };

  32. 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. }

  33. Program Running

  34. 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. }

  35. Program Running

  36. 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

More Related