Implement Double Linked List from Stack
来源:互联网 发布:178魔兽数据库 编辑:程序博客网 时间:2024/06/10 03:13
Imagine all items are organized into two stacks,one of them is head stack,and the other one is tail stack:
For Example:
head : 1,2,3,4->
tail : <-5,6,7,8
now u can move 4 to 5:
head : 3,2,1->
tail : <-4,5,6,7,8
or u can move 5 to 4;
head : 5,4,3,2,1->
tail : <-6,7,8
and them i will give the java code below this
import java.util.Stack;class DoubleLinkedList{Stack<Integer> s;Stack<Integer> t;int count;DoubleLinkedList(){s = new Stack<Integer>();t = new Stack<Integer>();count = 0;}// insert an data into the tail of head stack
// move all the item of head stack to tail stack and add count
public void insertInBeginning(int data){while(!s.isEmpty()){t.push(s.pop());}s.push(data);count++;}// insert an data into the tail of tail stack
// move all the item of tail stack to head stack and add count
public void insertAtEnd(int data){while(!t.isEmpty()){s.push(t.pop());}t.push(data);count++;}// if tail stack != null ,remove the top item of tail stack,and put it into head stackpublic void moveForward(){while(!t.isEmpty()){int temp = t.pop();System.out.println(temp);s.push(temp);}}// if head stack!=null, remove the top item of head stack and put it into tail stackpublic void moveBackward(){while(!s.isEmpty()){int temp = s.pop();System.out.println(temp);t.push(temp);}}// iterat item from head to tail,first move all the item of head stack to tail stack
// and then get the top item of tail stack , if it is equal to data ,remove it,if not remove it and put it to head stack
public void delete(int data){while(!s.isEmpty()){t.push(s.pop());}while(!t.isEmpty()){if(t.peek() == data){t.pop();return;}else{s.push(t.pop());}}}// find out the buttom item of head stack and then remove it public void deleteFirst(){while(!s.isEmpty()){int temp = s.pop();if(s.peek() == null){return;}t.push(temp);}}// find out the buttom item of tail stack and then remove itpublic void deleteLast(){while(!t.isEmpty()){int temp = t.pop();if(t.peek() == null){return;}s.push(temp);}}}
class P2{public static void main(String args[]){DoubleLinkedList list = new DoubleLinkedList();list.insertInBeginning(4);list.insertInBeginning(3);list.insertInBeginning(2);list.insertInBeginning(1);list.insertAtEnd(5);list.insertAtEnd(6);list.insertAtEnd(7);list.moveBackward();list.moveForward();list.delete(5);list.moveBackward();list.moveForward();}}
0 0
- Implement Double Linked List from Stack
- Double Circular Linked List
- bst double Linked List
- 双链表 double linked list
- List(double linked)
- malloc2 double linked list
- Stack (Linked List)
- implement deque using linked list
- Implement stack with Pyhon list
- double linked list of kernel
- smallbin double linked list corrupted
- BST to Double Linked List
- Double Linked List--Data Structure
- list实现ALDS1_3_C;double Linked List
- (3) double-linked list: insque remque
- 段错误:smallbin double linked list corrupted
- double linked list双向链表
- Flatten BST to Double Linked List
- 交换机端口untaged、taged、trunk、access 的区别
- iOS中的多线程
- 美术高考素描头像阅卷评分比重
- 十道海量数据处理面试题与十个方法大总结
- H3C S5048PV2-EI 交换机端口聚合
- Implement Double Linked List from Stack
- jdk_maven安装
- smack离线消息和文件
- SQL Server 存储过程
- perl对mysql的操作
- MATLAB凸包Convex hull运算
- Mahhout实现的算法、源码包分析
- 网络共享
- 交换机 Trunk(端口汇聚)的概念与设置