Sunteți pe pagina 1din 5

#include <stdio.h> #include <stdlib.

h> typedef struct listnode *lnode; struct listnode { int data; lnode next; // some lists will have prev also }; void insert(lnode *head, int value) { // has a runtime of O( ) // let's always insert "before" the first item in the list // create new node lnode tmp = (lnode)malloc(sizeof(listnode)); tmp->data = value; tmp->next = *head; *head = tmp; } /* poor implementation of printlist, destroys list while printing void printlist(lnode *head) { while (*head != NULL){ printf("%d\t",(*head)->data); *head = (*head)-> next; } printf("\n"); } */ // this implementation works, using a local copy called head // runtime is O(n) // space usage is O(1) void printlist(lnode tmphead) { while (tmphead != NULL){ printf("%d\t",(tmphead)->data); tmphead = (tmphead)-> next; } printf("\n"); } // recursive printlist in order, because reverse is in project // recursive printlist with * on lnode to see if destroys original list.. // runtime is O(n) // space usage is O(n) - using up parts of stack for each ftn call void recprintlist(lnode tmphead) { // base case if (tmphead == NULL) return; else { printf(" %d ",tmphead->data ); recprintlist(tmphead->next); }

} lnode find2(lnode head, int value) { lnode tmp = head; while ((tmp!= NULL) && (tmp->data != value)) { tmp = tmp -> next; } return tmp; } lnode find3(lnode head, int value) { lnode tmp = head; while ((tmp->data != value)&&(tmp!= NULL) ) { tmp = tmp -> next; } return tmp; } //O(N) lnode find(lnode head, int value) { // going to search through list to find first occurence of value // return a reference (pointer) to the node // if value not found, return NULL lnode tmp = head; while (tmp != NULL) { if ((tmp->data)== value) return tmp; tmp = tmp->next; } return tmp; } //remove2_changed, uses reference to pointer that locates the beginning of list so we // update that value as necessary // returns reference to possibly modified list lnode remove2_changed (lnode head, int value) { // find the value first, then remove the value lnode tmp = find(head,value); if (tmp== NULL) return head; else { if(tmp == head) { head = tmp->next; free(tmp); return head; }

// how do we find previous node ? lnode tmp3 = head; while (tmp3->next != tmp) tmp3 = tmp3->next; // when while loop is done, tmp3 points to tmp tmp3->next = tmp->next; free (tmp); return head; } } //remove2, uses reference to pointer that locates the beginning of list so we // update that value as necessary // returns 1 if successful, returns 0 if item not in list int remove2 (lnode *head, int value) { // find the value first, then remove the value lnode tmp = find(*head,value); if (tmp== NULL) return 0; else { if(tmp == *head) { *head = tmp->next; free(tmp); return 1; } // how do we find previous node ? lnode tmp3 = *head; while (tmp3->next != tmp) tmp3 = tmp3->next; // when while loop is done, tmp3 points to tmp tmp3->next = tmp->next; free (tmp); return 1; } } //remove // O(N) // returns 1 if successful, returns 0 if item not in list int remove (lnode head, int value) { // find the value first, then remove the value lnode tmp = find(head,value); if (tmp== NULL) return 0; else // let's look for tail if (tmp->next == NULL ) // at the tail need to free this node, and updat e //previous next { // how do we find previous node ? lnode tmp3 = head; while (tmp3->next != tmp) tmp3 = tmp3->next; // when while loop is done, tmp3 points to tmp

tmp3->next = NULL; free (tmp); return 1; } // general case when not tail // we found the node and need to remove the value { while(tmp->next->next != NULL){ tmp->data = tmp->next->data; tmp = tmp->next; } tmp->data = tmp->next->data; lnode tmp2 = tmp->next; free (tmp2); tmp->next = NULL; return 1; } } //O(N^2) because of repeated calls to remove and list could contain same value n times void removeall(lnode head, int value) { while (remove(head, value)==1) { } } int main() { lnode head=NULL; // we could have create insert htis way // head = insert(head,45); insert(&head,45); // method to insert a node into our list printlist(head); // will print items in the list insert(&head,25); insert(&head,13); printlist(head); // will print items in the list insert(&head,35); insert(&head,5); printlist(head); lnode ref = find(head,13); printf("find %x: %d\n",ref,ref->data); ref = find(head,130); if (ref==NULL) printf("find item not found\n"); else printf("find %x: %d\n",ref,ref->data); ref = find2(head,13); printf("find2 %x: %d\n",ref,ref->data); ref = find2(head,130);

if (ref==NULL) printf("find2 item not found\n"); else printf("find2 %x: %d\n",ref,ref->data); /* ref = find3(head,13); printf("find3 %x: %d\n",ref,ref->data); ref = find3(head,130); if (ref==NULL) printf("find3 item not found\n"); else printf("find3 %x: %d\n",ref,ref->data); */ recprintlist(head); printf("calling remove2_changed\n"); head=remove2_changed(head,13); recprintlist(head); printf("calling remove2_changed on head\n"); head=remove2_changed(head,5); // check if we remove head value printf("returned from remove2_changed on head node value\n"); recprintlist(head); printf("calling remove2_changed on tail\n"); head=remove2_changed(head,45); // remove value in tail recprintlist(head); head=remove2_changed(head,104); // what happens if value not there recprintlist(head); /* printf("Adding nodes with same value for testing removeall\n"); insert(&head,10); insert(&head,10); insert(&head,10); insert(&head,105); printlist(head); printf("\n"); removeall(head,10); printlist(head); printf("\n"); removeall(head,105); printlist(head); printf("\n"); */ }

S-ar putea să vă placă și