Linked Lists Dr. Jose Annunziato.

Slides:



Advertisements
Similar presentations
Chapter 17 Linked Lists.
Advertisements

Page 11 Solutions to Practice Problems Using a Linked List From Previous Days Notes Create C functions to solve the following problems with linked lists.
Linked Lists.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
CSCI2100B Linked List Jeffrey
Data Structure Lecture-5
Doubly-linked list library.
1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Ceng-112 Data Structures I 1 Chapter 3 Linear Lists.
Computer Programming Link List (Insertion, Printing and Deletion functions) Lecture 23.
Structured Data Dr. Jose Annunziato. Abstract Data Types Abstract Data Types (ADTs) are custom types that are defined in terms of other types, including.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Reference: Vinu V Das, Principles of Data Structures using C and C++
Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Linked List Chapter Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
CS212: DATASTRUCTURES Lecture 3: Searching 1. SinglyLinked list 2 A linked list is a series of connected nodes. Each node contains at least: – A piece.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Chapter 16: Linked Lists.
Programming Circular Linked List.
C++ Programming:. Program Design Including
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Review Deleting an Element from a Linked List Deletion involves:
Linked List.
List ADT & Linked Lists.
Traversing a Linked List
Linked Lists Damian Gordon.
Linked List Variations
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
CSCE 210 Data Structures and Algorithms
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Chapter 20: Binary Trees.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 21: Binary Trees.
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Chapter 18: Linked Lists.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Recursive Linked Lists
Linked Lists Chapter 4.
Chapter 17: Linked Lists.
Linked Lists Adapted from Dr. Mary Eberlein, UT Austin.
Lecture 14 Linked Lists CSE /26/2018.
ECE 103 Engineering Programming Chapter 63 Queue Implementation
CS148 Introduction to Programming II
Data Structures & Algorithms
General List.
Recursive Linked Lists
Recursive Linked Lists
CS148 Introduction to Programming II
Linked Lists.
Presentation transcript:

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

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

LinkedListAlgorithmsDemo.cpp