Главная » 2018 » Апрель » 27 » Списки. Добавить после максимального
10:22
Списки. Добавить после максимального

#include <iostream>
using namespace std;
struct elem
{
    int info; 
    elem *next;
};
elem *vvod(elem *ptr);
elem *insert_begin(elem *ptr, int a);
void insert_after_max(elem *ptr);
void print(elem *ptr);

int main()
{    
    setlocale(LC_ALL, "Russian");
    elem *head=NULL;
    head=vvod(head);
    cout<<"Список: ";
    print(head);
    insert_after_max(head);
    cout<<"Список новый: ";
    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 insert_after_max(elem *ptr)
{
    elem *ptr_max=ptr;
    int max=ptr->info;
    elem *ptr2=ptr;
    while (ptr2->next!=NULL)
    {
        if (ptr2->info > max)
        {
            max=ptr2->info;
            ptr_max=ptr2;
        }
        ptr2=ptr2->next;
    }
    elem *temp;
    temp=new elem;
    temp->info=max*2;
    temp->next=ptr_max->next;
    ptr_max->next=temp;
}
void print(elem *ptr)
{
    elem *temp;
    temp=ptr;
    while (temp!=NULL)
    {
        cout<<temp->info<<" ";
        temp=temp->next;
    }
    cout<<endl;
}

 

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