12月21 链表初体验

要考试了,但是,忍不住想写代码,哪怕不复习要挂科。。。本末倒置了

 

本来想写一个树,但是,思路有点乱,就像先写一个链表,考完试再说,对于链表,我都是自己看书的,由于前一张结构体理解不够深,踩了好多坑。

所以,学习还是要一步一步,沉心静气来呐。

 

又看了好些结构体,树的东西,然后,不断试错,调试了一下午,才写出这么一个东西。真好菜,但是我也很崩溃呐,一大半时间程序是崩溃的。

 

我试着用gdb,但是,gdb深入使用我也不会呐,实在看不出什么名堂。只能一点一点重新顺着看代码。捋。在每一步操作前添加输出,打印变量值,打印当前在哪一步,然后,两个小时以后,好吧,我头疼,睡了一会。

我得出了现在的这个程序。刚开始测试的时候,一直报错 error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token|  我找了半天实在找不出问题所在,花了巨量时间,我怀疑是结构体操作错了,所以这一步让我对结构体操作,深深的迷惑了 最后,发现是 漏了 主函数的括号。[捂脸][痛哭]

 

还有一个奇怪的报错,

 

 

也是找了相当长时间。所以,建议你们在做一个有好几个新内容 的东西的时候,把他们分开实验,不然,你会混乱的怀疑每一个操作,最后发现是没有提前声明函数,这是个使用哦个codeblocks惯出来的坏习惯。

codeblocks平时使用未声明函数根部不会报错,并正常运行,最后头疼,逼不得已,问了 小马哥,小马哥问。你声明了么。main前,我说,没有,应该不影响那,然后,打脸了。

 

所以,写程序,一定要规范,所有硬性规定的规范任何时候都要遵守,哪怕他现在看来,十分不起眼并没有任何影响。

最后,程序总算成功编译了,运行时出问题了,输入数据后闪退。又一翻查找。。。。。。。。。。。

最后,我在指针操作前,将指针变量输出,发现,00000008 这个肯定不正常,所以,指针,空指针,请一定将他 赋 为 NULL 。并且,操作前,请检查是否为NULL。

 

。。。。。又过了一小时其他小小问题后,有了下面这个混乱的程序。。。。大家好,我是渣渣C 。和之前的一些测试代码混在一起了,没有整理,稍后要整理的,到时候想起来再发,现在,我要将这个不太健壮的程序修补修补,测试一下,封到我的一个单词排版系统里面。

#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <stdbool.h>
#include <string.h>
#include  <windows.h>
#define  EnterAndCheck    while ( ( (c = getchar() ) == ' ') && c != '\n' )


int sameCounter ;
//单词树结构体
//typedef struct wtree
//{
//    char c;
//    char e[200];
//    bool treeflag[26];
//    struct word *childTree[26];
//} wtree;
//跟节点

typedef struct wchain
{
    char word[100];
    char exp[200];
    struct  wchain *pt;
} wchain;

//struct wtree defdata = {'\0','\0','0',NULL};
struct wchain * creatChild(wchain *ptran);


int searchInsert(wchain* ptran,char str[],char exp[])
{

    if (ptran == NULL)
    {
        printf("error,null pointer!\n");
        return 0;

    } else if (strlen(ptran->word) == 0)
    {

        strcpy(ptran->word,str);
        if (strlen(exp) != 0)
        {
            strcpy(ptran->exp,exp);
        }
        return 1;
    }else if (strcmp(ptran->word,str) == 0) //单词已经存在
    {

        printf("The word already exists!\n");
        sameCounter++;
        strcpy(ptran->exp,exp);  //更新释义
        return 0;
    }else if (strcmp(ptran->word,str) < 0) //a < b ,继续比较下一个
    {

        if (ptran->pt == NULL)
        {
            ptran->pt = creatChild(ptran->pt);
            wchain *temp = ptran->pt;
            strcpy(temp->word,str);

            strcpy(temp->exp,exp);
            return 1;
        } else
        {
            return searchInsert(ptran->pt,str,exp);
        }
    } else if (strcmp(ptran->word,str) > 0)
    {

        wchain *tempnow = ptran->pt;
        ptran->pt = creatChild(ptran->pt);
        *(ptran->pt) = *ptran;
        ptran->pt->pt = tempnow;
        strcpy(ptran->word,str);
        strcpy(ptran->exp,exp);
        return  1;
    }


}



