C++ 单链表问题
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
struct LongIntegerNode
{
short int data;
LongIntegerNode *next;
};
struct LinkNode
{
LongIntegerNode *pLongIntegerNode;
LinkNode *next;
};
LinkNode* tail;
class LIST
{
char num[100];
public:
struct LongIntegerNode* creat(string str);
struct LinkNode* add(LinkNode* Head,LongIntegerNode* pHead);
void sortpart(int m,int n);
void output(LinkNode* Head)
{
LinkNode* p;
p=Head->next;
LongIntegerNode* q;
while(p)
{
q=p->pLongIntegerNode->next;
while(q)
{
cout<<q->data;
q=q->next;
}
cout<<endl;
p=p->next;
}
}
void show(LongIntegerNode *pHead)
{
LongIntegerNode *p;
p=pHead->next;
while(p)
{
cout<<p->data;
p=p->next;
}
cout<<endl;
}
};
LinkNode* LIST::add(LinkNode* node,LongIntegerNode* pHead)
{
LinkNode* newLinkNode=new LinkNode;
newLinkNode->next=NULL;
newLinkNode->pLongIntegerNode=pHead;
node->next=newLinkNode;
node=newLinkNode;
return n;
}
LongIntegerNode* LIST::creat(string str)
{
string s=str.substr(0,5);
int i=5;
LongIntegerNode* pHead=new LongIntegerNode;
pHead->next=NULL;
LongIntegerNode* p;
p=pHead;
while(s.length())
{
LongIntegerNode* newLongIntegerNode=new LongIntegerNode;
newLongIntegerNode->next=NULL;
newLongIntegerNode->data=atoi(s.c_str());
p->next=newLongIntegerNode;
p=newLongIntegerNode;
if(s.length()<5)
return pHead;
s=str.substr(i,5);
i=i+6;
}
return pHead;
}
void LIST::sortpart(int m,int n)
{
}
int main(int argc, char *argv[])
{
LIST list;
LinkNode* Head=new LinkNode;
Head->next=NULL;
tail=Head;
string str;
cin>>str;
while(str!="exit")
{
LongIntegerNode* pHead;
pHead=list.creat(str);
list.show(pHead);
tail=list.add(tail,pHead);
cin>>str;
}
list.output(Head);
system("PAUSE");
return EXIT_SUCCESS;
}
实现是将超长整数存储到单链表中 并将超长数列(包含一堆超长整数)的头结点存储到单链表中
程序实现后会无故缺失超长整数中的几个数字而且会无规律存储和输入不同的整数 不清楚原因