LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: –...

40
LAB#4

Transcript of LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: –...

Page 1: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

LAB#4

Page 2: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

Linked List :Linked List :

• A linked list is a series of connected nodes.• Each node contains at least:

– A piece of data (any type)– Pointer to the next node in the list

• Head: pointer to the first node.• The last node points to NULL.

A 0

Head

B C

A

data pointer

node

Tail

Page 3: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

SinglySingly Linked List :Linked List :• We use two classes: Node and List

o Declare IntSLLNode class for the nodes

class IntSLLNode{ public:

IntSLLNode() { next = 0; } IntSLLNode(int i, IntSLLNode *ptr = 0)

{ info = i; next = ptr; }

int info; IntSLLNode *next; };

Page 4: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

SinglySingly Linked List :Linked List :o Declare IntSLList which contains :

class IntSLList {public:IntSLList() {head = tail =0; }void AddToHead(int);void AddToTail(int);

void DeleteFromHead;)(void DeleteFromTail;)(

void DeleteNode(int);bool isInList(int) const;

void DisplayList;)(private:

IntSLLNode *head, *tail; };

Page 5: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

Linked Lists 5

Inserting at the Head

1. Allocate a new node2. Insert new element3. Make new node

point to old head4. Update head to point

to new node

Page 6: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

SinglySingly Linked List :Linked List :o Declare IntSLList Class member function:1- void AddToHead(int);

void IntSLList::AddToHead(int el){

head = new IntSLLNode(el,head); if (tail == 0) tail = head;

}

Page 7: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

Linked Lists 7

Inserting at the Tail

.1Allocate a new node

.2Insert new element

.3Have new node point to null

.4Have old last node point to new node

.5Update tail to point to new node

Page 8: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

SinglySingly Linked List :Linked List :o Declare IntLList Class member function:2- void AddToTail(int);

void IntSLList::AddToTail(int el){

if (tail != 0) // if list not empty; { tail->next = new IntSLLNode(el);

tail = tail->next; } else

head = tail = new IntSLLNode(el);}

Page 9: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

Linked Lists 9

Removing at the Head

1. Update head to point to next node in the list

2. Allow garbage collector to reclaim the former first node

Page 10: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

SinglySingly Linked List :Linked List :o Declare IntLList Class member function:3- void DeleteFromHead();

void IntSLList::DeleteFromHead{)(if(head !=0){IntSLLNode *tmp =head;

if (head == tail) //if only one node in the list head = tail = 0;

else head = head ->next;delete tmp};

}

Page 11: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

Linked Lists 11

Removing at the Tail

• Removing at the tail of a singly linked list cannot be efficient!

• There is no constant-time way to update the tail to point to the previous node

Page 12: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

SinglySingly Linked List :Linked List :4- void DeleteFromTail();

void IntSLList::DeleteFromTail)({if(head != 0)

{if (head == tail) //if only one node in the list{delete head;

head=tail=0};else { IntSLLNode *tmp; //find the predecessor of tailfor(tmp=head; tmp->next != tail; tmp = tmp->next);delete tail;tail=tmp;

tail->next=0}};}

Page 13: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

SinglySingly Linked List :Linked List :o Declare IntLList Class member function:5- int DeleteNode(int el);

Page 14: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.
Page 15: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

SinglySingly Linked List :Linked List :o Declare IntLList Class member function:6- bool isInList(int) const;

bool IntSLList:: isInList(int el) const{IntSLLNode *tmp;for (tmp=head; tmp != 0 && !(tmp->info == el);tmp = tmp->next);return tmp !=0;}

Page 16: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

SinglySingly Linked List :Linked List :o Declare IntSLList Class member function:7- void DisplayList();

void IntSLList::DisplayList(){IntSLLNode *current;current = head;cout << "head = " << head << "\n";while(current != 0){cout << current->info << " " << current << "\n";current=current->next;}cout << "tail = " << tail << "\n";cout << "----------------------" << "\n";}

Page 17: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

SinglySingly Linked List :Linked List :• Using List :

void main(){IntSLList myllist;myllist.AddToHead(50);myllist.AddToHead(90);myllist.AddToHead(60);myllist.AddToHead(68);

myllist.DisplayList;)(myllist.DeleteFromHead;)(

myllist.DeleteNode(60);if (myllist.isInList(60)== 0)cout<<"60 isn't in the list" << endl;cout<<"60 is in the list" << endl;

myllist.DisplayList;)(}

Page 18: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

Linked Lists 18

Doubly Linked List• A doubly linked list is often more

convenient! • Nodes store:

– element– link to the previous node– link to the next node

• Special trailer and header nodes

prev next

elem

trailerheader nodes/positions

elements

node

Page 19: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

DoublyDoubly Linked List :Linked List :o Declare IntDLLNode which contains :

class IntDLLNode{public:IntDLLNode() {next=prev=0;}IntDLLNode(int el,IntDLLNode *n=0,IntDLLNode *p=0){info = el; next=n; prev =p;}Int info;IntDLLNode *next, *prev;};

Page 20: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

DoublyDoubly Linked List :Linked List :o Declare IntDLList which contains :

class IntDLList{public:IntDLList(){Head=tail=0;}

void addToDLLTail(int el);void deleteFromDLLTail();void IntDLList::DisplayFromHead();protected:IntDLLNode *head ,*tail;};

Page 21: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

Linked Lists 21

Insertion• We visualize operation insertAfter(p, X), which returns position q

A B X C

A B C

p

A B C

p

X

q

p q

Page 22: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

Linked Lists 22

Insertion AlgorithmAlgorithm insertAfter(p,e):

