For the last node its next pointer is set to nullptr to tell that it is the end

For the last node its next pointer is set to nullptr

This preview shows page 56 - 62 out of 108 pages.

For the last node , its next pointer is set to nullptr to tell that it is the end of the linked list. We need a pointer variable, usually called head to point to the first node . Once you get the head of the linked list, you get the whole list ! { kccecia, achung, lixin } @cse.ust.hk COMP2011 (Spring 2019) p.52 1. head node 2. last node 3. <all in the middle>
Image of page 56
Basic Operations of a Linked List /* To create a node */ ll_node* p = new ll_node; /* To access/modify the data in a node */ cout << p->data; cout << (*p).data; cin >> p->data; p->next = nullptr ; /* To set up the head of a linked list */ ll_node* head = nullptr ; // An empty linked list head = p; // head points to the node that p points to /* To delete a node */ delete p; // Dangling pointer p = nullptr ; // Reset the pointer for safety reason { kccecia, achung, lixin } @cse.ust.hk COMP2011 (Spring 2019) p.53 address of new linked list type dereferencing operation to access whole linked lis then do dot operation static pointer arrow operation
Image of page 57
Example: Create the LL-String “met” #include "ll_cnode.h" /* File: ll_main.cpp */ int main() // Create the LL-string "met" { // Create each of the 3 ll_cnodes ll_cnode* mp = new ll_cnode; mp->data = 'm' ; ll_cnode* ep = new ll_cnode; ep->data = 'e' ; ll_cnode* tp = new ll_cnode; tp->data = 't' ; // Hook them up in the required order to create the LL mp->next = ep; ep->next = tp; tp->next = nullptr ; // Traverse the LL and print out the data sequentially for (ll_cnode* p = mp; p; p = p->next) cout << p->data; cout << endl; // Clean up delete mp; delete ep; delete tp; return 0; } { kccecia, achung, lixin } @cse.ust.hk COMP2011 (Spring 2019) p.54 last node mp is now head pointer of linked list check whether p is a nullptr p is a working pointer - travel along linked list until last node
Image of page 58
Common Operations on a Linked List Common operations: Create a new linked list. Search data in the list. Delete a node in the list. Insert a new node in the list. For all these operations, again special attention is usually needed when the operation involves the first or the last node. { kccecia, achung, lixin } @cse.ust.hk COMP2011 (Spring 2019) p.55 insertion of new node
Image of page 59
Example: LL-String — ll cnode.h Let’s use a linked list (instead of an array) of characters to represent a string. #include <iostream> /* File: ll_cnode.h */ using namespace std; struct ll_cnode { char data; // Contains useful information ll_cnode* next; // The link to the next node }; const char NULL_CHAR = '\0' ; ll_cnode* ll_create( char ); ll_cnode* ll_create( const char []); int ll_length( const ll_cnode*); void ll_print( const ll_cnode*); ll_cnode* ll_search(ll_cnode*, char c); void ll_insert(ll_cnode*&, char , unsigned ); void ll_delete(ll_cnode*&, char ); void ll_delete_all(ll_cnode*&); { kccecia, achung, lixin } @cse.ust.hk COMP2011 (Spring 2019) p.56 function overloading count how many characters are there, given a head node given head node, print out list insertion of new node at particular location, change head pointer delete node containing certain character delete all nodes in the linked list 'read-only' operation -> use *
Image of page 60
Example: LL-String — ll create.cpp #include "ll_cnode.h" /* File: ll_create.cpp */ // Create a ll_cnode and initialize its data ll_cnode* ll_create( char c) { ll_cnode* p = new ll_cnode; p->data = c; p->next = nullptr ; return p; }
Image of page 61
Image of page 62

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture