Cod sursa(job #2761161)

Utilizator VladCaloVlad Calomfirescu VladCalo Data 30 iunie 2021 22:31:23
Problema Schi Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.89 kb


#include <iostream>
#include <fstream>


using namespace std;
ifstream fin("schi.in");
ofstream fout("schi.out");


struct node {
   int data;
   struct node *next = NULL;
};

struct node* insertAt(struct node* root, int data, int position) {
   if (position == 1) {
     struct node *elem = new node;
     elem->data = data;
     elem->next = root;
     return elem;
   }

   struct node* p = root;

   for (int i=0; i<position-2; i++) {
     p = p->next;
   }

   struct node *el = new node;
   el->data = data;
   el->next = p->next;
   p->next=el;
   return root;
}

int n;

void afisare(struct node* root) {
   for (struct node *p = root; p!=NULL; p=p->next)
    {
        fout<<p->data<<endl;
    }
}



int main(){
    fin>>n;
    struct node* root = NULL;

    for (int i=1;i<=n;i++)
    {
        int x;
        fin>>x;
        root = insertAt(root, i, x);
    }

    afisare(root);

    return 0;
}