struct wchain * creatChild(wchain *ptran)
{
    wchain *p;
    p = (wchain*) malloc(sizeof(wchain));
    if (p == NULL)
    {
        printf("NO enough memory!\n");
        return -1;
    }
    if (p != NULL)
        printf("old %p\nnew %p",ptran,p->pt);
    p->pt = ptran;
    p->word[0] = '\0';
    p->exp[0] = '\0';
    return p;
}


int main
{
    int a,i=0;
    wchain root;//创建根
    root.word[0] = 'a';
   strcpy(root.exp,"1");
    //creatChild(&root);
    root.pt = NULL;
    char c;
    char str[30];
    char exp[200];
    while (i < 4)
    {
        printf("please input;");
        scanf("%s",str);
        scanf("%s",exp);
        printf("\n%sE\n",str);
        printf("%sE\n",exp);
        searchInsert(&root,str,exp);
        i++;
    }
    chPrint(&root);

    //exchange(str);
    // checkAndInsert(&root,str);
    printf("a = %s,b = %s \n",root.word[2],exp);


}

void chPrint(wchain *pt)
{
    while(1)
    {
        if(strlen(pt->word) == 0)
        {
            printf("null\n");
        } else
        {
            printf("%s %s\n",pt->word,pt->exp);
        }
        pt = pt->pt;
        if (pt == NULL)
            break;
        else
        continue;
    }


}

///单词树,先舍弃,用链表
//
//int checkAndInsert(wtree *pfa,char str[])
//{
//    int i = 0;
//    int len = strlen(str) - 1;
//    if(pfa->childTree[i] != false)
//    {
//        if (str[len] = pfa->c)
//        {
//             str[len] == '\0';
//             return checkAndInsert(pfa->childTree[i],str);
//        } else if (strcmp(str[len],pfa->c) < 0)
//        {
//
//        }
//
//
//    }else if (pfa->childTree[i] == false)
//    {
//        pfa->childTree[i] = creaTree();
//        wtree *px = pfa->childTree[i];
//        px->c = str[len];
//    }
//
//
//
//
//
//}
//
//int creaTree()
//{
//    return 0;
//}
//
//int exchange(char str[])
//{
//    int left = 0;
//    int right = strlen(str) - 1;
//    char temp;
//    while(1)
//    {
//        if ( left <= right)
//        {
//            temp = str[left];
//            str[left++] = str[right] ;
//            str[right--] = temp;
//        }
//        else
//        {
//            break;
//        }
//    }
//
//}
//
//
//typedef struct Books
//{
//   char  title[50];
//   char  author[50];
//   char  subject[100];
//   int   book_id;
//};
//
///* 函数声明 */
//void printBook( struct Books book );
//int main( )
//{
//   struct Books Book1;        /* 声明 Book1,类型为 Books */
//   struct Books Book2;        /* 声明 Book2,类型为 Books */
//
//   /* Book1 详述 */
//   strcpy( Book1.title, "C Programming");
//   strcpy( Book1.author, "Nuha Ali");
//   strcpy( Book1.subject, "C Programming Tutorial");
//   Book1.book_id = 6495407;
//
//   /* Book2 详述 */
//   strcpy( Book2.title, "Telecom Billing");
//   strcpy( Book2.author, "Zara Ali");
//   strcpy( Book2.subject, "Telecom Billing Tutorial");
//   Book2.book_id = 6495700;
//
//   /* 输出 Book1 信息 */
//   printBook( Book1 );
//
//   /* 输出 Book2 信息 */
//   printBook( Book2 );
//
//   return 0;
//}
//void printBook( struct Books book )
//{
//   printf( "Book title : %s\n", book.title);
//   printf( "Book author : %s\n", book.author);
//   printf( "Book subject : %s\n", book.subject);
//   printf( "Book book_id : %d\n", book.book_id);
//}
//
//
//
//
//
//
//
//
//
//
//
//
////char comandOption[4000] = {"\*                    .::::. \n*                  .::::::::. \n*                 :::::::::::  FUCK YOU \n*             ..:::::::::::\' \n*           \':::::::::::\' \n*             .:::::::::: \n*        \'::::::::::::::.. \n*             ..::::::::::::. \n*           ``:::::::::::::::: \n*            ::::\`\`:::::::::'        .:::. \n*           ::::\'   \':::::\'       .::::::::. \n*         .::::\'      ::::     .:::::::\'::::. \n*        .:::\'       :::::  .:::::::::\' \':::::. \n*       .::\'        :::::.:::::::::\'      \':::::. \n*      .::\'         ::::::::::::::\'         ``::::. \n*  ...:::           ::::::::::::\'              ``::.\n* ```` \':.          \':::::::::\'                  ::::..\n*                    \'.:::::\'                    \':'````\.\."};
//
//int main()
//{
//    int i;int c;
////    for (i = 0;i < 4000; i++)
////    {
////        c =getchar();
////        comandOption[i] = c;
////        if (c == 'o')
////        {
////
////            comandOption[i] = '\0';
////            i = 4000;
////        }
////
////    }
//    for (i = 0; i < strlen(comandOption);i++)
//    {
//        if (comandOption[i] == '\n')
//        {
//            printf("\n");
//        } else if (comandOption[i] == '\0')
//        {
//           i = 4000;
//
//        } else
//        {
//            printf("%c",comandOption[i]);
//        }
//    }
//    Sleep(100);
//   // p=fopen("e:\\a.txt","w");
//    scanf("%c",&i);
//    scanf("%c",&i);
//    scanf("%c",&i);
//    return 0;
//}
//

//int getOption(char str[])
//{
//    int c;
//    int i = 0;
//    EnterAndCheck;
//    if ( c != '-' && c != 'f')
//    {
//        clean_Print("Wrong argument\nEnter to continue....\n");
//    } else if ( (c = getchar()) == '=') //route
//    {
//        c = getchar();
//        while(1)
//        {
//            if (c == '\n' || c == EOF || c == ' ')
//            {
//                str[i++] = '\0';
//                return 0;
//            }
//            else if (isalpha(c) || c == ':' || c == '\\' || c == '\/' || c == '\.' || c == '\_' || isdigit(c))
//            {
//                str[i++] = c;
//                c = getchar();
//                continue;
//            }
//            else
//            {
//                str[0] = '\0';
//                clean_Print("Illegal file names\nEnter to continue...\n");
//                return -1;
//            }
//        }
//    } else if ( isdigit(c) )
//    {
//
//            if (isdigit(c))
//            {
//                str[i++] = c;
//                c = getchar();
//                if (isdigit(c))
//                {
//                    str[i++] = c;
//                    if (isdigit(c = getchar()))
//                    {
//                        str[0] = '\0';
//                        clean_Print("Time must be within 1-99.\n");
//                        return -1;
//                    }else
//                    {
//                        str[i] = '\0';
//                        return 0;
//                    }
//                } else if (c == '\n' || c == ' ' )
//                {
//                    str[i] = '\0';
//                    return 0;
//                }else
//                {
//                    str[0] = '\0';
//                    clean_Print("Wrong argument\nEnter to continue....\n");
//                    return -1;
//                }
//
//            }
//    }else
//    {
//        printf("%c",c);
//        clean_Print("Wrong argument\nEnter to continue....\n");
//    }
//}
//
//
//void clean_Print( const char *str ) /* 清空缓存函数*/
//{
//    int ch;
//    printf( "%s", str );
//    while ( (ch = getchar() != '\n') && ch != EOF )
//        ;
//}



//
//路径 中反斜杠 用 双斜杠书写。用 sprintf 构成 文件全路径。
//程序例子如下。
//#include<stdio.h>
//int main( )
//{
//FILE *fp;
//char path1[80]="E:\\Users\\Wang";    //主路径
//char sub_path[40]="P1\\text\\win_ver.txt";   //子路径和文件名
//char f_name[120];    //文件全路径
//sprintf(f_name,"%s\\%s",path1,sub_path);   //构成文件全路径
//printf("I will open %s\n",f_name);
//fp=fopen(f_name,"r");    //打开文件
//if (!fp) {printf("Can not opne %s\n",f_name);exit(0);};
//printf("Good !\n");
//fclose(fp);   //关闭文件
//return 0;
//}

 

 

 

posted @ 2019-12-21 23:21  哒啉  阅读(285)  评论(1编辑  收藏  举报