佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1248|回复: 6

C++ 搞不清楚状况 linked list

[复制链接]
发表于 7-3-2009 07:15 AM | 显示全部楼层 |阅读模式
这是我的题目


Complete a program that performs following operations by using C++ programming:

a.
Enable user to create and manipulate a list of items (must be able to read integers, characters and strings).

b.
Enable user to sort the list. (any sorting algorithm except naïve sort)

c.
Enable user to search a particular item in the list. (use any searching algorithm)


可是我一点概念都没有。
我大概知道要用template来解决data type的问题。
可是具体一点我就不清楚了。
是不是要在main()那边一开始问user要enter哪种data type 的list先?
然后才pass去 tempate那里?


请大家指点迷津
谢谢

回复

使用道具 举报


ADVERTISEMENT

发表于 7-3-2009 12:50 PM | 显示全部楼层
把data type都做成string就可以了吧。。
回复

使用道具 举报

 楼主| 发表于 8-3-2009 08:43 AM | 显示全部楼层
这是我的cpp file:

  1. #include<iostream>
  2. #include "orderedArrayListType.h"
  3. using namespace std;
  4. int main()
  5. {
  6. orderedArrayListType<int> list;
  7. int num,choice;
  8. char input;
  9. char string[10];
  10. cout<<"****************************************"<<endl;
  11. cout<<"* WELCOME TO INSERTION SORTING PROGRAM *"<<endl;
  12. cout<<"****************************************"<<endl;
  13. cout<<endl;
  14. cout<<"Please select the type of list that you want to create: "<<endl;
  15. back:
  16. cout<<"Enter 1 for interger type."<<endl;
  17. cout<<"ENter 2 for character type."<<endl;
  18. cout<<"Enter 3 for string type."<<endl;
  19. cin>>choice;
  20. if (choice==1)
  21. {
  22.   cout<<"Please enter numbers ending with -1"<<endl;
  23.   cin>>num;
  24.   while(num!=-1)
  25.   {
  26.    list.insertOrd(num);
  27.    cin>>num;
  28.   }
  29. }
  30. else if (choice==2)
  31. {
  32.   cout<<"Please enter characters ending with "<<endl;
  33.   cin>>input;
  34.   while(num!=-1)
  35.   {
  36.    list.insertOrd(input);
  37.    cin>>input;
  38.   }
  39. }
  40. else if (choice==3)
  41. {
  42.   cout<<"Please enter strings ending with zzz"<<endl;
  43.   cin>>string;
  44.   while(string!="zzz")
  45.   {
  46.    list.insertOrd(string);
  47.    cin>>string;
  48.   }
  49. }
  50. else
  51. {
  52.   cout<<"Wrong input.Please enter again."<<endl;
  53.   goto back;
  54. }
  55. cout<<"The list before sorting: "<<endl;
  56. list.print();
  57. cout<<endl;
  58. list.selectionSort();
  59. cout<<"The list after sorting:"<<endl;
  60. list.print();
  61. cout<<endl;
  62. return 0;
  63. }

复制代码


然后这是我的header file.好多的errors可我不明白它说什么。所以改来改去都改不好。可否帮我看一下?


  1. template<class elemType>
  2. void orderedArrayListType<elemType>::insertOrd(const elemType& newitem)
  3. {
  4. nodeType<elemType> *current;
  5. nodeType<elemType> *trailCurrent;
  6. nodeType<elemType> *newNode;
  7. bool found;
  8. newNode=new nodeType<Type>;
  9. assert(newNode!=NULL);
  10. newNode->info=newitem;
  11. newNode->link=NULL;
  12. if(first==NULL)
  13. {
  14.   first=newNode;
  15.   count++;
  16. }
  17. else
  18. {current=first;
  19. found=false;
  20. while(current!=NULL && !found)
  21.   if(current->info>=newitem)
  22.    found=true;
  23.   else
  24.   {
  25.    trailCurrent=current;
  26.    current=current->link;
  27.   }
  28.   if(current==first)
  29.   {
  30.    newNode->link=first;
  31.    first=newNode;
  32.    count++;
  33.   }
  34.   else
  35.   {
  36.    trailCurrent->link=newNode;
  37.    newNode->link=current;
  38.    count++;
  39.   }
  40. }
  41. }
  42. template<class elemType>
  43. class orderedArrayListType:public arrayListType <elemType>
  44. {
  45. public:
  46.   void insertOrd(const elemType&);
  47.   int binarySearch(const elemType& item);
  48.   void selectionSort();
  49.   orderedArrayListType(int size=100);
  50. private:
  51.   void swap(int first,int second);
  52.   int minLocation(int first,int last);
  53. };
  54. template<class elemType>
  55. int orderedArrayListType<elemType>::minLocation(int first,int last)
  56. {
  57. int loc,minIndex;
  58. minIndex=first;
  59. for(loc=first+1;loc<=last;loc++)
  60. {
  61.   if(list[loc]<list[minIndex])
  62.    minIndex=loc;
  63. }

  64. return minIndex;
  65. }
  66. template<class elemType>
  67. void orderedArrayListType<elemType>::swap(int first,int second)
  68. {
  69. elemType temp;
  70. temp=list[first];
  71. list[first]=list[second];
  72. list[second]=temp;
  73. }
  74. template<class elemType>
  75. void orderedArrayListType<elemType>::selectionSort()
  76. {
  77. int loc,minIndex;
  78. for(loc=0;loc<length-1;loc++)
  79. {
  80.   minIndex=minLocation(loc,length-1);
  81.   swap(loc,minIndex);
  82. }
  83. }
  84. template<class elemType>
  85. void orderedArrayListType<elemType>::print()
  86. {
  87.   for (int i = 0; i < last; i++)
  88.   {
  89.     cout << list[i] << " ";
  90.   }
  91.   cout << endl;
  92. }
