Witam,
mam
taki fragment kodu dotyczący list chodzi o ustawieniu głowy listy na wartość NULL
lista *head[7];
for(int i=0; i<7; i++)
{
head[i]=NULL;
}
i tutaj wszystko działa, ale ja chcę żeby wielkość tej tablicy była odczytywana z pliku, więc ma być "n", ale nie mogę tego zrobić tak żeby działało.
Proszę o pomoc.
a tu cały kod:
// pactise.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <time.h>
using namespace std;
struct lista
{
double x;
lista *next;
};
void insert(lista *&head, double x)
{
lista *wsk;
if (head==NULL)
{
head=new lista;
head->x=x;
head->next=NULL;
}
else
{
wsk= new lista;
wsk->next = head;
wsk->x=x;
head=wsk;
}
}
void view(lista *head)
{
while(head!=NULL)
{
cout << head->x << " ";
head=head->next;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
float x,y,wart;
int n,nr;
FILE *plik=fopen("dane.txt","r");
if(plik==NULL) cout<< "dupa" << endl;
else
{
fscanf(plik,"%d", &n);
lista *head[7];
for(int i=0; i<7; i++)
{
head[i]=NULL;
}
while(!feof(plik))
{
fscanf(plik,"%d %f", &nr, &wart);
insert(head[nr],wart);
}
for(int i=0; i<7; i++)
{
view(head[i]);
cout << endl;
}
}
fclose(plik);
system("pause");
}