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.

Slides:



Advertisements
Similar presentations
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Advertisements

Stacks, Queues, and Linked Lists
DATA STRUCTURES USING C++ Chapter 5
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 Linked Lists III Template Chapter 3. 2 Objectives You will be able to: Write a generic list class as a C++ template. Use the template in a test program.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
1 Abstract Data Types Chapter 1. 2 Objectives You will be able to: 1. Say what an abstract data type is. 2. Implement a simple abstract data type in C++
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Linked List and Namespace ~ include dynamic objects.
IntNode Class class IntNode { public: int info; class IntNode *next; IntNode(int el, IntNode *ptr = 0) { info = el; next = ptr; } }; diagrammatic representation:
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Binary Search Trees Chapter 6.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
C++ Classes and Data Structures Jeffrey S. Childs
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
11 Introduction to Object Oriented Programming (Continued) Cats.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
1 Linked Lists II Doubly Linked Lists Chapter 3. 2 Objectives You will be able to: Describe, implement, and use a Doubly Linked List of integers.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
1 Getting Started with C++ Part 2 Linux. 2 Getting Started on Linux Now we will look at Linux. See how to copy files between Windows and Linux Compile.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
1 Introduction to Object Oriented Programming Chapter 10.
1 The Standard Template Library Drozdek Section 3.7.
1 Implementing Ticket Printer. Class Diagram 2 Dependencies A class that contains objects of another class, or has a reference to another class, depends.
Data Structure & Algorithms
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
CS212: Data Structures and Algorithms Lecture # 4 Linked List.
CSCI-255 LinkedList.
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 4 Linked Lists.
LAB#4#5 Linked List.
Chapter 4 Linked Lists
Chapter 16-2 Linked Structures
Chapter 4 Linked Lists.
Linked Lists Chapter 4.
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
Pointers & Dynamic Data Structures
LAB#4 Linked List.
Abstract Data Types Stacks CSCI 240
Data Structures & Programming
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: 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 #include "intSLLst.h" using namespace std; int main() { IntSLList myList; cout << " Testing addToHead();\n " ; cout << " Calling addtoHead for \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 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