Jednosměrný seznam (úvod)

#include <iostream>
using namespace std;
 
struct Item
{
    string name;
    int    value;
    Item * next;
};
 
Item* first = nullptr;
 
void add (string name0, int value0)
{
    Item* p = new Item;
    p->name = name0;
    p->value = value0;
    p->next = first;
 
    first = p;
}
 
void addLast (string name0, int value0)
{
    Item* p = new Item;
    p->name = name0;
    p->value = value0;
    p->next = nullptr;
 
    if (first == nullptr)
    {
        first = p;
    }
    else
    {
        Item* u = first;
        while (u->next != nullptr)
        {
            u = u->next;
        }
 
        u->next = p;
    }
}
 
void print()
{
    Item* u = first;
    while (u != nullptr)
    {
        cout << u->name << " ... " << u->value << endl;
        u = u->next;
    }
}
 
Item * find (string key)
{
    Item* u = first;
    while (u != nullptr && u->name != key )
    {
        u = u->next;
    }
    return u;
}
 
int main()
{
    addLast ("abc", 7);
    addLast ("klm", 8);
    addLast ("rst", 9);
    addLast ("xyz", 10);
    print();
 
    Item* u = find ("klmn");
    if (u != nullptr)
        cout << "Nasli jsme " << u->name << " ... " << u->value << endl;
    else
        cout << "Nenasli jsme " << endl;
};
 
zpro/introduction.txt · Last modified: 2020/11/11 17:14 by 88.103.111.44
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki