Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.

Similar presentations


Presentation on theme: "Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next."— Presentation transcript:

1

2 Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next

3 Memory Layout abcdecaedb A linked representation uses an arbitrary layout. Layout of L = (a,b,c,d,e) using an array representation.

4 Linked Representation pointer (or link) in e is NULL caedb use a variable first to get to the first element a first

5 Normal Way To Draw A Linked List link or pointer field of node data field of node abcde NULL first

6 Chain A chain is a linked list in which each node represents one element. There is a link or pointer from one element to the next. The last node has a NULL (or 0) pointer. abcde NULL first

7 Node Representation class Node { private: char data; Node *link; }; link data

8 Get(0) Node* p; p = first; // gets you to first node return p  data; abcde NULL first

9 Get(1) Node* p; p = first  link; // gets you to second node return p  data; abcde NULL first

10 Get(2) Node* p; p = first  link  link; // gets you to third node return p  data; abcde NULL first

11 Get(n) Node* p=first; int count=0; while (count<n) { if (p==NULL) return; p=p->link; count++; } return p->data;

12 Delete An Element Delete(0) abcde NULL first Node* deleteNode; deleteNode = first; first = first  link; delete deleteNode;

13 abde NULL first c Delete(2) first get to node just before node to be removed c c beforeNode = first  link ; b beforeNode

14 Delete(2) save pointer to node that will be deleted deleteNode = beforeNode  link; beforeNode abcde null first

15 Delete(2) now change pointer in beforeNode beforeNode  link = deleteNode  link; delete deleteNode; beforeNode abcde null first

16 How about delete the nth element?

17 Delete An Element void Delete(int theIndex) { if (first == NULL) throw “Cannot delete from empty chain”; Node* deleteNode; if (theIndex == 0) {// remove first node from chain deleteNode = first; first = first->link; }

18 Delete An Element else { // use p to get to beforeNode Node* p = first; for (int i = 0; i < theIndex - 1; i++) {if (p == NULL) throw “Delete element does not exist”; p = p->next;} deleteNode = p->link; p->link = p->link->link; } delete deleteNode; }

19 Insert(0,’f’) abcde NULL first f p Step 1: get a node, set its data and link fields p = new Node; p->data=f; p->link=first;

20 Insert(0,’f’) abcde NULL first f p Step 2: update first first = p;

21 Insert(3,’f’) first find node whose index is 2 abcde NULL first f newNode beforeNode c next create a node and set its data and link fields newNode = new Node; newNode->data=‘f’; newNode->link=beforeNode->link; finally link beforeNode to newNode beforeNode  link = newNode;

22 Insert An Element void Insert(int theIndex,char data) { if (theIndex < 0) throw “Bad insert index”; Node *newnode = new Node; newnode->data=data; if (theIndex == 0) // insert at front { newnode->link=first; first = newnode; }

23 Inserting An Element else { // find predecessor of new element Node* p = first; for (int i = 0; i < theIndex - 1; i++) {if (p == 0) throw “Bad insert index”; p = p->next;} // insert after p newnode->link=p->link; p->link = newnode; }

24 Linked List Variations (1) “Single Linked List” There exists several Linked List Variations: –With or without Tail Pointer? –One link or double links per node? –Circular or not? –And combinations of these… Node 1Node 2 Node... Node N Head

25 Linked List Variations (2) Single Linked List with dummy head node –Pro: Simplify Single Linked List code –Cons: Same as standard Single Linked List DummyNode 1 Node... Node N Head

26 Linked List Variations (3) Single Linked List with Tail Pointer –Other than head, we also have tail pointer pointing to the last item –Pro: Can visit the tail very fast or add new item from tail –Cons: Slow to delete the tail…  Node 1Node 2 Node... Node N HeadTail

27 Linked List Variations (4) Circular Single Linked List –Tail->link = Head, the link cycles back to the head –Pro: Can visit all nodes from any node, good for Round Robin stuffs –Can choose to remember Tail only… –Cons: Slow to delete the tail…  Node 1Node 2 Node... Node N Head Node 1Node 2 Node... Node N Tail

28 Linked List Variations (5) Double Linked List –Two pointers per node: forward/backward –Pro: Can go backwards –Pro: If you have tail pointer, you can also delete tail efficiently! –Cons: Extra pointer is an overhead…  –Can be circular or not…, with or without tail… abcde NULL firstNode NULL lastNode

29 Double linked list The difficulties of using a singly linked list –The search of the list is limited to single direction. The only way to the preceding node is to start at the beginning. The same problem arises when one wishes to delete an arbitrary node from the list. Doubly linked list –A node in a doubly linked list has at least two fields left (left link): link to the preceding node right (right link): link to the following node

30 Representation class DbListNode { friend class DbList; public: DbListNode(int d=0, DbListNode *llink=0, DbListNode *rlink=0) { data = d; left = llink; right = rlink; }; private: int data; DbListNode *left, *right; }; class DbList { private: DbListNode *first, *last; }; leftrightleftrightleftright0 left0 first last dummy

31 Construction and Destruction DbList::DbList() { first = new DbListNode(); last = new DbListNode(); first->right = last; first->left = NULL; last->left = first; last->right = NULL; } DbList::~DbList() { while (first != NULL) { DbListNode *temp = first->right; delete first; first = temp; }

32 Inserting a Node into an Ordered List Suppose the list is sorted in non-decreasing order. void DbList::Insertion(int d) { DbListNode *temp = first->right; while (temp != last) { if (temp->data >= d) break; temp = temp->right; } DbListNode *newNode = new DbListNode(d, temp->left, temp); temp->left->right = newNode; temp->left = newNode; } leftright 1 leftright 2 leftright 3 temp temp->left newNode

33 Deleting a Node from an Ordered List bool DbList::Deletion(int d) { DbListNode *temp = first->right; while (temp != last) { if (temp->data == d) { temp->left->right = temp->right; temp->right->left = temp->left; delete temp; return true; } temp = temp->right; } return false; } leftright 1 leftright 2 leftright 3 temp->righttemp->lefttemp

34 Behavior: Last In First Out (LIFO) Stack implemented using Array with top pointer (Perhaps) the best implementation? –Using Single Link List with head pointer only –http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/LinkListAppl.htmlhttp://www.cosc.canterbury.ac.nz/people/mukundan/dsal/LinkListAppl.html Stack Node 1Node 2 Node... Node N Head ADT Stack using SLL as underlying data structure Node 1 Node 2 Node... Node N Head is Top of Stack Node 0 Top: efficient Push: efficient Pop: efficient

35 Behavior: First In First Out (FIFO) Queue implemented using Circular Array Better: use Single Linked List with Tail Pointer –Head pointer (front) for delete, Tail pointer (rear) for insert We set these roles because pointer directions in SLL (arrows to the right) imply that only insertion is efficient at tail (thus only ok for enqueue) and both insertion/deletion are efficient at head (but since we already use tail for enqueue, head is for dequeue) –Or use Circular Single Linked List: save 1 pointer: Head = Tail.next ADT Queue using SLL with tail pointer as underlying data structure, circular SLL also ok Queue Node 1Node 2 Node... Node N Head Front of Queue Tail = Rear of Queue Node N+1 rear: efficient insert: efficient Front: efficient delete: efficient

36 Equivalence Classes Implementation

37 Introduction Definition of equivalence relation: –A relation ≡ over a set S, is said to be an equivalence relation over S iff it is reflexive, symmetric, and transitive over S. Example: the “equal to” (=) relationship is an equivalence relation, since 1.x = x. 2.x = y implies y = x. 3.x = y and y = z implies x = z.

38 Equivalence Class Problem To partition the set S into equivalence classes such that two members x and y of S are in the same equivalence class iff x ≡ y. Example: 0 ≡ 4, 3 ≡ 1, 6 ≡ 10, 8 ≡ 9, 7 ≡ 4, 6 ≡ 8, 3 ≡ 5, 2 ≡ 11, and 11 ≡ 0. Then, we get the following equivalence classes: {0, 2, 4, 7, 11}; {1, 3, 5}; {6, 8, 9, 10}

39 Idea Phase 1 –The equivalence pairs (i, j) are read in and stored. Phase 2 –Begin at 0 and find all pairs of (0, j). If 0 and j are in the same class, include k if any (j, k) exists. –Because, i ≡ j and j ≡ k implies i ≡ k (transitivity). –Continue in this way until the entire equivalence class containing 0 has been found and output. –Start an object that is not output for finding new equivalence class.

40 Program –What kind of data structure can be used to hold these pairs? void Equivalence () { initialize; while more pairs { input the next pair (i, j); process this pair; } initialize for output; for (each object not yet output) output the equivalence class that contains this object; } void Equivalence () { initialize; while more pairs { input the next pair (i, j); process this pair; } initialize for output; for (each object not yet output) output the equivalence class that contains this object; }

41 Implementation Using Linked List Use one linked list to represent each row of the array pairs. 01234567891011 first first is a 1D array with each element first[i] is a pointer to the first node for row i.

42 Example – Phase 1 Consider the following equivalence relations: 0 ≡ 4, 3 ≡ 1, 6 ≡ 10, 8 ≡ 9, 7 ≡ 4, 6 ≡ 8, 3 ≡ 5, 2 ≡ 11, and 11 ≡ 0. 01234567891011 first 4031 10 698 4 70 8 69513 11 202 4

43 Example – Phase 2 A Boolean array out[n] is used for determining whether object i is yet to be printed. –The array is initialized to false. –For each i such that out[i] = false, the elements in the list first[i] are output. –For satisfying transitivity, a linked stack is created. out 00 01 00 23 00 45 00 67 00 89 00 1011

44 01234567891011 first 4031 10 698 4 70 8 69513 11 202 4 out 00 01 00 23 00 45 00 67 00 89 00 1011 1 4 0, 11, 4 11 7, 7 1 null 2 1, 2 Linked stack: The first equivalence class

45 Program void Equivalence () { read n;//read in the number of objects initialize first[0:n-1] to 0 and out[0:n-1] to false; while more pairs { input the next pair (i, j); put j on the chain first[i]; put I on the chain first[j]; } initialize for output; for (each object not yet output) output the equivalence class that contains this object; }

46 Program – ENode class class ENode { friend void Equivalence(); public: ENode(int d=0, ENode *next=0)//constructor {data = d; link = next;} private: int data; ENode *link; };

47 Program –Phase 1 void Equivalence() { // initialization for (int i=0; i<n, i++) first[i] = 0; for (int i=0; i<n, i++) out[i] = false; //Phase 1 while (not end of input) { read pair (x,y); first[x] = new ENode(y, first[x]); first[y] = new ENode(x, first[y]); }

48 Program – Phase 2 for (int i=0; i<n; i++) { if (!out[i]) { cout << endl; << "A new class: " << i; out[i] = true; ENode *x = first[i]; ENode *top = 0; //initialize stack while (1) { while (x) { int j = x->data; if (!out[j]) { cout << ", " << j; out[j] = true; push(j) } x = x->link; } if (stack is empty) break; x = pop(); } Check if the stack is empty Check every node of out[i] if out[i] is true.


Download ppt "Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next."

Similar presentations


Ads by Google