Download presentation
Presentation is loading. Please wait.
1
Without alpha_list
2
struct node* next_alpha; }; struct node* head=null;
$ asn4 tail head null struct node { char word[20]; struct node* next; struct node* next_alpha; }; struct node* head=null; struct node* tail=null; node: word next next_alpha
3
asn4> add input word:dog
tail head dog null null struct node* newnode; newnode = (struct node*)malloc(sizeof(node)); strcpy(newnode->word, “dog”); head = tail = newnode;
4
asn4> add input word:cat
tail head dog next cat null null null newnode = (struct node*)malloc(sizeof(node)); strcpy(newnode->word, “cat”); struct node* curr = head; while(curr->next) curr = curr->next; curr->next = newnode; tail = newnode;
5
asn4> add input word:rabbit
tail head dog next cat next rabbit null null null null newnode = (struct node*)malloc(sizeof(node)); strcpy(newnode->word, “rabbit”); struct node* curr = head; while(curr->next) curr = curr->next; curr->next = newnode; tail = newnode;
6
asn4> insert input word:turtle
tail head turtle next dog next cat next rabbit null null null null null newnode = (struct node*)malloc(sizeof(node)); strcpy(newnode->word, “turtle”); newnode->next = head; head = newnode;
7
asn4> modify old:dog new:sniffer
tail head turtle next sniffer next cat next rabbit null null null null null struct node* curr=head; while(curr) if(strcmp(curr->word, oldword)==0) { strcpy(curr->word, neword); break; }
8
struct node* tmp = head; head = head->next; free(tmp);
asn4> delhead tail head sniffer next cat next rabbit null null null null struct node* tmp = head; head = head->next; free(tmp);
9
With alpha_list
10
struct node* next_alpha; }; struct node* head=null;
$ asn4 tail head alpha_head null struct node { char word[20]; struct node* next; struct node* next_alpha; }; struct node* head=null; struct node* tail=null; struct node* alpha_head=null; node: word next next_alpha
11
asn4> add input word:dog
tail head alpha_head dog null null struct node* newnode; newnode = (struct node*)malloc(sizeof(node)); strcpy(newnode->word, “dog”); head = tail = alpha_head = newnode;
12
asn4> add input word:cat
tail head alpha_head dog next cat null null next_alpha newnode = (struct node*)malloc(sizeof(node)); strcpy(newnode->word, “cat”); struct node* curr = alpha_head; If(strcmp(curr->word, newnode->word) > 0) { newnode->next_alpha = curr; alpha_head = newnode;} else { while(strcmp(curr->next_alpha->word,newnode->word)<0) curr=curr->next_alpha; newnode->next_alpha = curr->next_alpha; curr->next_alpha = newnode; }
13
asn4> add input word:rabbit
tail head alpha_head dog next cat next rabbit null null next_alpha next_alpha
14
asn4> insert input word:turtle
tail head alpha_head turtle next dog next cat next rabbit null null next_alpha next_alpha next_alpha
15
asn4> modify old:dog new:sniffer
tail head alpha_head turtle next sniffer next cat next rabbit null null next_alpha next_alpha next_alpha HINT: Delete first and then insert again into alpha_list. curr = alpha_head; if(curr == modifiednode) { alpha_head = curr->next; } else { while(curr->next != modifiednode) curr=curr->next; curr->next = curr->next->next; } Add modified node into the alpha_list again;
16
If(curr == nodetodelete) { alpha_head = curr->alpha_next; else {
asn4> delhead tail head alpha_head sniffer next cat next rabbit null null next_alpha next_alpha curr = alpha_head; If(curr == nodetodelete) { alpha_head = curr->alpha_next; else { while(curr->alpha_next != nodetodelete) curr=curr->alpha_next; curr->alpha_next = curr->alpha_next->alpha_next; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.