Download presentation
Presentation is loading. Please wait.
Published byYuliani Halim Modified over 6 years ago
1
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
2
Outline Singly linked list Data Object Operations 9/21/2018 7:08 PM
3
Outline Singly linked list Data Object Operations 9/21/2018 7:08 PM
4
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
5
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
6
Outline Singly linked list Data Object Operations 9/21/2018 7:08 PM
7
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
8
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
9
Create an empty list Chain::Chain() : size(0), first(0) { }
9/21/2018 7:08 PM Linked list
10
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
11
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
12
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
13
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
14
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
15
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
16
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
17
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
18
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
19
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
20
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
21
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
22
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
23
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
24
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
25
Thank you! 9/21/2018 7:08 PM Linked list
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.