复制代码


run出来的errors是这样的:
--------------------Configuration: Insertion Sort - Win32 Debug--------------------
Compiling...
Insertion Sort.cpp
error C2143: syntax error : missing ';' before '<'
error C2182: 'orderedArrayListType' : illegal use of type 'void'
C2059: syntax error : ';'
error C2143: syntax error : missing ';' before '<'
error C2039: 'insertOrd' : is not a member of '`global namespace''
error C2904: 'orderedArrayListType' : template-name already defined as 'int orderedArrayListType'
see declaration of 'orderedArrayListType'
error C2954: template definitions cannot nest
error C2039: 'minLocation' : is not a member of 'orderedArrayListType'
see declaration of 'orderedArrayListType'
error C2039: 'swap' : is not a member of 'orderedArrayListType'
see declaration of 'orderedArrayListType'
error C2039: 'selectionSort' : is not a member of 'orderedArrayListType'
see declaration of 'orderedArrayListType'
error C2039: 'print' : is not a member of 'orderedArrayListType'
see declaration of 'orderedArrayListType'
error C2146: syntax error : missing ';' before identifier 'list'
error C2065: 'list' : undeclared identifier
error C2228: left of '.insertOrd' must have class/struct/union type
error C2228: left of '.insertOrd' must have class/struct/union type
error C2228: left of '.insertOrd' must have class/struct/union type
error C2228: left of '.print' must have class/struct/union type
error C2228: left of '.selectionSort' must have class/struct/union type
error C2228: left of '.print' must have class/struct/union type
Error executing cl.exe.
Insertion Sort.obj - 19 error(s), 0 warning(s)
回复

使用道具 举报

发表于 8-3-2009 09:52 PM | 显示全部楼层
你的template从哪里抄回来?
我就奇怪连linked list都不掌握的人竟然去玩template class
你这题简单罢了,没必要拿越级的冬冬来跟自己过不去。
像上面人家讲的,把东西用string收就可以了,顶多加个boolean分别是不是integer

[ 本帖最后由 yeenfei 于 8-3-2009 09:58 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 9-3-2009 07:41 AM | 显示全部楼层
我改过了这些code
删了template
可是问题出现在我的string vaiable


  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. char string[20];
  5. void swap(int first,int second)
  6. {
  7. char temp;
  8. temp=string[first];
  9. string[first]=string[second];
  10. string[second]=temp;
  11. }
  12. int minLocation(int first,int last)
  13. {
  14. int loc,minIndex;
  15. minIndex=first;
  16. for(loc=first+1;loc<=last;loc++)
  17. {
  18.   if(string[loc]<string[minIndex])
  19.   {
  20.    minIndex=loc;
  21.   }
  22. }

  23. return minIndex;
  24. }
  25. void print(char string[], int size)
  26. {
  27.   for (int i = 0; i < size; i++)
  28.   {
  29.     cout << string[i] << " ";
  30.   }
  31.   cout << endl;
  32. }
  33. void selectionSort(char string[],int size)
  34. {
  35. int loc,minIndex;
  36. for(loc=0;loc<size-1;loc++)
  37. {
  38.   minIndex=minLocation(loc,size-1);
  39.   swap(loc,minIndex);
  40. }
  41. }
  42. int main()
  43. {
  44. int length;
  45. cout<<"****************************************"<<endl;
  46. cout<<"* WELCOME TO INSERTION SORTING PROGRAM *"<<endl;
  47. cout<<"****************************************"<<endl;
  48. cout<<endl;
  49. cout<<"Please enter strings ending with zzz"<<endl;
  50. do{
  51.   cin>>string;
  52. }
  53. while(string!="zzz");
  54. length=strlen(string);
  55. cout<<"The list before sorting: "<<endl;
  56. print(string,length);
  57. cout<<endl;
  58. selectionSort(string,length);
  59. cout<<"The list after sorting:"<<endl;
  60. print(string,length);
  61. cout<<endl;
  62. return 0;
  63. }

复制代码


十五个同样的error C2872: 'string' : ambiguous symbol
回复

使用道具 举报

发表于 9-3-2009 09:41 PM | 显示全部楼层
原帖由 蜡笔小烦 于 9-3-2009 07:41 AM 发表
我改过了这些code
删了template
可是问题出现在我的string vaiable

应该上课专心,你就忙着sms;
应该上网学习,你就跑去dota.....

连errorcode都给你了还不去自己动手了解,真替父母为你的spoonfed感到悲伤
class name/reserved keyword可以用来取为variable吗?
回复

使用道具 举报

Follow Us
发表于 11-3-2009 05:24 PM | 显示全部楼层
原帖由 yeenfei 于 9-3-2009 09:41 PM 发表

应该上课专心,你就忙着sms;
应该上网学习,你就跑去dota.....

连errorcode都给你了还不去自己动手了解,真替父母为你的spoonfed感到悲伤
class name/reserved keyword可以用来取为variable吗?


不用踩到酱用力的
回复

使用道具 举报

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

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 15-12-2025 03:59 AM , Processed in 0.140147 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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