המצגת נטענת. אנא המתן

המצגת נטענת. אנא המתן

Engineering Programming A

מצגות קשורות


מצגת בנושא: "Engineering Programming A"— תמליל מצגת:

1 Engineering Programming A
תרגול 10 רשימות מקושרות - המשך Introduction to C - Fall Amir Menczel

2 תרגיל 1 הפונקציה הבאה מחפשת את אמצע הרשימה וחוצה אותה לשתי רשימות. הפונקציה מחזירה את המצביע לחצי השני של הרשימה. השלם את הקטעים החסרים. Node* middle(Node* source){ Node *fast, *slow, *source2; if (source==NULL || ?? 1 ?? ) return NULL; slow = source; fast = ?? 2 ?? ; while (?? 3 ??) { fast = fast->next; if ( ?? 4 ?? ) { slow = slow->next; } source2 = slow->next; slow->next = ?? 5 ?? ; return ?? 6 ?? ; typedef struct node{ int value; struct node* next; }Node;

3 תרגיל 1 - פיתרון Node* middle(Node* source){
Node *fast, *slow, *source2; if (source==NULL || !source->next) return NULL; slow = source; fast = source->next; while ( fast->next ){ fast = fast->next; if ( fast->next ) { slow = slow->next; } source2 = slow->next; slow->next = NULL; return source2;

4 תרגיל 2 void single(item *head){ typedef struct item{ int value;
הפונקציה single מקבלת עוגן של רשימה משורשרת רגילה head ומשחררת מהרשימה את כל המבנים שהערך בהם הופיע כבר קודם ברשימה. (הפונקציה משאירה כל ערך פעם אחת ברשימה). השלם את 7 הקטעים המסומנים ב- ?? ?? כך שהפונקציה תבצע את הנדרש. void single(item *head){ item *prev, *temp; while( head ){ prev = head; temp = head -> next; while( temp ) if ( ?? 1 ?? ){ ?? 2 ?? = temp -> next; free( ?? 3 ?? ); temp = ?? 4 ?? ; } else } prev = ?? 5 ?? ; temp = ?? 6 ?? ; ?? 7 ?? ; typedef struct item{ int value; struct item *next; }item;

5 תרגיל 2 – פיתרון void single(item *head){ item *prev, *temp;
while( head ){ prev = head; temp = head -> next; while( temp ) if ( head->value == temp->value ){ prev->next = temp -> next; free( temp ); temp = prev->next; } else } prev = temp; temp = temp->next; head=head->next;

6 תרגיל 3 (מבחן – 20 נק') typedef struct node{ int value; node *next; } node; ממשו פונקציה int countDifItems(node *list) המקבלת כארגומנט מצביע לרשימה מקושרת ומחזירה את מספר הערכים השונים (value) שברשימה. לדוגמה, עבור הרשימה הבאה: head 22 11 12 11 22 NULL countDifItems(head) שווה 3.

7 תרגיל 3 (פתרון) int countDifItems(node *list) { node *temp;
int found, count = 0; while (list){ temp = list->next; found = 0; while(temp && !found){ if(temp->value == list->value) found = 1; temp = temp->next; } if(!found) count++; list = list->next; return count;

8 תרגיל 4 רשימות הפונקציה void reduce(item *head) שמקבלת עוגן של רשימה משורשרת של מבנים מטיפוס item ומצמצמת את הרשימה כך שמכל רצף של אותו מספר נשאר רק מבנה אחד עם המספר עצמו ומספר המופעים (occur) של המספר ברצף (שאר המבנים באותו רצף משתחררים). typedef struct item{ int number; //המספר int occur; // מספר המופעים struct item *next; }item;

9 תרגיל 4 רשימות השלימו בדפי התשובות את הקטעים החסרים. void reduce(item *head){ item* temp; int count = ?? 1 ??; if ?? 2 ?? ; while(?? 3 ??) if( ?? 4 ?? ){ ?? 5 ??; ?? 6 ??; ?? 7 ?? ; count++; } else{ head->occur = count; ?? 8 ?? ; ?? 9 ?? ; ?? 10 ?? ;

10 פתרון תרגיל 4 void reduce(item *head){ item* temp; int count = 1; if(!head) return; while(head->next) if(head->number == head->next->number){ temp = head->next; head->next = temp->next; free(temp); count++; } else{ head->occur = count; head = head ->next; count = 1;

11 תרגיל 5 ממשו פונקציה void iterReverse(node **head)
כל איבר ברשימה מוגדר באופן הבא: typedef struct node{ int val; struct node *next; }Node;

12 תרגיל 5 - פיתרון void iterReverse(Node** head){ while ( curNode ){
Node* curNode = *head; Node* nextNode = NULL; Node* prevNode = NULL; while ( curNode ){ *head = curNode; nextNode = curNode->next; curNode->next = prevNode; prevNode = curNode; curNode = nextNode; } {

13 תרגיל 6 - רשימה דו כיוונית (שאלת מבחן)
רשימה מקושרת ממוינת ודו-כיוונית של איברים מוגדרת על פי ההגדרה הבאה: struct item { int value; int howManyAfter; Item* next; Item* prev; } Item;

14 רשימה דו כיוונית (שאלת מבחן - המשך)
השדה value מציין את ערך האיבר, השדה howManyAfter מציין כמה איברים קיימים ברשימה אחרי האיבר, השדה next מצביע לאיבר הבא ברשימה והשדה prev מצביע לאיבר הקודם ברשימה. הרשימה ממוינת לפי השדה value בסדר עולה. הפונקציהitem* addItem(item *lst, int newVal) יוצרת ומוסיפה לרשימה הממויינת שראשה lst איבר חדש שערכו newVal למיקום הנכון ברשימה, ומעדכנת את כל השדות הרלוונטיים בכל הרשימה.

15 Item* addItem(Item *lst, int newVal) {
Item *prev, *newHead=lst, *newItem; newItem = ?? 1 ?? ; newItem->value = newVal; for (prev=NULL; ?? 2 ?? && newVal>lst->value; ?? 3 ?? ,lst=lst->next); newItem->next = lst; newItem->prev = prev; if (lst) { lst->prev = newItem; newItem->howManyAfter = lst->howManyAfter + 1; } else ?? 4 ??; if ( ?? 5 ?? ) ?? 6 ??; else newHead = newItem; while(prev) { ?? 7 ??; ?? 8 ??; } return newHead;

16 תרגיל 5 - פיתרון Item* addItem(Item *lst, int newVal) {
Item *prev, *newHead=lst, *newItem; newItem = (item*)malloc(sizeof(item)); newItem->value = newVal; for (prev=NULL; lst && newVal>lst->value; prev=lst ,lst=lst->next); newItem->next = lst; newItem->prev = prev; if (lst) { lst->prev = newItem; newItem->howManyAfter = lst->howManyAfter + 1; } else newItem->howManyAfter=0; if (prev) prev->next=newItem; else newHead = newItem; תרגיל 5 - פיתרון שימו לב שעבור השלמה 7 תיתכן גם התשובה: prev->howManyAfter++; while(prev) { prev->howManyAfter=prev->next->howManyAfter+1; prev=prev->prev; } return newHead;


הורד את "ppt "Engineering Programming A

מצגות קשורות


מודעות Google