07 ds linked lists 2 (1)

Upload: mamad

Post on 05-Jul-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/16/2019 07 DS Linked Lists 2 (1)

    1/12

  • 8/16/2019 07 DS Linked Lists 2 (1)

    2/12

    CT077-3-2-DSTR Data Structures 2

    Objectives

    By the end of this esson! you "i#

    • Be a$e to i%&e%ent the $asic o&erations on

    inked ists – Searchin' for aues in the ist

     – ccessin' (set and 'et aues in the ist)

     –

    Deetin' ite%s fro% the ist – *nsertin' ite% at ar$itrary &osition in the ist

  • 8/16/2019 07 DS Linked Lists 2 (1)

    3/12

    CT077-3-2-DSTR Data Structures 3

    Searching for an Item in the List

    • Deter%ine e+istence (Booean resut) of an ite%in a inked ist! "here the data structure is#

     – ,hen infos ty&e is an o$.ect! search &ara%eter can$e a key fied of that ty&e! such as *D fied

    class NodeType {public:

    int info;NodeType* link;

    };

    class LinkedList { public:

    NodeType* head;

    int size;

      bool search(int value);  !! the other i"ple"ented "ethods};

  • 8/16/2019 07 DS Linked Lists 2 (1)

    4/12

    CT077-3-2-DSTR Data Structures /

    Searching for an Item in the List

    •Traerse a ist ee%ents and co%&ared "ithdesired aue

    class LinkedList { !!data "e"bers and other "ethods bool search(int value){

      NodeType * current # head;  $hile(current %# N&LL)  if (current'info ## value)

    return true;  else

    current # current'link;

      return false;}};

    list!search()

    current

  • 8/16/2019 07 DS Linked Lists 2 (1)

    5/12

    CT077-3-2-DSTR Data Structures

    Accessing List Elements

    1et ee%ents info at a 'ien inde+class LinkedList {  !!data "e"bers and other "ethods  int +et,te"-t(int inde.){  if(inde./0 11 inde.#size){

    cout // 2,nde. out of bound!3n2 ;abort();  }  NodeType * current # head;  for(int i#0; i/inde.; i44)

    current # current'link;  return current'info;

    }};

    ote that accessin' an

    ee%ent at a 'ien

    rando% &osition is so"

    co%&ared "ith arrays

    The $i''er the inde+! the

    so"er the o&eration

    list!+et,te"-t(5)

    current

  • 8/16/2019 07 DS Linked Lists 2 (1)

    6/12

    CT077-3-2-DSTR Data Structures 4

    Accessing List Elements

    • Set ee%ents info at a 'ien inde+

    class LinkedList {  !!data "e"bers and other "ethods  void set,te"-t(int value6 int inde.){  if(inde./0 11 inde.#size){

    cout // 2,nde. out of bound!3n2 ;abort();

      }  NodeType * current # head;  for(int i#0; i/inde.; i44)

    current # current'link;

      current'info # value;}};

    ote that accessin' an

    ee%ent at a 'ienrando% &osition is so"

    co%&ared "ith arrays

    The $i''er the inde+! the

    so"er the o&eration

    list!set,te"-t(76)

    current

    9

  • 8/16/2019 07 DS Linked Lists 2 (1)

    7/12CT077-3-2-DSTR Data Structures 7

    Deleting List Items

    • Deete the first ee%ent in a inked istclass LinkedList {  !!data "e"bers and other "ethods  void delete8irst(){  if(size 0){else6 "ay $ant to +ive a "essa+e or

    errorNodeType * to9eeleted # head;head # head'link;delete to9eeleted;size'';

      }}

    };

    to9eeleted

    X

    3X

  • 8/16/2019 07 DS Linked Lists 2 (1)

    8/12CT077-3-2-DSTR Data Structures 5

    Deleting Last Item in the List

    class LinkedList {

      !!data "e"bers and other "ethods  void deleteLast(){  if(size 0){else6 "ay $ant to +ive a "essa+e orerror

    if (size ## ){list has only one ite"delete head;

      head # N≪}else{  NodeType * beforeLast # head;  $hile (beforeLast'link'link %# N&LL)  beforeLast # beforeLast'link;  delete beforeLast'link;

      beforeLast'link # N≪}size'';

      }}

    };

    beforeLast

    X

    3X

  • 8/16/2019 07 DS Linked Lists 2 (1)

    9/12CT077-3-2-DSTR Data Structures 6

    Deleting an Item at Arbitrary Index

    class LinkedList {

      !!data "e"bers and other "ethods  void delete,te"-t(int inde.){  if(inde. / size){else6 "ay $ant to +ive a "essa+e orerror

    if (inde. ## 0)  delete8irst();else{

      NodeType * prev # N&LL6 * toelete # head;  for (int i#0; i/inde.; i44){  prev # toelete;  toelete # toelete'link;  }  prev'link # toelete'link; N&LL also

  • 8/16/2019 07 DS Linked Lists 2 (1)

    10/12CT077-3-2-DSTR Data Structures 0

    Insert ne Item at Arbitrary Index

    class LinkedList {

      !!data "e"bers and other "ethods  void insert,te"-t(int value6 int inde.){  if(inde. /# size){else6 "ay $ant to +ive a "essa+e orerror

    if (inde. ## 0)  insert-t9e+innin+(value);else if (inde. ## size)

      insert-t>nd(value);else {  NodeType * ne$Node # ne$ NodeType; ne$Node'info #

    value;  NodeType * prev # head;

    for (int i#0; i/inde.'; i44)

      prev # prev'link;  ne$Node'link # prev'link;  prev'link # ne$Node;  size44;}

      }}

    };ist!insert,te"-t(?65) ne$Node

    X

    prev!

    "

  • 8/16/2019 07 DS Linked Lists 2 (1)

    11/12CT077-3-2-DSTR Data Structures

    Exercises

    •Check your i%&e%entation for a &reious functionaities usin'the foo"in' &ro'ra%#

    ,hat is the e+&ected out&ut of the &ro'ra% at each check&oint8

    void "ain(){LinkedList list;

    istinserttBe'innin'()9istinsertt:nd(2)9

    istinserttBe'innin'(3)9

    ist&rint()9

    istdeete;irst()9

    ist&rint()9

    istdeeteLast()9

    ist&rint()9istcear()9

    @heckpoint

    istinserttBe'innin'()9

    istinsertt:nd(7)9

    istinsertt:nd(5)9

    istinserttBe'innin'(/)9istdeete*te%t(3)9

    ist&rint()9

    @heckpoint 5istinsert*te%t(4! 2)9

    istinsert*te%t(3! 0)9

    ist&rint()9

    istinsert*te%t(6! ist'etSi

  • 8/16/2019 07 DS Linked Lists 2 (1)

    12/12CT077-3-2-DSTR Data Structures 2