1 CS 132 Spring 2008 Chapter 5 Linked Lists p. 273-317.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES USING C++ Chapter 5
Advertisements

Data Structures Using C++
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review Learn about linked lists
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,
Data Structures & Algorithms
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Linked Lists Dr. Youssef Harrath
Data Structures Using C++ 2E
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
C++ Classes and Data Structures Jeffrey S. Childs
Data Structures Using Java1 Chapter 4 Linked Lists.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
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.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Data Structures Using C++1 Chapter 5 Linked Lists.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists (part 2)
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists (part 2)
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Linked Lists Chapter 6 Section 6.4 – 6.6
Review Deleting an Element from a Linked List Deletion involves:
Lists CS 3358.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
UNIT-3 LINKED LIST.
Chapter 4 Linked Lists.
Linked lists.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Array Lists Chapter 6 Section 6.1 to 6.3
Chapter 16-2 Linked Structures
Chapter 18: Linked Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Chapter 16 Linked Structures
Linked lists.
Data Structures & Programming
Presentation transcript:

1 CS 132 Spring 2008 Chapter 5 Linked Lists p

2 Linked Lists A list of nodes in which each node has two components: one to store the info one to store the address of the next node in the list Structure of a node: Structure of a linked list: A pointer (head) points to the first node

