鱼C论坛

 找回密码
 立即注册
查看: 2794|回复: 6

链表查询问题,懂得帮我看下。

[复制链接]
发表于 2014-9-14 14:15:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 acreil 于 2014-9-16 20:44 编辑

写的一个链表,比较复杂。
有个检查序列是不是重复的函数,当链表有两条以上的记录的时候,在检查到重复,退出的时候,程序会崩溃。。。
附上代码图。
QQ图片20140914140606.png


QQ图片20140914141226.png


这个是查询函数

QQ图片20140914141240.png

调用代码。

QQ图片20140914141244.png
把网盘地址附上。。只有一个main.cpp文件,,应用台程序。。。麻烦大家看下。
http://pan.baidu.com/s/1bnvTgf1

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-9-14 15:15:26 | 显示全部楼层
代码多的话就把工程整个发过来,网盘或者附件什么的都可以。你发的这些代码看不出有什么逻辑错误。不调试很难找出错误。尤其是你那个退出代码,只给一部分,很难理解你是打算做什么。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-9-14 21:44:59 | 显示全部楼层
重新把文件附上了,大家帮忙看下。。。有不足的地方也请指出。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-9-15 09:37:37 | 显示全部楼层
本帖最后由 musilintan 于 2014-9-15 09:40 编辑
  1. book* insert(book* head)
  2. {
  3.         string num;
  4.     string price;
  5.     book *temp = head;
  6.         if (head != NULL)
  7.         {
  8.                 while (temp->next)
  9.                 {
  10.                         temp = temp->next;
  11.                 }
  12.         }
  13.     book *temp_next = NULL;
  14.     do
  15.     {
  16.         cout << "请输入书籍的编号:";
  17.         cin >> num;
  18.         while (check_int(num))
  19.         {
  20.             
  21.             cout << "你输入的格式有错误,请重新输入."<<endl;
  22.             cout << "请输入书籍的编号:";
  23.             cin >> num;
  24.         }
  25.                 while (find_num(head, num))
  26.                 {

  27.                         cout << "已存在编号,请重新输入." << endl;
  28.                         cout << "请输入书籍的编号:";
  29.                         cin >> num;
  30.                 }
  31.         if (num == "0")
  32.         {
  33.                         if (temp)
  34.                         {
  35.                                 return head;
  36.                         }
  37.                         else if (temp->next==NULL)
  38.                         {
  39.                                 return head;
  40.                         }
  41.                         else
  42.                         {
  43.                                 temp_next->next = NULL;
  44.                                 return head;
  45.                         }
  46.         }
  47.         cout << "请输入书籍的价格:";
  48.         cin >> price;
  49.         while (check_float(price))
  50.         {
  51.             cout << "你输入的格式有错误,请重新输入."<<endl;
  52.             cout << "请输入书籍的价格:";
  53.             cin >> price;
  54.         }
  55.         if (num == "0")
  56.         {
  57.                         if (temp == NULL)
  58.                         {
  59.                                 return head;
  60.                         }
  61.                         else if (temp_next == NULL)
  62.                         {
  63.                                 return head;
  64.                         }
  65.                         else
  66.                         {
  67.                                 temp_next->next = NULL;
  68.                                 return head;
  69.                         }
  70.                 }
  71.         temp_next = new book;
  72.         temp_next->num = atoi(num.c_str());
  73.         temp_next->price = (float)atof(price.c_str());
  74.                 temp_next->next = NULL;                        //这里
  75.                 if (head == NULL)
  76.                 {
  77.                         temp = temp_next;
  78.                         head = temp;
  79.                 }
  80.                 else if (temp->next == NULL)
  81.                 {
  82.                         temp->next = temp_next;
  83.                 }
  84.                 else
  85.                 {
  86.                         temp = temp->next;
  87.                         temp->next = temp_next;
  88.                 }
  89.     } while (true);
  90. }
复制代码


