Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package LinkedList;
- /**
- *
- * @author acer
- */
- public class LinkedList{
- public Node head;
- /* First Constructor:
- * Creates a linked list using the values from the given array. head will refer
- * to the Node that contains the element from a[0]
- */
- public LinkedList(Object [] a){
- // TO DO
- head=new Node(a[0],null);
- Node tail=head;
- for(int i=1;i<a.length;i++){
- Node n=new Node(a[i],null);
- tail.next=n;
- tail=n;
- }
- }
- /* Second Constructor:
- * Sets the value of head. head will refer
- * to the given LinkedList
- */
- public LinkedList(Node h){
- // TO DO
- head=h;
- }
- /* Counts the number of Nodes in the list */
- public int countNode(){
- // TO DO
- int count=0;
- for(Node n=head;n!=null;n=n.next){
- count++;
- }
- return count; // please remove this line!
- }
- /* prints the elements in the list */
- public void printList(){
- // TO DO
- for(Node n=head;n!=null;n=n.next){
- if(n.next==null){
- System.out.print(n.element+".");
- }else{
- System.out.print(n.element+",");
- }
- }
- System.out.println();
- }
- // returns the reference of the Node at the given index. For invalid index return null.
- public Node nodeAt(int idx){
- // TO DO
- if(idx<0){
- throw new ArrayIndexOutOfBoundsException();
- }
- int count=0;
- for(Node n=head;n!=null;n=n.next){
- if(count==idx){
- return n;
- }
- count++;
- }
- return null; // please remove this line!
- }
- // returns the element of the Node at the given index. For invalid idx return null.
- public Object get(int idx){
- // TO DO
- if(idx<0){
- throw new ArrayIndexOutOfBoundsException();
- }
- int index=0;
- for(Node n=head;n!=null;n=n.next){
- if(index==idx){
- return n.element;
- }
- index++;
- }
- return null; // please remove this line!
- }
- /* updates the element of the Node at the given index.
- * Returns the old element that was replaced. For invalid index return null.
- * parameter: index, new element
- */
- public Object set(int idx, Object elem){
- // TO DO
- int index=0;
- for(Node n=head;n!=null;n=n.next){
- index++;
- if(index==idx){
- Object replaced=n.element;
- n.element=elem;
- return replaced;
- }
- }
- return null; // please remove this line!
- }
- /* returns the index of the Node containing the given element.
- if the element does not exist in the List, return -1.
- */
- public int indexOf(Object elem){
- // TO DO
- int index=0;
- for(Node n=head;n!=null;n=n.next){
- if(n.element.equals(elem)){
- return index;
- }
- index++;
- }
- return -1; // please remove this line!
- }
- // returns true if the element exists in the List, return false otherwise.
- public boolean contains(Object elem){
- // TO DO
- int index=0;
- for(Node n=head;n!=null;n=n.next){
- if(n.element.equals(elem)){
- return true;
- }
- index++;
- }
- return false; // please remove this line!
- }
- // Makes a duplicate copy of the given List. Returns the reference of the duplicate list.
- public Node copyList(){
- // TO DO
- Node newHead=new Node(head.element,null);
- Node newTail=newHead;
- for(Node n=head;n!=null;n=n.next){
- Node mn=new Node(n.element,null);
- newTail.next=mn;
- newTail=mn;
- }
- return newHead; // please remove this line!
- }
- // Makes a reversed copy of the given List. Returns the head reference of the reversed list.
- public Node reverseList(){
- // TO DO
- Node rev=null;
- for(Node n=head;n!=null;n=n.next){
- Node mn=new Node(n.element,null);
- mn.next=rev;
- rev=mn;
- }
- return rev; // please remove this line!
- }
- /* inserts Node containing the given element at the given index
- * Check validity of index.
- */
- public void insert(Object elem, int idx){
- // TO D
- if(idx<0 ){
- throw new ArrayIndexOutOfBoundsException();
- }
- Node mn=new Node(elem,null);
- int index=0;
- if(idx==0){
- mn.next=head;
- head=mn;
- }
- else{
- Node pred=this.nodeAt(idx-1);
- mn.next=pred.next;
- pred.next=mn;
- }
- }
- /* removes Node at the given index. returns element of the removed node.
- * Check validity of index. return null if index is invalid.
- */
- public Object remove(int index){
- // TO DO
- Object removed=null;
- Node rn=null;
- int size=this.countNode();
- if(index<0 || index>size){
- return null;
- }
- if(index==0){
- rn=head;
- head=head.next;
- }else{
- Node pred=nodeAt(index-1);
- rn=pred.next;
- pred.next=rn.next;
- }
- removed=rn.element;
- rn.element=null;
- rn.next=null;
- return removed; // please remove this line!
- }
- // Rotates the list to the left by 1 position.
- public void rotateLeft(){
- // TO DO
- Node tail=null;
- for(tail=head;tail.next!=null;tail=tail.next){
- }
- tail.next=head;
- head=head.next;
- tail.next.next=null;
- }
- // Rotates the list to the right by 1 position.
- public void rotateRight(){
- // TO DO
- Node preTail=null;
- for(preTail=head;preTail.next.next!=null;preTail=preTail.next){
- }
- Node tail=preTail.next;
- tail.next=head;
- head=tail;
- preTail.next=null;
- }
- }
Add Comment
Please, Sign In to add comment