Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package assign1;
- public class MyList {
- // Node class
- private class Node {
- private char value;
- private Node next;
- public Node(char value) { this(value, null); }//add constructor
- public Node(char value, Node next) {
- this.next = next;
- this.value = value;
- }
- // Accessor methods
- public char getElement() { return value; }
- public Node getNext() { return next; }
- // Modifier methods
- public void setElement(char v) { value = v; }
- public void setNext(Node n) { next = n; }
- }
- // MyList class
- private Node head;
- // Implement required methods here
- public MyList(MyList rhs){
- head = null;
- }
- public MyList (char[] charArray, int n){//-----------------------------------MyList char[] int
- for(int i=0; i<n; i++){
- this.pushBack(charArray[i]);
- }
- }
- public boolean remove (int index){//-------------------------------------boolean remove index
- if(this.size()==0){
- System.out.println("No entries to remove.");
- return false;
- }
- else{
- if(index<0 || index>this.size()){
- System.out.println("Not a valid index number.");
- return false;
- }
- else{
- if(index == 0){ //remove object 0
- Node temp = head;
- head = head.next;
- temp.next = null;
- }
- else if(index == this.size()){ //remove last object
- Node temp = getNode(index-1);
- temp.next = null;
- }
- else{ //remove object inside list
- Node temp1 = getNode(index-1);
- Node temp2 = getNode(index);
- Node temp3 = getNode(index+1);
- temp1.next = temp3;
- temp2.next = null;
- }
- return true;
- }
- }
- }
- public boolean remove (char value){//-------------------------------------boolean remove char
- int index = this.find(value);
- boolean x = remove(index);
- return x;
- }
- public boolean removeAll (char value){//------------------------------------boolean removeAll char
- boolean j = findAll(value);
- if(j==false)
- return j;
- else{
- while(j==true){
- remove(value);
- j = findAll(value);
- }
- return true;
- }
- }
- public Node previous(Node curr){
- int i=0;
- while(getNode(i)!=curr)
- i++;
- return getNode(i-1);
- }
- public Node next(Node curr){return curr.next;}
- public boolean contains(char value){//---------------------------------boolean contains char
- boolean contain = findAll(value);
- return contain;
- }
- public char get(int index){//--------------------------------------------char get index
- Node temp1 = head;
- for(int i=0; i<index; i++){
- if(temp1.next != null)
- temp1 = temp1.next;
- }
- return temp1.value;
- }
- private Node getNode(int index){//-----------------------------------------Node getNode index
- Node temp1 = head;
- for(int i=0; i<index; i++){
- if(temp1.next != null)
- temp1 = temp1.next;
- }
- return temp1;
- }
- public void set(int index, char value){//-----------------------------------void set index value
- if(index<0 || index>this.size()){
- System.out.println("Invalid index number.");
- }
- else
- getNode(index).value = value;
- }
- public boolean equals(MyList Ilist){//----------------------------------------boolean equals MyList
- int i=0;
- while(this.getNode(i).value == Ilist.getNode(i).value){
- i++;
- if((i==this.size())&&(Ilist.getNode(i).next == null))
- return true;
- }
- return false;
- }
- public void pushFront(char value){//---------------------------------------void pushFront char
- this.insertBefore(0,value);
- }
- public void pushBack(char value){//----------------------------------------void pushBack char
- this.insertAfter(this.size(), value);
- }
- public void popFront(){//--------------------------------------------------void popFront
- this.remove(0);
- }
- public void popBack(){//-----------------------------------------------------void popBack
- this.remove(this.size());
- }
- public void swap(int i, int j){//--------------------------------------------void swap int int
- if(this.size()==0)
- System.out.println("No entries to swap.");
- else{
- if((i>=0)&&(i<=this.size())&&(j>=0)&&(j<=this.size())){
- Node temp1 = getNode(i);
- Node temp2 = getNode(j);
- this.remove(j);
- this.insertAtPos(j, temp1.value);
- this.remove(i);
- this.insertAtPos(i, temp2.value);
- }
- else
- System.out.println("Not a valid index number.");
- }
- }
- public void insertAtPos(int i, char value){//--------------------------------void insertAtPos int char
- Node temp = new Node(value,null);
- if(this.size()==0){
- head = temp;
- }
- else{
- if((i>this.size())||(i<0)){ //index out of list
- System.out.println("Not a valid index number.");
- }
- else if(i==this.size()){//insert at end
- getNode(i).next = temp;
- }
- else if(i==0){ //insert at beginning
- temp.next = getNode(i);
- head = temp;
- }
- else{ //insert inside list
- temp.next = getNode(i);
- getNode(i-1).next = temp;
- }
- }
- }
- public void insertAfter(int i, char value){//-------------------------------void insertAfter int char
- Node temp = new Node(value,null);
- if(this.size()==0){
- head = temp;
- }
- else{
- if((i>this.size())||(i<0)){ //index out of list
- System.out.println("Not a valid index number.");
- }
- else if(i==this.size()){//insert at end
- getNode(i).next = temp;
- }
- else{ //insert inside list
- temp.next = getNode(i+1);
- getNode(i).next = temp;
- }
- }
- }
- public void insertBefore(int i, char value){//--------------------------------void insertBefore int char
- Node temp = new Node(value,null);
- if(this.size()==0){
- System.out.println("No valid entries to insert before");
- }
- else{
- if((i>this.size())||(i<0)){ //index out of list
- System.out.println("Not a valid index number.");
- }
- else if(i==0){//insert at beginning
- temp.next = head;
- head = temp;
- }
- else{ //insert inside list
- Node temp3 = getNode(i-1);
- Node temp1 = getNode(i-2);
- temp.next = temp3;
- temp1.next = temp;
- }
- }
- }
- public MyList sublist(int fromIndex, int toIndex){//------------------MyList sublist int int
- MyList sublist = new MyList(null);
- if((fromIndex>=0)&&(fromIndex<=this.size())&&(toIndex>=0)&&(toIndex<=this.size())){
- for(int i=fromIndex; i<=toIndex; i++){
- sublist.pushBack(get(i));
- }
- }
- else
- System.out.println("Invalid index numbers provided.");
- return sublist;
- }
- public int find(char value){ //--------------------------------------------int find char
- Node temp = head;
- int i =0;
- while((temp.value!=value)&&(i<this.size())){
- i++;
- if(temp.next!=null)
- temp = temp.next;
- }
- if(temp.value == value)
- return i;
- else{
- System.out.println("Value '"+value+"' not found.");
- return -1;
- }
- }
- public boolean findAll(char value){ //--------------------------------------------boolean find char
- Node temp = head;
- int i =0;
- boolean j=true;
- while(temp.value!=value){
- i++;
- temp = temp.next;
- if(i==this.size()){
- j=false;
- return j;
- }
- }
- return j;
- }
- public int find(MyList queryStr){return 0;/*remove later*/}
- public char[] toArray(){//------------------------------------------------char[] toArray
- char[] tArray;
- tArray = new char[this.size()];
- if(head != null){
- for(int i=0; i<this.size(); i++){
- tArray[i] = getNode(i).value;
- }
- }
- else
- System.out.println("No valid entries.");
- return tArray;
- }
- public void reverse(){//------------------------------------------------------------void reverse
- Node temp1 = null;
- Node temp2=head;
- while(temp2!=null){
- Node temp3 = temp2.next;
- temp2.next = temp1;
- temp1 = temp2;
- temp2 = temp3;
- }
- head = temp1;
- }
- public int size(){//------------------------------------------------------------------int size
- int i=0;
- if(head!=null){
- //i++;
- Node temp = head;
- while(temp!=null){
- temp = temp.next;
- i++;
- }
- }
- return i;
- }
- public void print(){//-------------------------------------------------------------void print
- for(int i=0; i<this.size() ; i++){
- System.out.println(getNode(i).value);
- }
- }
- public String toString(){//------------------------------------------------------string toString
- StringBuilder string = new StringBuilder(this.size());
- for(char c: this.toArray())
- string.append(c);
- return string.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement