Как реализовать создание динамического массива, элементы (узлы) которого являются структурами - C (СИ)
Формулировка задачи:
Не могу понять как реализовать создание динамического массива, элементы(узлы) которого являются структурами.
У меня даны следующие структуры:
Не понимаю зачем мне создавать массив(а это требуется в задании), если мне задана структура intarray,узлы которой являются тоже структурами.
Или структура - это тоже массив, только с разными типами?!
В общем не знаю как создать динамический массив используя описанные структуры
Я начал так:
Заранее спасибо Всем, кто хоть чем то поможет)
// Объявляем шаблон структуры с полями для каждого узла
struct array_element {
int element;
struct array_element * next;
struct array_element * prev;
};
// Объявляем шаблон структуры с полями для самого массива
struct intarray {
struct array_element * first;
struct array_element * last;
int lowbound;
int highbound;
int buffer;
};
typedef struct intarray *intarray;intarray fun_number_one_array; fun_number_one_array = malloc(sizeof(struct intarray));
Решение задачи: «Как реализовать создание динамического массива, элементы (узлы) которого являются структурами»
textual
Листинг программы
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
class Queue
{
public:
int dat;
Queue *next;
Queue()
{
dat=0;
next=NULL;
}
};
class stack :public Queue
{
protected:
Queue *last;
public:
int num;
stack()
{
last=NULL;
num=0;
}
int push(int d)
{
Queue *s;
s=new Queue;
s->dat=d;
s->next=last;
last=s;
num++;
return d;
}
int pop()
{
int d;
Queue *s;
s=last;
d=last->dat;
last=s->next;
delete s;
num--;
return d;
}
};
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int n,r;
n=StrToInt(Edit4->Text);
StringGrid1->ColCount=n;
StringGrid2->ColCount=n;
StringGrid3->ColCount=n;
stack s;
int res,set;
for(int i=0;i<StringGrid1->ColCount;i++)
{for(int j=0;j<StringGrid1->RowCount;j++)
{
StringGrid1->Cells[i][j]="";
StringGrid2->Cells[i][j]="";
StringGrid3->Cells[i][j]="";
}
}
for(int i=0;i<n;i++)
{
res=random(10);
s.push(res);
StringGrid1->Cells[i][0]=IntToStr(s.push(res));
}
for(int i=0;i<n;i++)
{set=s.pop();
if(set%2==0)
{
StringGrid2->Cells[i][0]=IntToStr(s.pop());
}
else
{StringGrid3->Cells[i][0]=IntToStr(s.pop());}
}
}
//---------------------------------------------------------------------------