标签:清除 clu get style 链表 code ini null math
测试第一个博客
献上基于C语言的数据结构之线性表
//线性表 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <math.h> #define MAXSIZE 100 typedef struct Node { int data[MAXSIZE]; int last; } Node; typedef struct Node* Arr; //初始化链表 int initNode(Arr node){ node = (Arr)malloc(sizeof(Node)); node->last=0; return 1; } //查询数据位置 int Locate(Arr* arr,int value) { Arr node = *arr; if(node==NULL){ printf("当前表未初始化"); return -1; } int i = 0; while (i< node->last&&node->data[i]!=value) { i++; } if(i<=node->last){ return i+1; }else { return -1; } } //添加数据 int InsList(Node* node,int value,int index){ if(node==NULL||node->last>=MAXSIZE){ printf("已满载,新增失败\n"); return -1; } if(index>node->last+1||index<1){ printf("插入位置不合法\n"); return -1; } for (int i = node->last; i > index; i--) { node->data[i] = node->data[i-1]; } node->last++; node->data[index-1]=value; return 1; } //查看链表长度 int ListLength(Node* node) { if(node!=NULL){ return node->last; }else { return -1; } } //根据下标删除数据 int DelList(Node* node,int index){ if(node==NULL){ printf("未初始化!"); return -1; } if(index<1||index>node->last){ printf("删除位置不合法!"); return -1; } int e = node->data[index]; for (int i = index; i < node->last; i++) { node->data[i-1] = node->data[i]; } node->last--; return e; } //根据下标查询数据 int GetData(Node* node,int index){ if(node==NULL){ printf("未初始化!"); return -1; } if(index<1||index>node->last){ printf("删除位置不合法!"); return -1; } return (node->data[index-1]); } //销毁线性表 int DestroyList(Node* node){ free(node); return EXIT_SUCCESS; } //清除线性表 int ClearList(Node* node){ if(node==NULL){ printf("未初始化!"); return -1; } node->last = 0; return EXIT_SUCCESS; } //测试是否为空 int EmptyList(Node* node){ if(node==NULL){ printf("未初始化!"); return -1; }else{ return EXIT_SUCCESS; } } int main(int argc, char const *argv[]) { Arr node; int e = 0; initNode(node); InsList(node,1,1); InsList(node,2,2); InsList(node,3,3); InsList(node,7,4); InsList(node,4,5); InsList(node,5,6); InsList(node,6,7); InsList(node,10,2); int loca = Locate(node,6); printf("loca=%d\n",loca); int length = 0; length = ListLength(node); printf("length=%d\n",length); e = DelList(&node,1); printf("删除的值为:%d\n",e); for (int i = 0; i < node->last; i++) { printf(" %d\n",node->data[i]); } int value = GetData(&node,1); printf("查询到的值为:%d\n",value); DestroyList(node); system("pause"); return 0; }
标签:清除 clu get style 链表 code ini null math
原文地址:https://www.cnblogs.com/nniwx/p/14027046.html