qscool 2020-06-14
typedef int ElemType;//取别名 typedef struct link{ ElemType * head;//head是一个数组指针,不太清楚的同学,可以百度一下 int length; int size; }sqlink;//取别名
#include<stdio.h> #include<stdlib.h> #define MAX_SIZE 20 typedef int ElemType; typedef struct link{ ElemType * head; int length; int size; }sqlink; sqlink InitLink();//方法声明 int main(){ sqlink s; s = InitLink(); for(int i = 0; i < 10; i++){ s.head[i] = i; } printf("%d",s.head[6]); } sqlink InitLink(){//方法实现 sqlink s; s.head = (ElemType*)malloc(MAX_SIZE*sizeof(ElemType));//在内存中动态创建 if(!s.head)//如果创建不成功,就退出 exit(0); s.length = 0; s.size = MAX_SIZE; return s; }
#include<stdio.h> #include<stdlib.h> #define MAX_SIZE 20 typedef int ElemType; typedef struct link{ ElemType * head; int length; int size; }sqlink; sqlink InitLink();//方法声明 sqlink InsertLink(sqlink s ,int i , ElemType e); int main(){ sqlink s; s = InitLink(); for(int i = 0; i < 10; i++){ s.head[i] = i; } printf("%d\n",s.head[6]); s = InsertLink(s,7,99999); printf("%d\n",s.head[6]); printf("%d\n",s.head[7]); } sqlink InitLink(){//方法实现 sqlink s; s.head = (ElemType*)malloc(MAX_SIZE*sizeof(ElemType)); if(!s.head) exit(0); s.length = 0; s.size = MAX_SIZE; return s; } sqlink InsertLink(sqlink s ,int i , ElemType e){ if(i<1 && i>MAX_SIZE+1) exit(0); if(s.length == MAX_SIZE) exit(0); for(int j = s.length-1;j >= i-1;j--){ s.head[j+1] = s.head[j] ; } s.head[i-1] = e; return s; }