Главная » 2018 » Апрель » 20 » Списки. Ввод. Добавление в начало (стек)
11:16
Списки. Ввод. Добавление в начало (стек)

#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);
int main()
{    
    setlocale(LC_ALL, "Russian");
    elem *head=NULL;
    head=vvod(head);
    print(head);
    system("pause");
    return 0;
}
elem *vvod(elem *ptr)
{
    int a;
    cout<<"Введите элементы списка "<<endl;
    cin>>a;
    while (a)
    {
        ptr=insert_begin(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;
}
void print(elem *ptr)
{
    elem *temp;
    temp=ptr;
    while (temp!=NULL)
    {
        cout<<temp->info<<" ";
        temp=temp->next;
    }
    cout<<endl;
}

 

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