顺序表的实现(未完)

来源:互联网 发布:网络语 对话 编辑:程序博客网 时间:2024/06/08 01:37

简单的(使用全局变量):

#include<stdio.h>    typedef struct _Seqlist{      int data[100];      int length;  }Seqlist;    void creat_list();  void show_list();  Seqlist L;    void main()  {      L.length=0;      creat_list();      show_list();  }    void creat_list()  {      int length,i;         printf("please input the length of list\t");      scanf("%d",&length);      for(i=0;i<length;i++)      {          printf("please input the %dth number\t",i+1);          scanf("%d",&L.data[i]);          L.length++;      }  }    void show_list()  {      int a;      printf("the list\n");      for(a=0;a<L.length;a++)      {          printf("%d\t",L.data[a]);      }      printf("\n");  }  
使用指针作为形参:

#include<stdio.h>typedef struct _Seqlist{int data[100];int length;}Seqlist;void creat_list(Seqlist *p);void show_list(Seqlist L);void main(){Seqlist L;L.length=0;creat_list(&L);show_list(L);}void creat_list(Seqlist *p){int length,i;printf("please input the length of list\t");scanf("%d",&length);for(i=0;i<length;i++){printf("please input the %dth number\t",i+1);scanf("%d",&(*p).data[i]);(*p).length++;}}void show_list(Seqlist L){int a;printf("the list\n");for(a=0;a<L.length;a++){printf("%d\t",L.data[a]);}printf("\n");}



0 0
原创粉丝点击