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, *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; // else connecting to previous element i++; // count number of elements added current=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 and stores input on a 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) { 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

9 Searching by the name attribute Searching the List by Name ECE 175 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; } 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 Good for sorting Swapping two Elements in the List ECE 175 listp x y 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; }

12 Sorting the List By Attribute ECE 175 void sort_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 }

13 Adding a new Element at the End of the List ECE 175 nameagelistpnameagelistp headp NULL nameagelistp void add_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 }

14 Deleting a member ECE 175 nameagelistp nameagelistp nameagelistp headp NULL

15 Deleting a member ECE 175 void delete_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 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 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 }

16 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 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, &current); // 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