לוגו אתר Fresh          
 
 
  אפשרות תפריט  ראשי     אפשרות תפריט  צ'אט     אפשרות תפריט  מבזקים     אפשרות תפריט  צור קשר     חץ שמאלה ‎print ‎"Hello World!"; if‎ ‎not rules.‎know ‎then rules.‎read(); חץ ימינה  

לך אחורה   לובי הפורומים > מחשבים > תכנות ובניית אתרים
שמור לעצמך קישור לדף זה באתרי שמירת קישורים חברתיים
תגובה
 
כלי אשכול חפש באשכול זה



  #1  
ישן 22-07-2008, 09:21
  The_Equivocator The_Equivocator אינו מחובר  
 
חבר מתאריך: 11.02.04
הודעות: 16,543
מחלקה ב JAVA שעמלתי עליה לעבודה עם רשימות מקושרות- לשימושכם.

אשמח לתגובות...(הצעות ייעול)

קוד PHP:
 public class List {
    private 
Link head;
    private 
Link tail;
    private 
int length;//the length of the list.
        
    
List(){
        
length=0;
        
tail=new Link();
        
head=new Link();
    }
    
    public 
boolean isEmpty(){//is the list empty?(true/false)
        
if (head.next==null) return true;
        return 
false;
    }
    
    public 
int length(){//the length of the list(not including tail and the head.
        
return length;
    }
    
    public 
void addTail(Object object){//adds an object to the end of the list
        
length++;
        
Link tmp = new Link();
        
tmp.data=object;
        
        if (
head.next==null){//if list is yet empty
            
Link tmptail = new Link();
            
tail=tmptail;
            
tail.next=null;
            
tmp.next=tail;
            
tmp.prev=head;
            
tail.prev=tmp;
            
head.next=tmp;
        }
        else{
            
tmp.prev=tail.prev;
            
tmp.next=tail;
            
tail.prev.next=tmp;
            
tail.prev=tmp;
        }
    }
    
    public 
void addHead(Object object){////adds an object to the beginning of the list
        
length++;
        
Link tmp = new Link();
        
tmp.data=object;
        
        if (
head.next==null){//if list is yet empty
            
Link tmptail = new Link();
            
tail=tmptail;
            
tail.next=null;
            
tmp.next=tail;
            
tmp.prev=head;
            
head.next=tmp;
            
tail.prev=tmp;
        }
        else{
            
tmp.next=head.next;
            
tmp.prev=head;
            
head.next.prev=tmp;
            
head.next=tmp;                        
        }
    }
    
    public 
Object deleteIndex(int i){//delete an object at index i.
        
        
if (head.next==null i>length i<=0){ 
            throw new 
IllegalArgumentException("Error! no such index!");
            
//return
        
}
        
        
length--;
        
Object object;
    
        
Link tmp = new Link();
        
        
tmp=head;
        
int j=0;
        while (
i!=j){
            
tmp=tmp.next;
            
j++;
        }
        
object=tmp.data;
        
tmp.prev.next=tmp.next;
        
tmp.next.prev=tmp.prev;
        return 
object;
    }
    
    
    public 
void setDataInIndex(Object object,int i){//sets data at specific index in the list.
        
        
Link tmp = new Link();
        
        if ( (
head.next==null) | (i>length) | (i<=0) ){ 
            throw new 
IllegalArgumentException("Error! no such index!");
            
//return;
        
}
                        
        
tmp=head;
        
int j=0;
        while (
i!=j){
            
tmp=tmp.next;
            
j++;
        }
        
tmp.data=object;    
    }
    
    
    public 
Object getDataAtIndex(int i){ // gets data from index i in the list.
        
        
Link tmp = new Link();
        
        if ( (
head.next==null) | (i>length) | (i<=0) ){ 
            throw new 
IllegalArgumentException("Error! no such index!");
            
//return;
        
}
                        
        
tmp=head;
        
int j=0;
        while (
i!=j){
            
tmp=tmp.next;
            
j++;
        }
        return 
tmp.data;    
    }
    
    
    public 
int isPresent(Object object){ // if object is present in the list its index is being returned(the index of the first match!).
                                            //otherwise the function returns -1.
        
Link tmp = new Link();
        
        if (
head.next==null) return -1
                            
        
tmp=head.next;
        
int i=0;
        
        while (
tmp!=null){
            
i++;
            if (
tmp.data==object) return i;
            
tmp=tmp.next;
        }
        return -
1;    
    }
    
    
    public 
void joinList(List another){//joins two different lists.
        
if (this.isEmpty() | another.isEmpty()) return;
        
this.tail.prev.next=another.head.next;
        
this.tail=another.tail;
        
this.length=this.length+another.length;
    }
    
    
    public List 
copyList(){//returns a copy of *this* list. 
        
if (this.head.next==null) return new List();

        List 
nlist=new List();        
        
        for (
int i=1;i<=this.length;i++ ){
            
nlist.addTail(this.getDataAtIndex(i));
        }
        return 
nlist;
    }
        
    
//Class Link........
    
private class Link{
        private 
Link next;
        private 
Link prev;
        private 
Object data;
        
        
Link(){
            
next=null;
            
prev=null;
            
data=null;
        }
    }
//end Link.

}//end List. 

