Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees.

Similar presentations


Presentation on theme: "C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees."— Presentation transcript:

1 C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

2 Structs which contain themselves Sometimes programmers want structs in C to contain themselves. For example, we might design an electronic dictionary which has a struct for each word and we might want to refer to synonyms which are also word structures. word1:run synonym1 synonym2 word2: sprint synonym1 synonym2 word3: jog synonym1 synonym2

3 How structs can contain themselves Clearly a struct cannot literally contain itself. But it can contain a pointer to the same type of struct struct silly_struct { /* This doesn't work */ struct silly_struct s1; }; struct good_struct { /* This does work */ struct *good_struct s2; };

4 The linked list - a common use of structs which contain themselves Imagine we are reading lines from a file but don't know how many lines will be read. We need a structure which can extend itself. This is known as a linked list. By passing the value of the head of the list to a function we can pass ALL the information.

5 How to set up a linked list typedef struct list_item { information in each item struct list_item *nextptr; } LIST_ITEM; This structure (where information is what you want each "node" of your linked list to contain). It is important that the nextptr of the last bit of the list contains NULL so that you know when to stop. NULL head of list

6 Address book with linked lists typedef struct list { char name[MAXLEN]; char address[MAXLEN]; char phone[MAXLEN]; struct list *next; } ADDRESS; ADDRESS *hol= NULL; /* Set the head of the list */

7 Linked list concepts NULL head of list Adding an item to the middle of the list NULL head of list new item points at next item move this link Deleting an item from the middle of the list NULL head of list move this link delete this node

8 Creating a New Node Allocate space for a new node. Set the next pointer to the value of NULL Set the data value for the node.

9 Adding Nodes to the List If the start node is null then the start node becomes the new node. If start is not null then start becomes the new node’s next and the start becomes the new node.

10

11

12

13

14 Adding to our address book void add_to_list (void) /* Add a new name to our address book */ { ADDRESS *new_name; new_name= (ADDRESS *)malloc (sizeof (ADDRESS)); /* CHECK THE MEMORY! */ printf ("Name> "); fgets (new_name->name, MAXLEN, stdin); printf ("Address> "); fgets (new_name->address, MAXLEN, stdin); printf ("Tel> "); fgets (new_name->phone, MAXLEN, stdin); /* Chain the new item into the list */ new_name->next= hol; hol= new_name; }

15 Search in the Address Book ADDRESS * search (char *name) /* look for a particular name in our address book */ { ADDRESS *p = hol; while(p != NULL && !strcmp(p->name, name)) { p = p->next; } return p; }

16 Delete in the Address Book void delete(char *name) /* delete a particular name in our address book */ { ADDRESS *p = hol; ADDRESS *q = hol; if(hol != NULL && strcmp(hol->name, name)) { hol = hol->next; p->next = NULL; free(p); } else { while(p != NULL && !strcmp(p->name, name)) { q = p; p = p->next; } q->next = p->next; p->next = NULL; free(p); }

17 The Binary Tree A binary tree is a method for storing ordered data (for example a dictionary of words) where we wish to easily be able to add items and find items Each element in the tree can lead to two further elements – to the left and to the right, representing elements which are earlier in the alphabet and later in the alphabet respectively

18 The binary tree NULL typedef struct tree { char word[100]; struct tree *left; struct tree *right; } TREE;

19 Binary Tree Pros & Cons Finding an element is O(log n) Adding an element is O(log n) – O(1) if we already know where to add it. Deleting an element may be complex Programming complexity is higher than a linked list (just about)

20 Deleting an entire binary tree I think this code is elegant and worth looking at: void delete_tree (TREE *ptr) { if (ptr == NULL) return; delete_tree(ptr->left); delete_tree(ptr->right); free (ptr); } We can delete the whole tree with: delete_tree(root_of_tree);

21 Traversal Preorder Traversal Inorder Traversal Postorder Traversal

22 Traversal typedef struct node{ char data; struct node *lchild,*rchild; }BinTNode; typedef BinTNode *BinTree; BinTree CreatBinTree(void) { BinTree T; char ch; if((ch=getchar())==‘ ’) return(NULL); else{ T=(BinTNode *)malloc(sizeof(BinTNode)); T->data=ch; T->lchild=CreatBinTree(); T->rchild=CreatBinTree(); return(T); }

23 Traversal void Preorder(BinTree T) { if(T) { printf("%c",T->data); Preorder(T->lchild); Preorder(T->rchild); }

24 Traversal void Inorder(BinTree T) { if(T) { Inorder(T->lchild); printf(“%c”,T->data); Inorder(T->rchild); }

25 Traversal void Postorder(BinTree T) { if(T) { Postorder(T->lchild); Postorder(T->rchild); printf(“%c”,T->data); }


Download ppt "C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees."

Similar presentations


Ads by Google