Доброго времени суток. Начал изучать связные списки, возникли проблемки! Пользователь должен заполнять список, пока не захочет выйти! И перед каждым новым заполнением пользователю показывают все данные, которые он ввел, но у меня функция шоу показывает только то, что пользователь ввел самый первый раз!
Помогите "хакеру". ))
#include "stdafx.h"
struct STRUCT
{
string Name;
int Age;
STRUCT *pNext;
STRUCT *pPrev;
};
STRUCT *pHead;
void Addlist(int ageParam,string nameParam,int element)
{
STRUCT *node=new STRUCT;
node->Age=ageParam;
node->Name=nameParam;
if (pHead == NULL)
{
node->pNext=node;
node->pPrev=node;
pHead=node;
}
else
{
STRUCT *p=pHead;
for (int i=element; i > 1; i--)
p=p->pNext;
p->pPrev=p=node;
node->pPrev=p->pPrev;
node->pPrev=node;
}
cout << "Element was add" <<endl;
}
void DeleteList (int position)
{
if (pHead == NULL)
cout << "List is empty" <<endl;
if (pHead ==pHead->pNext)
{
delete pHead;
pHead=NULL;
}
else
{
STRUCT *a=pHead;
for (int i=position; i > 1; i--)
a=a->pNext;
if (a == pHead)
pHead=a->pNext;
a->pPrev->pNext=a->pNext;
a->pNext->pPrev=a->pPrev;
delete a;
}
cout << "Element was delete!" <<endl;
}
void show()
{
if (pHead == NULL)
cout << "List is empty!"<<endl;
else
{
STRUCT * a=pHead;
cout << "Your list: " <<endl;
do
{
cout << "The name and age of the person: ";
cout <<a->Name<<' ';
cout <<a->Age<<endl;
a=a->pNext;
} while (a !=pHead);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
string tName;
int tAge=0;
int exit=0;
int index=0;
while (exit != 1)
{
cout << "index= " <<index <<endl;
cout << "1-Yes, 2-No"<<endl;
cout << "Do you want exit from program?: " <<endl;
cin >> exit;
if (exit == 1)
break;
cout << "Enter name of the person: ";
cin >>tName;
cout << "Enter age of this person: ";
cin >>tAge;
Addlist(tAge,tName,index);
show();
index++;
}
DeleteList (0);
return 0;
}