נערך לאחרונה ע"י The_Equivocator בתאריך 22-07-2008 בשעה 09:41.
תגובה ללא ציטוט תגובה עם ציטוט חזרה לפורום
  #2  
ישן 22-07-2008, 12:38
  The_Equivocator The_Equivocator אינו מחובר  
 
חבר מתאריך: 11.02.04
הודעות: 16,543
תוספת קטנה...
בתגובה להודעה מספר 1 שנכתבה על ידי The_Equivocator שמתחילה ב "מחלקה ב JAVA שעמלתי עליה לעבודה עם רשימות מקושרות- לשימושכם."

קוד PHP:
 public class List {
    private 
Link head;
    private 
Link tail;
    private 
int length;//the length of the list.
        
    
List(){
        
length=0;
        
tail=new Link();
        
head=new Link();
    }
    
    public 
boolean isEmpty(){//is the list empty?(true/false)
        
if (head.next==null) return true;
        return 
false;
    }
    
    public 
int length(){//the length of the list(not including tail and the head.
        
return length;
    }
    
    public 
void addTail(Object object){//adds an object to the end of the list
        
length++;
        
Link tmp = new Link();
        
tmp.data=object;
        
        if (
head.next==null){//if list is yet empty
            
Link tmptail = new Link();
            
tail=tmptail;
            
tail.next=null;
            
tmp.next=tail;
            
tmp.prev=head;
            
tail.prev=tmp;
            
head.next=tmp;
        }
        else{
            
tmp.prev=tail.prev;
            
tmp.next=tail;
            
tail.prev.next=tmp;
            
tail.prev=tmp;
        }
    }
    
    public 
void addHead(Object object){////adds an object to the beginning of the list
        
length++;
        
Link tmp = new Link();
        
tmp.data=object;
        
        if (
head.next==null){//if list is yet empty
            
Link tmptail = new Link();
            
tail=tmptail;
            
tail.next=null;
            
tmp.next=tail;
            
tmp.prev=head;
            
head.next=tmp;
            
tail.prev=tmp;
        }
        else{
            
tmp.next=head.next;
            
tmp.prev=head;
            
head.next.prev=tmp;
            
head.next=tmp;                        
        }
    }
    
    public 
void addToRightAtIndex(Object object,int i){//adds an object to the right at index i 
        
if ( (i>length  & !(length==i==1)) | i<=) {        
            throw new 
IllegalArgumentException("Error! Index out of range!");
            
//return
        
}
        
        if (
head.next==null) {                         
            
addHead(object);
            return;
        }
        
        if (
i==length) {
            
addTail(object);
            return;
        }
        
        
length++;
        
Link nlink = new Link();
        
nlink.data=object;
        
Link tmp = new Link();
        
tmp=head;
        
        for (
int j=1;j<=i;j++ ){
            
tmp=tmp.next;
        }
        
        
nlink.prev=tmp;
        
nlink.next=tmp.next;
        
tmp.next.prev=nlink;
        
tmp.next=nlink;
    }
    
    public 
Object deleteIndex(int i){//delete an object at index i.
        
        
if (head.next==null i>length i<=0){ 
            throw new 
IllegalArgumentException("Error! no such index!");
            
//return
        
}
        
        
length--;
        
Object object;
    
        
Link tmp = new Link();
        
        
tmp=head;
        
int j=0;
        while (
i!=j){
            
tmp=tmp.next;
            
j++;
        }
        
object=tmp.data;
        
tmp.prev.next=tmp.next;
        
tmp.next.prev=tmp.prev;
        return 
object;
    }
    
    
    public 
void setDataInIndex(Object object,int i){//sets data at specific index in the list.
        
        
Link tmp = new Link();
        
        if ( (
head.next==null) | (i>length) | (i<=0) ){ 
            throw new 
IllegalArgumentException("Error! no such index!");
            
//return;
        
}
                        
        
tmp=head;
        
int j=0;
        while (
i!=j){
            
tmp=tmp.next;
            
j++;
        }
        
tmp.data=object;    
    }
    
    
    public 
Object getDataAtIndex(int i){ // gets data from index i in the list.
        
        
Link tmp = new Link();
        
        if ( (
head.next==null) | (i>length) | (i<=0) ){ 
            throw new 
IllegalArgumentException("Error! no such index!");
            
//return;
        
}
                        
        
tmp=head;
        
int j=0;
        while (
i!=j){
            
tmp=tmp.next;
            
j++;
        }
        return 
tmp.data;    
    }
    
    
    public 
int isPresent(Object object){ // if object is present in the list its index is being returned(the index of the first match!).
                                            //otherwise the function returns -1.
        
Link tmp = new Link();
        
        if (
head.next==null) return -1
                            
        
tmp=head.next;
        
int i=0;
        
        while (
tmp!=null){
            
i++;
            if (
tmp.data==object) return i;
            
tmp=tmp.next;
        }
        return -
1;    
    }
    
    
    public 
void joinList(List another){//joins two different lists.
        
if (this.isEmpty() | another.isEmpty()) return;
        
this.tail.prev.next=another.head.next;
        
this.tail=another.tail;
        
this.length=this.length+another.length;
    }
    
    
    public List 
copyList(){//returns a copy of *this* list. 
        
if (this.head.next==null) return new List();

        List 
nlist=new List();        
        
        for (
int i=1;i<=this.length;i++ ){
            
nlist.addTail(this.getDataAtIndex(i));
        }
        return 
nlist;
    }
        
    
//Class Link........
    
private class Link{
        private 
Link next;
        private 
Link prev;
        private 
Object data;
        
        
Link(){
            
next=null;
            
prev=null;
            
data=null;
        }
    }
//end Link.

}//end List. 

