#include<stdio.h>
#include<stdlib.h>
typedef struct LNode{
int data;
struct LNode*next;
}LNode;
//尾插法建立链表
void ListTailInsertAndprint(LNode*l){
int x;
//l=(LNode*)malloc(sizeof(LNode));//建立一个头结点,但并未赋值为NULL
LNode*s,*r=l; //*s为空指针,*r指向l
scanf("%d",&x);
while(x!=9999){ //数字是随机的
s=(LNode*)malloc(sizeof(LNode)); //申请新空间
s->data=x; //
r->next=s; //把头结点的next指向第二个结点的data
r=s; //把r指向第二个结点
scanf("%d",&x);
}
r->next=NULL;
LNode*cur=l;
while(cur!=NULL){
printf("%d->",cur->data);
cur=cur->next;
}
printf("NULL");
}
int main(){
LNode l;
ListTailInsertAndprint(&l);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node*next;
};
int main()
{//链表元素的输入
struct node *head;
struct node *p,*q,*t;
int i,n,a;
scanf("%d",&n);
head=NULL;
for(i=1; i<=n; i++) {
scanf("%d",&a);
p=(struct node*)malloc(sizeof(struct node));
p->data=a;
p->next=NULL;
if(head==NULL)
head=p;
else
q->next=p;
q=p;
}
//链表元素的插入
scanf("%d",&a);
t=head;
while(t!=0){
if(t->next==NULL || t->next->data>a)
{
p=(struct node*)malloc(sizeof(struct node));
p->data;
p->next=t->next;
t->next=p;
break;
}
t=t->next;
}
//输出链表所有数
t=head;
while(t!=NULL) {
printf("%d",t->next);
t=t->next;
}
return 0;
}