3 Implementation struct nodeType { int info; //data nodeType *link; //pointer to the next node in the list }; nodeType *head; //pointer to the first element Print the first element: Print the second element: Change the value in the second node to 60: Delete the first node: A memory leak. Better: cout << head→info; cout << head→link→info; head→link→info = 60; head = head→link temp=head; head = head→link; delete temp;

4 Linked Lists Basic operations: –search the list to determine whether a particular item is in the list –insert an item in the list –delete an item from the list These operations require traversal of the list –start with a pointer to the first node of the list and step through each node in the list

5 Traversing a Linked List nodeType *current; current=head; while (current!=NULL) { //process the element pointed to by current current = current→link; } E.g. print the contents of the list: while (current!=NULL) { cout << current→info; current = current→link; } current

6 Insertion in a Linked List Insert a node with value 50 at the beginning of the list: First create a new node and put 50 in it: nodeType *newNode; newNode= new nodeType; newNode → info= 50; Next: fix the pointers newNode 50

7 Insertion in a Linked List (cont.) Insert a node with value 50 at the beginning of the list: First create a new node and put 50 in it: nodeType *newNode; newNode= new nodeType; newNode → info= 50; Next: fix the pointers newNode  link = head; //a head = newNode //b newNode 50 b a

8 Insertion in the Middle Which is right: Code Sequence I newNode  link = p  link; //a p  link = newNode //b Code Sequence II p  link = newNode //b newNode  link = ? //a ab

9 Deleting from the Beginning of a List nodeType *temp; temp=head; head = head→link; delete temp; temp

10 Deletion from the Middle Node to be deleted is 34: q = p->link; p->link = q->link; delete q; q Result:

11 linkedList.cpp Nodes: struct nodeType { int info; //data nodeType *link; //pointer to the next node in the list }; The list: nodeType* theList=NULL; Basic operations: –initialize the list –check whether the list is empty –output the list –find length of list See linkedList.cpp

12 Linked List ADT What is different if linkedList is a class? 1. use a template for the element type: template struct nodeType { Type info; nodeType *link; }; 2. private will keep track of length and the last node: private: int count; nodeType *first; nodeType *last; 3. expect to use this as a base class for inherited classes (protected/private) See linkedList.h, testList.cpp

13 Other Functions constructors: default gives the empty list linkedListType ::linkedListType() { first = NULL; last = NULL; count = 0; } destroyList: –we need to be able to deallocate memory used by a list –explicitly: by program action –automatically: when a list goes out of scope (a destructor)

14 destroyList first nodeType *temp; while(first != NULL) { temp = first; first = first->link; delete temp; } last = NULL; count = 0; last temp

15 Destructors When an object with pointers goes out of scope, it is not sufficient to delete the pointer Destructor –the function that is automatically invoked to clean up Syntax in.h: ~className( ); Syntax in.cpp: className::~className(); ~linkedListType() template linkedListType ::~linkedListType() { destroyList(); }

16 copyList Make an identical copy of a linked list We can't just assign the pointers (shallow copy) Must recreate the list elements (deep copy)

17 Shallow Copy Shallow copy: copying a pointer to a list, but not the values int *p, *q; p = new int; //then insert nodes q = p; qdestroyList(): //deallocates all nodes, leaving q NULL, p confused p. q ? p q NULL

18 Deep Copy Deep copy: make duplicate copies of the nodes in the list –takes longer –uses more space –safer q.destroyList(); //leaves p alone, deallocates elements of q See code in linkedList.h p q 37921

19 Copy Constructor If a class has pointer data members: –during object declaration, the initialization of one object using the value of another object could lead to a shallow copying of the data –an object is passed by value (in a function) could lead to a shallow copying of the data Define a copy constructor as part of a class to ensure all pieces pointed to are deep copied during initialization The copy constructor automatically executes: –when an object is declared and initialized using the value of another object –when, as a parameter, an object is passed by value –when the return value of a function is an object

20 Copy Constructor Syntax to include the copy constructor in the class definition: className(const className& otherObject); Implementation: template linkedListType :: linkedListType(constlinkedListType<Type& otherList) { first = NULL; copyList(otherList); }

21 Overloading Assignment Again, a deep copy Need to –check that you are not assigning a list to itself –use pointer this template const linkedListType & linkedListType :: operator=(const linkedListType & otherList) { if(this != &otherList) copyList(otherList); return *this; }

22 Ordered Link List The elements are sorted What operations need to be modified? –search (stop when we pass the place in the list where it would have been) –insert (in the right location) –delete (stop when we pass the place where it would have been)

23 Doubly Linked List A linked list in which every node has a next pointer and a back pointer Every node (except the last node) contains the address of the next node, and every node (except the first node) contains the address of the previous node. Can be traversed in either direction

24 Circular Linked List A linked list in which the last node points to the first node Advantages?

25 Building a Linked List Forward/Backward We can build a linked list –forward (putting new elements at the end) or –backward (putting new elements at the beginning): Needed: –a pointer for the first node –a pointer for the last node –a pointer for the new node being added Steps: –create a new node called newNode –if first is NULL, the list is empty so you can make first and last point to newNode –if first is not NULL insert the node at the end/beginning of the list

26 Building an empty list forward //an empty list nodeType *first,*last,*newNode; int num; first=NULL; last=NULL; newNode = newnodeType; //adding a node with value num to the end newNode  info = num; newNode  link=NULL; if (first==NULL){ //list is empty first=newNode; last=newNode; } else{ //list is not empty last  link=newNode; last-newNode; }

27 Build a List Containing Start with an empty list: first nodeType *first,*last,*newNode; int num; first=NULL; last=NULL; last newNode NULL

28 Build a List Containing Add 4 to the beginning: nodeType *first,*last,*newNode; first int num; first=NULL; last=NULL; last newNode = newnodeType; newNode  info = 4; newNode  link=NULL; newNode if (first==NULL){ first=newNode; last=newNode; } else... 4 NULL

29 Build a List Containing Add 6: first (1) last (2) newNode newNode = newnodeType; newNode  info = num; newNode  link=NULL;... else{ last  link=newNode; (1) last=newNode; (2) } 4 6 NULL

30 Build a List Containing The list: first last Now add 7: first last newNode 4 6 NULL 7 4 6

31 Build a List Containing Backwards How will it differ if we put elements at the beginning instead of the end?

32 First Step is the Same Start with an empty list: first nodeType *first,*last,*newNode; int num; first=NULL; last=NULL; last newNode NULL

33 Second Step is the Same Add 4 to the beginning: nodeType *first,*last,*newNode; first int num; first=NULL; last=NULL; last newNode = newnodeType; newNode  info = 4; newNode  link=NULL; newNode if (first==NULL){ first=newNode; last=newNode; } else... 4 NULL

34 Adding 6: how do pointers differ? Add 6 (red =put at end of the list): first (1) last (2) newNode newNode = newnodeType; newNode  info = num; newNode  link=NULL;... else{ last  link=newNode; (1) last=newNode; (2) } 4 6 NULL

35 Adding 6: how do pointers differ? Add 6 (red =put at end of the list): first (1) last (2) newNode newNode = newnodeType; newNode  info = num; newNode  link=NULL;... else{ newNode  link=first; first=newNode; } 4 6 NULL

36 Building an empty list backward //an empty list nodeType *first,*last,*newNode; int num; first=NULL; last=NULL; newNode = newnodeType; //adding a node with value num to the beginning newNode  info = num; newNode  link = NULL; //same procedure for empty and populated list first=newNode; last=newNode; newNode  link=first; first=newNode;}