Главная » 2018 » Апрель » 24 » Списки. Добавление в конец (очередь)
09:01
Списки. Добавление в конец (очередь)

#include <iostream>
using namespace std;
struct elem
{
    int info; 
    elem *next;
};
elem *insert_begin(elem *ptr, int a);
void print(elem *ptr);
elem *vvod(elem *ptr);
elem *insert_end(elem *ptr, int a);
int main()
{    
    setlocale(LC_ALL, "Russian");
    elem *head;
    head=new elem;
    head->next=NULL;
    head=vvod(head);
    print(head->next);
    system("pause");
    return 0;
}
elem *vvod(elem *ptr)
{
    int a;
    cout<<"Введите элементы списка "<<endl;
    cin>>a;
    while (a)
    {
        //ptr=insert_begin(ptr,a);
        ptr=insert_end(ptr,a);
        cin>>a;
    }
    return ptr;
}
elem *insert_begin(elem *ptr, int a)
{
    elem *temp;
    temp=new elem;
    temp->info=a;
    temp->next=ptr;
    return temp;
}
elem *insert_end(elem *ptr, int a)
{
    elem *ptr2;
    ptr2=ptr;
    while (ptr2->next != NULL)
       ptr2=ptr2->next;
    elem *temp;
    temp=new elem;
    temp->info=a;
    temp->next=NULL;
    ptr2->next=temp;
    return ptr;
}
void print(elem *ptr)
{
    elem *temp;
    temp=ptr;
    while (temp!=NULL)
    {
        cout<<temp->info<<" ";
        temp=temp->next;
    }
    cout<<endl;
}

 

Просмотров: 345 | Добавил: denjes | Рейтинг: 0.0/0
Всего комментариев: 0
avatar