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.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

COMP171 Fall 2005 Lists.
Stacks, Queues, and Linked Lists
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
CS Data Structures Chapter 4 Lists. Chain (1/3) Chain: Chain: A singly linked list in which the last node has a null link A singly linked list in.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
CS Data Structures II Review COSC 2006 April 14, 2017
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
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.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Chapter 4.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
Queue using an array. .head.tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Queues.
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.
Stacks, Queues, and Deques
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
Tutorial 5 Linked List Variations, Stack, & Queue.
Chapter 4 1. Why “linked lists” 2 IndexWord A[0]BAT A[1]CAT A[2]EAT … A[n]WAT Insert “FAT” ? Or delete something.
Reference: Vinu V Das, Principles of Data Structures using C and C++
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
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.
Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Linked List (Part II). Introduction  Definition of equivalence relation: A relation ≡ over a set S, is said to be an equivalence relation over S iff.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
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,
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
1 PART 4 Linked Lists Singly Linked Lists Circular Lists Applications Doubly Linked Lists Generalized Lists.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
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 Containers. Useful Linked List Add-Ons Are there new variables/changes to the lists as they have been defined that could make our jobs as.
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.
Reminder: Course Policy
C++ Programming:. Program Design Including
Lists CS 3358.
Stack and Queue APURBO DATTA.
Queues Mohammad Asad Abbasi Lecture 5
Stacks Stack: restricted variant of list
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
The Class chain.
Programmazione I a.a. 2017/2018.
Stacks, Queues, and Deques
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Stacks, Queues, and Deques
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
ADT list.
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
The Class chain.
Lists.
The Class chain.
Stacks, Queues, and Deques
Data Structures Chapter 4: Linked Lists.
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:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

How about delete the nth element?

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

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

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;

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

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;

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

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

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

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

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

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

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

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

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

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

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

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

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 – 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

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

Equivalence Classes Implementation

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.

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}

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.

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

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

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 ≡ first

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

first out , 11, , 7 1 null 2 1, 2 Linked stack: The first equivalence class

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

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

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

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.