נערך לאחרונה ע"י The_Equivocator בתאריך 22-07-2008 בשעה 12:41.
תגובה ללא ציטוט תגובה עם ציטוט חזרה לפורום
  #3  
ישן 22-07-2008, 15:25
צלמית המשתמש של המממ
  המממ המממ אינו מחובר  
 
חבר מתאריך: 30.10.01
הודעות: 8,699
Follow me...
אבל למה ?
בתגובה להודעה מספר 2 שנכתבה על ידי The_Equivocator שמתחילה ב "תוספת קטנה..."

למה לא להשתמש ב Collection הקיים של ג'אווה:
java.util.LinkedList



שים לב שהמימוש הבסיסי של Java (ונראה לי במבט ראשון שגם שלך) לא בטוח לשימוש בקוד multi-threaded
ויש צורך לבצע synchronize ידנית כשקוראים לו.
או כמו שSUN כותבים בקישור שלמעלה:

ציטוט:
Note that this implementation is not synchronized. If multiple threads access a list concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list:

List list = Collections.synchronizedList(new LinkedList(...));

לכן מומלץ להשתמש ב: Collections.synchronizedList


לסיום, רשימה מלאה של Collections תמצא כאן:
http://java.sun.com/j2se/1.4.2/docs.../reference.html
_____________________________________
_________________________________________________
תמונה שהועלתה על ידי גולש באתר ולכן אין אנו יכולים לדעת מה היא מכילה
אזהרה: משרד הבריאות קובע כי העישון מזיק לבריאות !
תראו, אפילו החייזר נהיה ירוק מזה

תגובה ללא ציטוט תגובה עם ציטוט חזרה לפורום
תגובה

כלי אשכול חפש באשכול זה
חפש באשכול זה:

חיפוש מתקדם
מצבי תצוגה דרג אשכול זה
דרג אשכול זה:

מזער את תיבת המידע אפשרויות משלוח הודעות
אתה לא יכול לפתוח אשכולות חדשים
אתה לא יכול להגיב לאשכולות
אתה לא יכול לצרף קבצים
אתה לא יכול לערוך את ההודעות שלך

קוד vB פעיל
קוד [IMG] פעיל
קוד HTML כבוי
מעבר לפורום



כל הזמנים המוצגים בדף זה הם לפי איזור זמן GMT +2. השעה כעת היא 15:38

הדף נוצר ב 0.62 שניות עם 12 שאילתות

הפורום מבוסס על vBulletin, גירסא 3.0.6
כל הזכויות לתוכנת הפורומים שמורות © 2024 - 2000 לחברת Jelsoft Enterprises.
כל הזכויות שמורות ל Fresh.co.il ©

צור קשר | תקנון האתר