#include<stdio.h>
#include<stdlib.h>
#define len sizeof(struct people)
struct people{
int name;
struct people *next;
};
int cnt=1;
void print(struct people *head){
struct people *p;
p = head;
if(head!=NULL){
do{
printf("%d",p->name);
p = p->next;
if(p!=NULL){
printf(" ");
}
}while(p!=NULL);
}
}
struct people *insert(struct people *head,int i){
struct people *p;
struct people *q;
p = head;
q = (struct people *)malloc(len);
q->name = cnt++;
if(i==0){
q->next=head;
head = q;
}else{
do{
if(p->name==i){
q->next=p->next;
p->next=q;
break;
}else{
p=p->next;
}
}while(p!=NULL);
}
return(head);
}
int main(){
int n;
scanf("%d",&n);
struct people *head;
int o;
head = (struct people *)malloc(len);
head->name=cnt;
cnt++;
head->next=NULL;
int hi;
scanf("%d",&hi);
for(int i=0;i<(n-1);i++){
scanf("%d",&o);
head = insert(head,o);
}
print(head);
return 0;
}
#define LEN sizeof(struct student)
struct student *creat( )
{
struct student *head=NULL, *p1,*p2;
int n=1;
p1=p2=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
while(p1->num!=0)
{
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
n++;
}
p2->next=NULL;
return(head);
}
struct node *insert(struct node *head, int i)
{
struct node *p,*q;
p=head;
q=(struct student *)malloc(LEN);
scanf("%ld,%f",&q->num,&q->score);
if(i==1){q->=head; head=q;}
else
{
for(j=1;j<i-1;j++)
p=p->next;
q->next=p->next;
p->next=q;
}
return(head);
}
struct node *del(struct node *head, int d )
{
struct node *p1,*p2;
p1=head;
while( ) //查找要删除的结点
{
p2=p1;
p1=p1->next;
}
if(p1->num==d) //删除结点
{
if(p1==head) head=p1->next;
else p2->next=p1->next;
free(p1); //释放所删除结点的空间
}
else printf("找不到该结点\n");
return(head);
}
void print (struct student *head)
{
struct student *p;
p=head;
if (head !=NULL)
do
{
printf("%ld,%f",p->num,p->score);
p=p->next;
} while (p!=NULL);
}
原文:https://www.cnblogs.com/Xuuxxi/p/14556266.html