// 导入Scanner类用于获取用户输入
import java.util.Scanner;
// 定义泛型类SeqList,用于实现顺序表相关操作
class SeqList<DataType> {
// 定义顺序表的最大容量为100
private static final int MaxSize = 100;
// 用于存储元素的数组
private DataType[] data;
// 顺序表的实际长度
private int length;
// 构造函数,初始化数组和长度
public SeqList() {
data = (DataType[]) new Object[MaxSize];
length = 0;
}
// 用于展示顺序表长度的方法
public void show() {
System.out.println("The length:" + length);
}
// 查找元素x在顺序表中的位置,返回逻辑位置(从1开始),未找到返回0
public int Locate(DataType x) {
for (int i = 0; i < length; i++) {
if (data[i].equals(x)) {
return i + 1;
}
}
return 0;
}
// 在位置i插入元素x,若插入位置非法则直接返回
public void Insert(int i, DataType x) {
if (i < 1 || i > length + 1) {
return;
}
// 将插入位置及之后的元素后移
for (int j = length; j >= i; j--) {
data[j] = data[j - 1];
}
data[i - 1] = x;
length++;
}
// 逆序顺序表中的数据,通过交换对称位置的元素实现
public void Reverse() {
for (int i = 0; i < length / 2; i++) {
DataType temp = data[i];
data[i] = data[length - 1 - i];
data[length - 1 - i] = temp;
}
}
// 输出顺序表中的元素
public void DispList() {
for (int i = 0; i < length; i++) {
System.out.print(data[i] + " ");
}
System.out.println();
}
}
public class Main {
public static void main(String[] args) {
// 创建一个存储Integer类型的顺序表对象
SeqList<Integer> list = new SeqList<>();
Scanner scanner = new Scanner(System.in);
// 循环读取用户输入,将输入数据插入到位置1,直到输入0结束
int num = scanner.nextInt();
while (num != 0) {
list.Insert(1, num);
num = scanner.nextInt();
}
list.show();
System.out.println("The elements:");
list.DispList();
int target = scanner.nextInt();
// 查找目标元素并根据结果输出提示信息
if (list.Locate(target) == 0) {
System.out.println("No found");
} else {
System.out.println("Found position:" + list.Locate(target));
}
list.show();
list.Reverse();
System.out.println("The elements:");
list.DispList();
scanner.close();
}
}
// 导入Scanner类用于获取用户输入
import java.util.Scanner;
// 定义类Selist,用于实现顺序表相关操作
class Selist {
// 用于存储元素的数组
int[] data;
// 顺序表当前长度
int curlen;
// 插入元素方法,在指定位置i插入元素x,若数组满或位置非法抛出异常
public void insert(int i, int x) throws Exception {
if (curlen == data.length)
throw new Exception("Overflow");
if (i < 0 || i > curlen)
throw new Exception("position is wrong");
// 将插入位置及之后的元素后移
for (int j = curlen - 1; j >= i; j--)
data[j + 1] = data[j];
data[i] = x;
curlen++;
}
// 构造函数,初始化数组
public Selist(int Maxsize) {
data = new int[Maxsize];
curlen = 0;
}
// 删除指定位置i元素的方法,若位置非法抛出异常
public void remove(int i) throws Exception {
if (i < 0 || i > curlen - 1)
throw new Exception("position is wrong");
// 将指定位置之后的元素前移
for (int j = i + 1; j < curlen; j++)
data[j - 1] = data[j];
curlen--;
}
// 输出顺序表所有元素的方法
public void display() {
System.out.print("Data:");
for (int i = 0; i < curlen; i++)
System.out.print(data[i] + " ");
System.out.println();
}
// 输出顺序表中不是3的倍数的元素的方法
public void displayNum() {
System.out.print("Data:");
for (int i = 0; i < curlen; i++) {
if (data[i] % 3 != 0) {
System.out.print(data[i] + " ");
}
}
}
// 查找元素x在顺序表中的位置,找到返回位置(从1开始),未找到返回-1
public int indexof(int x) {
int i;
for (i = 0; i < curlen; i++) {
if (data[i] == x)
break;
}
if (i < curlen)
return i + 1;
else
return -1;
}
// 逆序顺序表中元素的方法
public void nizhi() {
int t;
for (int i = 0; i < curlen / 2; i++) {
t = data[i];
data[i] = data[curlen - 1 - i];
data[curlen - 1 - i] = t;
}
}
}
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int n, i;
n = sc.nextInt();
Selist obj = new Selist(5);
// 循环读取用户输入并插入到顺序表中,若插入失败输出异常信息
for (i = 0; i < n; i++) {
try {
obj.insert(0, sc.nextInt());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
obj.display();
obj.displayNum();
}
}
// 导入Scanner类用于获取用户输入
import java.util.Scanner;
class Seqlist {
// 用于存储元素的数组(使用Object类型可存储不同类型数据)
Object []data;
// 顺序表当前长度
int curlen;
// 清空顺序表,将长度置为0
public void clear() {
curlen = 0;
}
// 有序插入整数x的方法,找到合适位置插入以保持顺序
public void insertInt(int x) {
int i, d;
for (i = 0; i < curlen; i++) {
d = Integer.valueOf(data[i].toString());
if (d > x)
break;
}
for (int j = curlen - 1; j >= i; j--)
data[j + 1] = data[j];
data[i] = x;
curlen++;
}
// 有序插入字符x的方法,找到合适位置插入以保持顺序,若有异常抛出
public void insertchar(char x) throws Exception {
int i;
char d;
for (i = 0; i < curlen; i++) {
d = (Character)(data[i]);
if (d > x)
break;
}
for (int j = curlen - 1; j >= i; j--)
data[j + 1] = data[j];
data[i] = x;
curlen++;
}
// 构造函数,初始化数组
public Seqlist(int Maxsize) {
data = new Object[Maxsize];
curlen = 0;
}
// 删除指定位置i元素的方法,若位置非法抛出异常
public void remove(int i) throws Exception {
if (i < 0 || i > curlen - 1)
throw new Exception("position is wrong");
for (int j = i + 1; j < curlen; j++)
data[j - 1] = data[j];
curlen--;
}
// 输出顺序表信息,包括长度和所有元素
public void display() {
System.out.println("The length:" + curlen);
System.out.println("The elements:");
for (int i = 0; i < curlen; i++)
System.out.print(data[i] + " ");
System.out.println();
}
}
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int n, m;
Seqlist obj = new Seqlist(100);
n = sc.nextInt();
// 循环读取整数并有序插入到顺序表中
for (int i = 1; i <= n; i++) {
m = sc.nextInt();
obj.insertInt(m);
}
obj.display();
obj.clear();
Seqlist b = new Seqlist(100);
n = sc.nextInt();
String str = sc.next();
// 循环读取字符串中的字符并有序插入到新顺序表中
for (int i = 0; i < n; i++) {
b.insertchar(str.charAt(i));
}
b.display();
}
}
// 导入Scanner类用于获取用户输入
import java.util.Scanner;
class seqlist {
// 用于存储元素的数组
int[]data;
// 顺序表当前长度
int curlen;
// 有序插入整数x的方法,找到合适位置插入以保持顺序
public void insertInt(int x) {
int i, d;
for (i = 0; i < curlen; i++) {
if (data[i] > x)
break;
}
for (int j = curlen - 1; j >= i; j--)
data[j + 1] = data[j];
data[i] = x;
curlen++;
}
// 合并两个有序顺序表a和b到当前顺序表的方法
public void merge(seqlist a, seqlist b) {
int i = 0, j = 0;
// 比较a和b的元素,依次将较小元素插入到当前表
while (i < a.curlen && j < b.curlen) {
if (a.data[i] < b.data[j]) {
this.insertInt(a.data[i]);
i++;
} else {
insertInt(b.data[j]);
j++;
}
}
int k;
// 将a剩余元素插入
if (i < a.curlen) {
for (k = i; k < a.curlen; k++) {
insertInt(a.data[k]);
}
} else {
// 将b剩余元素插入
for (k = j; k < b.curlen; k++) {
insertInt(b.data[k]);
}
}
}
// 构造函数,初始化数组
public seqlist (int Maxsize) {
data = new int[Maxsize];
curlen = 0;
}
// 输出顺序表信息,包括长度和所有元素
public void display() {
System.out.println("The length:" + curlen);
System.out.println("The elements:");
for (int i = 0; i < curlen; i++)
System.out.print(data[i] + " ");
System.out.println();
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, m;
seqlist a = new seqlist(100);
seqlist b = new seqlist(100);
n = sc.nextInt();
// 循环读取整数并有序插入到顺序表a中
for (int i = 0; i < n; i++) {
m = sc.nextInt();
a.insertInt(m);
}
a.display();
n = sc.nextInt();
// 循环读取整数并有序插入到顺序表b中
for (int i = 0; i < n; i++) {
m = sc.nextInt();
b.insertInt(m);
}
b.display();
seqlist obj = new seqlist(100);
// 合并a和b到obj
obj.merge(a, b);
obj.display();
}
}
// 导入Scanner类用于获取用户输入
import java.util.Scanner;
// 定义链表节点类Node
class Node {
// 节点存储的数据
public Object data;
// 指向下一个节点的引用
public Node next;
// 无参构造函数,初始化数据和下一个节点引用
public Node() {
this(null, null);
}
// 带数据参数的构造函数,初始化数据,下一个节点引用为null
public Node(Object data) {
this(data, null);
}
// 带数据和下一个节点引用参数的构造函数
public Node(Object data, Node next) {
this.data = data;
this.next = next;
}
}
// 定义链表类LinkList
class LinkList {
// 链表头节点
private Node node;
// 链表当前长度
private int currth = 0;
// 构造函数,初始化头节点
public LinkList() {
node = new Node();
}
// 在指定位置i插入元素x的方法,若位置非法抛出异常
public void insert(int i, Object x) throws Exception {
Node p = node;
int j = 0;
while (p!= null && j < i - 1) {
p = p.next;
j++;
}
if (p == null || j > i + 1) {
throw new Exception("Illegal insertion position");
}
Node s = new Node(x);
s.next = p.next;
p.next = s;
currth++;
}
// 查找元素obj在链表中的位置,找到返回位置,未找到返回-1
public int get(Object obj) {
int i = 1;
Node p = node.next;
while (p!= null &&!p.data.equals(obj)) {
p = p.next;
i++;
}
if (p!= null) {
return i;
} else
return -1;
}
// 获取链表长度的方法
public int length() {
return currth;
}
// 输出链表所有元素的方法
public void display() {
Node dis = node.next;
while (dis!= null) {
System.out.print(dis.data + " ");
dis = dis.next;
}
System.out.println();
}
// 逆序链表的方法
public void reverse() {
Node head = node, p = node.next, q = null;
head.next = null;
while (p!= null) {
q = p.next;
p.next = head.next;
head.next = p;
p = q;
}
}
}
public class Main {
public static void main(String[] args) throws Exception {
LinkList lis = new LinkList();
Scanner sc = new Scanner(System.in);
int next = sc.nextInt();
// 循环读取用户输入,将输入元素插入到链表头部,直到输入0结束
while (next!= 0) {
lis.insert(1, next);
next = sc.nextInt();
}
next = sc.nextInt();
int length = lis.length();
System.out.println("The length:" + length);
System.out.println("The elements:");
lis.display();
if (lis.get(next)!= -1) {
System.out.println("Found position:" + lis.get(next));
} else {
System.out.println("No found");
}
System.out.println("The length:" + length);
lis.reverse();
System.out.println("The elements:");
lis.display();
}
}
// 导入Scanner类用于获取用户输入
import java.util.Scanner;
class SortList {
// 用于存储元素的数组
private Object[] objs;
// 数组当前元素数量
private int length;
// 构造函数,初始化数组
public SortList(Object[] obj) {
objs = obj;
length = 0;
}
// 获取数组的方法
public Object[] getobjs() {
return objs;
}
// 清空数组,将元素数量置为0
public void clean() {
length = 0;
}
// 有序插入整数num的方法,若数组已满抛出异常
public void sortListInt(int num) throws Exception {
int i = 0;
int index = 0;
if (length >= objs.length) {
throw new Exception("ArrayIndexOutOfBoundsException");
} else {
if (objs[0] == null) {
objs[0] = num;
} else {
// 找到合适的插入位置
for (i = length; i > 0; i--) {
if (Integer.parseInt(objs[i - 1].toString()) > num) {
objs[i] = objs[i - 1];
} else {
index = i;
break;
}
}
objs[index] = num;
}
length++;
}
}
// 获取数组元素数量的方法
public int length() {
return length;
}
// 输出数组所有元素的方法
public void display() {
for (int i = 0; i < length; i++) {
System.out.print(objs[i] + " ");
}
}
// 合并两个数组objs1和objs2到当前数组的方法
public void merge(Object[] objs1, Object[] objs2) {
int i1 = 0, i2 = 0, o1 = 0, o2 = 0, j = 0;
for (; j < objs1.length + objs2.length; j++) {
o1 = Integer.parseInt(objs1[i1].toString());
o2 = Integer.parseInt(objs2[i2].toString());
if (o1 > o2) {
objs[j] = o2;
i2++;
} else {
objs[j] = o1;
i1++;
}
length++;
if (i1 == objs1.length || i2 == objs2.length) {
break;
}
}
// 将剩余元素插入
while (i2!= objs2.length) {
objs[++j] = objs2[i2];
i2++;
length++;
}
while (i1!= objs1.length) {
objs[++j] = objs1[i1];
i1++;
length++;
}
}
}
public class Main {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
int length1 = input.nextInt();
Object[] ob1 = new Object[length1];
SortList arr1 = new SortList(ob1);
int num = 0;
// 循环读取整数并有序插入到数组arr1中
for (int i = 0; i < length1; i++) {
num = input.nextInt();
arr1.sortListInt(num);
}
int length2 = input.nextInt();
Object[] ob2 = new Object[length2];
SortList arr2 = new SortList(ob2);
// 循环读取整数并有序插入到数组arr2中
for (int i = 0; i < length2; i++) {
num = input.nextInt();
arr2.sortListInt(num);
}
System.out.println("The length:" + arr1.length());
System.out.println("The elements:");
arr1.display();
System.out.println();
System.out.println("The length:" + arr2.length());
System.out.println("The elements:");
arr2.display();
System.out.println();
Object[] obj3 = new Object[length1 + length2];
SortList object = new SortList(obj3);
// 合并arr1和arr2到object
object.merge(ob1, ob2);
System.out.println("The length:" + object.length());
System.out.println("The elements:");
object.display();
}
}
// 导入Scanner类用于获取用户输入
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
LinkList list = new LinkList();
Scanner scanner = new Scanner(System.in);
int input;
// 循环读取用户输入,将输入元素插入到链表中,直到输入0结束
do {
input = scanner.nextInt();
if (input != 0) {
list.inserts(input);
}
} while (input != 0);
System.out.print("Data:");
list.disList();
list.reverse();
System.out.print("Data:");
list.disList();
}
}
class Node {
// 节点存储的整数数据
int data;
// 指向下一个节点的引用
Node next;
// 带数据参数的构造函数,初始化数据,下一个节点引用为null
public Node(int data) {
this.data = data;
this.next = null;
}
// 无参构造函数,下一个节点引用为null
public Node() {
this.next = null;
}
// 获取节点数据的方法
public int getData() {
return data;
}
// 设置节点数据的方法
public void setData(int data) {
this.data = data;
}
// 获取下一个节点引用的方法
public Node getNext() {
return next;
}
// 设置下一个节点引用的方法
public void setNext(Node next) {
this.next = next;
}
}
class LinkList {
// 链表头节点
private Node head;
// 链表长度
private int length;
// 构造函数,初始化头节点和长度
public LinkList() {
head = new Node();
length = 0;
}
// 重新初始化链表的方法
public void linkList() {
head = new Node();
length = 0;
}
// 获取链表长度的方法
public int length() {
Node p = head.next;
int count = 0;
while (p != null) {
count++;
p = p.next;
}
return count;
}
// 插入元素x到链表合适位置的方法
public Node inserts(int x) {
Node p = head;
Node node = new Node(x);
if (p == null || x <= p.data) {
node.next = p;
return node;
}
Node current = p;
while (current.next != null && current.next.data < x) {
current = current.next;
}
node.next = current.next;
current.next = node;
return head;
}
// 输出链表所有元素的方法
public void disList() {
Node p = head.next;
while (p != null) {
System.out.print(p.data + " ");
p = p.next;
}
System.out.println();
}
// 逆序链表的方法
public void reverse() {
if (head == null || head.next == null) {
return;
}
Node pre = null;
Node cur = null;
Node next = null;
cur = head.next;
next = cur.next;
cur.next = null;
pre = cur;
cur = next;
while (cur.next != null) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
cur.next = pre;
head.next = cur;
}
}
// 导入Scanner类用于获取用户输入
import java.util.Scanner;
class Node {
// 节点存储的数据
Object data;
// 指向下一个节点的引用
Node next;
// 带数据和下一个节点引用参数的构造函数
public Node(Object data, Node next) {
this.data = data;
this.next = next;
}
// 带数据参数的构造函数,下一个节点引用为null
public Node(Object data) {
this.data = data;
next = null;
}
// 无参构造函数,数据和下一个节点引用都为null
public Node() {
data = null;
next = null;
}
}
class Linklist {
// 链表尾节点
Node rear;
// 构造函数,初始化尾节点为null
public Linklist() {
rear = null;
}
// 插入元素x到链表的方法,若链表为空则创建循环链表
public void insert(Object x) throws Exception {
Node s = new Node(x);
if (rear == null) {
rear = s;
rear.next = rear;
} else {
s.next = rear.next;
rear.next = s;
}
}
// 在链表头部插入元素data的方法
public void insertF(Object data) {
Node newNode = new Node(data);
newNode.next = rear;
rear = newNode;
}
// 在链表尾部插入元素input1的方法
public void insertR(Object input1) {
Node newNode = new Node(input1);
if (rear == null) {
rear = newNode;
} else {
Node temp = rear;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
}
// 输出链表所有元素的方法
public void disList() {
Node p = rear;
while (p != null) {
System.out.print(p.data + " ");
p = p.next;
}
System.out.println();
}
// 对链表中字符元素进行处理的方法
public void subdx(int dx) {
Node temp = rear;
while (temp != null) {
if ((char)(temp.data) >= 'a' && (char)(temp.data) <= 'z')
temp.data = (char)((char)temp.data - dx);
temp = temp.next;
}
}
}
public class Main {
public static void main(String[] args) {
Linklist list = new Linklist();
Linklist list1 = new Linklist();
Scanner scanner = new Scanner(System.in);
int n;
n = scanner.nextInt();
String input = scanner.next();
// 循环读取字符串中的字符并插入到链表list头部
for (int i = 0; i < n; i++) {
list.insertF(input.charAt(i));
}
int dx;
dx = scanner.nextInt();
int input1;
// 循环读取整数并插入到链表list1尾部,直到输入0结束
do {
input1 = scanner.nextInt();
if (input1 != 0) {
list1.insertR(input1);
}
} while (input1 != 0);
System.out.print("Data:");
list.disList();
list.subdx(dx);
System.out.print("Data:");
list.disList();
System.out.print("Data:");
list1.disList();
}
}
// 导入Scanner类用于获取用户输入
import java.util.Scanner;
class Node {
// 节点存储的数据
Object data;
// 指向下一个节点的引用
Node next;
// 带数据和下一个节点引用参数的构造函数
public Node(Object data, Node next) {
this.data = data;
this.next = next;
}
// 带数据参数的构造函数,下一个节点引用为null
public Node(Object data) {
this.data = data;
next = null;
}
// 无参构造函数,数据和下一个节点引用都为null
public Node() {
data = null;
next = null;
}
}
class Linklist {
// 链表尾节点
Node rear;
// 构造函数,初始化尾节点为null
public Linklist() {
rear = null;
}
// 插入元素x到链表的方法,若链表为空则创建循环链表
public void insert(Object x) throws Exception {
Node s = new Node(x);
if (rear == null) {
rear = s;
rear.next = rear;
} else {
s.next = rear.next;
rear.next = s;
}
}
// 在链表头部插入元素data的方法
public void insertF(Object data) {
Node newNode = new Node(data);
newNode.next = rear;
rear = newNode;
}
// 在链表尾部插入元素input1的方法
public void insert