1 / 21

Linked Lists

Linked Lists. Dr. Jose Annunziato. The Bank Application. enum TransactionType { DEPOSIT, WITHDRAW }; struct Date { int month, day, year; int hour, minute, second; }; struct Transaction { struct Node { Date date ; Transaction* tx ; // data

frey
Download Presentation

Linked Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked Lists Dr. Jose Annunziato

  2. The Bank Application enumTransactionType { DEPOSIT, WITHDRAW }; struct Date { int month, day, year; int hour, minute, second; }; struct Transaction { struct Node { Date date; Transaction* tx; // data TransactionType type; Node* next; string description; } float amount; };

  3. Linked Lists head Node tx next tx next tx next NULL Transaction

  4. Creating a List By Hand • Declaring the pointer: Transaction* tx1, tx2, tx3; Node* node1, node2, node3, head; • Creating the data: tx1 = new Transaction; (*tx1).amount = 1000.0; tx2 = new Transaction; (*tx2).amount = 2000.0; tx3 = new Transaction; (*tx3).amount = 3000.0;

  5. Creating the List Node* node1 = new Node; (*node1).next = NULL; (*node1).tx = tx1; Node* node2 = new Node; (*node2).next = node1; (*node2).tx= tx2; Node* node3 = new Node; (*node3).next = node2; (*node3).tx = tx3; head = node3;

  6. The -> Notation • Using (*structure).member notation can be cumbersome • Use the equivalent syntax structure->member instead This Syntax Is Equivalent To: (*tx1).amount = 1000.0; tx->amount = 1000.0; (*tx2).amount = 2000.0; tx->amount = 2000.0; (*tx3).amount = 3000.0; tx->amount = 2000.0;

  7. Create List Node* createList ( Transaction* transaction ) { Node* newNode = new Node; newNode->next = NULL; newNode->transaction = transaction; return newNode; }

  8. Push Node • Pushing a node needs to modify the head of the list, therefore we pass the address of an address (**) void push ( Node** list, Transaction* tx ) { Node* newNode = new Node; newNode->next = *list; newNode->transaction = tx; *list = newNode; }

  9. Traversing a List • Follow the next pointer until you reach NULL void displayList ( Node* list ) { Node* current = list; do { Transaction* tx = current->transaction; displayTransaction ( *tx ); current = current->next; } while ( current != NULL ); }

  10. Search By Amount • Search by traversing a list and comparing each node Transaction*searchAmount ( Node* list, float amt ) { Node* current = list; do { Transaction* tx = current->transaction; if ( tx->amount == amt ) return tx; current = current->next; } while ( current != NULL ); return NULL; }

  11. Search By Description Transaction*searchDesc ( Node* list, string desc) { Node* current = list; do { Transaction* tx = current->transaction; if ( tx->description == description ) return tx; current = current->next; } while ( current != NULL ); return NULL; }

  12. Search By Transaction • Search for a transaction you have a pointer to intsearchTxIndex ( Node* list, Transaction* tx1 ) { Node* current = list; int counter = 0; do { Transaction* tx = current->transaction; if ( tx == tx1) return counter; current = current->next; counter++; } while ( current != NULL ); return -1; }

  13. Retrieving Nodes By Index • Follow next until you reach the position Transaction* getAt ( Node* list, int position ) { Node* current = list; int counter = 0; do { Transaction* tx = current->transaction; if ( counter == position ) return tx; current = current->next; counter++; } while ( current != NULL ); return NULL; }

  14. Append to List • Go to end and then link last to new void append ( Node* list, Transaction* transaction ) { Node* current = list; do { Transaction* tx = current->transaction; current = current->next; } while ( current->next != NULL ); Node* newNode = new Node; newNode->next = NULL; newNode->transaction = transaction; current->next = newNode; }

  15. Inserting At the Beginning of a List • Inserting a node has several cases • If insertingat 0, just push voidinsertAt ( Node** list, Transaction* tx, int pos ) { if ( pos == 0 ) { push ( list, tx ); return; } … }

  16. Inserting Somewhere In the Middle void insertAt ( Node** list, Transaction* txn, int pos ) { … Node* current = *list;int counter = 1; do { Transaction* tx = current->transaction; if ( counter == pos ) { Node* newNode = new Node; newNode->next = current->next; newNode->transaction = txn; current->next = newNode; return; } current = current->next; counter++; } while ( current->next != NULL ); … }

  17. Inserting at the End • If inserting at the end, then just call append() void insertAt ( Node** list, Transaction* tx, int pos ) { if ( pos == 0 ) … do { … } while ( current->next != NULL ); append ( *list, tx ); }

  18. Delete First Element • Deleting first element updates head to next and deletes void deleteAt ( Node** list, int position ) { if ( position == 0 ) { Node* head = *list; *list = (*list)->next; delete head; return; } … }

  19. Deleting In the Middle void deleteAt ( Node** list, int position ) { … Node* prev = *list; Node* current = *list; current = current->next; int counter = 1; do { if ( counter == position ) { Node* deleteNode = current; prev->next = current->next; delete deleteNode; return; } prev = current; current = current->next; counter++; } while ( current->next != NULL ); … }

  20. Deleting At the End • If deleting the last, update next to last's next to NULL void deleteAt ( Node** list, int position ) { … prev->next = NULL; delete current; }

  21. LinkedListAlgorithmsDemo.cpp

More Related