64,651
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string>
using namespace std;
struct Node
{
string ID;
string Name;
Node *pNext;
};
Node* CrateList(int n)//创建n个元素的链表
{
Node* pHead;
Node* pNew;
pNew=new Node();
for(int i(0);i<n;i++)
{
do
{
cout<<"Please input the ID and name"<<endl;
cin>>pNew->ID>>pNew->Name;
if((pNew->Name).size()>10)
cout<<"输出超过10个字符,请重新输入"<<endl;
}while((pNew->Name).size()>10);
if(0==i)
{
pHead=pNew;
}
else
{
pNew=pNew->pNext;
}
}
return pHead;
}
void OutPut(Node *pHead)//输出链表所有元素
{
Node* pNew;
pNew=pHead;
for(;pNew!=NULL;)
{
cout<<pNew->ID<<" "<<pNew->Name<<endl;
pNew=pNew->pNext;
}
}
int main()
{
int i=3;
Node* p=CrateList(i);
OutPut(p);
return 0;
};
运行有错误
#include <iostream>
#include <string>
using namespace std;
struct Node
{
string ID;
string Name;
Node *pNext;
};
Node* CrateList(int n)//创建n个元素的链表
{
Node* pHead;
Node* pNew;
/////////////////////////
Node* pTemp;
//pNew=new Node();
/////////////////////////
for(int i(0);i<n;i++)
{
pNew=new Node();
do
{
cout<<"Please input the ID and name"<<endl;
cin>>pNew->ID>>pNew->Name;
if((pNew->Name).size()>10)
cout<<"输出超过10个字符,请重新输入"<<endl;
}while((pNew->Name).size()>10);
if(0==i)
{
pHead=pNew;
pTemp = pHead;
}
else
{
/////////////////////////
pTemp->pNext = pNew;
pTemp = pTemp->pNext;
//pNew=pNew->pNext;
/////////////////////////
}
}
/////////////////////////
pTemp->pNext = NULL;
/////////////////////////
return pHead;
}
void OutPut(Node *pHead)//输出链表所有元素
{
Node* pNew;
pNew=pHead;
for(;pNew!=NULL;)
{
cout<<pNew->ID<<" "<<pNew->Name<<endl;
pNew=pNew->pNext;
}
}
/////////////////////////
void DeleteList( Node *pHead )
{
//delete
}
/////////////////////////
int main()
{
int i=3;
Node* p=CrateList(i);
OutPut(p);
/////////////////////////
DeleteList( p );
/////////////////////////
getchar();
return 0;
};
#include <iostream>
#include <string>
using namespace std;
struct Node
{
string ID;
string Name;
Node *pNext;
};
Node* CrateList(int n)//创建n个元素的链表
{
Node* pHead;
Node* pTail;
Node* pNew;
for(int i(0);i<n;i++)
{
pNew=new Node();
pNew->pNext=NULL;
do
{
cout<<"Please input the ID and name"<<endl;
cin>>pNew->ID>>pNew->Name;
if((pNew->Name).size()>10)
cout<<"输出超过10个字符,请重新输入"<<endl;
}while((pNew->Name).size()>10);
if(0==i)
{
pHead=pNew;
pTail=pNew;
}
else
{
pTail->pNext=pNew;
pTail=pNew;
//pNew=pNew->pNext;
}
}
return pHead;
}
void OutPut(Node *pHead)//输出链表所有元素
{
Node* pNew;
pNew=pHead;
for(;pNew!=NULL;)
{
cout<<pNew->ID<<" "<<pNew->Name<<endl;
pNew=pNew->pNext;
}
}
int main()
{
int i=1;
Node* p=CrateList(i);
OutPut(p);
return 0;
};
pNew=new Node();