如:1,2,3,4,5 輸入2 結果爲 2,1,4,3,5
條件:單鏈表
思路:先將要逆序的元素從原鏈表中去掉,放在某一個數據結構中,再按照逆序插入原鏈表。單鏈表必須要考慮的是某個節點的父節點該如何找到,當然得考慮到右重複值得情況,對於,頭節點來說,是沒有父節點的,所以,每次插入都是在鏈表的頭節點的前面插入。我是從頭部開始去掉的,那麼這第一個去掉的元素(elem)再插入鏈表應該是最先插入,即先進先出。當第一次逆序結束後,第二次再去掉後面的某些部分,考慮到找插入點的父節點的麻煩,所以我選擇的是從某個元素(A)的的後面插入,而這裏的A我可以從頭次插入得到,即最先從隊列中出來的那個元素(elem),刪除的元素也是從elem開始往後刪,要逆序輸出,得先進來的元素後出去,即棧的性質
程序用到:隊列(在第一次逆序時,用隊列的先進先出性質),棧(在其它逆序輸出時,用到棧的先進後出)
import java.util.Scanner;
public class ListReverse{
public boolean isEmpty(){
return head==null;
}
public void insert(MyElem myElem){
if (isEmpty()) {
head=myElem;
}else{
tail.next=myElem;
}
tail=myElem;
}
public void insertHead(MyElem myElem){
if (isEmpty()) {
head=myElem;
tail=myElem;
tail.next=null;
return;
}
myElem.next=head;
head=myElem;
}
public void insertAfterPosition(MyElem pos,MyElem newElem){
if (pos.next==null) {
pos.next=newElem;
newElem.next=null;
tail=newElem;
return;
}
MyElem temp=pos.next;
pos.next=newElem;
newElem.next=temp;
}
public MyElem removeAfterPosition(MyElem pos){
if (pos.next==null) {
throw new Error("wrong,no next");
}
MyElem temp=pos.next;
if (pos.next.next==null) {
pos.next=null;
}else{
pos.next=temp.next;
}
return temp;
}
public MyElem removeHead(){
if(isEmpty()){
return null;
}
MyElem temp=head;
head=head.next;
return temp;
}
public void printMyList(){
MyElem temp=head;
while(temp!=null){
System.out.print(temp.elem+" ");
temp=temp.next;
}
System.out.println();
}
public int getLengthOfMyList(){
int count=0;
MyElem temp=head;
while(temp!=null){
count++;
temp=temp.next;
}
return count;
}
public void reverse(int k){
int len=getLengthOfMyList();
if(k>len|| k<=1){
return;
}
int count=1;
MyElem firstDotPre=null;//in next recyle ,this will be the first elemt.pre;
MyQueue mq=new MyQueue();
MyStack ms=new MyStack(k);
while(len/(k*count)!=0){
if (count==1) {
//首次從頭部逆序
//刪
for (int i=0;i<k ;i++ ) {
mq.enQueue(removeHead());
}
//存
for (int i=0;i<k ;i++ ) {
insertHead(mq.deQueue());
if (i==0) {
firstDotPre=head;
}
}
}else{
//其它地方逆序
//刪
for(int i=0;i<k;i++){
ms.push(removeAfterPosition(firstDotPre));
}
//here wll print more elem than it should be ,but it will not be a promblem
// mq.printMyQueue();
//存
MyElem temp=null;
for (int i=0;i<k ; i++) {
temp=ms.pop();
insertAfterPosition(firstDotPre,temp);
firstDotPre=temp;
}
}
count++;
}
}
public static void main(String[] args){
ListReverse lr=new ListReverse();
for (int i=0;i<5 ;i++ ) {
lr.insert(new MyElem(i,null));
}
lr.printMyList();
// lr.removeAfterPosition(lr.head);
// lr.insertInPosition(lr.tail,new MyElem(10,null));
Scanner scan=new Scanner(System.in);
int k=scan.nextInt();
lr.reverse(k);
lr.printMyList();
}
MyElem head=null;
MyElem tail=null;
}
//單個節點元素
class MyElem{
int elem;
MyElem next;
public MyElem(MyElem next){
this.next=next;
}
public MyElem(int elem , MyElem next){
this.elem=elem;
this.next=next;
}
}
//隊列
class MyQueue{
MyElem head=null;
MyElem tail=null;
public boolean isEmpty(){
return head==null;
}
public void enQueue(MyElem myElem){
if(isEmpty()){
head=myElem;
}else{
tail.next=myElem;
}
tail=myElem;
}
public MyElem deQueue(){
if (isEmpty()) {
throw new Error("no element");
}
MyElem temp=head;
head=head.next;
return temp;
}
public void printMyQueue(){
MyElem temp=head;
while(temp!=null){
System.out.print(temp.elem+" ");
temp=temp.next;
}
System.out.println();
}
}
//棧
class MyStack{
MyElem[] elems=null;
public MyStack(int k){
elems=new MyElem[k];
}
int topOfStack=-1;
public boolean isEmpty(){
return topOfStack==-1;
}
public boolean isFull(){
return topOfStack==10;
}
public void push(MyElem elem){
topOfStack++;
if(!isFull()){
elems[topOfStack]=elem;
}
}
public MyElem pop(){
if (!isEmpty()) {
MyElem temp=elems[topOfStack];
topOfStack--;
return temp;
}else{
throw new Error("wrong ,no elem in stack ");
}
}
}