C语言方面的

来源:百度知道 编辑:UC知道 时间:2024/06/21 17:34:10
#include<stdio.h>
#include<stdib.h>
typedef struct node
{int d;struct node *next;}NODE;
NODE *insert(NODE *head,int x,int key)
{NODE *s,*p,*q;
s=(NODE *)malloc(sizeof(NODE));
s->d=key;s->next=NULL;
if(head==NULL) {head=s;return head;}
if(head->d==x) {s->next=head;head=s;return head;}
else
{q=head;p=q->next;
while((p->d!=x)&&(p->next!=NULL)){q=p;p=p->next;}
if(p->d==x) {s->next=p;q->next=s;}
else {s->next=NULL;p->next=s;}
return head;
}
}
void print(NODE *head)
{if(head==NULL)return;
while(head->next!=NULL)
{printf("%d,"head->d);head=head->next;}
printf("%d\n",head->d);
}
main()
{NODE *head=NULL;
head=insert(head,o,3);print(head);
head=insert(head,3,1);print(head);
head=insert(head.4.5);print(head);
}
第1行是3
第2行是1,3
第3行是1,3,5
怎么算的

#include<stdio.h>
#include<stdlib.h>
typedef struct node
{int d;
struct node *next;
}NODE;
NODE *insert(NODE *head,int x,int key)
{NODE *s,*p,*q;
s=(NODE *)malloc(sizeof(NODE)); //分配结点空间
s->d=key;s->next=NULL; //给结点赋值
if(head==NULL) //建立首结点!
{head=s;return head;} //如果首结点已存在,上面又建立个新结点
if(head->d==x) //那么把head指针指向首结点,
{s->next=head;head=s;return head;}
else
{q=head;p=q->next; //利用q=head指针向后移动,这样head始终是首结点
while((p->d!=x)&&(p->next!=NULL))
{q=p;p=p->next;}
if(p->d==x)
{s->next=p;q->next=s;}
else
{s->next=NULL;p->next=s;}
return head; //始终返回首结点
}
}
void print(NODE *head)
{if(head==NULL)return;
while(head->next!=NULL)
{printf("%d,",head->d);head=head->next;}
printf("%d\n",head->d);
}
void main()
{NODE *head=NULL;
h