Структуры С++ добавление елементов в стурктуры
#include<iostream>
#include<ctime>
using namespace std;
struct G {
int infg;
G* nextg;
};
struct Q
{
int infq;
Q* nextq;
};
struct P
{
int infp;
P* nextp;
};
void InitG(G**Headg, G*&Tailg)
{
*Headg=NULL;
Tailg = NULL;
}
void InitQ(Q** HeadQ, Q*& TailQ)
{
*HeadQ = NULL;
TailQ = NULL;
}
void InitP(P** HeadP, P*& TailP)
{
*HeadP = NULL;
TailP = NULL;
}
void AddElemQ(Q*& HeadQ);
void AddElemG(G*& Headg);
Q* Find(Q* HeadQ, int elem);
G* FindG(G* Headg, int elem);
void FindP(Q* HeadQ, G* Headg, int elemp);
int main() {
srand(time(NULL));
G* Headg, * Tailg;
Q* HeadQ, * TailQ;
P* HeadP, * TailP;
InitG(&Headg, Tailg);
InitQ(&HeadQ, TailQ);
InitP(&HeadP, TailP);
cout << "Enter count of G list: " << endl;
int ng;
cin >> ng;
for (int i = 0; i < ng; i++) {
AddElemG(Headg);
}
cout<< "Enter count of Q list: " << endl;
int nq;
cin >> nq;
for (int i = 0; i < nq; i++) {
AddElemQ(HeadQ);
}
G*g = Headg;
cout << "In list G: " << endl;
while (g != NULL) {
cout << g->infg << " ";
g = g->nextg;
}
cout << endl;
Q* q = HeadQ;
cout << "In list Q: " << endl;
while (q != NULL) {
cout << q->infq << " ";
q = q->nextq;
}
cout << endl;
cout << "Enter the element in G: ";
int n2;
cin >> n2;
G* pg;
pg = FindG(Headg, n2);
cout << "Elem in G" << endl << pg << endl;
int n;
cout << "Enter the element in Q: ";
cin >> n;
Q* pq;
pq = Find(HeadQ, n);
cout << "Elem in Q" << endl << pq << endl;
cout << endl;
//FindP(HeadQ, Headg, n);
return 0;
}
void AddElemG(G*&Headg)
{
G* NewNode = new G;
NewNode->infg = rand() % 10;
if (!Headg) NewNode->nextg = NULL;
else NewNode->nextg = Headg;
Headg = NewNode;
}
void AddElemQ(Q*& HeadQ) {
Q* NewNodeQ = new Q;
NewNodeQ->infq = rand() % 10;
if (!HeadQ) NewNodeQ->nextq = NULL;
else NewNodeQ->nextq = HeadQ;
HeadQ = NewNodeQ;
}
Q* Find(Q* HeadQ, int elem)
{
Q* q = HeadQ;
while (q && (q->infq != elem))
q = q->nextq;
return q;
}
G* FindG(G* Headg, int elem)
{
G* g = Headg;
while (g && (g->infg != elem))
g = g->nextg;
return g;
}
void FindP(Q* HeadQ,G* Headg, int elemp)
{
Q* qp = HeadQ;
G* gp = Headg;
while(qp && gp &&(qp->infq !=elemp)&&(gp->infg !=elemp))
{
qp = qp->nextq;
gp = gp->nextg;
}
cout << gp<<endl;
cout << qp;
}
Источник: Stack Overflow на русском