Linked List Yumei Huo Department of Computer Science

Slides:



Advertisements
Similar presentations
Linear Lists – Array Representation
Advertisements

DATA STRUCTURES USING C++ Chapter 5
Queue Definition Ordered list with property: –All insertions take place at one end (tail) –All deletions take place at other end (head) Queue: Q = (a 0,
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
Linear Lists – Linked List Representation CSE, POSTECH.
L.Chen1 Chapter 3 DATA REPRESENTATION(2) L.Chen A Chain Iterator Class A Chain Iterator Class ( 遍历器类 )  An iterator permits.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
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:
3/19/2016 5:40 PMLinked list1 Array-based List v.s. Linked List Yumei Huo Department of Computer Science College of Staten Island, CUNY Spring 2009.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
CS212: Data Structures and Algorithms Lecture # 4 Linked List.
Sequences 5/10/2018 2:01 PM Linked Lists Linked Lists.
CSCI 3333 Data Structures Stacks.
UNIT – I Linked Lists.
Big-O notation Linked lists
Lists CS 3358.
CISC181 Introduction to Computer Science Dr
Stacks.
Programming Abstractions
Stack and Queue APURBO DATTA.
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
Chapter 4 Linked Lists
LinkedList Class.
Chapter 20: Binary Trees.
The Class chain.
Programmazione I a.a. 2017/2018.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Recursion.
Chapter 16-2 Linked Structures
Chapter 21: Binary Trees.
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Object Oriented Programming COP3330 / CGS5409
Chapter 4 Linked Lists.
Chapter 18: Linked Lists.
Linked List (Part I) Data structure.
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Basics of Algorithm Analysis
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Programming Abstractions
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Chapter 4 Linked Lists.
Chapter 17: Linked Lists.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
The Class chain.
Linked Lists.
The Class chain.
CS150 Introduction to Computer Science 1
Lecture No.02 Data Structures Dr. Sohail Aslam
Data Structures & Algorithms
General List.
ICOM 4015 Advanced Programming
More on Linked List Yumei Huo Department of Computer Science
21 Data Structures.
Stacks and Linked Lists
Presentation transcript:

Linked List Yumei Huo Department of Computer Science Sequences 9/21/2018 7:08 PM Linked List Yumei Huo Department of Computer Science College of Staten Island, CUNY 9/21/2018 7:08 PM Linked list

Outline Singly linked list Data Object Operations 9/21/2018 7:08 PM

Outline Singly linked list Data Object Operations 9/21/2018 7:08 PM

Singly Linked List (Chain) A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores Data (element) link to the next node next element node A B C D first  9/21/2018 7:08 PM Linked list

Singly Linked List v.s. Array Access an element Singly Linked List: O(n) Array: O(1) Memory Allocation Singly Linked List: no unused space; no need to be continuous space Array: Unused space Must be continuous space first  A B C D Singly Linked List S[10] A B C D S[3] Array 9/21/2018 7:08 PM Linked list

Outline Singly linked list Data Object Operations 9/21/2018 7:08 PM

Operations Create a linked list Determine whether the list is empty Determine the length of the list Find the kth element Search for a given element Delete the kth element Insert a new element just after the kth …. 9/21/2018 7:08 PM Linked list

Class definition for a linked list class Chain { public : Chain(); ~Chain(); bool IsEmpty() const; int Length() const ; bool Find(int k, int& x) const ; int Search(const int& x) const ; void Delete(int k, bool &success); void Insert(int k, int x, bool &success); … private : struct ChainNode{ int data; ChainNode * next; }; int size; ChainNode *first; // pointer to first node ChainNode * GetAt(int k) const; 9/21/2018 7:08 PM Linked list

Create an empty list Chain::Chain() : size(0), first(0) { } 9/21/2018 7:08 PM Linked list

Delete all nodes in a chain Chain::~Chain() {// Chain destructor. //Delete all nodes in chain. ChainNode *temp; while (first) { temp = first; first=first->next; delete temp; } The running time is O(n) 9/21/2018 7:08 PM Linked list

Determine the length of a chain int Chain::Length() const { return size; } Note: if size is not declared, then you can find the length by the following program segment: {// Return the number of elements in the chain. ChainNode *current = first; int len = 0; while (current) { len++; current = current->next; return len; The running time is O(n) 9/21/2018 7:08 PM Linked list

Get the kth node of a chain (ChainNode * GetAt(int k) const;) p ChainNode *p = first; k=3 A B C D first E  if (k <= 0) return 0; 9/21/2018 7:08 PM Linked list

Get the kth node of a chain (ChainNode * GetAt(int k) const;) p k=3 A B C D first E  if (k <= 0) return 0; ChainNode *p = first; 9/21/2018 7:08 PM Linked list

Get the kth node of a chain (ChainNode * GetAt(int k) const;) p k=3 A B C D first E  if (k <= 0) return 0; ChainNode *p = first; for(int index=1; index < k && p; ++index) p=p->next; 9/21/2018 7:08 PM Linked list

Get the kth node of a chain ChainNode* Chain::GetAt(int k) const { if (k <= 0) return 0; ChainNode *p = first; for(int index=1; index < k && p; ++index) p=p->next; return p; } The running time is O(k) 9/21/2018 7:08 PM Linked list

Find the kth element of a chain bool Chain::Find(int k, T& x) const { ChainNode * current = GetAt(k); if (!current) return false; // no kth element x = current->data; return true; } The running time is O(k) 9/21/2018 7:08 PM Linked list

Search for a given element int Chain::Search(int x) const { ChainNode *current = first; int index = 1; while (current && current->data != x) { current = current->next; index++; } if (current) return index; return 0; The running time is O(n) 9/21/2018 7:08 PM Linked list

Deleting the kth element (k=1) ChainNode *p = first; p x = p->data; delete p; A B C D first E  first = first->next; // remove B C D first E  9/21/2018 7:08 PM Linked list

Deleting the kth element (k=4) q ChainNode * q = GetAt(k-1); ChainNode * p = q->next; p x = p->data; delete p; A B C D first E  q->next = p->next; if (!q || !q->next) throw OutOfBounds(); // no kth A B C first E  9/21/2018 7:08 PM Linked list

Deleting the kth element (cont.) void Chain::Delete(int k, int & x, bool & success) { success=(k>=1 && k<=size); ChainNode* p=first; if (k == 1 && first) first = first->next; else { // use q to get to k-1st ChainNode * q = GetAt(k-1); p = q->next; // kth q->next = p->next; } x = p->data; delete p; // Note: if size is not declared, and no kth element, you can use the following statement to replace success=(k>=1 && k<=size); if (!q || !q->next) throw OutOfBounds(); The running time is O(k) 9/21/2018 7:08 PM Linked list

Insert a new element (data D) just after the kth (k=0) first A B C first = y;  y->next = first; D ChainNode *y = new ChainNode; y->data = ‘D’; y D A B first C  9/21/2018 7:08 PM Linked list

Insert a new element (data D) just after the kth (k=2) p ChainNode * p = GetAt(k); first A B C p->next = y;  y->next = p->next; D ChainNode *y = new ChainNode; y->data = ‘D’; y if (k!=0 && !p) throw OutOfBounds(); // no kth A B D first C  9/21/2018 7:08 PM Linked list

Insert a new element just after the kth void Chain::Insert(int k, int x) { ChainNode *p = GetAt(k); if (k!=0 && !p) throw OutOfBounds(); // no kth ChainNode *y = new ChainNode; y->data = x; if(k==0){ y->next = first; first = y; } else{ y->next = p->next; p->next = y; return *this ; The running time is O(k) 9/21/2018 7:08 PM Linked list

Running Time Summary to Singly Linked List Determine the length of the list Size is declared in the class ---- O(1) Do not need to visit any element, just return size Size is not declared in the class ---- O(n) Visit n elements Find the kth element ----O(k) K nodes need to be visited Search for a given element ----O(n) Worst case: n nodes need to be visited Average case: n/2 nodes need to be visited Delete the kth element ----O(k) Search the first k-1 element, then delete the kth element Insert a new element just after the kth ----O(k) Search the first k-1 element, then insert the new element after the kth element 9/21/2018 7:08 PM Linked list

Thank you! 9/21/2018 7:08 PM Linked list