|
查看: 1248|回复: 6
|
C++ 搞不清楚状况 linked list
[复制链接]
|
|
|
这是我的题目
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那里?
请大家指点迷津
谢谢
|
|
|
|
|
|
|
|
|
|
|
发表于 7-3-2009 12:50 PM
|
显示全部楼层
|
把data type都做成string就可以了吧。。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 8-3-2009 08:43 AM
|
显示全部楼层
这是我的cpp file:
- #include<iostream>
- #include "orderedArrayListType.h"
- using namespace std;
- int main()
- {
- orderedArrayListType<int> list;
- int num,choice;
- char input;
- char string[10];
- cout<<"****************************************"<<endl;
- cout<<"* WELCOME TO INSERTION SORTING PROGRAM *"<<endl;
- cout<<"****************************************"<<endl;
- cout<<endl;
- cout<<"Please select the type of list that you want to create: "<<endl;
- back:
- cout<<"Enter 1 for interger type."<<endl;
- cout<<"ENter 2 for character type."<<endl;
- cout<<"Enter 3 for string type."<<endl;
- cin>>choice;
- if (choice==1)
- {
- cout<<"Please enter numbers ending with -1"<<endl;
- cin>>num;
- while(num!=-1)
- {
- list.insertOrd(num);
- cin>>num;
- }
- }
- else if (choice==2)
- {
- cout<<"Please enter characters ending with "<<endl;
- cin>>input;
- while(num!=-1)
- {
- list.insertOrd(input);
- cin>>input;
- }
- }
- else if (choice==3)
- {
- cout<<"Please enter strings ending with zzz"<<endl;
- cin>>string;
- while(string!="zzz")
- {
- list.insertOrd(string);
- cin>>string;
- }
- }
- else
- {
- cout<<"Wrong input.Please enter again."<<endl;
- goto back;
- }
- cout<<"The list before sorting: "<<endl;
- list.print();
- cout<<endl;
- list.selectionSort();
- cout<<"The list after sorting:"<<endl;
- list.print();
- cout<<endl;
- return 0;
- }
-
复制代码
然后这是我的header file.好多的errors可我不明白它说什么。所以改来改去都改不好。可否帮我看一下?
- template<class elemType>
- void orderedArrayListType<elemType>::insertOrd(const elemType& newitem)
- {
- nodeType<elemType> *current;
- nodeType<elemType> *trailCurrent;
- nodeType<elemType> *newNode;
- bool found;
- newNode=new nodeType<Type>;
- assert(newNode!=NULL);
- newNode->info=newitem;
- newNode->link=NULL;
- if(first==NULL)
- {
- first=newNode;
- count++;
- }
- else
- {current=first;
- found=false;
- while(current!=NULL && !found)
- if(current->info>=newitem)
- found=true;
- else
- {
- trailCurrent=current;
- current=current->link;
- }
- if(current==first)
- {
- newNode->link=first;
- first=newNode;
- count++;
- }
- else
- {
- trailCurrent->link=newNode;
- newNode->link=current;
- count++;
- }
- }
- }
- template<class elemType>
- class orderedArrayListType:public arrayListType <elemType>
- {
- public:
- void insertOrd(const elemType&);
- int binarySearch(const elemType& item);
- void selectionSort();
- orderedArrayListType(int size=100);
- private:
- void swap(int first,int second);
- int minLocation(int first,int last);
- };
- template<class elemType>
- int orderedArrayListType<elemType>::minLocation(int first,int last)
- {
- int loc,minIndex;
- minIndex=first;
- for(loc=first+1;loc<=last;loc++)
- {
- if(list[loc]<list[minIndex])
- minIndex=loc;
- }
-
- return minIndex;
- }
- template<class elemType>
- void orderedArrayListType<elemType>::swap(int first,int second)
- {
- elemType temp;
- temp=list[first];
- list[first]=list[second];
- list[second]=temp;
- }
- template<class elemType>
- void orderedArrayListType<elemType>::selectionSort()
- {
- int loc,minIndex;
- for(loc=0;loc<length-1;loc++)
- {
- minIndex=minLocation(loc,length-1);
- swap(loc,minIndex);
- }
- }
- template<class elemType>
- void orderedArrayListType<elemType>::print()
- {
- for (int i = 0; i < last; i++)
- {
- cout << list[i] << " ";
- }
- cout << endl;
- }
复制代码
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
-
- #include<iostream>
- #include<string>
- using namespace std;
- char string[20];
- void swap(int first,int second)
- {
- char temp;
- temp=string[first];
- string[first]=string[second];
- string[second]=temp;
- }
- int minLocation(int first,int last)
- {
- int loc,minIndex;
- minIndex=first;
- for(loc=first+1;loc<=last;loc++)
- {
- if(string[loc]<string[minIndex])
- {
- minIndex=loc;
- }
- }
-
- return minIndex;
- }
- void print(char string[], int size)
- {
- for (int i = 0; i < size; i++)
- {
- cout << string[i] << " ";
- }
- cout << endl;
- }
- void selectionSort(char string[],int size)
- {
- int loc,minIndex;
- for(loc=0;loc<size-1;loc++)
- {
- minIndex=minLocation(loc,size-1);
- swap(loc,minIndex);
- }
- }
- int main()
- {
- int length;
- cout<<"****************************************"<<endl;
- cout<<"* WELCOME TO INSERTION SORTING PROGRAM *"<<endl;
- cout<<"****************************************"<<endl;
- cout<<endl;
- cout<<"Please enter strings ending with zzz"<<endl;
- do{
- cin>>string;
- }
- while(string!="zzz");
- length=strlen(string);
- cout<<"The list before sorting: "<<endl;
- print(string,length);
- cout<<endl;
- selectionSort(string,length);
- cout<<"The list after sorting:"<<endl;
- print(string,length);
- cout<<endl;
- return 0;
- }
复制代码
十五个同样的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吗? |
|
|
|
|
|
|
|
|
|
|
发表于 11-3-2009 05:24 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|