Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Lists Dr. Jose Annunziato.

Similar presentations


Presentation on theme: "Linked Lists Dr. Jose Annunziato."— Presentation transcript:

1 Linked Lists Dr. Jose Annunziato

2 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 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 = ; tx2 = new Transaction; (*tx2).amount = ; tx3 = new Transaction; (*tx3).amount = ;

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 = ; tx->amount = ; (*tx2).amount = ; tx->amount = ; (*tx3).amount = ; tx->amount = ;

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
int searchTxIndex ( 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 inserting at 0, just push void insertAt ( 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


Download ppt "Linked Lists Dr. Jose Annunziato."

Similar presentations


Ads by Google