Download presentation
Presentation is loading. Please wait.
1
Linked List (I) Ying Wu Electrical & Computer Engineering Northwestern University yingwu@ece.northwestern.edu ECE230 Lectures Series
2
What have we learned so far? C/C++ data type C/C++ control structures Pointer and reference Dynamic memory allocation Array Function C++ class and object Constructor and destructor
3
Create an array Suppose the my machine only has 100 bytes of memory. I want to store 10 integers It is easy, right? You can use int numbers[10]; Hold for numbers[]
4
What if … If some of the memory are being used by other programs, such that I can not find 40 consecutive bytes for the array. Thus, I can not create int number[10] Since there are many memory fragments, I do have more than 40 bytes in the memory! being used
5
Motivation: drawbacks of array Characteristics of array –Located in consecutive memory spaces –Allocated at compile time –Fixed size of memory space once allocated Pros of array –Fast and convenient data access Cons of array –If we do not know how large size we want, we generally allocate a much larger array, which will be a huge waste. –Array can be full –The size of the array can not be changed
6
Motivation: question When I am going to implement an array or a list, if I do not know the size in advance and it can be large or small, I want a mechanism of “memory-on- demand”. i.e., when I need some, I will get some from the system; when I am done, I will return the memory back. I will save memory! How? Free lunch? What are sacrificed?
7
Motivation: computation Two important computational resources –CPU time –Memory Computational complexity –Computation involved –Memory used
8
Self-Referential Classes self-referential class –class that contains a pointer to a class object of the same type –can be linked together to form useful data structures such as lists, queues, stacks and trees –terminated with a NULL pointer ( 0 ) Two self-referential class objects linked together 1015 NULL pointer (points to nothing) Data member and pointer
9
class Node { public: Node( const int & ); // constructor int getData() const; // return data in the node // private: // should not be private int data; // data Node *nextPtr; // next node in the list }; // Constructor Node::Node( const int &info ) : data( info ), nextPtr( 0 ) { } // Return a copy of the data in the node int Node::getData() const { return data; } nextPtr - points to an object of type Node –referred to as a link – ties one Node to another Node
10
Linked Lists linked list –linear collection of self-referential class objects, called nodes, connected by pointer links –accessed via a pointer to the first node of the list –subsequent nodes are accessed via the link-pointer member –the link pointer in the last node is set to null to mark the list’s end Use a linked list instead of an array when –the number of data elements is unpredictable
11
List H E L L firstPtr lasttPtr O
12
class List { public: List() : firstPtr(0), lastPtr(0) {}; ~List(); // destructor void insertAtFront(Node & ); void insertAtBack(Node & ); bool removeFromFront(int & ); bool removeFromBack(int & ); bool isEmpty() const { return firstPtr == 0; }; void print() const; private: Node *firstPtr; // pointer to first node Node *lastPtr; // pointer to last node // Utility function to allocate a new node Node *getNewNode( const Node & ); };
13
insertAtFront 5 newPtr 79 firstPtr if ( isEmpty() ) // List is empty firstPtr = lastPtr = newPtr; else { // List is not empty newPtr->nextPtr = firstPtr; firstPtr = newPtr; }
14
insertAtBack 5 newPtr 79 firstPtr lastPtr if ( isEmpty() ) // List is empty firstPtr = lastPtr = newPtr; else { // List is not empty lastPtr->nextPtr = newPtr; lastPtr = newPtr; }
15
firstPtrlastPtr 7953 removeFromFront tempPtr bool List::removeFromFront(int & value) { if ( isEmpty() ) // List is empty return false; // delete unsuccessful else { Node *tempPtr = firstPtr; // step I if ( firstPtr == lastPtr ) firstPtr = lastPtr = 0; else firstPtr = firstPtr->nextPtr; // step II value = tempPtr->data; // step III: data being removed delete tempPtr; // step IV return true; // delete successful }
16
removeFromBack firstPtrlastPtr 7953 tempPtrcurPtr if ( isEmpty() ) return false; // delete unsuccessful else { Node *tempPtr = lastPtr; if ( firstPtr == lastPtr ) firstPtr = lastPtr = 0; else { Node *currentPtr = firstPtr; while ( currentPtr->nextPtr != lastPtr ) currentPtr = currentPtr->nextPtr; lastPtr = currentPtr; currentPtr->nextPtr = 0; } value = tempPtr->data; delete tempPtr; return true; // delete successful }
17
Question to think about How can we search a node? How to insert a node in between? How to delete the whole list?
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.