chapter+2b+ +linked+list+ +implementation +implementation

Upload: thanh-bi-nguyen

Post on 14-Apr-2018

263 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    1/53

    Data Structures and Algorithms C++ Implementation

    Ho Chi Minh City University of TechnologyFaculty of Computer Science and Engineering

    BKTP.HCM

    BKTP.HCM

    Hunh Tn t

    Email: [email protected] Page: http://www.cse.hcmut.edu.vn/~htdat/

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    2/53

    Pointer in C++

    Declaration

    Node *ptr;

    Create an object

    ptr = new Node();

    A pointer usage

    ptr

    ptr

    ???

    Slide 2Faculty of Computer Science and Engineering HCMUT

    pr n a a n no e: , p r-> a a ;Destroy an object

    delete ptr;

    NULL pointerptr = NULL;

    ptr

    ???

    ptr

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    3/53

    Pointer in C++

    Be careful in these cases:

    ptr1

    ptr2

    Before

    Before

    ptr1

    ptr2

    Slide 3Faculty of Computer Science and Engineering HCMUT

    After

    ptr1 = ptr2;

    ptr1

    ptr2

    After

    delete ptr1; ptr1 = NULL;

    ptr1

    ptr2

    Garbage

    Dangling reference problem

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    4/53

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    5/53

    Parameter Passing Techniques

    void func(int* &a, int* b){

    int *t;

    t = a;

    a = b;

    b = t;

    void main() {int *p1 = new int;*p1 = 10;

    int *p2 = new int;*p2 = 20;func(p1, p2);

    *

    Slide 5Faculty of Computer Science and Engineering HCMUT

    void func(int* a, int* &b){

    int *t;

    t = a;

    a = b;

    b = t;

    }

    ,printf(%d, *p2);}

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    6/53

    Parameter Passing Techniques

    void func(int **a, int **b){

    int *t;

    t = *a;

    *a = *b;

    *b = t;

    void main() {int *p1 = new int;*p1 = 10;

    int *p2 = new int;*p2 = 20;func(&p1, &p2);

    *

    Slide 6Faculty of Computer Science and Engineering HCMUT

    ,printf(%d, *p2);}

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    7/53

    Linked Lists

    A linked list is an ordered collection of data in which eachelement contains the location of the next element

    Element = Data + Link

    head data link

    Slide 7Faculty of Computer Science and Engineering HCMUT

    emptylinked list

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    8/53

    Nodes

    number name

    A node with

    one data field

    number

    A node with

    three data fields

    id

    Slide 8Faculty of Computer Science and Engineering HCMUT

    name numberid

    A node with onestructured data field

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    9/53

    Nodes

    Linked List

    Structure

    count head

    list

    < >

    Data node

    structure

    data link

    node

    < >

    dataType

    ke

    Slide 9Faculty of Computer Science and Engineering HCMUT

    head endendendend list

    link endendendend node

    field1 field2

    fieldN

    endendendend dataType

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    10/53

    Nodes Implementation in C++

    struct Node {

    int data;

    Node *next;

    };

    nodedata link

    end node

    Slide 10Faculty of Computer Science and Engineering HCMUT

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    11/53

    Nodes Implementation in C++

    Node *p = new Node();

    p->data = 5;

    coutdata;

    Node *q = p;

    coutdata;

    p

    5

    q

    Slide 11Faculty of Computer Science and Engineering HCMUT

    o e r = new o e ;

    r->data = 10;

    q->next = r;

    coutnext->data;

    r10

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    12/53

    Nodes Implementation in C++

    struct Node {int data;Node *next;

    };

    struct Node {float data;Node *next;

    };

    Slide 12Faculty of Computer Science and Engineering HCMUT

    template struct Node {

    ItemType data;

    Node *next;};

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    13/53

    Nodes Implementation in C++

    Node *p = new Node();

    p->data = 5;

    coutdata;

    Node *q = p;

    coutdata;

    p

    5

    q

    Slide 13Faculty of Computer Science and Engineering HCMUT

    o e n r = new o e n ;

    r->data = 10;

    q->next = r;

    coutnext->data;

    r10

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    14/53

    Nodes Implementation in C++

    template

    class Node{

    public:

    Node(){this->next = NULL;

    }

    Slide 14Faculty of Computer Science and Engineering HCMUT

    o e em ype a a

    this->data = data;

    this->next = NULL;

    }

    ItemType data;

    Node *next;

    };

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    15/53

    Linked List Implementation in C++template

    class LinkedList{

    public:

    LinkedList();

    ~LinkedList();

    protected:

    listcount head

    end list

    Slide 15Faculty of Computer Science and Engineering HCMUT

    Node* head;

    int count;

    };

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    16/53

    Linked List Algorithms

    Create list

    Insert node

    Delete node

    Traverse

    Destroy list

    Slide 16Faculty of Computer Science and Engineering HCMUT

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    17/53

    Linked List Implementationtemplate

    class LinkedList{

    public:

    LinkedList();

    ~LinkedList();protected:

    int InsertNode(Node* pPre,

    List_ItemType value);

    Slide 17Faculty of Computer Science and Engineering HCMUT

    L st_ItemType DeleteNode(Node* pPre,

    Node* pLoc);

    int Search(List_ItemType value, Node*

    &pPre, Node* &pLoc);

    void Traverse();

    Node* head;

    int count;

    };

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    18/53

    Linked List Implementationtemplate

    class LinkedList{

    public:

    LinkedList();

    ~LinkedList();void InsertFirst(List_ItemType value);

    void InsertLast(List_ItemType value);

    int InsertItem(List_ItemType value, int Position);

    Slide 18Faculty of Computer Science and Engineering HCMUT

    L st_ItemType DeleteF rst();

    List_ItemType DeleteLast();

    int DeleteItem(int Postion);

    int GetItem(int Position, List_ItemType &dataOut);

    void Print2Console();

    void Clear();// Augment your methods for linked list here!!!

    LinkedList* Clone();

    protected:

    // ...

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    19/53

    Linked List Implementation

    How to use Linked List data structure?

    int main(int argc, char* argv[]) {

    LinkedList* myList =

    new LinkedList();myList->InsertFirst(15);

    myList->InsertFirst(10);

    Slide 19Faculty of Computer Science and Engineering HCMUT

    myList->InsertFirst(5);myList->InsertItem(18, 3);

    myList->InsertLast(25);

    myList->InsertItem(20, 3);

    myList->DeleteItem(2);

    printf("List 1:\n");

    myList->Print2Console();

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    20/53

    Linked List Implementation

    // ...

    int value;

    LinkedList* myList2 = myList->Clone();printf("\nList 2:\n");

    myList2->Print2Console();

    Slide 20Faculty of Computer Science and Engineering HCMUT

    myList2->GetItem(1, value);printf(Value at position 1: %d, value);

    delete myList;delete myList2;

    return 1;

    }

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    21/53

    Create List

    Before

    list.head = null

    ? ?

    count head

    list

    Slide 21Faculty of Computer Science and Engineering HCMUT

    0

    count head

    After list

    .

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    22/53

    Create List

    Algorithm createList (ref list )

    Initializes metadata for a linked list

    Pre list is a metadata structure passed by reference

    Post metadata initialized

    1 list.head = null

    Slide 22Faculty of Computer Science and Engineering HCMUT

    s .coun =

    3 return

    End createList

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    23/53

    Linked List Implementation

    template

    LinkedList::LinkedList(){

    this->head = NULL;

    this->count = 0;

    }

    Slide 23Faculty of Computer Science and Engineering HCMUT

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    24/53

    Insert Node

    Allocate memory for the new node and set up data

    Point the new node to its successor

    Point the new node's predecessor to it

    Slide 24Faculty of Computer Science and Engineering HCMUT

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    25/53

    Insert into Empty List

    Before 0

    count head

    list

    New -> link = list. head

    75

    pNew

    Slide 25Faculty of Computer Science and Engineering HCMUT

    After

    list.head = pNew

    1

    count head

    list

    75

    pNew

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    26/53

    Insert at the Beginning

    Before1

    count head

    list

    pNew -> link = list.head

    75

    pNew

    39

    Slide 26Faculty of Computer Science and Engineering HCMUT

    After

    list.head = pNew

    2

    count headlist

    75

    pNew

    39

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    27/53

    Insert in Middle

    Before2

    count head

    list

    New -> link = Pre -> link

    75

    pNew

    52

    39

    pPre

    Slide 27Faculty of Computer Science and Engineering HCMUT

    After

    pPre -> link = pNew

    3

    count head

    list

    75

    pNew

    52

    39

    pPre

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    28/53

    Insert at End

    Before

    New -> link = Pre -> link

    3

    count head

    list

    52

    pNew

    7539

    pPre

    134

    Slide 28Faculty of Computer Science and Engineering HCMUT

    After

    pPre -> link = pNew

    4

    count head

    list

    pPre

    52 7539

    pNew

    134

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    29/53

    Insert Node Algorithm

    Algorithm insertNode (ref list ,

    val pPre ,

    val dataIn )

    Inserts data into a new node in the linked list

    Pre list is metadata structure to a valid list

    Slide 29Faculty of Computer Science and Engineering HCMUT

    p re s po n er a a s og ca pre ecessor

    dataIn contains data to be inserted

    Post data have been inserted in sequence

    Return true if successful, false if memory overflow

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    30/53

    Insert Node Algorithm

    1 allocate(pNew)2 if (memory overflow)

    1 return false

    3 pNew -> data = dataIn4 if (pPre = null)Adding before first node or to empty list

    1 New -> link = list.head

    Slide 30Faculty of Computer Science and Engineering HCMUT

    2 list.head = pNew5 else

    Adding in middle or at end

    1 pNew -> link = pPre -> link

    2 pPre -> link = pNew6 list.count = list.count + 17 return trueEnd insertNode

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    31/53

    Insert Nodetemplate

    int LinkedList::InsertNode(

    Node *pPre, List_ItemType value) {

    Node *pNew = new Node();

    if (pNew == NULL)

    return 0;

    pNew->data = value;

    if Pre == NULL

    Slide 31Faculty of Computer Science and Engineering HCMUT

    pNew->next = this->head;this->head = pNew;

    } else {

    pNew->next = pPre->next;

    pPre->next = pNew;}

    this->count++;

    return 1;

    }

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    32/53

    Delete Node

    Locate the node to be deleted.

    Point the node predecessor's link to its successor.

    Release the memory for the deleted node

    Slide 32Faculty of Computer Science and Engineering HCMUT

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    33/53

    Delete First Node

    Before

    = ->

    3

    count head

    list

    52 7539

    pPre pLoc

    Slide 33Faculty of Computer Science and Engineering HCMUT

    recycledAfter

    .

    recycle(pLoc)

    2

    count head

    list

    52 75

    pPre pLoc

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    34/53

    General Delete Case

    Before

    - -

    3

    count head

    list

    52 7539

    pLocpPre

    Slide 34Faculty of Computer Science and Engineering HCMUT

    recycledAfter

    recycle (pLoc)

    2

    count head

    list

    39 75

    pLocpPre

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    35/53

    Delete Node Algorithm

    Algorithm deleteNode (ref list ,

    val pPre ,

    val pLoc

    ref dataOut )

    Delete data from a linked list and returns it to calling

    Slide 35Faculty of Computer Science and Engineering HCMUT

    Pre list is metadata structure to a valid list

    pPre is a pointer to predecessor node

    pLoc is a pointer to node to be deleted

    dataOut is variable to receive deleted data

    Post data have been deleted and returned to caller

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    36/53

    Delete Node Algorithm

    1 dataOut = pLoc -> data

    2 if (pPre = null)

    Delete first node

    1 list.head = pLoc -> link

    3 else

    Slide 36Faculty of Computer Science and Engineering HCMUT

    e e e o er no es

    1 pPre -> link = pLoc -> link

    4 list.count = list.count - 1

    5 recycle (pLoc)6 return

    End deleteNode

    D l N d

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    37/53

    Delete Nodetemplate

    List_ItemType LinkedList::DeleteNode(

    Node *pPre, Node *pLoc){

    List_ItemType result = pLoc->data;

    if (pPre == NULL)

    list->head = pLoc->next;

    else

    Slide 37Faculty of Computer Science and Engineering HCMUT

    pPre->next = pLoc->next;

    this->count--;

    delete pLoc;

    return result;

    }

    T Li

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    38/53

    Traverse List

    Traverse module controls the loop:

    calling a user-supplied algorithm to process data

    pWalker = list.headloop (pWalker not null)

    -

    Slide 38Faculty of Computer Science and Engineering HCMUT

    pWalker = pWalker -> link

    T Li t

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    39/53

    Traverse List

    template void LinkedList::Traverse() {Node *p = head;while (p != NULL){

    p->data++; // process data here!!!p = p->next;

    }}

    Slide 39Faculty of Computer Science and Engineering HCMUT

    template void LinkedList::Traverse(void(*visit)(List_ItemType &)) {Node *p = head;while (p != NULL){

    (*visit)(p->data);p = p->next;

    }}

    S hi i Li k d Li t

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    40/53

    Searching in Linked Listtemplate

    int LinkedList::

    Search(List_ItemType value, Node* &pPre,

    Node* &pLoc){

    pPre = NULL;

    pLoc = this->head;

    while (pLoc != NULL && pLoc->data != value){

    Slide 40Faculty of Computer Science and Engineering HCMUT

    pPre = pLoc;

    pLoc = pLoc->next;

    }

    return (pLoc != NULL); // found: 1; notfound: 0

    }

    D t Li t Al ith

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    41/53

    Destroy List Algorithm

    Algorithm destroyList (val list )Deletes all data in list.

    Pre list is metadata structure to a valid list

    Post all data deleted1 loop (list.head not null)

    1 dltPtr = list.head

    Slide 41Faculty of Computer Science and Engineering HCMUT

    s . ea = s. ea -> n3 recycle (dltPtr)No data left in list. Reset metadata

    2 list.count = 0

    3 returnEnd destroyList

    Destro list

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    42/53

    Destroy list

    template

    void LinkedList::Clear(){

    Node *temp;

    while (this->head != NULL){

    temp = this->head;

    this->head = this->head->next;

    delete temp;

    Slide 42Faculty of Computer Science and Engineering HCMUT

    }

    this->count = 0;

    }

    template

    LinkedList::~LinkedList(){this->Clear();

    }

    Exercises

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    43/53

    Exercises

    template class LinkedList{

    public:

    LinkedList();

    ~LinkedList();

    int InsertFirst(List_ItemType value);

    int InsertLast(List_ItemType value);

    Slide 43Faculty of Computer Science and Engineering HCMUT

    nt InsertItem(L st_ItemType value, nt Pos t on);

    List_ItemType DeleteFirst();List_ItemType DeleteLast();

    int DeleteItem(int Postion);

    int GetItem(int Position, List_ItemType &dataOut);

    void Print2Console();// Augment more methods for linked list here!!!

    LinkedList* Clone();

    protected: // as previous slide

    Pointer vs Object Variable

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    44/53

    void main(){LinkedList *p = new LinkedList();

    p->InsertLast(20);

    // do sth with p herefunc(p);

    delete p;

    Pointer vs. Object Variable

    p1

    count head20

    Slide 44Faculty of Computer Science and Engineering HCMUT

    void main(){

    LinkedList ob;

    ob.InsertLast(20);

    // do sth with ob herefunc(ob);

    }

    ob

    1

    count head

    20

    Pointer vs Object Variable

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    45/53

    Pointer vs. Object Variable

    void func(LinkedList *a)

    a->InsertFist(10);

    }

    void func(LinkedList myOb)

    a

    Slide 45Faculty of Computer Science and Engineering HCMUT

    my . nser s ;

    }

    What are the pros and cons?

    myOb

    Sample Solution: Insert

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    46/53

    Sample Solution: Insert

    template int LinkedList::InsertItem(List_ItemType value,

    int position) {

    if (position < 0 || position > this->count)

    return 0;

    Node* newPtr, *pPre;

    newPtr = new Node();

    if (newPtr == NULL)

    Slide 46Faculty of Computer Science and Engineering HCMUT

    newPtr->data = value;if (head == NULL) {

    head = newPtr;

    newPtr->next = NULL;

    }

    else if (position == 0) {

    newPtr->next = head;

    head = newPtr;

    }

    Sample Solution: Insert

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    47/53

    Sample Solution: Insert

    else {// Find the position of pPre

    pPre = this->head;

    for (int i = 0; i < position-1; i++)pPre = pPre->next;

    // Insert new node

    Slide 47Faculty of Computer Science and Engineering HCMUT

    new r- nex = p re- nex ;

    pPre->next = newPtr;

    }

    this->count++;

    return 1;}

    Sample Solution: Delete

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    48/53

    Sample Solution: Delete

    template int LinkedList::DeleteItem(int position){

    if (position < 0 || position > this->count)

    return 0;

    Node *dltPtr, *pPre;

    if (position == 0) {dltPtr = head;

    head = head->next;

    } else {

    Slide 48Faculty of Computer Science and Engineering HCMUT

    p re = s-> ea ;

    for (int i = 0; i < position-1; i++)pPre = pPre->next;

    dltPtr = pPre->next;

    pPre->next = dltPtr->next;

    }

    delete dltPtr;this->count--;

    return 1;

    }

    Sample Solution: Clone

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    49/53

    Sample Solution: Clone

    template LinkedList*

    LinkedList::Clone(){

    LinkedList* result =

    New LinkedList();

    Node* p = this->head;

    while (p != NULL) {

    Slide 49Faculty of Computer Science and Engineering HCMUT

    result->InsertLast(p->data);p = p->next;

    }

    result->count = this->count;

    return result;}

    Homework

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    50/53

    Homework

    Reverse a linked list

    a b c

    head

    a b

    Slide 50Faculty of Computer Science and Engineering HCMUT

    a b c

    head

    a b

    Result:

    Homework

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    51/53

    Homework

    Hint 1

    a b c

    head

    a b

    - =

    Slide 51Faculty of Computer Science and Engineering HCMUT

    a b c

    new head

    a b

    Result:

    new tail

    _

    head->next = NULL;head = new_head;

    Homework

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    52/53

    Homework

    Hint 2

    a b ca b

    p->next = head;

    head

    Slide 52Faculty of Computer Science and Engineering HCMUT

    a b c

    p

    a b

    head

    p= p1;p1=p1->next

    p1

    Homework

  • 7/27/2019 Chapter+2b+ +Linked+List+ +Implementation +Implementation

    53/53

    Homework

    template

    void LinkedList::Reverse(){

    ...

    }

    Slide 53Faculty of Computer Science and Engineering HCMUT