Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 Linked Lists Chapter 3

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

3 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 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 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 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 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 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 9 Creating a Project

10 10 Creating a Project

11 11 Creating a Project

12 12 Creating a Project

13 13 Creating a Project

14 14 Project Directory

15 15 Project Directory Download here.

16 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 17 Add Files to Project

18 18 Add Files to Project

19 19 Source Files in the Project

20 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 21 Adding a “main”

22 22 Adding a “main”

23 23 Project with main.cpp

24 24 Start with a stub

25 25 Compile and Run

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

27 27 Fixing the Errors

28 28 Ready to Run Click here to run.

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

30 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 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 32 main.cpp #include #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 33 Program Running

34 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 35 Program Running

36 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


Download ppt "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."

Similar presentations


Ads by Google