已知LinkList类部分代码如下(勿改动),请实现函数InsertR,InsertF,DispList和AddDx,达到对应的输出。 //头结点 template <class T> struct Node { T data; Node<T> *next; }; template <class T> class LinkList { public: LinkList(); //建立只有头结点的空链表 ~LinkList(); //析构函数 void InsertR(T x); //在链表的表尾位置插入元素值为x的结点 void InsertF(T x); //在链表的头结点后插入元素值为x的结点 void DispList(); //遍历链表,依次输出各元素 void AddDx(int dx); //将表中不是3的倍数的元素值增dx,其它值不变 private: Node<T> *first; //单链表的头指针 }; //构造只有头结点的空表 template <class T> LinkList<T>:: LinkList( ){ first=new Node<T>; first->next=NULL; } //析构 template <class T> LinkList<T>:: ~LinkList( ){ Node<T> *s; while (first){ s=first; first=first->next; delete s; } } int main( ){ LinkList<int> A; int x; while(1) { cin>>x; //输入整数 if(!x)break; //为0退出 A.InsertR(x); //表尾插入 } A.DispList(); int dx; cin>>dx; //输入dx A.AddDx(dx); A.DispList(); LinkList<char> B; char ch; int n; cin>>n; //输入个数n for(int i=1;i<=n;i++) { cin>>ch; //输入n个字符 B.InsertF(ch); //表头后插入 } B.DispList(); return 0; } 输出 每个输出数据之间用1个空格隔开,每次链表遍历结束需换行 样例输入 Copy 1 2 42 2 3 12 23 0 5 6 ASDFGH 样例输出 Copy Data:1 2 42 2 3 12 23 Data:6 7 42 7 3 12 28 Data:H G F D S A用java做
时间: 2025-05-21 07:49:03 浏览: 15
以下是基于您提供的`LinkList`类的部分代码,完成对`InsertR`, `InsertF`, `DispList`, 和 `AddDx` 函数的实现,并将其转换为 Java 版本。
```java
// 定义节点结构体
class Node<T> {
T data;
Node<T> next;
public Node() {
this.data = null;
this.next = null;
}
public Node(T data) {
this.data = data;
this.next = null;
}
}
// 链表类
public class LinkList<T> {
private Node<T> head; // 表示链表头指针
// 构造函数:创建仅有头节点的空链表
public LinkList() {
head = new Node<>();
}
// 插入到链表尾部
public void InsertR(T x) {
Node<T> newNode = new Node<>(x);
if (head == null || head.data == null) { // 如果当前为空列表,则直接设置新节点为首节点
head = newNode;
} else {
Node<T> temp = head;
while (temp.next != null) { // 找到最后一个节点
temp = temp.next;
}
temp.next = newNode; // 将最后一个节点指向新节点
}
}
// 在链表头部插入
public void InsertF(T x) {
Node<T> newNode = new Node<>(x);
newNode.next = head; // 新节点指向原头节点
head = newNode; // 更新头节点为新节点
}
// 输出整个链表内容
public void DispList() {
System.out.print("Data:");
Node<T> temp = head;
boolean hasElement = false;
while (temp != null && temp.data != null) { // 只打印有效数据
if (hasElement) {
System.out.print(" ");
}
System.out.print(temp.data.toString());
hasElement = true;
temp = temp.next;
}
System.out.println(); // 换行
}
// 增加符合条件的数据
@SuppressWarnings({"unchecked"})
public <U extends Number> void AddDx(U dx) {
double delta = ((Number) dx).doubleValue();
Node<T> temp = head;
while (temp != null && temp.data != null) {
if (!(temp.data instanceof Integer)) break; // 确保操作的对象是整型
int value = (Integer) temp.data;
if (value % 3 != 0) { // 对于非三倍数增加dx值
temp.data = (T) Double.valueOf(value + delta);
}
temp = temp.next;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 整型测试
LinkList<Integer> listInt = new LinkList<>();
System.out.println("请输入一系列整数并以0结束:");
while (true) {
int num = scanner.nextInt();
if (num == 0) break;
listInt.InsertR(num); // 添加至表尾
}
listInt.DispList();
System.out.println("请输入增量值dx:");
int incrementValue = scanner.nextInt();
listInt.AddDx(incrementValue);
listInt.DispList();
// 字符型测试
LinkList<Character> listChar = new LinkList<>();
System.out.println("请输入需要添加到链表头部后的字符个数n:");
int countChars = scanner.nextInt();
for (int i = 1; i <= countChars; ++i) {
char c = scanner.next().charAt(0);
listChar.InsertF(c); // 添加至表首后方
}
listChar.DispList();
}
}
```
### 解释:
上述程序实现了以下功能:
- **InsertR** - 向链表尾端加入元素;
- **InsertF** - 向链表前端加入元素;
- **DispList** - 显示完整链表的内容,按照“Data:x y z...”的形式输出;
- **AddDx** - 当前仅适用于数字类型的元素,检查是否是非3的倍数后再对其加上指定值;
注意此版本假定所有输入都是合法有效的,在实际应用中还需进一步完善错误处理机制。
阅读全文