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

#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语言习题 链表建立,插入,删除,输出

上一篇:paip.指针 引用 c++ java的使用总结.


下一篇:大端和小端C++转载记录