-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOrderList.java
155 lines (127 loc) · 3.88 KB
/
OrderList.java
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package dataStructure.orderList;
import java.util.Scanner;
/*
* 定义数据结构模型
*/
class DATA{
String key; // 结点的关键字
String name;
int age;
}
/*
* 定义顺序表结构
* Sequential storage structure
*/
class SLType{
static final int MAXLEN=100;
DATA[] ListData = new DATA[MAXLEN+1]; // 保存顺序表的结构数组
int ListLen; // 顺序表已存结点的数量
void SLInit(SLType SL){ // 初始化为空表
SL.ListLen = 0;
}
int SLLength(SLType SL){ // 返回顺序表的元素数量
return(SL.ListLen);
}
int SLInsert(SLType SL, int n, DATA data){ // 插入结点到顺序表中间的位置
if(SL.ListLen>=MAXLEN){ // 顺序表已满
System.out.println("顺序表已满,不能插入结点!\n");
return 0;
}
if(n<1 || n>SL.ListLen-1){ // 校验插入位置的合法性
System.out.println("插入元素序号错误,不能插入元素!\n");
return 0;
}
for(int i=SL.ListLen;i>=n;i--){ // 将插入位置n后续的结点都向后移动一位
SL.ListData[i+1]=SL.ListData[i];
}
SL.ListData[n]=data; // 插入结点
SL.ListLen++; // 顺序表节点数量加1
return 1; //成功插入,返回1
}
int SLAdd(SLType SL, DATA data){ // 增加元素到顺序表尾部
if(SL.ListLen>=MAXLEN){ // 顺序表已满
System.out.println("顺序表已满,不能再添加结点!\n");
return 0;
}
SL.ListData[++SL.ListLen]=data;
return 1;
}
int SLDelete(SLType SL, int n){ // 按位删除顺序表的结点
if(n<1 || n>SL.ListLen+1){ // 删除结点序号不正确
System.out.println("删除结点序号错误,不能删除结点!\n");
return 0;
}
for(int i=n;i<SL.ListLen;i++){ // 将插入位置n后续的结点都向前移动一位
SL.ListData[i]=SL.ListData[i+1];
}
SL.ListLen--;
return 1;
}
DATA SLFindByNum(SLType SL, int n){ // 根据序号返回数据元素
if(n<1 || n>SL.ListLen+1){
System.out.println("结点序号错误,不能返回结点!\n");
return null;
}
return SL.ListData[n];
}
int SLFindByKey(SLType SL, String key){ // 按关键字查询结点
for (int i = 1; i <= SL.ListLen; i++) { // 搜寻整个顺序表,返回和key匹配的结点
if(SL.ListData[i].key.compareTo(key)==0){
return i;
}
}
return 0;
}
int SLALL(SLType SL){ // 显示顺序表中的所有结点
for(int i=1; i<=SL.ListLen; i++){
System.out.printf("(%s,%s,%d)\n", SL.ListData[i].key, SL.ListData[i].name, SL.ListData[i].age);
}
return 0;
}
}
/**
* 测试用例
* @author [email protected]
*
*/
public class OrderList {
public static void main(String[] args) {
int i;
SLType SL = new SLType();
DATA pdata;
String key;
System.out.println("顺序表操作演示!");
SL.SLInit(SL); // 初始化顺序表
System.out.println("初始化顺序表完成!");
Scanner input = new Scanner(System.in);
do{
System.out.println("输入添加的结点(学号 姓名 年龄):");
DATA data = new DATA();
data.key = input.next();
data.name = input.next();
data.age = input.nextInt();
if(data.age!=0){
if(SL.SLAdd(SL, data)==0){
break;
}
}else{
break;
}
}while(true);
System.out.println("\n 顺序表中的结点顺序为:");
SL.SLALL(SL);
System.out.println("\n 要取出结点的序号:");
i = input.nextInt();
pdata = SL.SLFindByNum(SL, i);
if(pdata!=null){
System.out.printf("第%d个结点为:(%s,%s,%d)\n",i,pdata.key,pdata.name,pdata.age);
}
System.out.println("\n要查找结点的关键字:");
key = input.next();
i=SL.SLFindByKey(SL, key);
pdata=SL.SLFindByNum(SL, i);
if(pdata!=null){
System.out.printf("第%d个结点为:(%s,%s,%d)\n",i,pdata.key,pdata.name,pdata.age);
}
}
}