使用二进制搜索树的学生数据库不会在树节点中插入数据

时间:2019-05-25 13:55:24

标签: c database struct binary-search-tree

我有一个项目,我需要使用二进制搜索树创建学生数据库,同时所有数据都从.txt文件导入,并执行一些操作(插入,搜索,删除,编辑数据,显示树等)使用案例系统。我的问题是,在我的代码运行时,似乎无法将学生数据导入树节点,结果,所有其他动作在被选择时均不返回任何内容。只要我的.txt文件具有结构,我就会提供我的代码!预先感谢!

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

struct node
{
    struct node *left;
    char ID[100];
    char NAME[100];
    char LAST_NAME[100];
    float Grade;
    struct node *right;
};

struct node *newNode(char, char, char, float);
struct node *insertNode(struct node node, char id, char name, char last_name, float grade);
void search(struct node *root, char *ID);
void deleteFromBST(char *ID);

int main() 
{

  char ID[100];
  int insertDone = 0;
  int ch;

  while (1) {
    printf("\n1. Insertion\t2. Deletion\n");
    printf("3. Searching\t4. Display In Order\n5. Edit\t        6. Exit\n");
    printf("Enter your choice:");
    scanf("%d", &ch);


    switch (ch) {
    case 1: 
      if (insertDone)
        puts("Inserton was already done");
      else {
        struct node* insertNode(struct node *node,char *id, char *name, char *last_name, float grade)
        {
            if (node == NULL)
            return newNode(*id,*name,*last_name,grade);

            if ( strcmp(id , node->ID) < 0)
            node->left = insertNode(node->left,id,name,last_name,grade);

            else if ( strcmp( id , node->ID) >= 0)
            node->right = insertNode(node->right,id,name,last_name,grade);

            return node;
        }   
        insertDone = 1;
      }
      break;

    case 2: 
      printf("Enter the AM to perform deletion:");
      scanf("%99s", ID);
      // delete Node with "ÉD"
      void deleteFromBST(char *ID)
        {
        int flag =0;
        struct node *root;

        void search(struct node *root, char *ID);
        free(root);

        if (flag == 1)
        {
        printf("Succesfully deleted from database \n");
        }   
        }

      break;

    case 3: 
      printf("Enter the AM to search:");
      scanf("%99s", ID);
      void search(struct node *root, char *ID)
        {
        int flag=0;

        if (!root) 
        {
        printf("Search element unavailable in BST\n");
        return;
        }

        while (root != NULL) {
        if (strcmp(root->ID, ID) == 0) {
            printf("Student ID  : %s\n", root->ID);
            printf("First Name     : %s\n", root->NAME);
            printf("Last Name     : %s\n", root->LAST_NAME);
            printf("grade      : %lg\n", root->Grade);
            flag = 1;
            break;
             }

        else if (strcmp(ID , root->ID) > 0)
        { 
         return search(root->right,ID); 
        }

        else if(strcmp(ID , root->ID) < 0)
        {
        return search(root->left,ID);
        }

          if (!flag)
          printf("Search element unavailable in BST\n");
        }
       }

      break;
    case 4: 
      //display();
      break;
    case 5:
      break;
    case 6: 
      exit(0);
    default: 
      printf("U have entered wrong option!!\n");
      break;
    }
  }
  struct node* newNode(char *id, char *name, char *last_name, float grade)
{

  struct node *newnode = malloc(sizeof(struct node));
  struct node Node;

  FILE *fp;
  fp = fopen ("Foitites-Vathmologio-DS.txt","rb");

  if (fp == NULL) 
  { 
    fprintf(stderr,"Could not open file");  
    return;
  } 

  char line[4096];

  while (fgets(line, sizeof line,fp)) 
  {
    size_t len = strlen(line);
    if (len && (line[len - 1] == '\n')) 
    {
      /* incomplete line */
      if (sscanf(line,"%99s %99s %99s %f",Node.ID, Node.NAME, Node.LAST_NAME, &Node.Grade) != 4) 
      {
      puts("invalid file");
      return;
      }

    strcpy(newnode->ID , id);
    strcpy(newnode->NAME , name);
    strcpy(newnode->LAST_NAME , last_name);
    newnode->Grade = grade;
    newnode->left = newnode->right = NULL;
    return newnode;

    }
  }
fclose(fp);
}
  return 0;
}

AE797989 Spyridon Sioutas 10.00 AA566734 Makris Christos 10.00 AE808090迪米特里奥斯·格奥尔加科普洛斯5.00 AA677789约阿尼斯·乔治·托普洛斯6.00 CE778889 Athanasios Karageorgos 6.00 DE797988乔治斯·卡拉奇奥戈斯7.00 BH379908康斯坦丁斯·哥索普洛斯5.00 FB675390列奥尼达斯·萨克拉里迪斯4.00 GA797989尼古拉斯·卡里迪斯9.00 FA564329 Spyridon Skiadopoulos 3.00 LA112233托马斯·马尼斯6.00 KK333333 Christos Papanikolaou 9.00 KA444444安东尼奥·帕帕佐普洛普洛斯10.00 HE897533 Dimitrios Polyzos 3.00 XA438976康斯坦丁诺斯·丘纳斯2.00 ZA542980 Aikaterini Farmaki 8.00 ZH555555 Athanasios Sioutas 10.00 TA453217 Anastasios Papadimitriou 10.00 LH556677乔治·彼得罗8.00


When you compile, I get 0 errors or warnings but every option you select does nothing!

eg. 
  1. 插入2.删除
  2. 搜索4.按顺序显示
  3. 编辑6.退出 输入您的选择:1

  4. 插入2.删除

  5. 搜索4.按顺序显示
  6. 编辑6.退出 输入您的选择:4

  7. 插入2.删除

  8. 搜索4.按顺序显示
  9. 编辑6.退出 输入您的选择:

0 个答案:

没有答案