你的思路没问题,引起错误的原因是再查询函数里面。而最终的病因是因为创建的新节点的next指针没有制NULL导致的。所以在查询的时候会出现无限查询,直到差出链表的范围,最终报错。
程序本身的整体思路很好,只是链表方面的思路比较乱,多看看别人的代码,提高空间很大。
创建节点时需要注意2点:
1.当前节点和上一个节点的联系有没有建立。
2.当前节点的next指针有没有为NULL。
上面的注意事项针对的是链表尾插法。
不过,要是养成下面这个习惯,就会在以后编程中,为你减少很多不必要麻烦。
这是你的代码:
  1. class book
  2. {
  3. public:
  4.     int num;
  5.     float price;
  6.     book *next;
  7. };
复制代码

改进后:
  1. class book
  2. {
  3. public:
  4.     int num;
  5.     float price;
  6.     book *next;
  7.         
  8. public:
  9.         book()
  10.         {
  11.                 num = -1;
  12.                 price = 0.0;
  13.                 next = NULL;
  14.         }
  15. };
复制代码

加一个初始化进去,这样做以后就不用考虑next指针是否为NULL了。在创建链表的时候,会自动将next制空。结构体同样可以这样做。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-9-15 10:36:18 | 显示全部楼层
        if (num == "0")
                {
                        if (temp)
                        {
                                return head;
                        }
                        else if (temp->next == NULL)
                        {
                                return head;
                        }
                        else
.....
这段代码你想做什么?输入0则返回head么??


我看了一下你的程序,修改了一些,,如下:

  1. //  main.cpp
  2. //  ConsoleApplication
  3. //
  4. //  Created by 廖庆飞 on 14/9/9.
  5. //  Copyright (c) 2014年 廖庆飞. All rights reserved.
  6. //

  7. #include<iostream>
  8. #include<string>

  9. using namespace std;

  10. class book
  11. {
  12. public:
  13.     int num;
  14.     float price;
  15.     book *next;
  16. };

  17. book* creat();
  18. book* delete_node(book*);
  19. int delete_all(book*);
  20. void find(book*);
  21. bool find_num(book*,string);
  22. book* insert(book*);
  23. book* sort(book*);
  24. bool check_int(string);
  25. bool check_float(string);
  26. int showbook(book*);

  27. int main(int argc, const char * argv[])
  28. {
  29.         string str;
  30.         int i;
  31.         book *head = NULL;
  32.         while (true)
  33.         {
  34.                 cout << "1.重建图书2.添加图书3.删除图书4.图书排序5.图书查找6.显示图书7.屏幕清除0.程序退出" << endl;
  35.                 cin >> str;
  36.                 while (check_int(str))
  37.                 {

  38.                         cout << "你输入的格式有错误,请重新输入." << endl;;
  39.                         cout << "请输入选项编号:";
  40.                         cin >> str;
  41.                 }
  42.                
  43.                 if (str == "0")
  44.                 {
  45.                         delete_all(head);
  46.                         return 0;
  47.                 }
  48.                
  49.                 i = atoi(str.c_str());
  50.                
  51.                 switch (i)
  52.                 {
  53.                 case 1:
  54.                         if (head == NULL)
  55.                         {
  56.                                 head = creat();
  57.                         }
  58.                         else
  59.                         {
  60.                                 delete_all(head);
  61.                                 head = creat();
  62.                         }
  63.                         break;
  64.                 case 2:
  65.                         head = insert(head);
  66.                         break;
  67.                 case 3:
  68.                         head = delete_node(head);
  69.                         break;
  70.                 case 4:
  71.                         head = sort(head);
  72.                         break;
  73.                 case 5:
  74.                         find(head);
  75.                         break;
  76.                 case 6:
  77.                         showbook(head);
  78.                         break;
  79.                 case 7:
  80.                         system("cls");
  81.                         break;
  82.                 default:
  83.                         cout << "对不起,没有这个选项." << endl;
  84.                         break;
  85.                 }
  86.         }
  87.        
  88.     return 0;
  89. }
  90. //--------------------create---------------------------------
  91. book *creat()
  92. {
  93.     string num;
  94.     string price;
  95.     book *head = NULL;
  96.         book *temp = NULL;
  97.     book *tail = head;
  98.     while(tail!=NULL && tail->next !=NULL) //找到链表的尾
  99.         {
  100.             tail = tail->next;
  101.     }
  102.    
  103.     do
  104.     {
  105.         cout << "请输入书籍的编号:";
  106.         cin >> num;
  107.         while (check_int(num))
  108.         {
  109.             
  110.                         cout << "你输入的格式有错误,请重新输入." << endl;;
  111.             cout << "请输入书籍的编号:";
  112.             cin >> num;                          //输入格式错误后重新输入
  113.         }
  114.         
  115.                 while (find_num(head,num))
  116.                 {

  117.                         cout << "已存在编号,请重新输入." << endl;
  118.                         cout << "请输入书籍的编号:";
  119.                         cin >> num;
  120.                 }
  121.                
  122.                 if (num == "0")
  123.                 {
  124.                         if(head)
  125.                         {
  126.                                 return head;
  127.                         }
  128.                         cout<<"没有创建新图书"<<endl;
  129.                         return NULL;               
  130.                 }
  131.         cout << "请输入书籍的价格:";
  132.         cin >> price;
  133.         while (check_float(price))
  134.         {
  135.                         cout << "你输入的格式有错误,请重新输入." << endl;
  136.             cout << "请输入书籍的价格:";
  137.             cin >> price;
  138.         }
  139.                 if (price == "0")
  140.                 {
  141.                         if(head)
  142.                         {
  143.                                 return head;
  144.                         }
  145.                         cout<<"没有创建新图书"<<endl;
  146.                         return NULL;               
  147.                 }
  148.         temp=new book;
  149.         temp->num = atoi(num.c_str());
  150.         temp->price = (float)atof(price.c_str());
  151.         temp->next = NULL;
  152.         if (head == NULL)
  153.         {
  154.                 head=temp;
  155.                 tail=head;
  156.         }
  157.                        
  158.         else
  159.         {
  160.             tail->next=temp;
  161.             tail=temp;
  162.         }
  163.     } while (true);
  164. }

  165. //---------------------------------------------check_int

  166. //检测输入的字符是否为数字字符
  167. //若是返回false,否则返回true
  168. bool check_int(string str)
  169. {
  170.     for (unsigned i = 0; i < str.length();i++)
  171.     {
  172.                 if (str[i] > '9' || str[i] < '0')
  173.         {
  174.             return true;
  175.         }
  176.     }
  177.     return false;
  178. }

  179. ///--------------------------------------------check_float
  180. bool check_float(string str)
  181. {
  182.     int ax = 0;//用来控制小数点的个数
  183.     for (unsigned i = 0; i < str.length(); i++)
  184.     {
  185.         if (str[i] == '.')
  186.         {
  187.             ax += 1;
  188.         }
  189.         if (ax > 1)
  190.         {
  191.             return true;
  192.         }
  193.         if (str[i] > '9' || str[i] < '.' || str[i] == '/')//由于0到.之间的ASSCII只有一个'/'所把'/'字符排除
  194.         {
  195.             return true;
  196.         }
  197.     }
  198.     return false;
  199. }

  200. //-----------------------------------------------showbook
  201. int showbook(book *head)
  202. {
  203.     if (head == NULL)
  204.     {
  205.         cout << "没有图书信息." << endl;
  206.         return 0;
  207.     }
  208.     while (head)
  209.     {
  210.                 cout << "编号:\t" << head->num << "\t" << "价格:\t" << head->price << endl;
  211.         head = head->next;
  212.     }
  213.     return 0;
  214. }
  215. //-------------------------------------------delete_node
  216. book *delete_node(book* head)
  217. {
  218.     if (head == NULL)
  219.     {
  220.         cout << "没有数据." << endl;
  221.         return head;
  222.     }
  223.     string str;
  224.     cout << "请输入查找的编号:";
  225.     cin >> str;
  226.     while (check_int(str))
  227.     {
  228.         
  229.                 cout << "你输入的格式有错误,请重新输入." << endl;
  230.         cout << "请输入查找的编号:";
  231.         cin >> str;
  232.     }
  233.     if (str == "0")
  234.     {
  235.         return head;
  236.     }
  237.     int num = atoi(str.c_str());
  238.     book* temp = head;
  239.     book*temp_two = NULL;
  240.     if (head->num == num)
  241.     {
  242.         temp = head->next;
  243.         delete head;
  244.         head = temp;
  245.         cout << "删除完成";
  246.         return head;
  247.     }
  248.     else
  249.     {
  250.         while (head)
  251.         {
  252.             temp_two = temp->next;
  253.             if (temp_two->num == num)
  254.             {
  255.                 if (temp_two->next == NULL)
  256.                 {
  257.                     delete temp_two;
  258.                     temp->next = NULL;
  259.                     return head;
  260.                 }
  261.                 temp->next = temp_two->next;
  262.                 delete temp_two;
  263.                 cout << "删除完成." << endl;
  264.                 return head;
  265.             }
  266.             else
  267.             {
  268.                 temp = temp_two->next;
  269.             }
  270.         }
  271.     }
  272.     cout << "没有查找到输入的相关数据." << endl;
  273.     return head;
  274. }
  275. //======---------------------------------------delete_all 0
  276. int delete_all(book* head)
  277. {
  278.     if (head == NULL)
  279.     {
  280.         cout << "没有数据." << endl;
  281.         return 0;
  282.     }
  283.     else
  284.     {
  285.         book *temp = NULL;
  286.         while (head)
  287.         {
  288.             temp = head->next;
  289.             delete head;
  290.             head = temp;
  291.         }
  292.     }
  293.     if(head == NULL)
  294.             cout << "删除成功." << endl;
  295.            else
  296.                    cout<<"删除失败"<<endl;
  297.     return 0;
  298. }
  299. ///------------------------------------------------find
  300. void find(book* head)
  301. {
  302.     if (head == NULL)
  303.     {
  304.         cout << "没有数据." << endl;
  305.         return;
  306.     }
  307.     string str;
  308.     cout << "请输入查找的编号:";
  309.     cin >> str;
  310.     while (check_int(str))
  311.     {
  312.         
  313.                 cout << "你输入的格式有错误,请重新输入." << endl;
  314.         cout << "请输入查找的编号:";
  315.         cin >> str;
  316.     }
  317.     if (str == "0")
  318.     {
  319.         return;
  320.     }
  321.     int num = atoi(str.c_str());
  322.     while (head)
  323.     {
  324.         if (head->num == num)
  325.         {
  326.             cout << "图书编号:" << head->num << endl;
  327.             cout << "图书价格:" << head->price << endl;
  328.             return;
  329.         }
  330.         else
  331.         {
  332.             head = head->next;
  333.         }
  334.     }
  335.     cout << "没有查找到相关数据." << endl;
  336.     return;
  337. }

  338. //=--------------------------------------------find_num
  339. //寻找链表中是否存在输入序列
  340. //若是返回true,否则false
  341. bool find_num(book* head,string str)
  342. {
  343.         if (head == NULL)
  344.         {
  345.                 return false;
  346.         }
  347.        
  348.         int num=atoi(str.c_str());
  349.         book *temp = head;
  350.         while (temp)
  351.         {
  352.                 if (temp->num == num)
  353.                 {
  354.                         return true;
  355.                 }
  356.                 temp = temp->next;
  357.         }
  358.         return false;
  359. }
  360. //----------------------------------------insert  这里和create内部实现基本一样,是否考虑封装一下
  361. book* insert(book* head)
  362. {
  363.         string num;
  364.     string price;
  365.     book *temp=NULL;
  366.     book *tail = head;
  367.         if (head != NULL)
  368.         {
  369.                 while (tail->next)
  370.                 {
  371.                         tail = tail->next;  //寻找链表尾
  372.                 }
  373.         }
  374.        
  375.    do
  376.     {
  377.         cout << "请输入书籍的编号:";
  378.         cin >> num;
  379.         while (check_int(num))
  380.         {
  381.             
  382.                         cout << "你输入的格式有错误,请重新输入." << endl;;
  383.             cout << "请输入书籍的编号:";
  384.             cin >> num;                          //输入格式错误后重新输入
  385.         }
  386.         
  387.                 while (find_num(head,num))
  388.                 {

  389.                         cout << "已存在编号,请重新输入." << endl;
  390.                         cout << "请输入书籍的编号:";
  391.                         cin >> num;
  392.                 }
  393.                
  394.                 if (num == "0")
  395.                 {
  396.                         if(head)
  397.                         {
  398.                                 return head;
  399.                         }
  400.                         cout<<"没有创建新图书"<<endl;
  401.                         return NULL;               
  402.                 }
  403.         cout << "请输入书籍的价格:";
  404.         cin >> price;
  405.         while (check_float(price))
  406.         {
  407.                         cout << "你输入的格式有错误,请重新输入." << endl;
  408.             cout << "请输入书籍的价格:";
  409.             cin >> price;
  410.         }
  411.                 if (price == "0")
  412.                 {
  413.                         if(head)
  414.                         {
  415.                                 return head;
  416.                         }
  417.                         cout<<"没有创建新图书"<<endl;
  418.                         return NULL;               
  419.                 }
  420.         temp=new book;
  421.         temp->num = atoi(num.c_str());
  422.         temp->price = (float)atof(price.c_str());
  423.         temp->next = NULL;
  424.         if (head == NULL)
  425.         {
  426.                 head=temp;
  427.                 tail=head;
  428.         }
  429.                        
  430.         else
  431.         {
  432.             tail->next=temp;
  433.             tail=temp;
  434.         }
  435.     } while (true);
  436. }

  437. ///----------------------------------------------------------sort  4
  438. book* sort(book* head)
  439. {
  440.     if (head == NULL)
  441.     {
  442.                 cout << "没有数据" << endl;
  443.         return NULL;
  444.     }
  445.         /*
  446.     book *p=head, *p1=NULL, *p2=NULL, *p3=NULL,*temp;
  447.         p1 = p->next;
  448.         p2 = p1->next;
  449.         if (p > p1)   //这个地址有什么可排列的。。。
  450.         {
  451.                 temp = p;
  452.                 p = p1;
  453.                 p = temp;
  454.                 head = p;
  455.                 if (p2 == NULL)
  456.                 {
  457.                         return head;
  458.                 }
  459.                 p = p->next;
  460.                 p1 = p1->next;
  461.                 p2 = p2->next;
  462.         }
  463.         do
  464.         {
  465.                 if (p1 > p2)
  466.                 {
  467.                         p3 = p2->next;
  468.                         temp = p1;
  469.                         p1 = p2;
  470.                         p2 = temp;
  471.                         p->next = p1;
  472.                         p1->next = p2;
  473.                         p2->next = p3;
  474.                 }
  475.                 p = p->next;
  476.                 p1 = p1->next;
  477.                 p2 = p2->next;
  478.         } while (p3);
  479.         */
  480.         //实现一个按序号排列  冒泡
  481.        
  482.         book * tempf = head; //链表前面节点
  483.         book * tempb = head; //链表 后面节点
  484.         int temp;
  485.         while(tempb=tempf){
  486.                 while(tempb){
  487.                         if(tempf->num > tempb->num)
  488.                         {
  489.                                 temp =  tempb->num;
  490.                                 tempb->num = tempf->num;
  491.                                 tempf->num = temp;
  492.                         }
  493.                         tempb=tempb->next;    //指向下一个节点
  494.                 }
  495.                 tempf=tempf->next;
  496.         }       
  497.         cout<<"结束排列!"<<endl;
  498.     return head;
  499. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-9-16 20:28:39 | 显示全部楼层
musilintan 发表于 2014-9-15 09:37
你的思路没问题,引起错误的原因是再查询函数里面。而最终的病因是因为创建的新节点的next指针没有制NU ...

嗯,看明白了,是在插入方法里面忘记对next赋NULL,导致在下一次的输入,在检查重复,调用了未知指针。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-9-16 20:42:55 | 显示全部楼层
elvo 发表于 2014-9-15 10:36
if (num == "0")
                {
                        if (temp)

刚开始写链表,有些方法确实用得比较笨。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-14 16:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表