1 / 25

C Programming –Application of Pointers to Linked List and Binary Tree

C Programming –Application of Pointers to Linked List and Binary Tree. In this lecture we learn about: Linked Lists Binary Trees. Structs which contain themselves. Sometimes programmers want structs in C to contain themselves.

kaden
Download Presentation

C Programming –Application of Pointers to Linked List and Binary Tree

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. 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 word3: jog synonym1 synonym2 word2: sprint 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. head of list NULL 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.

  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. head of list NULL Linked list concepts Adding an item to the middle of the list head of list NULL move this link new item points at next item Deleting an item from the middle of the list move this link head of list NULL 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. 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; }

  11. 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; }

  12. 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); • } • }

  13. 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

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

  15. 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)

  16. 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);

  17. Traversal • Preorder Traversal • Inorder Traversal • Postorder Traversal

  18. 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); } }

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

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

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

More Related