| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 550 人关注过本帖
标题:[求助]创建单链表的问题
只看楼主 加入收藏
爱上夜
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2006-9-12
收藏
 问题点数:0 回复次数:1 
[求助]创建单链表的问题
我先定义一个结构体,然后编写了一个建立学生管理信息的函数.
可在调用此函数时如果录入三个学生信息只能输出两个,录入两个只能输出一个
输出函数没问题,估计是struct stu *con(unsigned n)函数出问题了.请教各位,给看看!谢谢!!!

typedef struct stu{
unsigned no;
char name[10];
float sc[2];
unsigned age;
char sex[10];
struct stu *next;
}stu;
struct stu *con(unsigned n) //建立学生管理系统
{
stu *p,*q,*h;
char ch;
int x;
p=new struct stu;
cout<<"***********************************************************************"<<endl;
cout<<" 请输入学生信息 "<<endl;
cout<<"***********************************************************************"<<endl;
h=q=p;
for(unsigned i=0;i<n;i++)
{ cout<<"请输入学号:"<<endl;
cin>>p->no;
cout<<"请输入姓名:"<<endl;
cin>>p->name;
cout<<"请输入英语成绩:"<<endl;
cin>>p->sc[0];
cout<<"请输入C++成绩:"<<endl;
cin>>p->sc[1];
cout<<"请输入年龄:"<<endl;
cin>>p->age;
cout<<"请输入性别b/g:"<<endl;
cin>>ch;
for(x=0;x<100;x++)
{
if(ch=='b')
{ strcpy(p->sex,"男");
break;}
if(ch=='g')
{ strcpy(p->sex,"女");
break;}
if(ch!='b'&&ch!='g')
{ cout<<"输入有误,请重新输入:"<<endl;
cout<<"请输入性别b/g:"<<endl;
cin>>ch;
}
}
q->next=p;
q=p;
p=new struct stu;
}
delete p;
p->next=0;
return(h);
}
搜索更多相关主题的帖子: 单链 
2006-12-16 17:26
sanjin
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2005-12-14
收藏
得分:0 

用VC++使用类的思想学习数据结构会比较好理解。
给个例子如下。
头文件:
typedef struct pronode
{
int wx;
int up;
int down;
boolean haverule;
CString rule;
CString name;
pronode *next;
}mypro;

class profile
{
private:
int len;
mypro node;
mypro *headnode;
public:
profile()
{
len=0;
headnode=new mypro;
headnode->next=NULL;
}
void addnode(mypro node);
bool instert(int n,mypro node);
bool delnode(int n);
mypro* getnode(int n);
int getlen();
};

CPP文件
//-----------------------------------
//profile
//-----------------------------------
void profile::addnode(mypro node)
{
mypro *addnode,*getnode,*nextnode;
if(headnode->next==NULL)
{
addnode=new mypro;
(*addnode).down=node.down;
(*addnode).up=node.up;
(*addnode).wx=node.wx;
(*addnode).haverule=node.haverule;
(*addnode).name.GetBuffer(100);
(*addnode).name.Insert((*addnode).name.GetLength(),(LPCTSTR)node.name);
(*addnode).name.ReleaseBuffer();
(*addnode).rule.GetBuffer(100);
(*addnode).rule.Insert((*addnode).rule.GetLength(),(LPCTSTR)node.rule);
(*addnode).rule.ReleaseBuffer();
(*addnode).next=NULL;
headnode->next=addnode;
len++;
}
else
{
getnode=headnode;
nextnode=headnode;
do
{
getnode=nextnode;
nextnode=getnode->next;
}while(nextnode!=NULL);
addnode=new mypro;
getnode->next=addnode;
(*addnode).down=node.down;
(*addnode).up=node.up;
(*addnode).wx=node.wx;
(*addnode).haverule=node.haverule;
(*addnode).name.GetBuffer(100);
(*addnode).name.Insert((*addnode).name.GetLength(),(LPCTSTR)node.name);
(*addnode).name.ReleaseBuffer();
(*addnode).rule.GetBuffer(100);
(*addnode).rule.Insert((*addnode).rule.GetLength(),(LPCTSTR)node.rule);
(*addnode).rule.ReleaseBuffer();
(*addnode).next=NULL;
(*getnode).next=addnode;
len++;
}
}

mypro* profile::getnode(int n)
{
int intpos=0;
mypro *renode,*getnode,*nextnode;
renode=new mypro;
(*renode).down=-1;
(*renode).up=-1;
(*renode).wx=-1;
(*renode).haverule=false;
(*renode).name="";
(*renode).rule="";
(*renode).next=NULL;
if(n>len-1)
{
return renode;
delete renode;
}
else
{
nextnode=headnode;
getnode=headnode;
do
{
getnode=nextnode;
nextnode=getnode->next;
intpos++;
}while(intpos<=n);
renode->down=nextnode->down;
renode->up=nextnode->up;
renode->name=nextnode->name;
renode->haverule=nextnode->haverule;
renode->rule=nextnode->rule;
renode->next=nextnode->next;
renode->wx=nextnode->wx;
return renode;
delete renode;
}
}

int profile::getlen()
{
return len-1;
}

bool profile::delnode(int n)
{
int intpos=0;
mypro *getnode,*nextnode,*delnode;
if(n>len-1)
return false;
else
{
nextnode=new mypro;
getnode=headnode;
do
{
nextnode=getnode->next;
getnode=nextnode;
intpos++;
}while(intpos<n);
delnode=nextnode->next;
nextnode->next=nextnode->next->next;
delete delnode;
len--;
return true;
}//if(n>len-1)
}

bool profile::instert(int n,mypro node)
{
int intpos=0;
mypro *getnode,*nextnode,*addnode1;
if(n>len-1)
return false;
else
{
addnode1=new mypro;
nextnode=headnode;
do
{
getnode=nextnode->next;
nextnode=getnode;
(*addnode1).next=nextnode->next;
}while(++intpos<=n);
if(nextnode->next=NULL)
this->addnode(node);
else
{
(*addnode1).down=node.down;
(*addnode1).up=node.up;
(*addnode1).wx=node.wx;
(*addnode1).haverule=node.haverule;
(*addnode1).name=node.name;
(*addnode1).rule=node.rule;
nextnode->next=addnode1;
}
len++;
return true;
}//if(n>len-1)
}


”我游啊游”,“那边才是C边啊”,“哦”
2006-12-18 16:29
快速回复:[求助]创建单链表的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011133 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved