写了3个多小时,还是太慢了、太菜了!
StdAfx.h文件:
1 // stdafx.h : include file for standard system include files, 2 // or project specific include files that are used frequently, but 3 // are changed infrequently 4 // 5 6 #if !defined(AFX_STDAFX_H__D36E9D40_3BCB_4A85_9D48_AC876E7A2942__INCLUDED_) 7 #define AFX_STDAFX_H__D36E9D40_3BCB_4A85_9D48_AC876E7A2942__INCLUDED_ 8 9 #if _MSC_VER > 1000 10 #pragma once 11 #endif // _MSC_VER > 1000 12 13 #include <stdc++.h>//万能头文件,非常好用!我用的是VC6.0,里面没有; 14 //因此手动将codeblocks的 stdc++.h 文件导入到 D:\software\Microsoft Visual Studio\VC98\Include 中 15 16 typedef int elementType; 17 const int maxn = 10000+13; 18 using namespace std; 19 20 // TODO: reference additional headers your program requires here 21 22 //{{AFX_INSERT_LOCATION}} 23 // Microsoft Visual C++ will insert additional declarations immediately before the previous line. 24 25 #endif // !defined(AFX_STDAFX_H__D36E9D40_3BCB_4A85_9D48_AC876E7A2942__INCLUDED_Code
1 // stdafx.h : include file for standard system include files, 2 // or project specific include files that are used frequently, but 3 // are changed infrequently 4 // 5 6 #if !defined(AFX_STDAFX_H__D36E9D40_3BCB_4A85_9D48_AC876E7A2942__INCLUDED_) 7 #define AFX_STDAFX_H__D36E9D40_3BCB_4A85_9D48_AC876E7A2942__INCLUDED_ 8 9 #if _MSC_VER > 1000 10 #pragma once 11 #endif // _MSC_VER > 1000 12 13 #include <stdc++.h>//万能头文件,非常好用!我用的是VC6.0,里面没有; 14 //因此手动将codeblocks的 stdc++.h 文件导入到 D:\software\Microsoft Visual Studio\VC98\Include 中 15 16 typedef int elementType; 17 const int maxn = 10000+13; 18 using namespace std; 19 20 // TODO: reference additional headers your program requires here 21 22 //{{AFX_INSERT_LOCATION}} 23 // Microsoft Visual C++ will insert additional declarations immediately before the previous line. 24 25 #endif // !defined(AFX_STDAFX_H__D36E9D40_3BCB_4A85_9D48_AC876E7A2942__INCLUDED_)
SeqList1.h文件:
// SeqList1.h: interface for the SeqList class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_SEQLIST1_H__5F69CE41_7D8B_4396_BAAE_F849B1FD54D1__INCLUDED_) #define AFX_SEQLIST1_H__5F69CE41_7D8B_4396_BAAE_F849B1FD54D1__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class SeqList { public: SeqList(); virtual ~SeqList(); void printList(); int Length(); int locate( elementType value );//返回第一个值对value的位置,没有则返回-1 bool isEmpty();//判空 bool isFull();//判满 bool getElement( int pos, elementType& value );//获取pos位置的值 bool insertList( int pos, elementType value );//在pos位置前插入value值 bool insertList_1( elementType value );//在尾部插入value值 bool deleteListNode( int pos, elementType& value );//按位置删除元素 bool deleteListNode_1( int value );//按值删除元素 bool deleteListNode_2( int value );//按值删除所有对应元素 private: elementType Arr[maxn];//存放表元素的数组 size_t listSize;//记录当前顺序表的大小 }; #endif // !defined(AFX_SEQLIST1_H__5F69CE41_7D8B_4396_BAAE_F849B1FD54D1__INCLUDED_)
SeqList1.cpp文件:
1 // SeqList1.cpp: implementation of the SeqList class. 2 // 3 ////////////////////////////////////////////////////////////////////// 4 5 #include "stdafx.h" 6 #include "SeqList1.h" 7 8 ////////////////////////////////////////////////////////////////////// 9 // Construction/Destruction 10 ////////////////////////////////////////////////////////////////////// 11 12 SeqList::SeqList() 13 { 14 listSize = 0; 15 } 16 17 SeqList::~SeqList() 18 { 19 cout << this << " 顺序表已销毁!" << endl; 20 } 21 22 void SeqList::printList() 23 { 24 int column = 0; 25 for( int i = 0; i < listSize; i ++ ) 26 { 27 cout<< setiosflags(ios::left) << setw(5) << Arr[i] << " "; 28 if( ++ column % 10 == 0 ) 29 cout << endl; 30 } 31 cout << endl; 32 } 33 34 int SeqList::Length() 35 { 36 return listSize; 37 } 38 39 int SeqList::locate( elementType value ) 40 { 41 for( int i = 0; i < listSize; i ++ ) 42 if( Arr[i] == value ) 43 return i + 1; 44 return -1; 45 } 46 47 bool SeqList::isEmpty() 48 { 49 return listSize == 0; 50 } 51 52 bool SeqList::isFull() 53 { 54 return listSize == maxn; 55 } 56 57 bool SeqList::insertList( int pos, elementType value ) 58 { 59 if( isFull() ) 60 { 61 cout << "顺序表已满!插入失败!" << endl; 62 return false; 63 } 64 if( pos > listSize ) 65 { 66 cout << "插入位置超过当前顺序表容量!插入失败!" << endl; 67 return false; 68 } 69 if( pos <= 0 ) 70 { 71 cout << "插入位置必须大于0!插入失败!" << endl; 72 return false; 73 } 74 for( int i = listSize - 1; i >= pos - 1; i -- ) 75 Arr[ i + 1 ] = Arr[i]; 76 Arr[ pos - 1 ] = value; 77 listSize ++;//一定不能少! 78 return true;//一定不能少! 79 } 80 81 bool SeqList::insertList_1( elementType value ) 82 { 83 if( isFull() ) 84 { 85 cout << "顺序表已满!插入失败!" << endl; 86 return false; 87 } 88 Arr[ listSize ++ ] = value; 89 return true;//一定不能少! 90 } 91 92 bool SeqList::deleteListNode( int pos, elementType& value ) 93 { 94 if( isEmpty() ) 95 { 96 cout << "顺序表为空!删除失败!" << endl; 97 return false; 98 } 99 if( pos > listSize ) 100 { 101 cout << "删除位置大于表长!删除失败!" << endl; 102 return false; 103 } 104 value = Arr[ pos - 1 ]; 105 for( int i = pos; i < listSize - 1; i ++ ) 106 Arr[ i - 1 ] = Arr[i]; 107 listSize --;//一定不能少! 108 return true;//一定不能少! 109 } 110 111 bool SeqList::deleteListNode_1( int value ) 112 { 113 if( isEmpty() ) 114 { 115 cout << "顺序表为空!删除失败!" << endl; 116 return false; 117 } 118 if( locate(value) == -1 ) 119 { 120 cout << "表中无此元素!删除失败!" << endl; 121 return false; 122 } 123 int index = locate(value); 124 for( int i = index - 1; i < listSize; i ++ ) 125 Arr[i] = Arr[ i + 1 ]; 126 listSize --;//一定不能少!否则会出现已失效的位置仍占有先前元素的错误! 127 return true;//一定不能少! 128 /*精简版如下! 129 void delete(int A[],int key,int& n) 130 { 131 int i,j; 132 for(i=0;i<n&&A[i]-key;i++); //查找key值元素 133 if(i>=n) 134 cout<<"not found"<<endl; 135 else 136 { 137 for(j=i;j<n-1;A[j]=A[j+1],j++);//若找到,将该元素后边的值向前覆盖 138 --n;//数组长度减1 139 } 140 } 141 --------------------- 142 作者:castle_kao 143 来源:CSDN 144 原文:https://blog.csdn.net/castle_kao/article/details/53487610?utm_source=copy 145 版权声明:本文为博主原创文章,转载请附上博文链接! 146 */ 147 } 148 149 bool SeqList::deleteListNode_2( int value ) 150 { 151 if( isEmpty() ) 152 { 153 cout << "顺序表为空!删除失败!" << endl; 154 return false; 155 } 156 if( locate(value) == -1 ) 157 { 158 cout << "表中无此元素!删除失败!" << endl; 159 return false; 160 } 161 int cnt = 0; 162 for( int i = 0; i < listSize; i ++ ) 163 if( Arr[i] == value ) 164 cnt ++; 165 while( cnt -- ) 166 { 167 int pos = locate(value), data; 168 deleteListNode( pos, data ); 169 } 170 return true; 171 }
SeqList.cpp(测试函数)文件:
1 // SeqList.cpp : Defines the entry point for the console application. 2 // 3 4 #include "stdafx.h" 5 #include "SeqList1.h" 6 7 int main(int argc, char* argv[]) 8 { 9 ios::sync_with_stdio(false); 10 freopen( "x1.in", "r", stdin ); 11 //freopen( "x1.out", "w", stdout ); 12 //printf( "%d\n", (bool)-1 ); 13 SeqList L1; 14 if( L1.isEmpty() ) 15 { 16 cout << "空表!" << endl; 17 } 18 int n; 19 cin >> n; 20 for( int i = 0; i < n; i ++ ) 21 { 22 int num; 23 cin >> num; 24 L1.insertList_1(num); 25 } 26 cout << "当前表长为:" << L1.Length() << endl; 27 L1.printList(); 28 L1.insertList( 5, -1 ); 29 cout << "当前表长为:" << L1.Length() << endl; 30 L1.printList(); 31 int data; 32 L1.deleteListNode( 4, data ); 33 cout << "值为 " << data << " 的元素已删除!" << endl; 34 35 36 L1.deleteListNode_1(6); 37 cout << L1.Length() << endl; 38 L1.printList(); 39 40 L1.deleteListNode_1(7); 41 cout << "当前表长为:" << L1.Length() << endl; 42 L1.printList(); 43 44 int delKey = 2; 45 L1.deleteListNode_2(delKey); 46 cout << "所有值为" << delKey << "元素删除后," << "当前表长为:" << L1.Length() << endl; 47 L1.printList(); 48 49 SeqList L2; 50 if( L2.isEmpty() ) 51 { 52 cout << "空表!" << endl; 53 } 54 for( int j = 0; j < maxn; j ++ ) 55 { 56 L2.insertList_1( j + 1 ); 57 } 58 if( L2.isFull() ) 59 { 60 cout << "表满!" << endl; 61 } 62 cout << "当前表长为:" << L2.Length() << endl; 63 L2.printList();//改为 L1.printList(); 会有意想不到的效果! 64 65 return 0; 66 }