首页 > 编程语言 > 详细

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

时间:2014-03-24 15:10:08      阅读:521      评论:0      收藏:0      [点我收藏+]
#include<iostream>
#include<iomanip>
using namespace std;
struct student
{int num,score;
student *p;
};

student *creatlink(void)
{
	student *head,*p;
	head=new student;
	p=head;
	while(1)
	{cin>>(*p).num>>(*p).score;
	if((*p).num==0&&(*p).score==0)
	{delete (*p).p;(*p).p=NULL; return head;}
	(*p).p=new student;
	p=p->p;
	(*p).p=NULL;
	}
	
	}
student *dellink(student *head,long a)
{student *p,*h;
h=p=head;
if((*p).num==a)
{head=(*p).p;
delete p;   //////////////////////////
return head;}
while(1)
{
	if((*p).num==a)
{h->p=p->p;delete p;return head;}
h=p;
p=(*p).p;
}
return head;}
student *insertlink(student *head,student *a)
{student *p,*h;
h=head;
p=new student ;
p->num=a->num;
p->score=a->score;
p->score=NULL;
if(p->num<head->num)
{head=p;
p->p=h;return head;}
while(1)
{if(h->num<p->num)
{
p->p=h->p;
h->p=p;return head;}

h=h->p;
}
}
void printlink(student *head)
{  
while(head!=NULL)
{
	cout<<head->num<<‘ ‘<<head->score<<endl;
	head=head->p;

}
}


    void freelink(student *head)
	{
		 student *p;
		 while(head!=NULL)
		 {
			 p=head->p;
			 delete head;
			 head=p;
		 
		 }
	
	}
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语言习题 链表建立,插入,删除,输出,布布扣,bubuko.com

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

原文:http://blog.csdn.net/rememberautumn/article/details/21945203

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!