Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eg.edu.alexu.csd.datastructure.linkedList.cs89;
- import eg.edu.alexu.csd.datastructure.linkedList.ILinkedList;
- public class LinkedlistDoubly implements ILinkedList{
- public Link head = null;
- public int size=0;
- @Override
- public void add(int index, Object element) {
- if(index < 0 || index > size || element.equals(null)) throw new RuntimeException("unfound 1");
- Link NOW = head ;
- int count=0;
- while(count < index-1 ){
- NOW = NOW.next;
- count++;
- }
- if( index ==0 ){
- if(head == null){
- Link addone = new Link(null, element ,null);
- head = addone ;
- }
- else{
- Link addone = new Link(null, element ,NOW);
- head = addone ;
- }
- }
- else{
- Link addone = new Link(NOW, element ,NOW.next);
- NOW.next = addone ;
- }
- size++;
- // TODO Auto-generated method stub
- }
- @Override
- public void add(Object element) {
- if (element.equals(null))throw new RuntimeException("unfound 1");
- else if( head == null){
- Link end = new Link (null,element ,null);
- head= end;
- }
- else{
- Link NOW = head ;
- for(int counter =0 ; NOW.next != null ; counter++){
- NOW= NOW.next;
- }
- Link end = new Link (NOW,element ,null);
- NOW.next= end;
- }
- size++;
- // TODO Auto-generated method stub
- }
- @Override
- public Object get(int index) {
- if(index<0 || index >= size) throw new RuntimeException("unfound 2");
- if( head == null) throw new RuntimeException("unfound 3");
- Link NOW = head ;
- for(int counter =0 ; counter < index ; counter++){
- NOW= NOW.next;
- }
- return NOW.data;
- // TODO Auto-generated method stub
- }
- @Override
- public void set(int index, Object element) {
- if(index<0 || index >= size || element.equals(null) ) throw new RuntimeException("unfound 4 ") ;
- if( head == null) throw new RuntimeException("unfound 5 ") ;
- Link NOW = head ;
- for(int counter =0 ; counter < index ; counter++){
- NOW= NOW.next;
- }
- NOW.data = element;
- // TODO Auto-generated method stub
- }
- @Override
- public void clear() {
- if(head != null){
- head=null;
- }
- size=0;
- // TODO Auto-generated method stub
- }
- @Override
- public boolean isEmpty() {
- if (head==null) return true;
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public void remove(int index) {
- if(index < 0 || index >= size) throw new RuntimeException("unfound 6");;
- if( head == null || size < 0) throw new RuntimeException("unfound 7") ;
- Link NOW = head ;
- if(index == 0){
- head = NOW.next;
- NOW.next = null;
- head.previous = null;
- }
- else{
- for(int count = 1; count < index; count++){
- NOW = NOW.next;
- }
- Link second = new Link();
- Link third = new Link();
- second = NOW.next;
- if(second.next == null){
- NOW.next = null;
- second.previous = null;
- }
- else{
- NOW.next = second.next;
- third = second.next;
- third .previous = NOW;
- }
- }
- /*
- for(int counter =0 ; counter < index ; counter++){
- NOW= NOW.next;
- }
- if (size == 1 && index == 0){
- head = null;
- }
- else if (index == 0 && NOW.next != null ){
- head = NOW.next;
- NOW.next.previous=null;
- }
- else if (NOW.next== null){
- NOW.previous.next= null;
- }
- else{
- /*NOW.next = NOW.next.next;
- NOW.next.next = null;
- NOW.next.previous = null;
- NOW.next.next.previous = NOW;
- */
- /*NOW.previous.next= NOW.next;
- NOW.next.previous= NOW.previous;*/
- size--;
- // TODO Auto-generated method stub
- }
- @Override
- public int size() {
- //if( head==null) return 0;
- /*Link NOW = head ;
- for(int counter =0 ; NOW.next != null ; counter++){
- size++;
- NOW= NOW.next;
- }
- // TODO Auto-generated method stub*/
- return size;
- }
- @Override
- public ILinkedList sublist(int fromIndex, int toIndex) {
- if(fromIndex <0 || fromIndex>= size || (fromIndex > toIndex )) throw new RuntimeException("unfound 8");
- if(toIndex <0 || toIndex>= size) throw new RuntimeException("unfound 9");
- if( head == null) throw new RuntimeException("unfound 10");
- Link NOW = head ;
- for(int counter =0 ; counter <fromIndex ; counter++){
- NOW= NOW.next;
- }
- ILinkedList sub = new LinkedlistDoubly();
- for( int count = fromIndex ; count<= toIndex; count++ ){
- sub.add(NOW.data);
- NOW=NOW.next;
- }
- /*if( NOW.next != null && counter < toIndex)
- {
- }*/
- // TODO Auto-generated method stub
- return sub;
- }
- @Override
- public boolean contains(Object o) {
- Link NOW = head ;
- if( o.equals(null))throw new RuntimeException("unfound 1111");
- for(int counter =0 ; NOW != null ; counter++){
- if( NOW.data.equals( o)){
- return true;
- }
- NOW= NOW.next;
- }
- // TODO Auto-generated method stub
- return false;
- }
- public void print (){
- Link NOW = head ;
- while( NOW != null ){
- System.out.println(NOW.data);
- NOW =NOW.next;
- }
- System.out.println("#"+size);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement