160 likes | 243 Views
Computer Programming for Engineering Applications . ECE 175. Intro to Programming . Need for Truly Dynamic “ Arrays ”. Assume the following structure Consecutive executions of malloc reserve different parts of the memory Goal is to connect the different pieces of memory.
E N D
Computer Programming forEngineering Applications ECE 175 Intro to Programming
Need for Truly Dynamic “Arrays” Assume the following structure Consecutive executions of malloc reserve different parts of the memory Goal is to connect the different pieces of memory typedefstructnode_s { char name[20]; int age; structnode_s *listp; } node_t; name age name age name age ECE 175
Connecting Pieces of Memory – Linked Lists Linked Lists: non-sequential pieces of memory connected using pointers How are linked lists different than arrays? Memory cells are not sequential List size can be dynamic, no need to predefine it as with arrays One can add and delete nodes to the list ECE 175
Structures with Pointer attributes Declaration of structures with pointer attributes to themselves typedefstructnode_s { char name[20]; int age; structnode_s *listp; } node_t; name age listp ECE 175
Creation of a Linked List To create a linked list we need to Create a head pointer that points to the first element, so we can traverse the list from the start Make each element of the list point to the next one Make the last pointer point to NULL, so we can denote the end of the list headp name age listp name age listp name age listp NULL ECE 175
Creation of a Linked List headp name age listp name age listp name age listp int main(void) { int i=0; node_t *headp, *temp, *current=NULL, *lastp; FILE *inp; char c; char s1[20]; inp=fopen("database.dat", "r"); while(!feof(inp)) { temp=(node_t *)malloc(sizeof (node_t)); // creation of memory scan_fun(temp, inp); // initialization of element of list if (current==NULL) headp=temp; // setting the head of the list else current->listp=temp; // elseconnecting to previous element i++; // countnumber of elements added current=temp; // updating the current element temp->listp=NULL; // setting pointer to null. } NULL ECE 175
Scanning and Printing Functions Scanning elements from a file, and printing on the screen voidscan_fun(node_t *pt, FILE *in) { //scans file and stores input on a node_t structure fscanf(in, "%s%d", pt->name, &pt->age); } voidprint_fun(node_t *pt) { //prints a node_t structure printf("%s, %d\n", pt->name, pt->age); } void print_list(node_t *pt) { if (pt==NULL) printf("The list is empty\n"); else { while (pt!=NULL) { // traversing the list print_fun(pt); pt=pt->listp; } } } ECE 175
Traversing the List headp name age listp name age listp name age listp NULL ECE 175
Searching the List by Name Searching by the name attribute node_t *find_name(node_t *pt, char *query) { // finds the name query in the database // returns pointer to structure matched or null while(strcmp(pt->name, query)!=0 && pt!=NULL) { pt=pt->listp; } returnpt; } ECE 175
Searching the List by Age Searching by a range of ages voidfind_age(node_t *pt, int min, int max) { // finds elements within a range of ages while(pt!=NULL) { if(pt->age >= min && pt->age <=max) print_fun(pt); pt=pt->listp; } } ECE 175
Swapping two Elements in the List Good for sorting void swap(node_t *x, node_t *y) { // swaps two elements on the list node_t t; // temporary node strcpy(t.name, x->name); t.age=x->age; strcpy(x->name, y->name); x->age=y->age; strcpy(y->name,t.name); y->age=t.age; } x y listp listp listp listp listp ECE 175
Sorting the List By Attribute voidsort_list(node_t *pt) { // sorts the list alphabetically node_t *temp, *min; while (pt!=NULL) // while list is not sorted { temp=pt; // point temp to the first unsorted element min=pt; // make the first unsroted element the current minimum while(temp!=NULL) { if(strcmp(min->name, temp->name)>0) {// if new minimum is found min=temp; // update to the new min } temp=temp->listp; // inspect the next element } swap(pt, min); // swap the first element with min pt=pt->listp; // move to the next unsorted element } } ECE 175
Adding a new Element at the End of the List headp name age listp name age listp NULL name age listp voidadd_member(node_t **h, node_t **c) { // adds an element at the end of the list node_t *temp; temp=(node_t *)malloc(sizeof (node_t)); // memory allocation printf("Enter the age of the new member:"); scanf("%d",&temp->age); //scan for the age printf("Enter the name of the new member:"); scanf("%s", temp->name); // scan for the name if (*h==NULL) // if list is empty *h=temp; // point the head to temp else { (*c)->listp=temp; // point previous last element to temp } *c=temp; // update the current last element temp->listp=NULL; // point temp->listp to NULL } ECE 175
Deleting a member name age listp headp name age listp name age listp NULL ECE 175
Deleting a member voiddelete_member(node_t **h, node_t **c) { node_t *target; // pointer to string to be deleted node_t **temp=h; // pointer to the head of the list char s[20]; printf("Enter the name of the entryyouwant to delete:"); fflush(stdin); scanf("%s",s); target=find_name(*h, s); // finding the element to be deleted if (target==NULL) printf("This entry doesnotexist\n"); else { while ((*temp)!=target) { temp=&(*temp)->listp; // locating the address of the previous element } if (target==*h) // if first element must be deleted *h=target->listp; // make head point to the next element if (target==*c) // if last element is to be deleted *c=*temp; // update the position of the last element *temp=target->listp; // skip element to be deleted free(target); // free the memory } } ECE 175
Calling List Functions add_member(&headp, ¤t); // add a new member to the list print_list(headp); // print the list pause(); // wait for user input sort_list(headp); // sort the list print_list(headp); // print the list pause(); find_age(headp, 22, 28); //find elements with age between 22 and 28 pause(); delete_member(&headp, ¤t); // delete a member print_list(headp); // print the list ECE 175