Download presentation
Presentation is loading. Please wait.
Published byHelena Webb Modified over 9 years ago
1
1 CS 132 Spring 2008 Chapter 5 Linked Lists p. 273-317
2
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
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
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
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
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
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
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
9 Deleting from the Beginning of a List nodeType *temp; temp=head; head = head→link; delete temp; temp
10
10 Deletion from the Middle Node to be deleted is 34: q = p->link; p->link = q->link; delete q; q Result:
11
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
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
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
14 destroyList 37921 first nodeType *temp; while(first != NULL) { temp = first; first = first->link; delete temp; } last = NULL; count = 0; last temp
15
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
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
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 37921 p. q ? p q NULL
18
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 37921 p q 37921
19
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
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
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
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
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
24 Circular Linked List A linked list in which the last node points to the first node Advantages?
25
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
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
27 Build a List Containing 4 6 7 Start with an empty list: first nodeType *first,*last,*newNode; int num; first=NULL; last=NULL; last newNode NULL
28
28 Build a List Containing 4 6 7 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
29 Build a List Containing 4 6 7 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
30 Build a List Containing 4 6 7 The list: first last Now add 7: first last newNode 4 6 NULL 7 4 6
31
31 Build a List Containing 4 6 7 Backwards How will it differ if we put elements at the beginning instead of the end?
32
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
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
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
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
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;}
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.