主页 > 程序人生 > 线性表

线性表

c++很久没用了。。。好生疏阿。。。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
using namespace std;
 
typedef int ElemType;
const int MaxNum=100;
 
class List {
	public :
	ElemType list[MaxNum+1];
	int size;
	void Clear(){
		this->size=0;
	}
	int GetSize(){
		return this->size;
	}
	bool isEmpty(){
		if(this->size)
			return false;
		else 
			return true;
	}
 
	ElemType GetElem(int pos)
	{
		if(0<=pos && pos<size)
		return this->list[pos];
		else {
			exit(1);
		}
 
	}
 
	bool find(ElemType& it)
	{
		int i;
		for(i=0;i<size;i++)
		{
			if(it==this->list[i])
				return true;
		}
		return false;
	}
 
	bool update(int pos,const ElemType& it){
		int i;
		for(i=0;i<size;i++)
		{
			if(it==this->list[i]){
				this->list[i]=it;
				return true;
			}
		}
		return false;
	}
	bool InsertRear(const ElemType& it){
 
		if(size>=MaxNum)
			return false;
		this->list[size]=it;
		size++;
		return true;
	}
	bool InsertFront(const ElemType& it){
 
		int i;
		if(size>=MaxNum)
			return false;
		for(i=size;i>0;i--){
			this->list[i]=this->list[i-1];
		}
		this->list[0]=it;
		size++;
		return true;
 
	}
	bool Insert(int pos,const ElemType& it){
		int i;
		if(size>=MaxNum)
			return false;
		for(i=size;i>pos;i--){
			this->list[i]=this->list[i-1];
		}
		this->list[pos]=it;
		size++;
		return true;
	}
	ElemType DeletFront(){
		int i;
		if(size==0)
			exit(1);
		ElemType temp=this->list[0];
		for(i=0;i<size-1;i++)
		{
			this->list[i]=this->list[i+1];
		}
		return temp;		
	}
 
	ElemType Delet(int pos){
		int i;
		if(size<=pos)
			exit(1);
		ElemType temp=this->list[pos];
		for(i=pos;i<size-1;i++)
		{
			this->list[i]=this->list[i+1];
		}
		return temp;		
	}
};
 
int main(int argc, char *argv[]) {
 
	List a;
	a.InsertFront(1);
	a.InsertRear(2);
	cout<<a.GetSize()<<endl;
	cout<<a.GetElem(0)<<endl;
	a.Insert(0,3);
	int b=1;
	cout<<a.find(b)<<endl;
	cout<<a.Delet(1)<<endl;
 
    return 0;
}

相关日志

, , , ,

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

引用:0

下面所列的是引用到本博客的链接
线性表 来自 混沌的云
顶部