代码如下:
头插法:
#include <stdio.h>
#include <stdlib.h>
typedef struct LNode{
int val;
struct LNode *next;
}LNode,*List;
//头插法
List headInsert(List &head){
LNode *node;
head=(LNode*)malloc(sizeof(LNode));
head->next=NULL;
int x;
scanf("%d",&x);
while(x!=0){
node=(LNode*)malloc(sizeof(LNode));
node->val=x;
node->next=head->next;
head->next=node;
scanf("%d",&x);
}
return head->next;
}
int main(){
List head;
head=headInsert(head);
while(head!=NULL){
printf("%d",head->val);
if(head->next!=NULL){
printf("->");
}
head=head->next;
}
return 0;
}
代码如下:
尾插法:
#include <stdio.h>
#include <stdlib.h>
typedef struct LNode{
int val;
struct LNode *next;
}LNode,*List;
//尾插法
List tailInsert(List &head){