- 创建顺序表
- 打印顺序表
- 顺序表按位置插入
- 顺序表插入
- 修改指定节点
- 删除指定节点
#define _CRT_SECURE_NO_WARNINGS #include#define Maxsize 100 #define Max 50 typedef int Elemtype; typedef struct { Elemtype data[Maxsize]; int length; }Sqlist; void Crealist(Sqlist* L, int n, int a[]);//创建顺序表 void Print(Sqlist L);//打印顺序表 void Insert_num(Sqlist* L);//顺序表按位置插入 void Insert(Sqlist* L);//尾部插入 void Deleta_node(Sqlist* L);//指定位置删除节点 void modify_elem(Sqlist* L);//修改 int main() { int a[Max]; int n; Sqlist list; list.length = 0; printf("请输入表长:"); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } Crealist(&list, n, a); //Insert_num(&list); //Insert(&list); //Deleta_node(&list); modify_elem(&list); Print(list); return 0; } void Insert(Sqlist* L) { if (L->length == Max) return; Elemtype n; printf("请输入插入值:"); scanf("%d", &n); L->data[L->length] = n; L->length++; } void Insert_num(Sqlist* L) { if (L->length == Max) return; int i,num; Elemtype n; printf("请输入插入的位置和插入的值:x yn"); scanf("%d %d", &num,&n); for (i =L->length; i >=num; i--) { L->data[i] = L->data[i - 1]; } L->data[i] = n; L->length++; } void Deleta_node(Sqlist* L) { if (L->length<0 || L->length>Max) return; int i; printf("请输入需要删除的节点:"); scanf("%d", &i); for (i=i; i length; i++) { L->data[i-1] = L->data[i]; } L->length--; } void modify_elem(Sqlist* L) { if (L->length < 0) return; Elemtype elem; int node; printf("请输入需要修改的节点:"); scanf("%d", &node); if (node<0 || node>Max) { printf("修改节点不存在!"); return; } printf("n修改为:"); scanf("%d", &elem); L->data[node - 1] = elem; } void Crealist(Sqlist* L, int n, int a[]) { if (n<0 || n>Max) return; for (int i = 0; i < n; i++) { L->data[i] = a[i]; } L->length = n; printf("创建成功:"); Print(*L); } void Print(Sqlist L) { if (L.length == 0) return; int i; for (i = 0; i < L.length; i++) { printf("%d,", L.data[i]); } printf("n"); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)