Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @author haytham
- */
- public class SingleLinkedList<E> {
- private Node<E> head;
- private Node<E> tail;
- private int size=0;
- public SingleLinkedList(){}
- public SingleLinkedList(E e){
- add(e);
- }
- public SingleLinkedList(E[] e){
- for(E x:e)
- add(x);
- }
- public void add(E e){
- if(head==null){
- head=tail=new Node<E>(e);
- size++;
- }
- else {
- Node<E> node=new Node<E>(e);
- tail.next=node;
- tail=tail.next;
- size++;
- }}
- public void add(int index,E e){
- if(index<0||index>=size){
- return ;
- }
- Node<E> node=new Node<E>(e);
- Node<E> current=head;
- for(int i=0;i<index-2;i++){
- current=current.next;
- }
- node.next=current.next;
- current.next=node;
- size++;
- }
- public void addFirst(E e){
- if(isEmpty()){
- head=tail=new Node<E>(e);
- size++;
- }
- else{
- Node<E> node=new Node<E>(e);
- node.next=head;
- head=node;
- size++;
- }
- }
- public void addAll(E[] arr){
- for(E x:arr){
- add(x);
- }
- }
- public void clear(){
- head=null;
- size=0;
- }
- public boolean contains(E e){
- Node<E> current=head;
- while(current!=null){
- if(current.data.equals(e))
- return true;
- current=current.next;
- }
- return false;
- }
- public E get(int index){
- if(size()==0)
- return null;
- Node<E> current=head;
- for(int i=0;i<index;i++){
- current=current.next;
- }
- return current.data;
- }
- public boolean isEmpty(){
- return size()==0;
- }
- public int size(){
- return size;
- }
- public E getFirst(){
- return head.data;
- }
- public E getLast(){
- return tail.data;
- }
- public int indexOf(E e){
- if(!contains(e)||isEmpty())
- return -1;
- Node<E> current=head;
- for(int i=0;i<size();i++){
- if(get(i).equals(e))
- return i;
- }
- return -1;
- }
- public int lastIndexOf(E e){
- Node<E> current=head;
- int index=-1;
- int i=0;
- while(current!=null){
- if(current.data.equals(e)){
- index=i;
- current=current.next;
- } else {
- current=current.next;
- }
- i++;
- }
- return index;
- }
- public E removeFirst(){
- if(size()==0)
- return null;
- Node<E> node=head;
- head=head.next;
- size--;
- return node.data;
- }
- public E removeLast(){
- if(size()==0)
- return null;
- if(size()==1){
- head=tail=null;
- size--;
- }
- Node<E> node=tail;
- Node<E> current=head;
- while(current.next.next!=null){
- current=current.next;
- }
- tail=null;
- tail=current;
- size--;
- return node.data;
- }
- public boolean remove(E e){
- if(contains(e)){
- remove(indexOf(e));
- return true;
- }
- return false;
- }
- public E remove(int index){
- if(index>=size()||index<0)
- return null;
- if(index==0)
- return removeFirst();
- if(index==size()-1)
- return removeLast();
- Node<E> element;
- Node<E> current=head;
- for(int i=0;i<index-2;i++){
- current=current.next;
- }
- element=current.next;
- current.next=element.next;
- size--;
- return element.data;
- }
- public void reverse(){
- reverse(head);
- }
- private void reverse(Node<E> node){
- if(node.next==null){
- head=node;
- return ;
- }
- reverse(node.next);
- node.next.next=node;
- node.next=null;
- }
- public void MoveLastToHead(){
- if(head==null||size()==1){
- return;
- }
- else {
- addFirst(removeLast());
- }
- }
- public void MoveLastToFirst(){
- Node<E> last=tail;
- Node<E> current=head;
- while(current.next.next!=null){
- current=current.next;
- }
- tail=null;
- tail=current;
- last.next=head;
- head=last;
- }
- public void reverse2(){
- Node<E> current=head;
- Node<E> prev=null;
- Node<E> next;
- while(current!=null){
- next=current.next;
- current.next=prev;
- prev=current;
- current=next;
- }
- head=prev;
- }
- public void RotateLeft(int times){
- for(int i=0;i<times;i++){
- RotateLeft();
- }
- }
- private void RotateLeft(){
- if(size()==0||size()==1)
- return ;
- Node<E> node=tail;
- Node<E> current=head;
- while(current.next.next!=null){
- current=current.next;
- }
- current.next=null;
- tail=current;
- node.next=head;
- head=node;
- }
- public void RotateRgiht(int times){
- for(int i=0;i<times;i++){
- RotateRight();
- }
- }
- public void RotateRight(){
- if(size()==0||size()==1)
- return ;
- Node<E> node=head;
- head=head.next;
- tail.next=node;
- tail=node;
- }
- public static void main(String[] args) {
- SingleLinkedList<Integer> list=new SingleLinkedList<Integer>();
- list.add(1);
- list.add(2);
- list.add(3);
- list.add(4);
- for(int i=0;i<list.size();i++){
- System.out.print(list.get(i)+" ");
- }
- System.out.println();
- list.RotateRgiht(1);
- for(int i=0;i<list.size();i++){
- System.out.print(list.get(i)+" ");
- }
Advertisement
Add Comment
Please, Sign In to add comment