【c语言编程基础】结构体单向链表的基本操作
前言
关注点
code
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h> #include <stdlib.h> #include <string.h> // strcat #define Size 4 typedef struct Table{ int len; int size; int aa; }Table; typedef struct TableNode{ Table ta; struct TableNode *next; } TableNode; bool table_eq(Table a, Table b){ bool eq = false; if((a.len == b.len) && (a.size==b.size)) eq=true; return eq; } TableNode *trackers = NULL; int nodelen = 0; // Table *t1=(Table*)calloc(Size, sizeof(Table)); void trackernode_append(Table tracker){ printf("trackernode_append\n"); TableNode* p = trackers; TableNode* tmp = (TableNode*)malloc(sizeof(TableNode)); tmp->ta = tracker; tmp->next = NULL; if(trackers==NULL) trackers=tmp; else{ TableNode* head = trackers; while(head->next){ head = head->next;} head->next = tmp; } nodelen++; trackers = p; } void trackernode_delete_val(Table tracker){ printf("trackernode_delete_val\n"); TableNode* head = trackers; if(table_eq(head->ta, tracker)) { TableNode* tmp = head; if(head->next) { head = head->next; trackers=head; } else trackers=NULL; free(tmp); }else{ TableNode* pre = head; TableNode* cur = head->next; while(!table_eq(cur->ta,tracker) && (cur!=NULL)){ pre = cur; cur = cur->next; } if(cur!=NULL) { pre->next = cur->next; free(cur); } } trackers = head; } void trackernode_delete_node(TableNode* delnode){ // TODO // TableNode* head = trackers; } void trackernode_update(Table old, Table newd){ printf("trackernode_update\n"); TableNode* head = trackers; while(head){ if(table_eq(head->ta,old)) { head->ta = newd; return; } head = head->next; } } int main() { char a[8] = {0}; strcat(a, "circle_yellow: 0.99999"); printf("a: %s\n", a); int arr[Size] = {2, -1, 0, 1}; int *p = arr; TableNode *head=(TableNode*)malloc(sizeof(TableNode)); Table ta; ta.len = 0; ta.size = 0; head->ta = ta; head->next = NULL; trackers = head; nodelen++; for(int i=1; i<Size; i++){ ta.len = i; ta.size = i; TableNode *tmp=(TableNode*)malloc(sizeof(TableNode)); tmp->ta = ta; tmp->next = NULL; head->next = tmp; head = tmp; nodelen++; } // Table tracker_arr[Size]; head = trackers; int k=0; while(head){ printf("k: %d\n", k); tracker_arr[k] = head->ta; head=head->next; k++; } printf("k: %d, Size: %d\n", k, Size); for(int i=0; i<k; i++){ if(p[i]==-1){ // delete trackernode_delete_val(tracker_arr[i]); nodelen--; }else{ // update Table newdata; newdata.len = 6; newdata.size = 8; trackernode_update(tracker_arr[i], newdata); } } Table newdata; newdata.len = 3; newdata.size = 3; trackernode_append(newdata); head=trackers; while(head){ printf("len: %d, size: %d, aa: %d\n", (head->ta).len, (head->ta).size, (head->ta).aa); head = head->next; } head= trackers; int i=0; while(head){ i++; printf("i: %d\n", i); if(head->next) { printf("while\n"); head = head->next; } else {printf("break\n"); break; } } printf("************i: %d\n", i); printf("nodelen: %d\n", nodelen); return 0; }
完
各美其美,美美与共,不和他人作比较,不对他人有期待,不批判他人,不钻牛角尖。
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/