C++ 单链表问题

c5395348 2015-02-04 10:42:27
#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;
}
实现是将超长整数存储到单链表中 并将超长数列(包含一堆超长整数)的头结点存储到单链表中

程序实现后会无故缺失超长整数中的几个数字而且会无规律存储和输入不同的整数 不清楚原因
...全文
78 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
c5395348 2015-02-05
  • 打赏
  • 举报
回复
引用 1 楼 jiht594 的回复:

struct LongIntegerNode
{
       short int data;//去掉short,一次5位超过short范围了
       LongIntegerNode *next;
};
恩 早上起来突然也想到了 就是这个
jiht594 2015-02-04
  • 打赏
  • 举报
回复

struct LongIntegerNode
{
       short int data;//去掉short,一次5位超过short范围了
       LongIntegerNode *next;
};

64,652

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