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

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

``````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``````

``````#include <stdio.h>
#include <stdlib.h>
struct student
{
long num;
float score;
struct student *next;
};

{
struct student *p1,*p2;
int n=0;
p1=p2=(struct student*)malloc(sizeof(struct student));       //开辟一个新单元，并使p1,p2指向它
scanf("%ld%f",&p1->num,&p1->score);
while(p1->num!=0)
{
n=n+1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(sizeof(struct student));
scanf("%ld%f",&p1->num,&p1->score);
}
p2->next=NULL;
}

{
struct student *p1,*p2;
{
//cout<<"list null!"<<endl;
}
while(num!=p1->num && p1->next!=NULL) //p1指向的不是所要找的结点且后面还有结点
{
p2=p1;    //p1后移一个结点
p1=p1->next;
}
if(num==p1->num)                        //找到了
{
else p2->next=p1->next;    //否则将下一结点地址赋给前一结点地址
//cout<<"delete:"<<num<<endl;
//        n=n-1;
}
//else cout<<"cannot find "<<num;     //找不到该结点
}

{
struct student *p0,*p1,*p2;
//p0=stud;                          //指向要插入的结点
p0 =(struct student*)malloc(sizeof(struct student));    //edit by lyh
*p0 = *stud;        // 2013.11.7
{
p0->next=NULL;
}
else
{
while((p0->num>p1->num) && (p1->next!=NULL))
{
p2=p1;                       //使ｐ２指向刚才p１指向的结点
p1=p1->next;
}                //p1后移一个结点
if(p0->num<=p1->num)
{
else p2->next=p0;            //插到p2指向的结点之后*／
p0->next=p1;
}
else
{
p1->next=p0;    //插到最后的结点之后
p0->next=NULL;
}
}
//    n=n+1;                         //结点数加１
}

{
struct student *p;
//cout<<"Now，These "<<n<<" records are："<<endl;
do
{
printf("%ld %.2f\n",p->num,p->score);
p=p->next;
}
while(p!=NULL);
}
{
struct student *p,*q;
//cout<<"Now，These "<<n<<" records are："<<endl;
do
{
q =p ;
p=p->next;
free(q);
}
while(p!=NULL);
}

int main()
{
struct student *insertlink(struct student *,struct student *);
long del_num;
scanf("%ld",&del_num);
scanf("%ld%f",&stu.num,&stu.score);
scanf("%ld%f",&stu.num,&stu.score);
return 0;
}``````

``````#include <iostream>
#include <iomanip>
using namespace std;
struct student
{
long num;
float score;
student *next;
};

{
student *p1,*p2;
int n=0;
p1=p2=new student;       //开辟一个新单元，并使p1,p2指向它
cin>>p1->num>>p1->score;
while(p1->num!=0)
{
n=n+1;
else p2->next=p1;
p2=p1;
p1=new student;
cin>>p1->num>>p1->score;
}
p2->next=NULL;
}

{
student *p1,*p2;
{
//cout<<"list null!"<<endl;
}
while(num!=p1->num && p1->next!=NULL) //p1指向的不是所要找的结点且后面还有结点
{
p2=p1;    //p1后移一个结点
p1=p1->next;
}
if(num==p1->num)                        //找到了
{
else p2->next=p1->next;    //否则将下一结点地址赋给前一结点地址
//cout<<"delete:"<<num<<endl;
//        n=n-1;
}
//else cout<<"cannot find "<<num;     //找不到该结点
}

{
student *p0,*p1,*p2;
//p0=stud;                          //指向要插入的结点
p0 = new student;    //edit by lyh
*p0 = *stud;        // 2013.11.7
{
p0->next=NULL;
}
else
{
while((p0->num>p1->num) && (p1->next!=NULL))
{
p2=p1;                       //使ｐ２指向刚才p１指向的结点
p1=p1->next;
}                //p1后移一个结点
if(p0->num<=p1->num)
{
else p2->next=p0;            //插到p2指向的结点之后*／
p0->next=p1;
}
else
{
p1->next=p0;    //插到最后的结点之后
p0->next=NULL;
}
}
//    n=n+1;                         //结点数加１
}

{
student *p;
//cout<<"Now，These "<<n<<" records are："<<endl;
do
{
cout<<p->num<<" "<<p->score<<endl;
p=p->next;
}
while(p!=NULL);
}
{
student *p,*q;
//cout<<"Now，These "<<n<<" records are："<<endl;
do
{
q =p ;
p=p->next;
delete q;
}
while(p!=NULL);
}
int main()
{
long del_num;
cin>>del_num;
cin>>stu.num>>stu.score;
cin>>stu.num>>stu.score;