一.上机内容
1、实现顺序存储结构下线性表的基本操作,数据类型自己确定。 2、输入一组数据,建立带头结点的单链表,实现线性表的基本操作,线性表中数据元素的类型自己确定。 3、试写一算法,实现顺序表的就地逆置,即利用原表的存储空间将线性表(a1,a2…,an)逆置为(an,an-1…,a1)。 4、已知有序表中的元素以值递增有序排列,并以单链表作存储结构。试写一高效的算法,删除表中所有值大于mink且小于 maxk的元素(若表中存在这样的元素)同时释放被删结点空间,并分析你所写的算法的时间复杂度(注意mink和maxk是给定的两个参变量,它们的值可以和表中的元素相同,也可以不同)。 5、删除单链表中重复的元素,即留下单链表中值不相同的结点,并输出删除后单链表中的所有元素。 6*、某百货公司仓库中有一批电视机,试按价格从高到底的次序建立一个循环链表,每个结点有价格)数量和链指针三个域。现新到m台价格为h的电视机,修改原链表并输出修改后链表的所有内容。 7*、在理解一元多项式的加法算法基础上,编程实现一元多项式的减法。
二.代码实现
1.实现顺序存储结构下线性表的基本操作,数据类型自己确定
public class SqList<T> implements Ilist<T>{
T
[] array
;
int length
;
public SqList(int capacity
) {
this.array
= (T
[]) new Object[capacity
];
this.length
= 0;
}
@Override
public void clear() {
this.length
= 0;
}
@Override
public boolean isEmpty() {
return this.length
==0;
}
@Override
public int getLength() {
return this.length
;
}
@Override
public T
get(int i
) throws Exception
{
if (i
<0 || i
>=this.length
){
throw new Exception("第 " + i
+" 元素不存在");
}
return this.array
[i
];
}
public void insert(T t
) throws Exception
{
insert(this.length
,t
);
}
@Override
public void insert(int i
, T t
) throws Exception
{
if (i
== this.array
.length
){
throw new Exception("长度已满,无法插入");
}
if (i
<0 || i
> this.length
){
throw new Exception("插入位置不合法,无法插入");
}
for (int j
= this.length
; j
> i
; j
--) {
this.array
[j
] = this.array
[j
-1];
}
this.array
[i
] = t
;
this.length
++;
}
@Override
public void remove(int i
) throws Exception
{
if (i
< 0 || i
> this.length
){
throw new Exception("删除位置不合法,无法删除");
}
for (; i
< this.length
- 1; i
++) {
this.array
[i
] = this.array
[i
+1];
}
this.length
--;
}
@Override
public int indexOf(T t
) {
for (int i
= 0; i
< this.length
; i
++) {
if (t
.equals(this.array
[i
])){
return i
;
}
}
return -1;
}
}
2、输入一组数据,建立带头结点的单链表,实现线性表的基本操作,线性表中数据元素的类型自己确定。
public class LinkList<T
extends Comparable> implements Ilist<T>{
Node head
;
int length
;
private class Node{
T item
;
Node next
;
public Node(Node next
,T item
) {
this.item
= item
;
this.next
= next
;
}
}
public LinkList() {
this.head
= new Node(null
,null
);
this.length
= 0;
}
@Override
public void clear() {
this.head
= new Node(null
,null
);
this.length
= 0;
}
@Override
public boolean isEmpty() {
return this.length
==0;
}
@Override
public int getLength() {
return this.length
;
}
@Override
public T
get(int i
) throws Exception
{
if (i
<0 || i
>=this.length
){
throw new Exception("第 " + i
+" 元素不存在");
}
Node current
= head
;
for (int j
= 0;j
<= i
;j
++){
current
= current
.next
;
}
return current
.item
;
}
public void insert(T t
) throws Exception
{
insert(this.length
,t
);
}
@Override
public void insert(int i
, T t
) throws Exception
{
if (i
<0 || i
> this.length
){
throw new Exception("插入位置不合法,无法插入");
}
Node pre
= head
;
for (int j
= 0; j
<= i
-1 ; j
++) {
pre
= pre
.next
;
}
Node newNode
= new Node(pre
.next
,t
);
pre
.next
= newNode
;
this.length
++;
}
@Override
public void remove(int i
) throws Exception
{
if (i
< 0 || i
> this.length
){
throw new Exception("删除位置不合法,无法插入");
}
Node pre
= head
;
for (int j
= 0; j
< i
; j
++) {
pre
= pre
.next
;
}
if (i
== this.length
){
pre
.next
= null
;
}else {
pre
.next
= pre
.next
.next
;
}
this.length
--;
}
@Override
public int indexOf(T t
) {
int count
= -1;
Node current
= head
;
while(current
.next
!=null
){
if (t
.equals(current
.item
)){
return count
;
}
count
++;
current
= current
.next
;
}
return -1;
}
@Override
public void display() {
Node current
= head
.next
;
while (current
!= null
){
System
.out
.print(current
.item
+ " ");
current
= current
.next
;
}
System
.out
.println();
}
}
3.试写一算法,实现顺序表的就地逆置,即利用原表的存储空间将线性表(a1,a2…,an)逆置为(an,an-1…,a1)
public void reverse(){
for (int i
= 0, j
= this.length
-1; i
< j
; i
++,j
--) {
T temp
= this.array
[i
];
this.array
[i
] = this.array
[j
];
this.array
[j
] = temp
;
}
}
4、已知有序表中的元素以值递增有序排列,并以单链表作存储结构。试写一高效的算法,删除表中所有值大于mink且小于 maxk的元素(若表中存在这样的元素)同时释放被删结点空间,并分析你所写的算法的时间复杂度(注意mink和maxk是给定的两个参变量,它们的值可以和表中的元素相同,也可以不同)。
public void remove(T mink
,T maxk
){
Node current
= head
.next
;
Node firstNodePre
= head
;
Node lastNodeNext
= null
;
boolean flag
= true;
while(current
.next
!=null
){
if (flag
){
if (mink
.compareTo(current
.item
) < 0){
flag
= false;
}else {
firstNodePre
= firstNodePre
.next
;
}
}
if (maxk
.compareTo(current
.item
) < 0){
lastNodeNext
= current
;
break;
}
current
= current
.next
;
}
firstNodePre
.next
= lastNodeNext
;
}
5、删除单链表中重复的元素,即留下单链表中值不相同的结点,并输出删除后单链表中的所有元素。
public void removeSame() throws Exception
{
ArrayList
<Node> arrayList
= new ArrayList<>();
Node currentNode
= head
.next
;
while(currentNode
!= null
){
int i
= indexOf(currentNode
.item
);
Node nextNode
= currentNode
.next
;
while (nextNode
!= null
){
if (nextNode
.item
.equals(currentNode
.item
)){
arrayList
.add(nextNode
);
remove(i
);
}else {
i
++;
}
nextNode
= nextNode
.next
;
}
currentNode
= currentNode
.next
;
}
for (Node node
: arrayList
){
System
.out
.println(node
.item
);
}
}
6*、某百货公司仓库中有一批电视机,试按价格从高到底的次序建立一个循环链表,每个结点有价格)数量和链指针三个域。现新到m台价格为h的电视机,修改原链表并输出修改后链表的所有内容。
7*、在理解一元多项式的加法算法基础上,编程实现一元多项式的减法。
public PolynList
minusPolyn(PolynList LA
,PolynList LB
){
Node ha
= LA
.head
;
Node qa
= LA
.head
.next
;
Node qb
= LB
.head
.next
;
while (qa
!= null
&& qb
!= null
){
PolynNode a
= (PolynNode
) qa
.data
;
PolynNode b
= (PolynNode
) qb
.data
;
switch (cmp(a
,b
)){
case -1:
ha
.next
= qa
;
ha
= qa
;
qa
= qa
.next
;
break;
case 0:
double sum
= a
.coef
- b
.coef
;
if(sum
!= 0.0){
a
.coef
= sum
;
ha
.next
= qa
;
ha
= qa
;
qa
= qa
.next
;
qb
= qb
.next
;
}else {
qa
= qa
.next
;
qb
= qb
.next
;
}
break;
case 1:
ha
.next
= qb
;
ha
= qb
;
qb
= qb
.next
;
break;
}
}
ha
.next
= (qa
== null
? qb
:qa
);
return LA
;
}