数据结构之线性表
package csdn.线性表;
public class day01 {
public static void main(String[] args) {
list s1 = new list(10);
for (int i = 1; i < 6; i++)
s1.insert(i);
s1.output();
System.out.println("*************");
boolean isempty = s1.isempty();
System.out.println(isempty);
boolean isfull = s1.isfull();
System.out.println(isfull);
System.out.println("***************");
int get = s1.get(3);
System.out.println(get);
System.out.println("*************");
s1.set(6, 6);
s1.output();
System.out.println("**************");
boolean contain = s1.contain(7);
System.out.println(contain);
System.out.println("***********");
s1.insert(7, 8);
s1.output();
System.out.println("*************");
s1.delete(8);
s1.output();
}
}
class list {
public int table[];
public int n;
public list(int length) {
table = new int[length];
n = 0;
}
public boolean isempty() {
return n == 0;
}
public boolean isfull() {
return n == table.length;
}
public int length() {
return n;
}
public void output() {
for (int i = 1; i <= n; i++) {
System.out.println(table[i - 1]);
}
}
public int get(int i) {
if (i > 0 && i <= n)
return table[i - 1];
else
return -1;
}
public void set(int i, int k) {
if (i > 0 && i <= n + 1) {
table[i - 1] = k;
if (i == n + 1)
n++;
}
}
public boolean contain(int k) {
int j = indexof(k);
if (j > n)
return false;
else
return true;
}
public int indexof(int k) {
int j = 1;
while (j <= n && table[j - 1] != k)
j++;
return j;
}
public void insert(int i, int k) {
if (!isfull()) {
if (i <= 0)
i = 1;
if (i > n)
i = n + 1;
int j = n;
for (; j >= i; j--) {
table[j] = table[j - 1];
}
table[i - 1] = k;
n++;
}
}
public void insert(int k) {
insert(n + 1, k);
}
public void delete(int k) {
int indexof = indexof(k);
int j = indexof;
if (indexof != -1) {
for (; j <= n - 1;) {
table[j - 1] = table[j];
j++;
}
table[n - 1] = 0;
n--;
}
}
}
class ysfcs {
public static void main(String[] args) {
ysf a = new ysf();
a.jj(5, 1, 2);
}
}
class ysf {
public void jj(int n, int s, int d) {
int k = n;
int i = s - 1;
list s2 = new list(10);
for (int m = 1; m < 6; m++) {
s2.insert(m);
}
while (k > 1) {
int j = 0;
while (j < d) {
i = i % n + 1;
if (s2.get(i) != 0) {
j++;
}
}
System.out.println("出队" + s2.get(i));
s2.set(i, 0);
k--;
}
int p = 1;
while (p <= n && s2.get(p) == 0) {
p++;
}
System.out.println("最后剩下的是" + s2.get(p));
}
}