顺序表及相关操作C++实现
1 // ConsoleApplication12.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include"stdlib.h" 6 #include"iostream" 7 using namespace std; 8 9 #define MAX 100 10 11 12 typedef int date; 13 14 typedef struct//定义 15 { 16 date a[MAX]; 17 int size; 18 19 }squ; 20 void init(squ *p); 21 void insert_rear(squ *p, date a); 22 void dis(squ *p); 23 bool is_empty(squ *p); 24 void find(squ *p, int n); 25 void insert(squ *p, int n, date x); 26 void del(squ *p, int n); 27 28 int _tmain(int argc, _TCHAR* argv[]) 29 { 30 squ a, *p; 31 p = &a; 32 33 init(p); 34 35 for (int i = 0; i < 10; i++)//插入一个伪随机数 36 insert_rear(p, rand()); 37 38 dis(p); 39 40 insert(p, 5, 9999); 41 dis(p); 42 43 del(p, 8); 44 dis(p); 45 46 return 0; 47 } 48 49 void init(squ *p)//初始化 50 { 51 p->size = 0; 52 } 53 54 void insert_rear(squ *p, date a)// 在顺序表尾部插入数据 55 { 56 if (p->size == MAX) 57 cout << "It is full" << endl; 58 else 59 { 60 p->a[p->size] = a; 61 p->size++; 62 } 63 } 64 65 void dis(squ *p)//输出顺序表 66 { 67 if (p->size == 0) 68 cout << "It is empty" << endl; 69 else 70 for (int i = 0; i < p->size; i++) 71 cout << p->a[i] << endl; 72 } 73 74 bool is_empty(squ *p) 75 { 76 bool a = false; 77 78 if (p->size == 0) 79 a = false; 80 else 81 a = true; 82 return a; 83 } 84 85 86 void find(squ *p, int n) 87 { 88 if (n <= 0 || n > p->size) 89 cout << "不在范围" << endl; 90 else 91 cout << p->a[n] << endl; 92 } 93 94 95 void insert(squ *p, int n, date x)//制定位置插入数据 96 { 97 if (n<0 || n>MAX || n > p->size) 98 cout << "Wrong position!" << endl; 99 else 100 { 101 for (int i = p->size; i >= n; i--) 102 p->a[i] = p->a[i - 1]; 103 p->a[n - 1] = x; 104 p->size++; 105 } 106 } 107 108 void del(squ *p, int n)//删除指定位置的数据 109 { 110 if (n<0 || n>p->size) 111 cout << "Wrong Position!" << endl; 112 else 113 { 114 for (int i = n; i < p->size; i++) 115 p->a[i - 1] = p->a[i]; 116 p->size--; 117 } 118 }
到了年底各种加班,完全没时间搞自己的事。

浙公网安备 33010602011771号