Problem B: C语言习题 链表建立,插入,删除,输出
Problem B: C语言习题 链表建立,插入,删除,输出
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 976 Solved: 505
[Submit][Status][Web Board]
Description
编写一个函数creatlink,用来建立一个动态链表。(包含学号和成绩)
编写一个函数printlink,用来输出一个链表。
编写一个函数dellink,用来删除动态链表中一个指定的结点(由实参指定某一学号,表示要删除该学生结点)。
编写一个函数insertlink,用来向动态链表插入一个结点。
编写一个函数freelink,用来释放一个动态链表。
Input
输入多个学生的学号和成绩,建立动态链表,以0 0 结束
输入学号,删除链表中的对应结点
插入两个链表结点
Output
输出的链表
Sample Input
1001 100
1002 95
1005 90
1008 76
0 0
1005
1006 98
1009 99
Sample Output
1001 100.00
1002 95.00
1006 98.00
1008 76.00
1009 99.00
HINT
主函数已给定如下,提交时不需要包含下述主函数
/* C代码 */
int main()
{
struct student *creatlink(void);
struct student *dellink(struct student *,long);
struct student *insertlink(struct student *,struct student *);
void printlink(struct student *);
void freelink(struct student *);
struct student *head,stu;
long del_num;
head=creatlink();
scanf("%ld",&del_num);
head=dellink(head,del_num);
scanf("%ld%f",&stu.num,&stu.score);
head=insertlink(head,&stu);
scanf("%ld%f",&stu.num,&stu.score);
head=insertlink(head,&stu);
printlink(head);
freelink(head);
return 0;
}
/* C++代码 */
int main()
{
student *creatlink(void);
student *dellink(student *,long);
student *insertlink(student *,student *);
void printlink(student *);
void freelink(student *);
student *head,stu;
long del_num;
head=creatlink();
cin>>del_num;
head=dellink(head,del_num);
cin>>stu.num>>stu.score;
head=insertlink(head,&stu);
cin>>stu.num>>stu.score;
head=insertlink(head,&stu);
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
printlink(head);
freelink(head);
return 0;
}
/* C代码 */
int main()
{
struct student *creatlink(void);
struct student *dellink(struct student *,long);
struct student *insertlink(struct student *,struct student *);
void printlink(struct student *);
void freelink(struct student *);
struct student *head,stu;
long del_num;
head=creatlink();
scanf("%ld",&del_num);
head=dellink(head,del_num);
scanf("%ld%f",&stu.num,&stu.score);
head=insertlink(head,&stu);
scanf("%ld%f",&stu.num,&stu.score);
head=insertlink(head,&stu);
printlink(head);
freelink(head);
return 0;
}
/* C++代码 */
int main()
{
student *creatlink(void);
student *dellink(student *,long);
student *insertlink(student *,student *);
void printlink(student *);
void freelink(student *);
student *head,stu;
long del_num;
head=creatlink();
cin>>del_num;
head=dellink(head,del_num);
cin>>stu.num>>stu.score;
head=insertlink(head,&stu);
cin>>stu.num>>stu.score;
head=insertlink(head,&stu);
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
printlink(head);
freelink(head);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
struct student
{
long num;
float score;
struct student *next;
};
struct student *creatlink(void)
{
struct student *head;
struct student *p1,*p2;
int n=0;
p1=p2=(struct student*)malloc(sizeof(struct student));
scanf("%ld%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student*)malloc(sizeof(struct student));
scanf("%ld%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
struct student *dellink(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL)
{
return(head);
}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
}
return(head);
}
struct student *insertlink(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;
p0=(struct student*)malloc(sizeof(struct student));
*p0=*stud;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
while((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{
if(head==p1)
head=p0;
else
p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
return(head);
}
void printlink(struct student *head)
{
struct student *p;
p=head;
if(head!=NULL)
do
{
printf("%ld %.2f\n",p->num,p->score);
p=p->next;
}
while(p!=NULL);
}
void freelink(struct student *head)
{
struct student *p,*q;
p=head;
if(head!=NULL)
do
{
q=p;
p=p->next;
free(q);
}
while(p!=NULL);
}
#include<stdio.h>
#include<stdlib.h>
/********************************************************/
typedef struct student
{
long num;
float score;
struct student *next;
}student;
/********************************************************/
student *creatlink(void)//建立链表
{
student *st=(student *)malloc(sizeof(student)),*pend,*head;//申请内存空间
scanf("%ld%f",&st->num,&st->score);//输入学生信息
head=NULL;
while(st->num!=0)//当输入0 0时结束
{
if(head==NULL)
head=st;//head起个头
else
pend->next=st;//指向下一个结点的地址
pend=st;
st=(student *)malloc(sizeof(student));//重新申请内存地址
scanf("%ld%f",&st->num,&st->score);
}
pend->next=NULL;
free(st);//把多申请的那个释放
return head;//返回首地址,千万别改
}
/********************************************************/
void printlink(student *head)//链表输出
{
while(head!=NULL)
{
printf("%ld %.2f\n",head->num,head->score);
head=head->next;
}
}
/********************************************************/
student *dellink(student *head,long del)//删除
{
student *p1,*p2=head;
while(p2!=NULL)
{
if(del==p2->num)
{
p1->next=p2->next;
//上一个的next指向被删除结点的下一个地址
//也就是上一个的next的值换成下一个next的值
break;
}
else
{
p1=p2;
p2=p2->next;
}
}
return head;
}
/********************************************************/
student *insertlink(student *head,student *std)//插入
{
student *p2=head,*p1,*st=(student *)malloc(sizeof(student));//一定要申请新的内存地址
st->num=std->num;//把值赋给st就可以了,别赋地址
st->score=std->score;
st->next=std->next;
while(p2->next!=NULL)
{
if(p2->num<st->num&&st->num<p2->next->num)
//简单的排一下序
{
p1=p2->next;
p2->next=st;
st->next=p1;
return head;
//插进去了就退出吧
}
else
p2=p2->next;
}
if(p2->next==NULL)//当在最后插入结点时
{
p2->next=st;
st->next=NULL;//别忘了
}
return head;
}
/********************************************************/
void freelink(student *head)//释放链表
{
student *p1=head,*p2;
while(p1!=NULL)//排着释放
{
p2=p1->next;
free(p1);
p1=p2;
}
}
int main()
{
struct student *creatlink(void);
struct student *dellink(struct student *,long);
struct student *insertlink(struct student *,struct student *);
void printlink(struct student *);
void freelink(struct student *);
struct student *head,stu;
long del_num;
head=creatlink();
scanf("%ld",&del_num);
head=dellink(head,del_num);
scanf("%ld%f",&stu.num,&stu.score);
head=insertlink(head,&stu);
scanf("%ld%f",&stu.num,&stu.score);
head=insertlink(head,&stu);
printlink(head);
freelink(head);
return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct student
{
long num;
float score;
struct student *next;
}student;
student *creatlink(void)
{
student *head = NULL;
student *last , *p ;
p =(student *)malloc(sizeof(student));
scanf("%ld%f",&p->num,&p->score);
while(p->num!=0)
{
if(head==NULL)
{
head=p;
}
else
last->next=p;
last=p;
p =(student *)malloc(sizeof(student));
scanf("%ld %f",&p->num,&p->score);
}
last->next=NULL;
free(p);
return head;
}
student *dellink(student *head,long del)
{
student *p1,*p2=head;
while(p2)
{
if(del==p2->num)
{
p1->next=p2->next;
free(p2);
break;
}
else
{
p1=p2;
p2=p2->next;
}
}
return head;
}
void printlink(struct student *head)
{
while(head!=NULL)
{
printf("%ld %.2f\n",head->num,head->score);
head=head->next;
}
}
void freelink(struct student *head)
{
student *p1=head;
student *p2;
while(p1)
{
p2=p1->next;
free(p1);
p1=p2;
}
}
student *insertlink(student *head,student *stu)
{
student *p =head ;
student *pe ; //记录前驱
student *pLast =NULL ; //指向尾节点的;
student *newbase = (student *)malloc( sizeof(student));
if(newbase==NULL) //分配内存失败
exit(-1);
newbase->num =stu->num ;
newbase->score =stu->score ;
newbase->next=NULL;
while(p->next!=NULL)
{
p= p->next ;
}
pLast = p ;
p = head ;//重新指向
if(head->num>newbase->num)
{
/*作为头结点的*/
newbase->next =head;
head =newbase ;
}
else if(head->num < newbase->num && newbase->num < pLast->num)
{
/*在中间插入*/
while((p->num<=newbase->num)&&(p->next!=NULL))
{
pe= p ;
p=p->next ;
}
pe->next =newbase ;
newbase->next = p;
}
else
{
/*尾*/
pLast->next =newbase ;
}
return head ;
}
int main()
{
struct student *creatlink(void);
struct student *dellink(struct student *,long);
struct student *insertlink(struct student *,struct student *);
void printlink(struct student *);
void freelink(struct student *);
struct student *head,stu;
long del_num;
head=creatlink();
scanf("%ld",&del_num);
head=dellink(head,del_num);
scanf("%ld%f",&stu.num,&stu.score);
head=insertlink(head,&stu);
scanf("%ld%f",&stu.num,&stu.score);
head=insertlink(head,&stu);
printlink(head);
freelink(head);
return 0;
}

浙公网安备 33010602011771号