2016-11-25 回答
#include
#include
#include
#include
#include
typedef struct node
{int score;
char name[10];
struct node *next;
}stu,*pstu;
#define size sizeof(stu)//这里要放到结构体之后,因为stu定义结构体后才//声明的
pstu head,last;
pstu creatlinks(int);
void deletenode(pstu,int);
void freelink(pstu);
void insertnode(pstu,int);
pstu findnode(pstu,int);
void printflink(pstu);
void sortlink(pstu);
int main()
{
head=creatlinks(7);
last = head;
last=last->next;
printf("%d\n",last->score);
last=last->next;
printf("%d\n",last->score);
last=last->next;
printf("%d\n",last->score);
last=last->next;
printf("%d\n",last->score);
last=last->next;
printf("%d\n",last->score);
printflink(head);
deletenode(head,4) ;
// sortlink(head);
//这个函数在下面定义的时候看不明白
//你要做什么,所以没 改,我把它注释出来,自己根据功能改吧
printflink(head);
insertnode(head,6);
printflink(head);
getch();
return 0;
}
pstu creatlinks(int n)//这里创建链表我重新写过了
{
pstu head = null, first=null,ptr=null;
head = first = (pstu)malloc(size);
printf("input the number 1 information: ") ;
scanf("%s%d",&first->name,&first->score);
for ( int i = 1; i < n; i++)
{
ptr = (pstu)malloc(size);
printf("input the number %d information: ", i+1) ;
scanf("%s%d",&ptr->name,&ptr->score);
first->next = ptr;
first = ptr;
ptr = null;
}
first->next = null;
return head;
}
void deletenode(pstu f,int i)
{pstu first=null,now=null;
now=f;
while(--i)
{first=now;now=now->next;}
first->next=now->next;
free(now);
}
void freelink(pstu e)
{ pstu first=null,last=null;
first=e;
while(first!=null)///first不能是0改为null
{last=first->next; free(first);first=last;}
}
void insertnode(pstu d,int j)
{pstu now=null,pp=null, p;
now=d;
int i = 0;
printf("input the information:") ;
pp = (pstu)malloc(size);//你要插入的结点,要先分配内存给它
pp->next = null;
scanf("%s%d",pp->name,&pp->score);
while(now != null && i != j)
{p = now;
now=now->next;
}
p->next=pp;
pp->next=now;
}
pstu findnode(pstu c,int k)
{pstu now=null;
now=c;
while(--k)
{now=now->next;}
return now;
}
void printflink(pstu b)
{
while(b != null)//这里你要输出链表里的东西,用一个循环就ok
{
printf("%d\n",b->score);
b=b->next;
}
}
/*void sortlink(pstu a)
{pstu p=null,o=null;
int t;
char *tt;
for(p=a->next;p->next;p=p->next)
for(o=p->next;o;o=o->next)
if(p->scorescore||(p->score==o->score&&strcmp(o->name,p->name)))
{t=p->score;p->score=o->score;o->score=t;
tt=p->name;p->name=o->name;o->name=tt;
}
} */