C implementation of linear table

C implementation of linear table

still usable

#include <stdio.h>
#include <stdlib.h>

// create an array of integer

// rename the int to size_t
typedef int ElemType;
typedef int bool;
#define true 1
#define false 0
#define MaxSize 5

// create the struct of the List
typedef struct {
    
    
  ElemType data[MaxSize];
  //   define the length of the array.
  int len;
} List;

void init(List* list) {
    
     list->len = 0; }

// create the function of judge the array's full?
bool isFull(List list) {
    
    
  if (list.len == MaxSize) {
    
    
    return true;
  }
  return false;
}

// create the function of add element
bool add(List* list, ElemType elem) {
    
    
  bool flag = isFull(*list);
  if (flag) {
    
    
    printf("the List's data is full. Please don't input the data into it.");
    return false;
  }
  list->data[list->len++] = elem;
  return true;
}

// create the function of judge the List is empty?
bool isEmpty(List list) {
    
    
  if (list.len == 0) {
    
    
    return true;
  }
  return false;
}

int locate(List list, ElemType elem) {
    
    
  for (int i = 0; i < list.len; i++) {
    
    
    if (list.data[i] == elem) {
    
    
      return i;
    }
  }
  return -1;
}

// create the function of delete element, the variable "elem" is the data what
// to delete.
bool delete (List* list, ElemType elem) {
    
    
  int index = locate(*list, elem);
  // if flag is not -1, so list have the elem.
  if (index != -1) {
    
    
    for (int i = index; i < list->len; i++) {
    
    
      list->data[i] = list->data[i + 1];
    }
    list->len--;
    return true;
  }
  return false;
}

void list_print(List list) {
    
    
  printf("\nthis is the List's data:\n");
  for (int i = 0; i < list.len; i++) {
    
    
    printf("%-4d", list.data[i]);
  }
  printf("\n");
}

void display() {
    
    
  printf("********************************************\n");
  printf("--------- 1. add data\t\t---------\n");
  printf("--------- 2. detele data \t---------\n");
  printf("--------- 3. print data \t---------\n");
  printf("--------- 4. print len \t\t---------\n");
  printf("--------- 0. exit \t\t---------\n");
  printf("*********************************************\n");
}

int main() {
    
    
  List list;
  init(&list);
  int choose = -1;
  int x, y;
  while (choose != 0) {
    
    
    display();
    printf("choose func what you want to do.\n");
    scanf("%d", &choose);
    switch (choose) {
    
    
      case 1:
        scanf("%d", &x);
        add(&list, x);
        break;
      case 2:
        scanf("%d", &y);
        delete (&list, y);
        break;
      case 3:
        list_print(list);
        break;
      case 4:
        printf("list's length is:%d\n", list.len);
        break;
      case 0:
        return 0;
        break;
      default:
        break;
    }
  }

  //   List list;
  //   init(&list);

  //   int x;
  //   printf("input the number what you want to add:(3 * ctrl+z to finish)\n");
  //   while (scanf("%d", &x) != EOF) {
    
    
  //     bool flag = add(&list, x);
  //     if (flag == false) break;
  //   }

  //   int elem;
  //   printf("\ninput the number what you want to delete:(3 * ctrl+z to
  //   finish)\n"); while (scanf("%d", &elem) != EOF) {
    
    
  //     bool d_flag = delete (&list, elem);
  //     if (d_flag == false) {
    
    
  //       printf("the data %d is no exist in the List.\n", elem);
  //     }
  //   }
  //   list_print(list);
  return 0;
}

Guess you like

Origin blog.csdn.net/qq_45022687/article/details/126548151