Presentation is loading. Please wait.

Presentation is loading. Please wait.

LAB#4#5 Linked List.

Similar presentations


Presentation on theme: "LAB#4#5 Linked List."— Presentation transcript:

1 LAB#4#5 Linked List

2 Linked List Linked List :
A B C Head Tail 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. node A data pointer

3 Linked List Singly Linked List : We use two classes: Node and List
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; };

4 Linked List Singly Linked List : 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; };

5 Linked List Singly Linked List : void IntSLList::AddToHead(int el) {
Declare IntSLList Class member function: 1- void AddToHead(int); void IntSLList::AddToHead(int el) { head = new IntSLLNode(el,head); if (tail == 0) tail = head; }

6 Linked List Singly Linked List : void IntSLList::AddToTail(int el) {
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); }

7 Linked List Singly Linked List : void IntSLList::DeleteFromHead(){
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;} }

8 Linked List Singly 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 tail for(tmp=head; tmp->next != tail; tmp = tmp->next); delete tail; tail=tmp; tail->next=0;}} }

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

10 Linked List

11 Linked List Singly Linked List :
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; }

12 Linked List Singly Linked List : void IntSLList::DisplayList()
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";}

13 Linked List Singly 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; Else cout<<"60 is in the list" << endl; myllist.DisplayList();}

14 Linked List Doubly Linked List : 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; };

15 Linked List Doubly Linked List : Declare IntDLList which contains :
class IntDLList{ public: IntDLList(){ Head=tail=0;} void addToDLLTail(int el); void deleteFromDLLTail(); void DisplayFromHead(); protected: IntDLLNode *head ,*tail; };

16 Linked List Doubly Linked List : void IntDLList:: addToDLLTail(int el)
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;} else head=tail=new IntDLLNode(el); }

17 Linked List Doubly Linked List : void IntDLList:: deleteFromDLLTail()
Declare IntDLList Class member function: 2- void deleteFromDLLTail() void IntDLList:: deleteFromDLLTail() {if(tail !=0){ if(head==tail) { //if only one node in the list delete head; head = tail =0;} else { tail = tail->prev; delete tail->next; tail->next = 0;}}}

18 Linked List Doubly Linked List : void IntDLList::DisplayFromHead() {
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; }


Download ppt "LAB#4#5 Linked List."

Similar presentations


Ads by Google