一 直接寻找到最后进行插入如下列代码fristadd()函数所示
二 标记尾指针,如addlist()函数所示
#include<stdlib.h>
#include<stdio.h>
typedef struct node{
int data;
struct node *next;
}Node,*LNode;
void Initlist(Node* &l){
l = (Node*)malloc(sizeof(Node));
l->next = NULL;
}
void addlist(LNode &r,int data){
Node *p;
Initlist(p);
p->data = data;
p->next = r->next;
r->next = p;
r = p;
r->next = NULL;
}
void fristadd(Node* l,int data){
while(l->next != NULL){
l = l->next;
}
Node *p;
Initlist(p);
p->data = data;
p->next = l->next;
l->next = p;
}
int main(){
Node *list,*r;
Initlist(list);
r = list;
int a;
scanf("%d",&a);
while(a != 1234){
fristadd(list,a);
scanf("%d",&a);
}
while(list->next != NULL){
list = list->next;
printf("%d\n",list->data);
}
}