一道简单的C语言遍历线性表的方法,主要用到结构体和指针
#include<stdio.h> #define OK 1 #define ERROR 0 #define MAXSIZE 20 //我们自己定义的顺序表的抽象数据类型 typedef struct { int data[MAXSIZE]; int length; }seqlist; seqlist iniList(seqlist *L , int num) { L->length = L->length + 1; L->data[L->length-1] = num; return *L; } void print(seqlist *L) { int i; printf("遍历线性表为:"); for(i = 0 ; i < L->length ; i++) { printf("%d ",L->data[i]); } printf("\n"); } int main() { int n; while(scanf("%d",&n) != EOF) { int i , num; seqlist L = {0,0}; for(i = 0 ; i < n ; i++) { scanf("%d",&num); L = iniList(&L,num); } print(&L); } return 0; }