1 import java.util.Scanner;
2 class DATA{ //模拟一个班级的学生记录
3 String key;
4 String name;
5 int age;
6 }
7
8 class SLType{
9
10 static final int MAXLEN = 100;
11 DATA[] ListData = new DATA[MAXLEN+1];
12 int ListLen; //顺序表已存结点的数量
13
14
15 void SLInit(SLType sl){
16 sl.ListLen = 0;
17 }
18
19
20 int SLLength(SLType sl){
21 return (sl.ListLen);
22 }
23
24 //插入节点
25 int SLInsert(SLType SL,int n , DATA data){
26 int i ;
27 if(SL.ListLen>=MAXLEN){
28 System.out.println("顺序表已满,不能插入节点");
29 return 0;
30 }
31
32 if(n<1 || n>SL.ListLen-1){
33 System.out.println("插入序号有误,不能插入节点");
34 return 0;
35 }
36 //将顺序表中的数据向后移动
37 for(i = SL.ListLen; i >=n ; i--){
38 SL.ListData[i+1] = SL.ListData[i];
39 }
40 SL.ListData[n] = data;
41 SL.ListLen++;
42 return 1;
43
44 }
45
46
47 //追加节点
48 int SLAdd(SLType SL,DATA data){
49 if(SL.ListLen>=MAXLEN){
50 System.out.println("顺序表已满,不能插入节点");
51 return 0;
52 }
53 SL.ListData[++SL.ListLen]=data;
54 return 1;
55 }
56
57 //删除节点
58 int SLDelete(SLType SL,int n ){
59 int i;
60 if(n<1||n>SL.ListLen+1){
61 System.out.println("序号输入有误,不能插入节点");
62 return 0;
63 }
64 //往前挪
65 for(i = n ; i<SL.ListLen;i++){
66 SL.ListData[i] = SL.ListData[i+1];
67 }
68 SL.ListLen--;
69 return 1;
70 }
71
72 //查找节点
73 DATA SLFindByNum(SLType SL,int n){
74 if(n<1||n>SL.ListLen+1){
75 System.out.println("序号输入有误,不能插入节点");
76 return null;
77 }
78 return SL.ListData[n];
79 }
80
81 //按照关键字查找节点
82 int SLFindByCont(SLType SL,String key){
83 int i;
84 for(i = 1; i <= SL.ListLen ; i++){
85 if(SL.ListData[i].key.compareTo(key)==0){
86 return i;
87 }
88 }
89 return 0;
90 }
91
92 //显示所有节点
93 int SLAll(SLType SL){
94 int i;
95 for(i = 1; i <=SL.ListLen ; i++){
96 System.out.println(SL.ListData[i].key+"#"+SL.ListData[i].name+"#"+SL.ListData[i].age);
97 }
98 return 0;
99 }
100
101 }
102
103 public class SequentialList {
104 public static void main(String[] args) {
105 int i;
106 SLType SL=new SLType(); //定义顺序表变量
107 // DATA data=new DATA(); //定义结点保存数据类型变量
108 DATA pdata; //定义结点保存指针变量
109 String key; //保存关键字
110
111 System.out.print("顺序表操作演示!\n");
112
113 SL.SLInit(SL); //初始化顺序表
114 System.out.print("初始化顺序表完成!\n");
115
116 Scanner input=new Scanner(System.in);
117
118 do
119 { //循环添加结点数据
120 System.out.print("输入添加的结点(学号 姓名 年龄):");
121 DATA data=new DATA();
122 data.key=input.next();
123 data.name=input.next();
124 data.age=input.nextInt();
125
126 if(data.age!=0) //若年龄不为0
127 {
128 if(SL.SLAdd(SL,data)==0) //若添加结点失败
129 {
130 break; //退出死循环
131 }
132 }
133 else //若年龄为0
134 {
135 break; //退出死循环
136 }
137 }while(true);
138 System.out.print("\n顺序表中的结点顺序为:\n");
139 SL.SLAll(SL); //显示所有结点数据
140
141
142 System.out.print("\n要取出结点的序号:");
143 i=input.nextInt(); //输入结占点序号
144 pdata=SL.SLFindByNum(SL,i); //按序号查找结点
145 if(pdata!=null) //若返回的结点指针不为NULL
146 {
147 System.out.printf("第%d个结点为:(%s,%s,%d)\n",i,pdata.key,pdata.name,pdata.age);
148 }
149 System.out.print("\n要查找结点的关键字:");
150 key=input.next(); //输入关键字
151 i=SL.SLFindByCont(SL,key); //按关键字查找 ,返回结点序号
152 pdata=SL.SLFindByNum(SL,i); //按序号查询,返回结点指针
153 if(pdata!=null) //若结点指针不为NULL
154 {
155 System.out.printf("第%d个结点为:(%s,%s,%d)\n",i,pdata.key,pdata.name,pdata.age);
156 }
157 }
158 }