| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 686 人关注过本帖
标题:动态链表插入节点问题
只看楼主 加入收藏
la_zhao
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-4-27
收藏
 问题点数:0 回复次数:0 
动态链表插入节点问题
在main函数中调用相应的函数完成下列四个功能的操作:
(1)input函数功能,用于输入五个学生信息(结点)的动态链表。在main函数中通过调用print函数输出整个新链表。
(2)print函数功能,根据头指针,用于输出从头指针开始的整个链表。在main函数中通过调用print函数输出整个链表的值。
(3)insert函数功能:从键盘输入一个动态新结点,必须插入在倒数第1个之前,作为倒数第2个。在main函数中通过调用print函数输出整个新链表;
(4)swaphead函数功能:将原链表的第1个结点与第2个结点互换形成新链表。在main函数中通过调用print函数输出整个新链表。
插入新节点后输出有问题。
求教是哪里错了?
#include<iostream>
using namespace std;
struct st
{int sno;
float score[3];
st *next;
};

int main()
{st *input();
void print(st *);
st *insert(st *p);
st *swaphead(st *p);
st *head;
cout<<"输入5个学生的信息"<<endl;
head=input();
print(head);
head=insert(head);
print(head);
head=swaphead(head);
print(head);
system("pause");
return 0;}

st *swaphead(st *p)
{st *h,*c;
h=p;c=p=p->next;
p->next=h->next;h->next=c->next;
h=p;
return h;}


st *insert(st *p)
{st *c,*x,*h=p;
int i;
x=new st;
cout<<"输入新信息"<<endl;
cin>>x->sno;
for(i=0;i<=2;i++)
cin>>x->score[i];
x->next=NULL;
while(p->next!=NULL){c=p;p=p->next;}
(c->next)=x;(x->next)=p;
p->next=NULL;
return h;}


st *input()
{st *head,*c,*p;
int i,j;
for(j=1;j<=5;j++)
{p=new st;
cin>>p->sno;
for(i=0;i<=2;i++)
cin>>p->score[i];
p->next=NULL;
if(j==1){head=p;c=p;}
else{c->next=p;c=p;}}
return head;}

void print(st *head)
{int i;
    st *p;
p=head;
while (p!=NULL)
{cout<<p->sno<<" ";
for(i=0;i<=2;i++)
cout<<p->score[i]<<" ";
cout<<endl;
p=p->next;} }
搜索更多相关主题的帖子: insert 动态 倒数 键盘 信息 
2014-04-27 17:59
快速回复:动态链表插入节点问题
数据加载中...
 
   



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

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