Create a new node vv.setElement(e)v.setPrev(p) {link v to its predecessor}v.setNext(p.getNext()) {link v to its successor}(p.getNext()).setPrev(v) {link p’s old successor to v}p.setNext(v) {link p to its new successor, v}return v {the position for the element e}

Page 23: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

DoublyDoubly Linked List :Linked List :o Declare IntDLList Class member function:1- void addToDLLTail(int el);

void IntDLList:: addToDLLTail(int el){if (tail!=0){tail=new IntDLLNode(el,0,tail);tail->prev->next=tail;}elsehead=tail=new IntDLLNode(el);}

Page 24: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

Linked Lists 24

Deletion• We visualize remove(p), where p == last()

A B C D

p

A B C

D

p

A B C

Page 25: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

Linked Lists 25

Deletion Algorithm

Algorithm remove(p):t = p.element {a temporary variable to hold the

return value}(p.getPrev()).setNext(p.getNext()) {linking out p}(p.getNext()).setPrev(p.getPrev())p.setPrev(null){invalidating the position p}p.setNext(null)return t

Page 26: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

DoublyDoubly Linked List :Linked List :o Declare IntDLList Class member function:2- void deleteFromDLLTail()

void IntDLList:: deleteFromDLLTail(){if(tail !=0){if(head==tail) { //if only one node in the listdelete head;head = tail =0;}else {tail = tail->prev;delete tail->next;tail->next = 0;}}}

Page 27: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

DoublyDoubly Linked List :Linked List :o Declare IntDLList Class member function:2- void IntDLList::DisplayFromHead();

void IntDLList::DisplayFromHead)({

IntDLLNode *current;

for( current = head ; current != 0 ; current = current->next )

cout<<current->info<<endl; cout<<"--------------------------------"<<endl;

}

Page 28: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

Exercise # 1: Complete, compile and run the following program.

#include <iostream>using namespace std;//------ class IntSLLNode for creating new node ---------------//class IntSLLNode{ public: IntSLLNode() { next = 0; } IntSLLNode(int i, IntSLLNode *ptr = 0) { info = i; next = ptr; } int info; IntSLLNode *next;};//------ class IntSLList for dealing with nodes ---------------//class IntSLList {public:IntSLList() {head = tail =0; }void AddToHead(int);void AddToTail(int);void DisplayList();

Page 29: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

private:IntSLLNode *head, *tail; };void IntSLList::AddToHead(int el){}void IntSLList::AddToTail(int el){}void IntSLList::DisplayList(){}

Page 30: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

void main(){IntSLList myllist;myllist.AddToHead(50);myllist.AddToHead(90);myllist.AddToHead(60);myllist.DisplayList();myllist.AddToTail(88);myllist.AddToTail(77);myllist.AddToHead(66);myllist.AddToHead(50);myllist.DisplayList();}

Page 31: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

TheAnswer# #include <iostream>using namespace std;//------ class IntSLLNode for creating new node ---------------//class IntSLLNode{ public:

IntSLLNode() { next = 0; } IntSLLNode(int i, IntSLLNode *ptr = 0)

{ info = i; next = ptr; }

int info; IntSLLNode *next;

};//------ class IntSLList for dealing with nodes ---------------//class IntSLList {public:IntSLList() {head = tail =0; }void AddToHead(int);void AddToTail(int);void DisplayList();private:IntSLLNode *head, *tail; };

Page 32: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

void IntSLList::AddToHead(int el){head= new IntSLLNode (el,head);If (tail==0)Tail= head;}void IntSLList::AddToTail(int el){ if (tail !=0 ) // if list not empty{tail-> next = new IntSLLNode (el);tail=tail->next;}}

Page 33: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

void IntSLList::DisplayList(){IntSLLNode *current;current = head;cout << "head = " << head << "\n";while(current != 0){cout << current->info << " " << current << "\n";current=current->next;}cout << "tail = " << tail << "\n";cout << "----------------------" << "\n";} }void main(){IntSLList myllist;myllist.AddToHead(50);myllist.AddToHead(90);myllist.AddToHead(60);myllist.DisplayList();myllist.AddToTail(88);myllist.AddToTail(77);myllist.AddToHead(66);myllist.AddToHead(50);myllist.DisplayList();}

Page 34: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

Exercise # 2: Edit the previous program to:Add (30) to the head.Add (22) to the tail.Display all nodes.

Page 35: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

#include <iostream>using namespace std;//------ class IntSLLNode for creating new node ---------------//class IntSLLNode{ public: IntSLLNode() { next = 0; } IntSLLNode(int i, IntSLLNode *ptr = 0) { info = i; next = ptr; } int info; IntSLLNode *next;};//------ class IntSLList for dealing with nodes ---------------//class IntSLList {public:IntSLList() {head = tail =0; }void AddToHead(int);void AddToTail(int);void DisplayList();private:IntSLLNode *head, *tail; };

Page 36: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

void IntSLList::AddToHead(int el){head= new IntSLLNode (el,head);if (tail==0)tail= head;

}void IntSLList::AddToTail(int el){ {tail-> next = new IntSLLNode (el);tail=tail->next;}}

Page 37: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

void IntSLList::DisplayList(){IntSLLNode *current;current = head;cout << "head = " << head << "\n";while(current != 0){cout << current->info << " " << current << "\n";current=current->next;}cout << "tail = " << tail << "\n";cout << "----------------------" << "\n";}

void main(){IntSLList myllist;myllist.AddToHead(30);myllist.DisplayList();myllist.AddToTail(22);myllist.DisplayList();}

Page 38: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.
Page 39: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

Evolution question

• Create empty linked list and then add a new node with data 50

Page 40: LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.

Answer of Evolution question