1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: What operations should we implement?
DATA STRUCTURES USING C++ Chapter 5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Data Structures: A Pseudocode Approach with C
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
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.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Typical operations on data –Add data.
COMP103 - Linked Lists (Part B)1 Chapter 17 Linked List as Objects.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Data Structures Using C++ 2E
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
(1 - 1) Introduction to C Data Structures & Abstract Data Types Instructor - Andrew S. O’Fallon CptS 122 (August 26, 2015) Washington State University.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
CS2006- Data Structures I Chapter 5 Linked Lists III.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists (part 2)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists (part 2)
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 6. Dictionaries(3): Binary Search Trees.
What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
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.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
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.
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
Chapter 16: Linked Lists.
CSCE 210 Data Structures and Algorithms
C++ Programming:. Program Design Including
CSCE 210 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
CS505 Data Structures and Algorithms
Linked Lists Chapter 6 Section 6.4 – 6.6
Data Structures and Algorithms
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Chapter 4 Linked Lists
Linked Lists Chapter 4.
Chapter 16-2 Linked Structures
Linked Lists.
Chapter 4 Linked Lists.
Chapter 18: Linked Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Doubly Linked List Implementation
Data Structures and Algorithms
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
(1 - 2) Introduction to C Data Structures & Abstract Data Types
Doubly Linked List Implementation
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

1 Data Structures and Algorithms Linked List

2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation Example Application

3 2. The Linked List Linked List Abstraction: Linked List: a container of data in the form of a linear configuration of nodes in which we can insert and delete nodes in any order. Each Node is linked to its successor in the list. If it also supports search by contents, it can represent a dictionary ADT. Node: a container for one data item and has a direct relationship with at most two other nodes, its predecessor (if any) and its successor (if any). Head node or first node is the only node without a predecessor. Current node: special node in the list, indicated by the current position. Previous Node: the predecessor of the current node

4 Ordered Linked List Class We will construct a class “List” whose objects are linked lists. They will be implemented as dynamic lists. The data members will be the nodes and the pointers to these nodes. A node contains a key field and a data field. Search is by content (key) The list will be ordered on the key field.

5 (a) ADT Linked List Linked List Data Members  Nodes. Each node has: Data or information field of type dataType. A key of type keyType Link field (next), a pointer to next node  Pointers: head, a pointer to the first node; cursor, a pointer to the current node; prev, a pointer to the previous node.

6 Data Members head NULL cursor First Last prev Current keydatanext

7 Node Specification // The linked structure for a node can be // specified as a Class in the private part of // the main linked list class. class node// Hidden from user { public: keyType key; // key dataType data;// Data node *next;// pointer to next node }; // end of class node declaration typedef node * NodePointer; // Pointers NodePointer head, cursor, prev;

8 Linked List Operations NotationMeaning headthe head pointer cursorpointer to current node prevpointer to predecessor node pnewpointer to a new node d item with the same type as the data portion of a node kitem with type as the key portion of the node bboolean value LLength of list

9 Linked List Class Operations construct & initialize list to empty listIsEmpty  b : return True if list is empty curIsEmpty  b : return True if current position is empty toFirst : to make the current node the first node; if list is empty, the current position is still empty atFirst  b : to return True if the current node is the first node or if the list and the current position are both empty.

10 Linked List Class Operations advance : to advance to next node. Assume the current position is nonempty initially. toEnd : to make the current node the tail node; if list is empty, the current position is still empty atEnd  b : to return True if the current node is the tail node or if the list and the current position are both empty. listSize  L : to return the size of the list updateData (d) : to update the data portion of the current node to contain d; assume the current position is nonempty.

11 Linked List Class Operations retrieveData  d: to return the data in the current node; assume the current position is nonempty. insertFirst (k,d) : insert a node with key (k) and data (d) at the head of the list; the new node becomes the current node. insertAfter (k,d) : insert a node after the current node without changing the current position; assume the current position is nonempty in a non-empty list. insertBefore (k,d) : insert a node before the current node ; current position becomes the new node

12 Linked List Class Operations insertEnd(k,d): insert a node at the end of the list, current position becomes the new node. deleteNode : delete the current node and set the current position to the next node; if the current node is the last node initially, the current position becomes empty; assume the current position is nonempty initially. deleteFirst: delete the first node and set the current position to the next node; if it was initially the only node, the current position becomes empty;

13 Linked List Class Operations deleteEnd: delete the last node and set the current position to empty. makeListEmpty : delete whole list search (k)  b : search the list for the node with key part that matches (k). If found, set cursor to the node and return True, else return false and the current position becomes empty.

14 Linked List Class Operations orderInsert (k,d) : insert a node in a position that maintains an ascending order of the key portion of the nodes. traverse: traverse list to print key and info fields. The Linked List will be implemented as a template class to allow different types for the key and data fields.

15 // File: List.h // Definition of Simple Linked List Template Class #ifndef LIST_H #define LIST_H // Specification of the class template class List { public: (b) Linked List Class Definition

16 List Class Header File // Member Functions // Create an empty List List(); // Class Destructor ~List(); // Functions Prototype Definitions bool listIsEmpty() const; bool curIsEmpty() const; void toFirst(); bool atFirst() const; void advance();

17 List Class Header File void toEnd(); bool atEnd() const; int listSize() const; void updateData (const dataType & ); void retrieveData (dataType &) const; void insertFirst (const keyType &, const dataType & ); void insertAfter (const keyType &, const dataType & ); void insertBefore (const keyType &, const dataType & ); void insertEnd (const keyType &, const dataType & ); void deleteNode(); void deleteFirst();

18 List Class Header File void deleteEnd(); void makeListEmpty(); bool search (const keyType & ); void orderInsert(const keyType &, const dataType & ); void traverse();

19 List Class Header File private: // Node Class class node { public: keyType key; // key dataType data;// Data node *next;// pointer to next node }; // end of class node declaration keydatanext

20 List Class Header File typedef node * NodePointer; // Pointers NodePointer head, cursor, prev; }; // End of class List declaration #endif // LIST_H #include "List.cpp"

21 Part of Implementation file // Class Constructor template List ::List() { head = NULL; cursor = NULL; prev = NULL; }

22 // Class destructor template List ::~List() { makeListEmpty(); } // return True if list is empty template bool List ::listIsEmpty() const { return (head == NULL); }

23 // to make the current node the first node; // if list is empty, // the current position is still empty template void List ::toFirst() { cursor = head; prev = NULL; }

24 // to advance to next node. Assume //the current position // is nonempty initially. template void List ::advance() { prev = cursor; cursor = cursor->next; }

25 // to make the current node the tail node; // if list is empty, the current position is still empty template void List ::toEnd() { toFirst(); if (! listIsEmpty()) while ( cursor->next != NULL) advance(); }

26 // to return the size of the list template int List ::listSize() const { NodePointer q; int count; q = head;count = 0; while (q != NULL) { count++; q = q->next; } return count; }

27 // insert a node with data at the head of the list // the new node becomes the current node. template void List ::insertFirst(const keyType &k, const dataType &d ) { NodePointer pnew; pnew = new node; pnew->key = k; pnew->data = d; pnew->next = head; head = pnew; cursor = head; prev = NULL; }

28 Learn on your own about: Check the rest of the list operation Array-based implementation of Linked Lists