Presentation is loading. Please wait.

Presentation is loading. Please wait.

רשימות. רשימה משורשרת typedef struct link{ int data; struct link * next; }link;

Similar presentations


Presentation on theme: "רשימות. רשימה משורשרת typedef struct link{ int data; struct link * next; }link;"— Presentation transcript:

1 רשימות

2 רשימה משורשרת typedef struct link{ int data; struct link * next; }link;

3 סוגי הכנסה לרשימה

4 העומד בתור התאורטי void insertLast(link * head, link * newLink){ while (head->next!=NULL) head=head->next; head->next=newLink; newLink->next=NULL; }

5 "אני אחריך" void insertAfter (link *where, link *newLink){ newLink->next=where->next; where->next=newLink; }

6 "אני לפניך" link * insertBefore (link *head, link *where, link *newLink){ if (head==where){ newLink->next=head; return newLink; } while (head->next!=where) head=head->next; newLink->next=head->next; head->next=newLink; return head; }

7 "אני רק שאלה" link * insertFirst (link *head,link *newLink){ newLink->next=head; return newLink; }

8 פונקציות עזר void printList(link *head){ while (head){ printf("%d -> ", head->data); head=head->next; } printf("\n"); } link * createLink(int data, link * next){ link * l=(link *)malloc(sizeof (link)); if (l){ l->data=data; l->next=next; } return l; }

9 main void main(){ link * head; head=createLink(5,NULL); printList(head); head=insertFirst(head, createLink(7,NULL)); printList(head); insertLast(head,createLink(9,NULL)); printList(head); head=insertBefore(head,head->next,createLink(3,NULL)); printList(head); insertAfter(head->next->next,createLink(4,NULL)); printList(head); }

10 סוגי הוצאה מרשימה

11 "יש כרטיס מועדון??" link * removeFirst(link * head){ link *temp=head; head=head->next; free(temp); return head; }

12 משפרי עמדה link * removeLast(link * head){ link * temp=head; if (head->next==NULL){ free(head); return NULL; } while (head->next->next != NULL) head=head->next; free(head->next); head->next=NULL; return temp; }

13 "שכחתי לקנות במבה" link * removeLink(link *head, link *toRemove){ link * temp=head; if (head==toRemove) return removeFirst(head); while (head->next != toRemove) head=head->next; head->next=head->next->next; free(toRemove); return temp; }

14 הפיכת פונקציה ל void link * insertFirst(link *head, link *newLink){ newLink->next=head; return newLink; } void insertFirst(link **head, link *newLink){ newLink->next=*head; *head=newLink; }

15 הסתכלות רקורסיבית int findMax(link * head){ int restMax; if (head==NULL) return 0; restMax = findMax(head->next); if (restMax > head->data) return restMax; return head->data; }

16 רשימה דו כוונית

17 הגדרת המבנה typedef struct dlink{ int data; struct dlink * next; struct dlink * prev; }dlink;

18 הוספת חוליה void insertAfter1 (dlink *where, dlink *newLink){ newLink->next=where->next; newLink->prev=where; if (where->next!=NULL){ where->next->prev=newLink; } where->next=newLink; }

19 מחיקת חוליה dlink *removeLink1(dlink *head,dlink *toRemove){ if (toRemove->next != NULL) toRemove->next->prev = toRemove->prev; if (toRemove->prev != NULL) toRemove->prev->next = toRemove->next; else head=toRemove->next; free(toRemove); return head; }


Download ppt "רשימות. רשימה משורשרת typedef struct link{ int data; struct link * next; }link;"

Similar presentations


Ads by Google