210 likes | 435 Views
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
E N D
Linked Lists Dr. Jose Annunziato
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; };
Linked Lists head Node tx next tx next tx next NULL Transaction
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;
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;
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;
Create List Node* createList ( Transaction* transaction ) { Node* newNode = new Node; newNode->next = NULL; newNode->transaction = transaction; return newNode; }
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; }
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 ); }
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; }
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; }
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; }
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; }
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; }
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; } … }
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 ); … }
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 ); }
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; } … }
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 ); … }
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; }