Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C Programming CE00312-1 Lecture 19 Linear Linked Lists.

Similar presentations


Presentation on theme: "Introduction to C Programming CE00312-1 Lecture 19 Linear Linked Lists."— Presentation transcript:

1 Introduction to C Programming CE00312-1 Lecture 19 Linear Linked Lists

2 Insertion into an ordered array Suppose we wanted to insert the value, 7. To keep the integers in ascending order, we can’t just store this 7 in element A[10]. We would have to shift all elements from A[2] to A[9] one location to the right, and then store the 7 in A[2]. 3 5 8 12 23 34 44 50 56 76 0 1 2 3 4 5 6 7 8 9 10 11 A

3 Insertion into an ordered array This is very cumbersome for a large array and if many insertions are required. Sooner or later we would run out of space in the array But, if we make the array very large, then most of the time most of the space is wasted! Deletion of values from the array causes similar problems. So as not to leave ‘gaps’, many values would have to be shifted left for each deletion. We should not use arrays if we need to make insertions and deletions.

4 Linear Linked List Each node may be stored anywhere in memory and can only be reached by a pointer. The pointer variable, list, points to the first node and each node points to the next node. The last node has a NULL pointer (‘-‘). 35812 - list

5 Insertion into a ordered Linear Linked List To insert an item, e.g. 7, between two nodes requires creating a new node and linking it with the other two nodes using just two pointers. 35812 - 7 new list

6 Node Structure struct node { int value; struct node *next; }; typedef struct node *Listpointer; // Listpointer is new type value next

7 Here is a header file - “LLL.h” containing structure & type definitions and function prototypes for linked lists: #include "stdio.h“// NULL defined here #include "stdlib.h"// for malloc function struct node { int value; struct node *next; }; typedef struct node *Listpointer; // new type void print_list(Listpointer); // prototypes void print_reverse(Listpointer); Listpointer insert(int, Listpointer); int add_items(Listpointer); Listpointer delete(int, Listpointer); int search(int, Listpointer);

8 print_list function When ptr is NULL then it is not pointing to anything, ie there is no list, or in the loop, no more nodes in the list. #include "LLL.h“ // structure,type,prototypes void print_list(Listpointer ptr) { while (ptr != NULL) { printf("%d\n", ptr -> value);//print value ptr = ptr -> next; // point to next }

9 35812 - ptr value next

10 add_items function #include "LLL.h” int add_items(Listpointer ptr) { int sum = 0; while (ptr != NULL) { sum = sum + ptr -> value; ptr = ptr -> next; } return sum; }

11 #include "LLL.h" int search(int item, Listpointer ptr) { while (ptr != NULL) {// not end of list if (ptr -> value == item) { return 1; // true } else { ptr = ptr -> next; } }// end while return 0; // false }// end search


Download ppt "Introduction to C Programming CE00312-1 Lecture 19 Linear Linked Lists."

Similar presentations


Ads by Google