use_link.cpp
Upload User: dq031136
Upload Date: 2022-08-08
Package Size: 802k
Code Size: 4k
Development Platform:

C++ Builder

  1. #include <iostream.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. class dblinkob {
  5.  public:
  6.    char info;
  7.    dblinkob *next;
  8.    dblinkob *previous;
  9.    dblinkob() {
  10.       info = 0;
  11.       next = NULL;
  12.       previous = NULL;
  13.     }
  14.    dblinkob *getnext() {return next;}
  15.    dblinkob *getprevious() {return previous;}
  16.    void getinfo(char &c) { c = info;}
  17.    void change(char c) {info = c;}
  18.    friend ostream &operator<<(ostream &stream, dblinkob o)
  19.     {
  20.       stream << o.info << endl;
  21.       return stream;
  22.     }
  23.    friend ostream &operator<<(ostream &stream, dblinkob *o)
  24.     {
  25.       stream << o->info << endl;
  26.       return stream;
  27.     }
  28.    friend istream &operator>>(istream &stream, dblinkob &o)
  29.     {
  30.       cout << "Enter information: " << endl;
  31.       stream >> o.info;
  32.       return stream;
  33.     }
  34.  };
  35. class dllist : public dblinkob {
  36.    dblinkob *start, *end;
  37.  public:
  38.    dllist() {start = end = NULL;}
  39.    void store(char c);
  40.    void remove(dblinkob *ob);
  41.    void frwdlist();
  42.    void bkwdlist();
  43.    dblinkob *find(char c);
  44.    dblinkob *getstart() {return start;}
  45.    dblinkob *getend() {return end;}
  46.  };
  47. void dllist::store(char c)
  48.  {
  49.    dblinkob *p;
  50.    p = new dblinkob;
  51.    if(!p) {
  52.       cout << "Allocation error." << endl;
  53.       exit(1);
  54.     }
  55.    p->info = c;
  56.    if(start==NULL)
  57.     {
  58.       end = start = p;
  59.     }
  60.    else
  61.     {
  62.       p->previous = end;
  63.       end->next = p;
  64.       end = p;
  65.     }
  66.  }
  67. void dllist::remove(dblinkob *ob)
  68.  {
  69.    if(ob->previous)
  70.     {
  71.       ob->previous->next = ob->next;
  72.       if(ob->next)
  73.          ob->next->previous = ob->previous;
  74.       else
  75.          end = ob->previous;
  76.     }
  77.    else
  78.     {
  79.       if(ob->next)
  80.        {
  81.          ob->next->previous = NULL;
  82.          start = ob->next;
  83.        }
  84.       else
  85.          start = end = NULL;
  86.     }
  87.  }
  88. void dllist::frwdlist()
  89.  {
  90.    dblinkob *temp;
  91.    temp = start;
  92.    do {
  93.       cout << temp->info << " ";
  94.       temp = temp->getnext();
  95.     } while(temp);
  96.    cout << endl;
  97.  }
  98. void dllist::bkwdlist()
  99.  {
  100.    dblinkob *temp;
  101.    temp = end;
  102.    do {
  103.       cout << temp->info << " ";
  104.       temp = temp->getprevious();
  105.    } while(temp);
  106.    cout << endl;
  107.  }
  108. dblinkob *dllist::find(char c)
  109.  {
  110.    dblinkob *temp;
  111.    temp = start;
  112.    while(temp) {
  113.       if(c==temp->info) return temp;
  114.       temp = temp ->getnext();
  115.     }
  116.    return NULL;
  117.  }
  118. int main()
  119.  {
  120.    dllist list;
  121.    char c;
  122.    dblinkob *p;
  123.    list.store('1');
  124.    list.store('2');
  125.    list.store('3');
  126.    cout << "here is list backwards, then forwards." << endl;
  127.    list.bkwdlist();
  128.    list.frwdlist();
  129.    cout << endl;
  130.    cout << "'Manually' walk through the list." << endl;
  131.    p = list.getstart();
  132.    while(p) {
  133.       p->getinfo(c);
  134.       cout << c << " ";
  135.       p = p->getnext();
  136.     }
  137.    cout << endl << endl;
  138.    cout << "Looking for item 2." << endl;
  139.    p = list.find('2');
  140.    if(p)
  141.     {
  142.       p->getinfo(c);
  143.       cout << "Found: " << c << endl;
  144.     }
  145.    cout << endl;
  146.    p->getinfo(c);
  147.    cout << "Removing item: " << c << endl;
  148.    list.remove(p);
  149.    cout << "Here is new list forwards." << endl;
  150.    list.frwdlist();
  151.    cout << endl;
  152.    cout << "Adding an item." << endl;
  153.    list.store('4');
  154.    cout << "Here is list forwards." << endl;
  155.    list.frwdlist();
  156.    cout << endl;
  157.    p = list.find('1');
  158.    if(!p)
  159.     {
  160.       cout << "Error, item not found." << endl;
  161.       return 1;
  162.     }
  163.    p->getinfo(c);
  164.    cout << "Changing " << c << " to 5." << endl;
  165.    p->change('5');
  166.    cout << "Here is list forwards, then backwards." << endl;
  167.    list.frwdlist();
  168.    list.bkwdlist();
  169.    cout << endl;
  170.    cin >> *p;
  171.    cout << p;
  172.    cout << "Here is list forwards again." << endl;
  173.    list.frwdlist();
  174.    cout << endl;
  175.    cout << "Here is list after removing head of list." << endl;
  176.    p = list.getstart();
  177.    list.remove(p);
  178.    list.frwdlist();
  179.    cout << endl;
  180.    cout << "Here is list after removing end of list." << endl;
  181.    p = list.getend();
  182.    list.remove(p);
  183.    list.frwdlist();
  184.    return 0;
  185.  }
  186.