资讯详情

C语言实现顺序表

#include <stdio.h> #include <stdlib.h>  #define N 32  typedef int datatype_t;  typedef struct {  datatype_t data[N];  int last; } seqlist_t;  // 判断顺序表是否满 int seqlist_full(seqlist_t *l) {  return l->last == N - 1 ? 1 : 0; }  // 创建线性表 seqlist_t* seqlist_create() {  seqlist_t *s1 = (seqlist_t *)malloc(sizeof(seqlist_t));  s1->last = -1;  return s1; }  // 插入不指定位置的数据节点 int seqlist_insert(seqlist_t *l, int value) {  if (seqlist_full(l))  {   printf("seqlist is full\n");   return -1;  }   l->last  ;  l->data[l->last] = value;  return 0; }  // 将元素插入指定位置 int seqlist_pos_insert(seqlist_t *l, int pos, datatype_t value) {  int i = 0;  if (seqlist_full(l))  {   printf("seqlist is full\n");   return -1;  }   // 判断要插入的位置是否合理  if (pos < 0 || pos > l->last)  {   printf("pos value invalied input\n");   return -1;  }   // 通过for循环 将表中大于pos位置的元素向后移动  for (int i = l->last   1; i > pos; --i)  {   l->data[i] = l->data[i - 1];  }   // 将要插入的值放在指定位置  l->data[pos] = value;  l->last  ;  return 0; }  // 打印线性表的内容 int seqlist_show(seqlist_t *l) {  for (int i = 0; i <= l->last;   i)  {   printf("%d ", l->data[i]);  }  printf("\n");  return 0; }  int seqlist_empty(seqlist_t *l) {  return l->last == -1 ? 1 : 0; }  // 删除最后一个数据节点 int seqlist_delete(seqlist_t *l) {  if (seqlist_empty(l))  {   printf("seqlist empty\n");   return -1;  }   datatype_t value;  value = l->data[l->last];  l->last--;  return value; }  // 删除指定位置的数据节点 int seqlist_pos_delete(seqlist_t *l, int pos) {  // 判断删除是否为空  if (seqlist_empty(l))  {   printf("seqlist empty\n");   return -1;  }   // 判断删除位置是否合理  if (pos > l->last || pos <= -1)  {   printf("pos value invalied input\n");   return -1;  }   datatype_t value = l->data[pos];  for (int i = pos; i < l->last; i  )  {   l->data[i] = l->data[i 1];  }    l->last--;  return value; }  // 修改数据节点的值 int seqlist_change(seqlist_t *l, datatype_t old_value, datatype_t new_value) {  // 判断删除是否为空  if (seqlist_empty(l))  {   printf("seqlist empty\n");   return -1;  }   for (int i = 0; i <= l->last;   i)  {   if (l->data[i] == old_value)   {    l->data[i] = new_value;    return 0;   }  }   printf("未找到数据\n");  return -1; }  // 找到数据节点的位置 int seqlist_search(seqlist_t *l, datatype_t value) {  // 判断删除是否为空  if (seqlist_empty(l))  {   printf("seqlist empty\n");   return -1;  }   for (int i = 0; i <= l->last;   i)  {   if (l->data[i] == value)   {    return i;   }  }  return -1; }  // 删除线性表中重复的数据节点 int seqlist_purge(seqlist_t *l) {  for (int i = 0; i < l->last; i  )  {   for (int j = i   1; j <= l->last; j  )   {    if (l->data[i] == l->data[j]) // 发现重复元素    {     seqlist_pos_delete(l, j);// 删除元素会导致所有元素向前移动 所以要减一     j--;    }   }  }  return 0; }  // 合并顺序表 int seqlist_union(seqlist_t *l1, seqlist_t *l2) {  for (int i = 0; i <= l2->last; i  )  {   if (seqlist_search(l1, l2->data[i]) == -1)   {    seqlist_insert(l1, l2->data[i]);   }  }    return 0; }  // int main() { //  seqlist_t *s1; //  s1 = seqlist_create(); //  printf("%d\n", seqlist_full(s1)); //  seqlist_insert(s1, 10); //  seqlist_insert(s1, 20); //  // seqlist_insert(s1, 20); //  seqlist_insert(s1, 30); //  seqlist_insert(s1, 40); //  seqlist_show(s1);  //  printf("%d\n", seqlist_full(s1));  //  seqlist_pos_insert(s1,0,5); //  seqlist_pos_insert(s1,2,15); //  seqlist_pos_insert(s1,4,25); //  seqlist_pos_insert(s1,6,35);  //  seqlist_show(s1);  //  seqlist_empty(s1); //  seqlist_delete(s1);  //  seqlist_pos_delete(s1, 0); //  seqlist_show(s1); //  return 0; // }  int main() {  seqlist_t *sl1, *sl2;// 定义结构体指针  sl1 = seqlist_create();// 创建空顺序表  seqlist_insert(sl1, 10);  seqlist_insert(sl1, 20);  seqlist_insert(sl1, 30);  seqlist_insert(sl1, 40);   seqlist_show(sl1);   seqlist_change(sl1, 30, 20);// 修改顺序表中的数据 将30修改为 20   seqlist_show(sl1);   seqlist_purge(sl1);   seqlist_show(sl1);   sl2 = seqlist_create();  seqlist_insert(sl2, 30);  seqlist_insert(sl2, 50);  seqlist_insert(sl2, 70);   seqlist_union(sl1, sl2);  seqlist_show(sl1);  return 0; }

输出结果

标签: sl2继电器线路板底座

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台