Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C Programming CE00312-1 Lecture 20 Insertion and Deletion with Linear Linked Lists.

Similar presentations


Presentation on theme: "Introduction to C Programming CE00312-1 Lecture 20 Insertion and Deletion with Linear Linked Lists."— Presentation transcript:

1 Introduction to C Programming CE00312-1 Lecture 20 Insertion and Deletion with Linear Linked Lists

2 Insertion To insert a data item into an ordered linked list involves: creating a new node containing the data, finding the correct place in the list, and linking in the new node at this place.

3 Example of an Insertion Create new node for the 7 Find correct place – when ptr finds the 8 (7 < 8) Link in new node with previous (even if last) and ptr nodes Also check insertion before first node! 35812 - 7 new first prevptr

4 Create_node function Listpointer create_node(int data) { Listpointer new; new = (Listpointer) malloc (sizeof (struct node)); new -> value = data; return (new); }

5 insert function Listpointer insert(int data,Listpointer ptr) { Listpointer new, prev, first; new = create_node(data); if (ptr == NULL || data value) {// insert as new first node new -> next = ptr; return new; // return pointer to first node }

6 else// not first one { first = ptr; // remember start prev = ptr; ptr = ptr -> next; // second while (ptr != NULL && data > ptr -> value) {// move along prev = ptr; ptr = ptr -> next; } prev -> next = new; // link in new -> next = ptr; //new node return first; }// end else }// end insert

7 Growing a list using inserts Read items (integers), in no particular order, into an ordered (ascending) linear linked list. This will involve inserting each item into the linked list, which is initially empty. Printing the linked list items, produces the items in order. Growing this linear linked list has become a sort!

8 The node structure, type Listpointer, and prototypes (for insert and print_list) are in the header file “LLL.h”. The function definitions themselves are in “LLL.c”. The following main function is in “sort.c”. All these can be compiled together by entering: cc sort.c LLL.c “LLL.h” is ‘included’ in both “sort.c” and “LLL.c” and hence, does not need to be separately compiled.

9 Main function grows a list #include "LLL.h" int main(void)// insertion sort { Listpointer list; int item; list = NULL; // initialise to empty list while (scanf("%d", &item) != EOF) {// read item, continue while not EOF list = insert(item, list); } print_list(list); // print sorted list return 0; }

10 Deletion To delete a data item from a linked list involves (assuming it occurs only once!): finding the data item in the list, and linking out this node, and freeing up this node as free space.

11 Example of Deletion When ptr finds the item to be deleted, e.g. 8, we need the previous node to make the link to the next one after ptr (i.e. ptr -> next). Also check whether first node is to be deleted. 35812 - prevptr first

12 #include “LLL.h" // delete the item from ascending list Listpointer delete_item(int data, Listpointer ptr) { Listpointer prev, first; first = ptr;// remember start if (ptr == NULL) { return NULL; } else if (data == ptr -> value) // first node { ptr = ptr -> next;// second node free(first);// free up node return ptr;// second }

13 else// check rest of list { prev = ptr; ptr = ptr -> next; // find node to delete while (ptr != NULL && data > ptr -> value) { prev = ptr; ptr = ptr -> next; }

14 if (ptr == NULL || data != ptr -> value) // NOT found in ascending list // nothing to delete { return first;// original } else// found, delete ptr node { prev -> next = ptr -> next; free(ptr);// free node return first;// original } }// end delete


Download ppt "Introduction to C Programming CE00312-1 Lecture 20 Insertion and Deletion with Linear Linked Lists."

Similar presentations


Ads by Google