Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming for Engineering Applications ECE 175 Intro to Programming.

Similar presentations


Presentation on theme: "Computer Programming for Engineering Applications ECE 175 Intro to Programming."— Presentation transcript:

1 Computer Programming for Engineering Applications ECE 175 Intro to Programming

2 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 ECE 175 nameage nameage nameage typedef struct node_s { char name[20]; int age; struct node_s *listp; } node_t;

3 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 Connecting Pieces of Memory – Linked Lists ECE 175

4 Declaration of structures with pointer attributes to themselves Structures with Pointer attributes ECE 175 nameagelistp typedef struct node_s { char name[20]; int age; struct node_s *listp; } node_t;

5 Creation of a Linked List ECE 175 nameagelistp nameagelistp nameagelistp headp NULL 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

6 Creation of a Linked List ECE 175 nameagelistp nameagelistp nameagelistp headp NULL int main(void) { int i = 0; node_t *headp, *temp, *last=NULL; FILE *inp = fopen("database.txt", "r"); char c; char s1[20]; while(!feof(inp)) { temp = (node_t *)malloc(sizeof (node_t)); // creation of memory scan_fun(temp, inp); // initialization of element of list if (last == NULL) headp = temp; // setting the head of the list else last->listp=temp; // else connecting to previous element i++; // count number of elements added last = temp; // updating the current element temp->listp = NULL; // setting pointer to null. }

7 Scanning and Printing Functions Scanning elements from a file, and printing on the screen ECE 175 void scan_fun(node_t *pt, FILE *in) //scans file for node_t structure { fscanf(in, "%s%d", pt->name, &pt->age); } void print_fun(node_t *pt) //prints a node_t structure { printf("%s, %d\n", pt->name, pt->age); } void print_list(node_t *pt) //prints the entire list { if (pt == NULL) printf("The list is empty\n"); else { while (pt != NULL) // traversing the list { print_fun(pt); pt = pt->listp; }

8 Traversing the List ECE 175 nameagelistp nameagelistp nameagelistp headp NULL while (pt != NULL) // traversing the list { print_fun(pt); pt = pt->listp; // point to next element }

9 Searching by the name attribute Searching the List by Name ECE 175 node_t *find_name(node_t *pt, char *query) { // finds a name in the list // returns pointer to structure matched or null while(strcmp(pt->name, query) != 0 && pt! = NULL) { pt=pt->listp; } return pt; }

10 Searching by a range of ages Searching the List by Age ECE 175 void find_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; }

11 Adding a new Element at the End of the List ECE 175 nameagelistpnameagelistp headp NULL nameagelistp void add_member(node_t **h, node_t **l) // adds a list element { 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); if (*h == NULL) // if list is empty *h = temp; // point the head to temp else { (*l)->listp = temp; // point previous last element to temp } *l = temp; // update the current last element temp->listp=NULL; // point temp->listp to NULL }

12 Deleting a member ECE 175 nameagelistp nameagelistp nameagelistp headp NULL

13 Deleting a member ECE 175 void delete_member(node_t **h, node_t **l) { 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 entry you want to delete:"); fflush(stdin); scanf("%s", s); target = find_name(*h, s); // finding the element to be deleted if (target == NULL) printf("This entry does not exist\n"); else { while ((*temp) != target) { temp = &(*temp)->listp; // locating the address of previous element } if (target == *h) // if first element must be deleted *h = target->listp; // make head point to the next element if (target == *l) // if last element is to be deleted *l = *temp; // update the position of the last element *temp = target->listp; // skip element to be deleted free(target); // free the memory }

14 Calling List Functions ECE 175 add_member(&headp, &current); // add a new member to the list print_list(headp); // print the list pause(); // wait for user input print_list(headp); // print the list pause(); find_age(headp, 22, 28); //find elements with age between 22 and 28 pause(); delete_member(&headp, &last); // delete a member print_list(headp); // print the list


Download ppt "Computer Programming for Engineering Applications ECE 175 Intro to Programming."

Similar presentations


Ads by Google