java数据结构labWord文件下载.docx
《java数据结构labWord文件下载.docx》由会员分享,可在线阅读,更多相关《java数据结构labWord文件下载.docx(25页珍藏版)》请在冰豆网上搜索。
实现代码:
packagehomework2;
顺序存储:
publicclassSeqList<
T>
{
publicObject[]element;
publicintlen;
publicSeqList(T[]ele)
{
this.element=newObject[ele.length];
element=ele;
this.len=element.length;
}
publicSeqList()
this(null);
publicvoidappend(SeqList<
list)
Object[]temp=this.element;
this.element=newObject[this.len+list.len];
for(inti=0;
i<
temp.length;
i++)
{
element[i]=temp[i];
}
for(intj=this.len,k=0;
j<
this.len+list.len;
j++,k++)
element[j]=list.element[k];
}
publicclassSeqCombine{
publicstaticvoidmain(Stringargs[])
Integer[]a={3,5,8,11};
Integer[]b={2,6,8,9,11,15,20};
SeqList<
Integer>
A=newSeqList<
(a);
B=newSeqList<
(b);
A.append(B);
=A.element.length-1;
System.out.print(A.element[i]+"
"
);
链式存储
使用尾节点
publicclassNode<
Tdata;
Node<
next;
Node(Tdata,Node<
next)
this.data=data;
this.next=next;
Node()
this(null,null);
publicclassSinglyLinkedList<
publicNode<
head;
rear;
publicSinglyLinkedList()
this.head=newNode<
();
this.rear=newNode<
publicSinglyLinkedList(T[]element)
this();
rear=this.head;
element.length;
rear.next=(Node<
)newNode<
(element[i],null);
rear=rear.next;
publicclassCombine{
SinglyLinkedList<
A=newSinglyLinkedList<
B=newSinglyLinkedList<
A.rear.next=B.head.next;
Node<
p=A.head.next;
while(p!
=null)
System.out.print(p.data+"
p=p.next;
不用尾节点。
rear=this.head;
p=A.head;
while(p.next!
p.next=B.head.next;
q=A.head.next;
while(q!
System.out.print(q.data+"
q=q.next;
粘贴运行结果
输出:
3,5,8,11,2,6,9,11,15,20
②将LA与LB表归并,要求仍有序(相同元素要保留)
LC=(2,3,5,6,8,8,9,11,11,15,20)
----------顺序存储
publicvoidinsert(inti,Tx)
if(x==null)
return;
this.element=newObject[temp.length+1];
for(intj=0;
j++)
element[j]=temp[j];
if(i<
0)i=0;
if(i>
this.len)i=this.len;
for(intj=this.len-1;
j>
=i;
j--)
this.element[j+1]=this.element[j];
this.element[i]=x;
this.len++;
inti=0,j=0;
while(i<
A.len)
if((Integer)A.element[i]<
(Integer)B.element[j])
{
B.insert(j,(Integer)A.element[i]);
}
if((j+1)<
B.len&
&
(Integer)A.element[i]>
(Integer)B.element[j]&
(Integer)A.element[i]<
(Integer)B.element[j+1])
j++;
i++;
if(i>
=A.len)
break;
if(j<
(Integer)A.element[i]==(Integer)B.element[j+1])
B.insert(j+1,(Integer)A.element[i]);
if((j+1)>
=B.len&
for(intk=0;
k<
B.len;
k++)
System.out.print(B.element[k]+"
2,3,5,6,8,8,9,11,11,15,20
SeqLis