Sie sind auf Seite 1von 4

import java.io.

*; class Node{ int data; Node next; } class CSLL{ Node first; int size; public boolean isEmpty(){ return size == 0; } public int size(){ return size; } public void checkIndex(int index){ if(index < 0 index >= size) throw new IndexOutOfBoundsException("index = "+index+" s ize = "+size); } public int indexOf(int x){ Node cur = first; int index = 0; while(cur.next != first && cur.data != x){ cur = cur.next; index++; } if(cur == first && cur.data != x) return -1; else return index; } public int get(int index){ checkIndex(index); Node cur = first; for(int i = 0; i < index; i++) cur = cur.next; return cur.data; } public int remove(int index){ //working changed checkIndex(index); int removed; Node cur; if (index == 0){ cur = first; removed = first.data; first = first.next; for(int j = 0; j < size-1; j++) cur = cur.next; cur.next = first; }

else{ cur = first; for( int i = 0; i < index - 1; i++) cur = cur.next; // see Note1 removed = cur.next.data; if(cur.next.next == null) //see note2 cur.next = null; else cur.next = cur.next.next; } size--; return removed; }

public void add(int index, int theData){ checkIndex(index); Node cur; Node newNode = new Node(); newNode.data = theData; if(index == 0){ cur = first; newNode.next = first; first = newNode; for(int i = 0; i < size-1; i++) cur = cur.next; cur.next = first; } else{ cur = first; for(int i = 0; i < index - 1; i++) cur = cur.next; newNode.next = cur.next; cur.next = newNode; } size++; } public String toString(){ StringBuffer s = new StringBuffer("["); Node cur = first; do{ if(cur.data == 0) s.append("0, "); else s.append(cur.data+", "); cur = cur.next; }while(cur != first); if(size > 0) s.delete(s.length()-2, s.length()); s.append("]"); return new String(s); } public void create(int n){ Node cur = null; size = n; for(int i=1; i<=n; i++){

if(i==1) cur = first = new Node(); else{ cur.next = new Node(); cur = cur.next; } DataInputStream rd = new DataInputStream(System.in); try{ System.out.print("Enter data "); cur.data = Integer.parseInt(rd.readLine()); }catch(IOException ex){} } cur.next = first; } public void traverse(){ Node cur = first; System.out.println("Contents of Linked List are"); do{ System.out.print(""+cur.data+"\t"); cur = cur.next; }while(cur != first); } public int search(int key){ Node cur = first; while(cur.data != key && cur.next != first){ cur = cur.next; } if(cur.data == key) return indexOf(key); else return -1; } public CSLL merge(CSLL L){ Node cur = first; while(cur.next != first) cur = cur.next; cur.next = L.first; cur = cur.next; while(cur.next != L.first) cur = cur.next; cur.next = first; size = size + L.size; return this; } public void reverse(){ if(first == null) return ; Node prev = first; Node cur = first.next; Node next = new Node(); Node last = first; for(int i =0; i < size-1; i++) last = last.next; first.next = last; while(cur != first){ next = cur.next;

cur.next = prev; prev = cur; cur = next; } first = prev; } }

Das könnte Ihnen auch gefallen