【学习记录】链表的插入

来源:互联网 发布:惠普清零软件 编辑:程序博客网 时间:2024/06/04 19:07

一只菜鸟猿,仅仅作为一个学习记录,看到的人还望批评指正


Q:在单向链表中找到数据元素mybalance,并在其后插入新节点。

typedef struct {char name[30];int balance;} Data;typedef struct student{Data data;student *next;} Node;Node* insertNode(Node* head, int mybalance, Data mydata){Node* ptemp;Node* p;if (!(ptemp = (Node*)malloc(sizeof(Node)))){printf("申请内存失败!");return 0;}if (NULL == head)return 0;ptemp->data = mydata;p = head;while (p->data.balance != mybalance)p = p->next;if (p){if (p->next == NULL){ptemp->next = NULL;p->next = ptemp;return head;}ptemp->next = p->next;p->next = ptemp;return head;}else{printf("节点不存在\n");free(ptemp);}}


————————————2017.9.25

 
阅读全文
0 0
原创粉丝点击