1 / 22

Turgay Korkmaz Office: NPB 3.330 Phone: (210) 458-7346 Fax: (210) 458-4437

Do Quiz 12, 12lib. CS 2123 Data Structures Ch 9a – Single Linked List and Abstract Data Types (ADT). Turgay Korkmaz Office: NPB 3.330 Phone: (210) 458-7346 Fax: (210) 458-4437 e-mail: korkmaz@cs.utsa.edu web: www.cs.utsa.edu/~korkmaz. Objectives.

margeryr
Download Presentation

Turgay Korkmaz Office: NPB 3.330 Phone: (210) 458-7346 Fax: (210) 458-4437

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Do Quiz 12, 12lib CS 2123 Data Structures Ch 9a – Single Linked List and Abstract Data Types (ADT) Turgay Korkmaz Office: NPB 3.330 Phone: (210) 458-7346 Fax: (210) 458-4437 e-mail: korkmaz@cs.utsa.edu web: www.cs.utsa.edu/~korkmaz

  2. Objectives • To understand the concept of a linked list and how it can be used to provide a basis for efficient insertion and deletion operations. • To implement linked list as ADT

  3. Linked List Concept NULL

  4. Linked List Concept (cont’d) • How can we define cell structure in C? typedefstruct { char ch; cellT *link; } cellT; • What can we do? typedefstructcell { char ch; struct cell *link; } cellT; typedefstruct cell cellT; … struct cell { char ch; cellT *link; }

  5. Quiz 12 Single Linked List – Basics typedefstruct point { int x; struct point *next; } myDataT; myDataT *a, *b; a = (myDataT *) malloc(sizeof(myDataT)); // if NULL a->x = 5; • a->next = (myDataT *) malloc(sizeof(myDataT)); // if NULL a->next->x = 8; • a->next->next =(myDataT *)malloc(sizeof(myDataT));// if NULL a->next->next->x = 10; a->next->next->next = NULL; • typedefstruct point { • int x; • struct point *next; • } *myDataTptr; • myDataTptr a, b;

  6. Quiz 12 5 8 10 NULL Linked List (cont’d) • Show how the linked list conceptually looks like after the above code. a

  7. typedefstruct point { • int x; • struct point *next; • } myDataT; • Quiz 12 Linked List (Display) • Write a function Display(myDataT *h) that can print the values stored in the list h. void Display(myDataT *h) { while(h) { printf("%d ", h->x); h = h->next; } } int Sum(myDataT *h) { }

  8. Quiz 12 5 8 10 9 NULL Linked List (insert front) • Now suppose we create a new element pointed by b as follows b=(myDataT *)malloc(sizeof(myDataT)); b->x = 9; b->next=NULL; • How can you add this new cell to the beginning of the linked list? b a • b->next = a; • a = b;

  9. Linked List (insert ordered) • But, the values in the linked list were sorted. a5810 • How can you add the new cell with 9 to the right position in the sorted list? • Your solution should be general enough to work with any sorted list! • New value could be 3, 9, 12…

  10. Quiz 12 Linked List (insert ordered) • Solution strategy • first search and find the right position for the new element, and • then insert the new element into the linked list at that position • If needed, declare new temp pointers

  11. Quiz 12 5 8 10 9 NULL • typedefstruct point { • int x; • struct point *next; • } myDataT; a NULL prev prev curr curr b myDataT *prev, *curr; prev= NULL; curr = a; while(curr) { if (curr->x >= b->x ) break; prev = curr; curr = curr->next; } if(prev == NULL) { b->next = a; a = b; } else{ // middle or end b->next = prev->next; prev->next = b; } How about inserting 3 or 20? If b->x is 3, then prev=NULL, curr points the first If b->x is 20, then prev points to the last, curr=NULL • b->next = a; • a = b; • b->next = prev->next; • prev->next = b;

  12. Quiz 12 Linked List (insert function) • Instead of the previous code segment, write a function that can dynamically allocate memory for a given number and inserts it into the link list in a sorted manner… • How will you call it in the main program? insert1_sorted(a, 9); insert2_sorted(&a, 9); a = insert3_sorted(a, 9); a = insert4_sorted(&a, 9);

  13. Quiz 12 int main() { myDataT *a=NULL; /*…*/ insert2_sorted(&a, 9); } void insert2_sorted(myDataT **aptr, int value) { myDataT*b, *prev, *curr; b = (myDataT *) malloc(sizeof(myDataT)); if (b==NULL) return; b->x = value; b->next = NULL; prev = NULL; curr = *aptr; while(curr) { if (curr->x >= b->x ) break; prev = curr; curr = curr->next; } if(prev == NULL) { b->next = *aptr; *aptr = b; } else{ b->next = prev->next; prev->next = b; } } • typedefstruct point { • int x; • struct point *next; • } myDataT; myDataT *prev, *curr; prev= NULL; curr = a; while(curr) { if (curr->x >= b->x ) break; prev = curr; curr = curr->next; } if(prev == NULL) { b->next = a; a = b; } else{ b->next = prev->next; prev->next = b; }

  14. int main() {myDataT *a=NULL; /*…*/ a = insert3_sorted(a, 9); } myDataT *insert3_sorted(myDataT *loc_a, int value) { myDataT *b, *prev, *curr; b = (myDataT *) malloc(sizeof(myDataT)); if (b==NULL) return; b->x = value; b->next = NULL; prev = NULL; curr = loc_a; while(curr) { if (curr->x >= b->x ) break; prev = curr; curr = curr->next; } if(prev == NULL) { b->next = loc_a; loc_a = b; } else{ b->next = prev->next; prev->next = b; } return loc_a; } • Quiz 12 myDataT *prev, *curr; prev= NULL; curr = a; while(curr) { if (curr->x >= b->x ) break; prev = curr; curr = curr->next; } if(prev == NULL) { b->next = a; a = b; } else{ b->next = prev->next; prev->next = b; }

  15. Exercise: How about finding and deleting a given number • How will you call it in the main program? find_delete1_sorted(a, 9); find_delete2_sorted(&a, 9); a = find_delete3_sorted(a, 9); a = find_delete4_sorted(&a, 9); • Implement at least one of the possible design choice?

  16. Quiz 12lib Link list ADT

  17. /**************************** list.h *************************/ #ifndef _list_h_ #define _list_h_ typedefintlistElementT; // base type int can be changed to char, double or any other type • typedefstructlistCDT *listADT; // listADT is a pointer type! listADTNewList(); • void list_insert_sorted(listADT a, listElementTval); • void list_insert_unsorted(listADT a, listElementTval); • // just add val to end of the list #endif • /******* Now we can simply implement driver.c****************/ #include <stdlib.h> • #include <stdio.h> #include "list.h" main() { listADT X; X = NewList(); • list_insert_sorted(X, 9); // why not list_insert_sorted(&X, 9); } In this design, X will point a structure in the list library which holds the header and footer that will change. But X will always point to that structure. Since it will not be changed, we do not need to pass its address. > gccdriver.clist.c –o driver • > driver

  18. /******************************* list.c ***********************/ • #include <stdlib.h> • #include <stdio.h> • #include "list.h" typedefstruct point { • listElementT x; struct point *next; } myDataT; structlistCDT { • myDataT *start; // myDataT *header; • myDataT *end; // myDataT *foother; }; listADTNewList() { listADTtmp; • tmp = (listADT) malloc(sizeof(structlistCDT)); // New(listADT); if (tmp==NULL) return NULL; tmp->start = tmp->end = NULL; returntmp; }

  19. void list_insert_sorted(listADT a, intval) { • myDataT *b, *prev, *curr; • b = (myDataT *) malloc(sizeof(myDataT)); • if (b==NULL) return; • b->x = val; • b->next = NULL; • prev = NULL; • curr = a->start; • while(curr) { • if (curr->x >= b->x ) break; • prev = curr; • curr = curr->next; • } • if(prev == NULL) { • b->next = a->start; • a->start = b; • } else{ • b->next = prev->next; • prev->next = b; • } • if (b->next == NULL) { • a->end = b; • } } myDataT *prev, *curr; prev= NULL; curr = a; while(curr) { if (curr->x >= b->x ) break; prev = curr; curr = curr->next; } if(prev == NULL) { b->next = a; a = b; } else{ b->next = prev->next; prev->next = b; }

  20. void list_insert_unsorted(listADT a, intval) // add val at the end of the link list { • myDataT *b; • b = (myDataT *) malloc(sizeof(myDataT)); • if (b==NULL) return; • b->x = val; • b->next = NULL; • if(a->start == NULL) { • a->start = b; • } else{ • a->end->next = b; • } • a->end = b; }

  21. Exercises • Extend list library with the following additional functions (you will actually implement and use them in recit08) • void FreeList(listADT a); // free the elements/cells in the list and then free a • void list_print_values(listADT a, char *name); // print name: then the values in the list • double list_average(listADT a); • void list_delete_by_value(listADT a, listElementT x) ; // this function only deletes the first element it finds. • listADT list_n_copy(listADT a, int n); // make a new list, copy the first n values from list a

More Related