1 / 10

Complex Structures

Complex Structures. Nested Structures Self referential structures A structure may have Data variables Internal structures/unions Pointer links Function pointers. struct course { int course_num; struct student[20]; struct course *next; int (*average)(int []); };.

taniel
Télécharger la présentation

Complex Structures

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. Complex Structures • Nested Structures • Self referential structures • A structure may have • Data variables • Internal structures/unions • Pointer links • Function pointers struct course { int course_num; struct student[20]; struct course *next; int (*average)(int []); };

  2. Dynamic Structures • Link Lists • A type of data structure that have its elements linked together • Generic on-demand dynamic structure • Easy manipulation, but may not be efficient • Trees • A type of data structures in which elements are connected in a tree form • Complex data structures • More efficient, use only if the number of elements are large

  3. Link Lists struct slink { int data; struct slink *next; }; • Single Link Lists head struct dlink { int data; struct dlink *next; struct dlink *prev; }; • Double Link lists head Sample Usage: Maintain a dynamic structure of sorted numbers using any of the previous structures Important Functions: void Display_list(struct slink * llist) void Append_Node(struct slink* llist, int data) void Delete_Node(struct slink* llist, int data) int Search_Value(struct slink* llist, int data)

  4. Functions: Single link list //Build a single linked list #include<stdlib.h> #include<stdio.h> struct slink { int data; struct slink *next; }; //Insert is the same way void main() { slink * curr, * head; int i; head = NULL; for(i=1;i<=10;i++) { curr = (slink *)malloc(sizeof(slink)); curr->data = i; curr->next = head; head = curr; } curr = head; } //Display_list(struct slink * llist) void display_list(struct slink *llist) { while(llist->next != NULL) { printf("%d ", llist->data); llist = llist->next; } printf("%d", llist->data); //the last node } //Append_Node(struct slink* llist, int data) void append_node(struct slink *llist, int data) { while(llist->next != NULL) { llist = llist->next; llist->next = (struct slink )malloc(sizeof(struct slink)); llist->next->data = data; llist->next->next = NULL; } }

  5. Functions: Single link list //Delete_Node(struct slink* llist, int data) void delete_node(struct slink *llist, int data) { struct slink *temp; temp = (struct slink *)malloc(sizeof(struct slink)); if(llist->data == data){ /* remove the node */ temp = llist->next; free(llist); llist = temp; } else { //Transverse while(llist->next->data != data) { llist = llist->next; } temp = llist->next->next; free(llist->next); llist->next = temp; } } • //Search_Value(struct slink* llist, int data), return position • int search_value(struct slink *llist, int data) • { • int retval = -1; • int i = 1; • while(llist->next != NULL) • { • if(llist->next->data == data) return i; • else i++; • llist = llist->next; • } • return retval; • }

  6. Functions: Double link list void insert (struct dlink *head, struct dlink *entry) { struct dlink *tmp; for (tmp = head->next; tmp!= head && tmp->data < entry->data; tmp = tmp->next); tmp->prev->next = entry; entry->prev= tmp->prev; tmp->prev = entry; entry->next = tmp; }; void delete (struct dlink *entry) { entry>next->prev = entry->prev; entry>prev->next = entry->next; entry->next = entry->prev = NULL; }; struct dlink * search (struct dlink *head, int val) { struct dlink * tmp; for (tmp = head->next; tmp!= head && tmp->data != val; tmp = tmp->next); return (tmp==head)? NULL : tmp; } int main() { struct dlink num[10] = { … }; struct dlink head={0, NULL, NULL}; for (int i=0; i<10; i++) insert (&head, &num[i]); … return 0; }

  7. Other Link Lists • Hash link lists bin[size] • Other variants: queues and stacks • Queue: a link list grown at one end, shrink at another, i.e. FIFO • Stack: a LIFO link list

  8. Trees – binary trees root struct node { int data; struct node *left; struct node *right; }; left right height leaf

  9. More Trees • Tertiary Trees • Each node has three children • K-ary Trees • Each node has K children • B (Balanced)- Trees • A tree that maintains a balanced height as it grows/shrinks • …

  10. Binary Search Tree void insert (struct node *root, struct node *entry) { /* where to insert is really algorithm dependent. A generic case only: entry is to replace the left node of head */ if (head->left) { entry->left = head->left->left; entry->right = head->left->right; } head->left = entry; }; void delete (struct node *root, struct node *entry) { /* The key is to find a node that has an entry as a child */ }; struct node * search (struct node *root, int val) { if (!root) return NULL; else if (root->data != val) tmp = search(root->left, val); if (!tmp) tmp = search(root->right, val); return tmp; } int main() { struct node num[10] = { … }; struct node root = {0, NULL, NULL}; for (int i=0; i<10; i++) { insert (&root, &num[i]); } … return 0; }

More Related