C语言习题 链表建立,插入,删除,输出

Home Web Board ProblemSet Standing Status Statistics
OJ系统新功能测试中,如有问题请联系 17865569030 17865569180 17865571035 尽量不要在上课时间打电话

Problem A: C语言习题 链表建立,插入,删除,输出

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 1389   Solved: 687
[ 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;

}

[ Submit][ Status][ Web Board]

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<iomanip>
typedef struct student{
	
	long num; 
	double score ;
	struct student *next ;
}student;
using namespace std ;
student *creatlink()
{

	student * head =NULL;
	student *last ;
	student *p ;
	long n ;
	double s ;
	scanf("%ld%lf",&n,&s);
	while(n&&s)
	{
		p=(student *) malloc(sizeof(student));
		p->num = n ;
		p->score =s ;
		p->next =NULL ;
		if(head==NULL)
		head = p ;
		else
		{
			last->next = p; 
		}
		last = p ;		
		
		scanf("%ld%lf",&n,&s);
	}
return head ;
	
}
student *dellink(student *head,long d)
{/*删除 d 学号的节点*/
	student *p = head;
	student *pe  ;
	if(head==NULL)
	{
		printf("空链表\n");
		exit(-1);
	}
	while( d!=p->num && p->next!=NULL)
	{
		pe =p ; //前驱节点记录 
		p=p->next ;//当前节点记录 
		
	}
	
	if(d==p->num)
	{
		if(p==head)
		{
			head =p->next ;
		}
		else
		pe->next =p->next ;
		free(p);
		
	}
	else
	{
		cout<<"not found \n";
	} 

	return head; 
}
student *insertlink(student *head,student *s)
{
	/**/
	student *p  =head ;
	student *pe =head ;
 	student *pc  ; 
 	student *last ;
 	student *pl ;
	student *newbase = (student *)malloc(sizeof(student));
	newbase->num =s->num ;
	newbase->score =s->score ;
	newbase->next =NULL;
		while(p)
		{
			last = p ;
			p=p->next ;
		}

		p =head;
	
		if(s->num < head->num)
		{
			/*作为头*/
			head =newbase ;
			newbase->next =pe->next ;			
		}	
		else if(s->num <last->num &&s->num >head->num )
		{
			while(s->num>=p->num && p->next !=NULL)
			{	pc= p ;							
				p=p->next ;				
			}
			newbase->next = p ;
			pc->next =newbase ;
						
		}
		else	
	{
		while(p)
		{
			pl =  p ;
			p=p->next ;
		}
		pl->next =newbase ;
		newbase->next =NULL ;
			
	}
				
	return head ;
		
}
void printlink(student *head)
{
	student *p =head; 
	while(p)
	{
		cout<<p->num<<" "<<p->score<<endl;		
		p=p->next ;
	}

	
}
 void freelink(student *head)
 {
 	student *p =head ;
 	while(p)
 	{
 		free(p);
 		p=p->next ;
 		
	 }	
 	
 }

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;
}


猜你喜欢

转载自blog.csdn.net/qq_41661809/article/details/80821080