C语言习题 链表建立,插入,删除,输出
时间: 1ms 内存:128M
描述:
编写一个函数creatlink,用来建立一个动态链表。(包含学号和成绩)
编写一个函数printlink,用来输出一个链表。
编写一个函数dellink,用来删除动态链表中一个指定的结点(由实参指定某一学号,表示要删除该学生结点)。
编写一个函数insertlink,用来向动态链表插入一个结点。
编写一个函数freelink,用来释放一个动态链表。
输入:
输入多个学生的学号和成绩,建立动态链表,以0 0 结束
输入学号,删除链表中的对应结点
插入两个链表结点
输出:
输出的链表
示例输入:
1001 100
1002 95
1005 90
1008 76
0 0
1005
1006 98
1009 99
示例输出:
1001 100.00
1002 95.00
1006 98.00
1008 76.00
1009 99.00
提示:
参考答案(内存最优[752]):
#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)); //开辟一个新单元,并使p1,p2指向它
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) //是空表
{
//cout<<"list null!"<<endl;
return(head);
}
p1=head; //使p1指向第一个结点
while(num!=p1->num && p1->next!=NULL) //p1指向的不是所要找的结点且后面还有结点
{
p2=p1; //p1后移一个结点
p1=p1->next;
}
if(num==p1->num) //找到了
{
if(p1==head) head=p1->next; //若p1指向的是首结点,把第二个结点地址赋予head
else p2->next=p1->next; //否则将下一结点地址赋给前一结点地址
//cout<<"delete:"<<num<<endl;
// n=n-1;
}
//else cout<<"cannot find "<<num; //找不到该结点
return(head);
}
struct student *insertlink(struct student *head,struct student *stud) //插入结点的函数
{
struct student *p0,*p1,*p2;
p1=head; //使p1指向第一个结点
//p0=stud; //指向要插入的结点
p0 =(struct student*)malloc(sizeof(struct student)); //edit by lyh
*p0 = *stud; // 2013.11.7
if(head==NULL) //原来的链表是空表
{
head=p0; //使p0指向的结点作为头结点
p0->next=NULL;
}
else
{
while((p0->num>p1->num) && (p1->next!=NULL))
{
p2=p1; //使p2指向刚才p1指向的结点
p1=p1->next;
} //p1后移一个结点
if(p0->num<=p1->num)
{
if(head==p1) head=p0; //插到原来第一个结点之前
else p2->next=p0; //插到p2指向的结点之后*/
p0->next=p1;
}
else
{
p1->next=p0; //插到最后的结点之后
p0->next=NULL;
}
}
// n=n+1; //结点数加1
return (head);
}
void printlink(struct student *head) //输出链表的函数
{
struct student *p;
//cout<<"Now,These "<<n<<" records are:"<<endl;
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;
//cout<<"Now,These "<<n<<" records are:"<<endl;
p=head;
if(head!=NULL)
do
{
q =p ;
p=p->next;
free(q);
}
while(p!=NULL);
}
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;
}
参考答案(时间最优[0]):
#include <iostream>
#include <iomanip>
using namespace std;
struct student
{
long num;
float score;
student *next;
};
student *creatlink(void) //建立链表的函数
{
student *head;
student *p1,*p2;
int n=0;
p1=p2=new student; //开辟一个新单元,并使p1,p2指向它
cin>>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=new student;
cin>>p1->num>>p1->score;
}
p2->next=NULL;
return(head);
}
student *dellink(student *head,long num) //删除结的函数
{
student *p1,*p2;
if (head==NULL) //是空表
{
//cout<<"list null!"<<endl;
return(head);
}
p1=head; //使p1指向第一个结点
while(num!=p1->num && p1->next!=NULL) //p1指向的不是所要找的结点且后面还有结点
{
p2=p1; //p1后移一个结点
p1=p1->next;
}
if(num==p1->num) //找到了
{
if(p1==head) head=p1->next; //若p1指向的是首结点,把第二个结点地址赋予head
else p2->next=p1->next; //否则将下一结点地址赋给前一结点地址
//cout<<"delete:"<<num<<endl;
// n=n-1;
}
//else cout<<"cannot find "<<num; //找不到该结点
return(head);
}
student *insertlink(student *head,student *stud) //插入结点的函数
{
student *p0,*p1,*p2;
p1=head; //使p1指向第一个结点
//p0=stud; //指向要插入的结点
p0 = new student; //edit by lyh
*p0 = *stud; // 2013.11.7
if(head==NULL) //原来的链表是空表
{
head=p0; //使p0指向的结点作为头结点
p0->next=NULL;
}
else
{
while((p0->num>p1->num) && (p1->next!=NULL))
{
p2=p1; //使p2指向刚才p1指向的结点
p1=p1->next;
} //p1后移一个结点
if(p0->num<=p1->num)
{
if(head==p1) head=p0; //插到原来第一个结点之前
else p2->next=p0; //插到p2指向的结点之后*/
p0->next=p1;
}
else
{
p1->next=p0; //插到最后的结点之后
p0->next=NULL;
}
}
// n=n+1; //结点数加1
return (head);
}
void printlink(student *head) //输出链表的函数
{
student *p;
//cout<<"Now,These "<<n<<" records are:"<<endl;
p=head;
if(head!=NULL)
do
{
cout<<p->num<<" "<<p->score<<endl;
p=p->next;
}
while(p!=NULL);
}
void freelink(student *head) //释放链表
{
student *p,*q;
//cout<<"Now,These "<<n<<" records are:"<<endl;
p=head;
if(head!=NULL)
do
{
q =p ;
p=p->next;
delete q;
}
while(p!=NULL);
}
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;
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。
