#include
#include
struct node {
int data;
struct node* next;
};
//头指针初始化
struct node *head=NULL;
//插入结点
void insertList(int data){
struct node Link;
Link=(struct node)malloc(sizeof(struct node));
Link->data=data;
Link->next=head;
head=Link;
}
//打印链表
void printList(){
struct node *std=head;
while(std!=NULL){
printf("%d",std->data);
std=std->next;
}
}
//计算链表的长度
int length(){
int length=0;
struct node *tempList=head;
while(tempList!=NULL){
length++;
tempList=tempList->next;
}
return length;
}
int main(){
insertList(1);
insertList(2);
insertList(3);
insertList(4);
insertList(5);
printf("\n");
printf("输出单链表:\n");
printList();
printf("\n");
printf("输出单链表的长度:\n");
length();
return 0;
}
#include <stdio.h>
#include <malloc.h>
struct node {
int data;
struct node* next;
};
//头指针初始化
struct node *head=NULL;
//插入结点
void insertList(int data){
struct node *Link; //這裏改成這樣 加上*
Link=(struct node*)malloc(sizeof(struct node)); //這句也是一樣 加上*
Link->data=data;
Link->next=head;
head=Link;
}
//打印链表
void printList(){
struct node *std=head;
while(std!=NULL){
printf("%d",std->data);
std=std->next;
}
}
//计算链表的长度
int length(){
int length=0;
struct node *tempList=head;
while(tempList!=NULL){
length++;
tempList=tempList->next;
}
return length;
}
int main(){
insertList(1);
insertList(2);
insertList(3);
insertList(4);
insertList(5);
printf("\n");
printf("输出单链表:\n");
printList();
printf("\n");
printf("输出单链表的长度:\n");
printf("%d\n", length()); //這句加上
return 0;
}
望採納
它是有返回值的 你要把它的返回值处理掉才能这么用