Pagini recente » Cod sursa (job #288572) | Cod sursa (job #1819164) | Cod sursa (job #1644035) | Cod sursa (job #689204) | Cod sursa (job #2761161)
#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;
}