上机题目1:
题目大概是这样的,要求用顺序表存储一个链表,然后删除重复元素和排序(要求用快排)
输出结果。
1 //============================================================================ 2 // Name : shangji1.cpp 3 // Author : 4 // Version : 5 // Copyright : Your copyright notice 6 // Description : Hello World in C++, Ansi-style 7 //============================================================================ 8 9 #include <iostream> 10 using namespace std; 11 12 template<class T> //qsort 快排,返回递增序列 13 void quicksort(T *p,int s,int e) //array low high 14 { 15 if(s<e) 16 { 17 int i=s,j=e; 18 T tmp=p[e]; 19 while(i<j) 20 { 21 while(p[i]<tmp&&i<j) ++i; 22 if(i<j) p[j--]=p[i]; 23 while(tmp<p[j]&&i<j) --j; 24 if(i<j) p[i++]=p[j]; 25 } 26 p[i]=tmp; 27 quicksort(p,s,i-1); 28 quicksort(p,i+1,e); 29 } 30 } 31 32 #define max_size 100 33 34 class mListArray{ // 35 /** 36 * mListArray(); //构造函数 37 * ~mListArray(); 38 * void insert(int value,int pos); //在pos位置插入value 39 * int getValue(int pos); //返回pos位置的值 40 * void del(int pos); //删除pos位置的节点 41 * int find(int value); //返回value的位置并 42 * void print(); // 输出链表 43 */ 44 private: 45 int *head; 46 int count; 47 public: 48 mListArray(){ 49 head = new int [max_size]; 50 count = 0; 51 memset(head,0,sizeof(int));//clear to 0 52 } 53 ~mListArray(){ 54 delete []head; 55 } 56 void insert(int value,int pos){ 57 if(pos<0) 58 { 59 cout<<"input error!"; 60 exit(1); 61 } 62 if(count>max_size){ 63 cout<<"array overflow!"; 64 exit(1); 65 } 66 if(pos>count){ 67 head[count]=value; 68 count++; 69 cout<<">element "<<value<<" had insert to posion "<<count<<endl; 70 return ; 71 } 72 if(pos==count){ 73 head[pos]=value; 74 count ++; 75 cout<<"=element "<<value<<" had insert to posion "<<count<<endl; 76 return ; 77 } 78 if(pos<count){ //insert and move the elements behind 79 //cout<<count<<" "<<pos<<endl; 80 for(int i = count ; i >= pos ;i--){ 81 //move form the back 82 head[i+1] = head[i]; 83 } 84 head[pos] = value; 85 count++; 86 cout<<"<element "<<value<<" had insert to posion "<<pos+1<<endl; 87 } 88 89 90 91 } 92 int getValue(int pos){ 93 return head[pos-1]; 94 } 95 void del(int pos){ 96 for(int i = pos ; i< count ;i++){ 97 head[i] = head[i+1]; 98 } 99 count--; 100 cout<<"element posion "<<pos+1<<" had deleted"<<endl; 101 } 102 int find(int value){ 103 for(int i = 0;i<count ;i++){ 104 if(getValue(i)==value) 105 return i; 106 } 107 cout<<"element \""<<value<<"\"not find"<<endl; 108 return -1; 109 } 110 void print(){ 111 cout<<"链结:"; 112 for(int i = 0; i < count ;i++){ 113 cout<<head[i]; 114 cout<<"->"; 115 } 116 cout<<endl; 117 } 118 void sort(){ 119 quicksort(head,0,count-1); 120 } 121 //====================================== 122 void dele(){ 123 int i ; 124 for(i=0;i<count;){ 125 int t; 126 t = head[i]; 127 while(t == head[i+1]){ 128 del(i); //就是这里出错了,后面加了个一,就出错 129 } 130 i++; 131 } 132 } 133 //==================================== 134 }; 135 136 137 int main() { 138 mListArray m; 139 m.insert(55,1); 140 m.insert(6,2); 141 m.insert(55,3); 142 m.insert(23,4); 143 m.insert(34,5); 144 m.insert(34,6); 145 m.insert(55,7); 146 m.print(); 147 148 m.sort(); 149 m.print(); 150 151 m.dele(); 152 m.print(); 153 154 155 return 0; 156 }
总结:
在用数组进行删除后,在删除函数中就已经对count——操作了,就不用对count——了,否则会出现当出现有3个以上的相同元素时出错。
上机后给自己的感受也是比较深刻的,跟卿哥哥的代码能力目前是比不了的,对于给定的问题,想出代码并实现。
还有代码要经常写,这样才能有提升。
那个时候刚出来我就想有种写代码的冲动啊,竞争才有压力啊!
结果:
>element 55 had insert to posion 1
>element 6 had insert to posion 2
>element 55 had insert to posion 3
>element 23 had insert to posion 4
>element 34 had insert to posion 5
>element 34 had insert to posion 6
>element 55 had insert to posion 7
链结:55->6->55->23->34->34->55->
链结:6->23->34->34->55->55->55->
element posion 3 had deleted
element posion 4 had deleted
element posion 4 had deleted
链结:6->23->34->55->
转载文章请注明出处: http://www.cnblogs.com/menglei/