insertanode B typedef struct node int data struct
insert_a_node. B typedef struct node { int data; struct node *next; } node; node *insert_a_node(node *head, node *p) { node *t=head; while (t->next !=NULL && if(p==NULL) return head; t->next->data < p->data) if(head == NULL) return p; t = t->next; if (p->data == head->data) return head; if(t->next!=NULL && if (p->data <= head->data){ t->next->data == p->data) return head; p->next = t->next; return p; t->next = p; } return head; } calling: head = insert_a_node(head, p); National Cheng Kung University CSIE Computer & Internet Architecture Lab 1
insert_a_node typedef struct node { int data; struct node *next; } node; node *insert_a_node(node *head, node *p) { node *t=head; while (t->next !=NULL && if(p==NULL) return head; t->next->data < p->data) if(head == NULL) return p; t = t->next; p->next = t->next; if (p->data <= head->data){ t->next = p; p->next = head; return p; } } calling: head = insert_a_node(head, p); National Cheng Kung University CSIE Computer & Internet Architecture Lab 2
Search a List typedef struct node { int data; struct node *next; } node; node *search_a_list(node *head, int d) { node *t=head; while (t != NULL && t->data != d){ t = t -> next } return t; } Calling: p = search_a_list(head, 10); National Cheng Kung University CSIE Computer & Internet Architecture Lab 3
typedef struct node { int data; struct node *next; } node; build_a_list node *build_a_list(int data[], int n) { node *head=NULL, *p; int i; for(i=0; i<n: i++){ p = create_a_node(data[i]); head = insert_a_node(head, p); } return head; node *create_a_node(int n) { node *p=NULL; } } calling: int A[10]={3, 8, 33, 9, 11, 2, 1, 22, 100, 6}; head = build_a_list(A, 10); p = (node *) malloc(sizeof(node)); if (p == NULL) { printf(“malloc error n”); exit(1); } return p; National Cheng Kung University CSIE Computer & Internet Architecture Lab 4
typedef struct node { int data; struct node *next; } node; count_a_list int count_a_list(nod *head) { node *t=head; int count=0; while(t != NULL){ count++; t = t -> next } return count; return head } calling: n = count_a_list(head); National Cheng Kung University CSIE Computer & Internet Architecture Lab 5
Insert a node no return typedef struct node { int data; struct node *next; } node; insert_a_node_no_return(node **head, node *p) calling: { f = insert_a_node_no_return(&head, p); node *t = *head; if(p==NULL) return; if(*head == NULL) {*head = p; return; } } if (p->data <= (*head)->data){ p->next = *head; *head = p; return; } while (t->next !=NULL && t->next->data < p->data) t = t->next; p->next = t->next; t->next = p; return; National Cheng Kung University CSIE Computer & Internet Architecture Lab 6
Insert a node no return. B int insert_a_node_no_return(node **head, node *p) node *t = *head; typedef struct node { int data; struct node *next; } node; calling: f = insert_a_node_no_return(&head, p); if(p==NULL) return 1; if(*head == NULL) {*head = p; return 0; } if (p->data == head->data) return 1; if (p->data <= (*head)->data){ p->next = *head; *head = p; return 0; } while (t->next !=NULL && t->next->data < p->data) t = t->next; if(t->next!=NULL && t->next->data == p->data) return 1; p->next = t->next; t->next = p; return 0; } National Cheng Kung University CSIE Computer & Internet Architecture Lab 7
Insert a node no return A insert_a_node. A(node **h, node *p) { node **x=h; x head 3 if(p==NULL) return; typedef struct node { int data; struct node *next; } node; while ((*x)!=NULL && (*x)->data < p>data) x = &((*x)->next); p->next = (*x); (*x) = p; return; } 5 7 5 calling: insert_a_node. A(&head, p); National Cheng Kung University CSIE Computer & Internet Architecture Lab 8
typedef struct node { int data; struct node *next; } node; delete_a_node *delete_a_node(node **head, int d) { node *t=*head, *prev=NULL; while (t != NULL && t->data != d){ if(t->data >d) return NULL; prev = t; t = t -> next } if(t==NULL) {return NULL; } if(prev==NULL) {*head = t->next; t->next=NULL; return t; } prev->next = t->next; t->next=NULL; return t; } calling: p = delete_a_node(&head, data); National Cheng Kung University CSIE Computer & Internet Architecture Lab 9
delete_a_node-1 node *delete_a_node(node **head, int d) { node *t=*head, *prev=NULL, *p; p = search_a_list(t, d); while (t != NULL && t->data != d){ if(t->data >d) return NULL; prev = t; t = t -> next } if(t==NULL) {return NULL; } if(prev==NULL) {*head = t->next; t->next=NULL; return t; } prev->next = t->next; t->next=NULL; return t; } calling: p = delete_a_node(&head, data); 10
delete_a_node-1 node *search_a_list(node *head, int d) { node *t=head, *prev=NULL; while (t != NULL && t->data != d){ if(t->data >d) return NULL; t = t -> next; } return t; } National Cheng Kung University CSIE Computer & Internet 11 Architecture Lab
delete_a_node. A typedef struct node { int data; struct node *next; } node; node *delete_a_node. A(node **head, int d) { node **t=head, *p; while (*t != NULL && (*t)->data != d){ t = &((*t) -> next); } if(*t==NULL) {return NULL; } if(t==head) {*head = (*t)->next; (*t)->next=NULL; return *t; } p = *t; *t = (*t)->next; return p; } calling: p = delete_a_node. A(&head, data); National Cheng Kung University CSIE Computer & Internet Architecture Lab 12
dummy_insert_a_node typedef struct node { int data; struct node *next; } node; dummy_insert_a_node(node *dh, node *p) { node *t=dh; if(p==NULL) return; while (t->next !=NULL && t->next->data < p->data) t = t->next; p->next = t->next; t->next = p; return; } ===== calling: head = insert_a_node(head, p); National Cheng Kung University CSIE Computer & Internet Architecture Lab 13
duplicate node *duplicate(node *h) { node *t=NULL, *newh=NULL, *tail; while (h !=NULL){ t=create_a_node(h->data); if(newh==NULL) {newh=tail=t; } else{tail->next = t; tail=tail->next; }//tail=tail>next=t; h = h->next; } return newh; } calling: h = merge(h 1); National Cheng Kung University CSIE Computer & Internet Architecture Lab 14
Merge node *merge 1(node *h 1, node *h 2) { node *t, *newh=NULL, *tail; while (h 1 !=NULL && h 2!=NULL){ if(h 1 ->data < h 2 ->data){ t= h 1; h 1=h 1 ->next; t->next=NULL; } else { t= h 2; h 2=h 2 ->next; t->next=NULL; if(newh == NULL) newh = tail= t; else {tail->next = t; tail=t; } } if(h 1!=NULL) tail->next = h 1}; if(h 2!=NULL) tail->next = h 2}; return newh; } calling: head = merge 1(h 1, h 2); How to improve the speed? } National Cheng Kung University CSIE Computer & Internet Architecture Lab 15
Merge 2 node * merge 2(node *h 1, node *h 2) { node *t; calling: head = merge 2(h 1, h 2) while (h 1 !=NULL){ t = h 1; h 1 = h 1 ->next; t->next = NULL; h 2 = insert_a_node(h 2, t); } return h 2; } National Cheng Kung University CSIE Computer & Internet Architecture Lab 16
insert_a_node_DLL typedef struct node { int data; struct node *next, *prev; } node; doubly linked list (DLL) node *insert_a_node. DLL(node *head, node *p) { while (t->next !=NULL && node *t=head; t->next->data < p->data) if(p==NULL) return head; t = t->next; if(head == NULL) return p; 1. p->next = t->next; if (p->data <= head->data){ 2. t->next = p; p->next = head; head->prev = p; 3. p->prev=t; return p; 4. if(p->next != NULL) p->next ->prev =p; } return head; } calling: head = insert_a_node. DLL(head, p); National Cheng Kung University CSIE Computer & Internet Architecture Lab 17
delete_a_node. DLL node *delete_a_node. DLL(node **head, int d) { node *t=*head; while (t != NULL && t->data != d){ if(t->data >d) return NULL; t = t -> next } if(t==NULL) {return NULL; } if(t == *head) *head = t->next; if (t->prev !=NULL) t->prev->next; =t->next; if (t->next!=NULL) t->next->prev=t->prev; t->next=t->prev=NULL; return t; } calling: p = delete_a_node(&head, data); National Cheng Kung University CSIE Computer & Internet Architecture Lab 18
delete_a_node_dummy_DLL node *delete_a_node_dummy_DLL(node *head, int d) { node *t=head, *p; while (t->next != NULL && t->next->data != d){ if(t->next->data > d) return NULL; t = t -> next } if(t->next==NULL) return NULL; p = t->next; t->next = t->next; if (t->next!=NULL) t->next->prev=t; p->next=p->prev=NULL; return p; } calling: p = delete_a_node_dummy_DLL(head, data); National Cheng Kung University CSIE Computer & Internet 19 Architecture Lab
BST_insert_a_node typedef struct node { int data; struct node *left, *right; } node; node *BST_insert_a_node(node *root, int d) { calling: root = BST_insert_a_noderoot, d); node *t; if(root==NULL){ return create_a_BST_node(data); } t = root; while ((t->data < d && t->right!=NULL) || (t->data >= d && t->left!=NULL)){ if (t->data < d) t = t->right; else t = t->left; } if (t->data < d) t->right = create_a_BST_node(data); else t->left = create_a_BST_node(data) return root; } National Cheng Kung University CSIE Computer & Internet Architecture Lab 20
BST_insert_a_node_a typedef struct node { int data; struct node *left, *right; } node; BST_insert_a_node_a(node **root, int d) { node *t; while ( *root !=NULL){ if ((*root)->data < d) root = &((*root)->right); else root = &((*root)->left); } *root = create_a_BST_node(d); return ; } calling: BST_insert_a_node_a(&root, d); National Cheng Kung University CSIE Computer & Internet Architecture Lab 21
BST_delete_a_node typedef struct node { int data; struct node *left, *right; } node; node *BTS_delete_a_node(node *root, int d) { node *t, *tp =NULL; if(root==NULL) return root; if(root->data == d && root->left == NULL && root->right==NULL){ free(root); return root; } t = root; while ((t->data < d && t->right!=NULL) || (t->data > d && t->left!=NULL)){ tp = t; if (t->data < d) t = t->right; else t = t->left; } if (t->data != d) return root; while (t->left != NULL || t->right!=NULL) { [[search_replacing_node(t, &t 1, &tp 1); ]] t->data = t 1 ->data; t = t 1; tp = tp 1; } if(t = tp->left) tp->left = NULL; else tp->right = NULL; free(t); } National Cheng Kung University CSIE Computer & Internet Architecture Lab 22
- Slides: 22