220 likes | 230 Views
Learn the concept of linked lists for efficient insertion and deletion operations in C language. Implement linked lists as an Abstract Data Type (ADT). Includes inserting, displaying, sorting, and deleting elements in a linked list.
E N D
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 • 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
Linked List Concept NULL
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; }
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;
Quiz 12 5 8 10 NULL Linked List (cont’d) • Show how the linked list conceptually looks like after the above code. a
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) { }
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;
Linked List (insert ordered) • But, the values in the linked list were sorted. a5810 • 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…
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
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;
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);
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; }
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; }
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?
Quiz 12lib Link list ADT
/**************************** 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
/******************************* 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; }
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; }
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; }
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