+ All Categories
Home > Documents > Linked Lists

Linked Lists

Date post: 22-Feb-2016
Category:
Upload: frey
View: 28 times
Download: 0 times
Share this document with a friend
Description:
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 - PowerPoint PPT Presentation
21
Linked Lists Dr. Jose Annunziato
Transcript
Page 1: Linked Lists

Linked Lists

Dr. Jose Annunziato

Page 2: Linked Lists

The Bank Applicationenum TransactionType { DEPOSIT, WITHDRAW };struct Date {

int month, day, year;int hour, minute, second;

};

struct Transaction { struct Node {Date date; Transaction* tx; // dataTransactionType type; Node* next;string description; }float amount;

};

Page 3: Linked Lists

Linked Lists

tx next tx next tx next

head Node

Transaction

NULL

Page 4: Linked Lists

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;

Page 5: Linked Lists

Creating the ListNode* 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;

Page 6: Linked Lists

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;

Page 7: Linked Lists

Create List

Node* createList ( Transaction* transaction ) {Node* newNode = new Node;newNode->next = NULL;newNode->transaction = transaction;return newNode;

}

Page 8: Linked Lists

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;

}

Page 9: Linked Lists

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 );}

Page 10: Linked Lists

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;}

Page 11: Linked Lists

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;}

Page 12: Linked Lists

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;

}

Page 13: Linked Lists

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;

}

Page 14: Linked Lists

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;

}

Page 15: Linked Lists

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;

}…

}

Page 16: Linked Lists

Inserting Somewhere In the Middlevoid 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 );…}

Page 17: Linked Lists

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 );

}

Page 18: Linked Lists

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;}…

}

Page 19: Linked Lists

Deleting In the Middlevoid 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 );…

}

Page 20: Linked Lists

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;

}

Page 21: Linked Lists

LinkedListAlgorithmsDemo.cpp


Recommended