链表及链表嵌套的操作

  1#include <stdio.h>
  2#include <string.h>
  3#include <stdlib.h>
  4
  5typedef struct childLink
  6{
  7char nm[100];
  8struct childLink *next;
  9}CHILDNODE;
 10
 11typedef struct linkTable
 12{
 13int birthYear;
 14CHILDNODE *mz;
 15struct linkTable *next;
 16}LINKNODE;
 17
 18LINKNODE * insert(LINKNODE *head, LINKNODE *input);
 19CHILDNODE * insert_nm(CHILDNODE *head_c, CHILDNODE *input_nm);
 20void print(LINKNODE *head);
 21
 22/************************************************************************/
 23/* main
 24/************************************************************************/
 25void main()
 26{
 27LINKNODE *head, *input;
 28CHILDNODE *head_c, *input_c;
 29head = NULL;
 30head_c = NULL;
 31
 32while(1)
 33{
 34if ((input=(LINKNODE *)malloc(sizeof(*input))) == NULL)
 35{
 36printf("malloc fail!");
 37exit(0);
 38}
 39
 40printf("input birthYear(0:quit):\n");
 41scanf("%d", &input->birthYear);
 42
 43if (input->birthYear == 0)
 44{
 45break;
 46}
 47
 48while(1)
 49{
 50if ((input_c=(CHILDNODE *)malloc(sizeof(*input_c))) == NULL)
 51{
 52printf("nm malloc fail!");
 53exit(0);
 54}
 55
 56printf("input CHILDNODE:\n");
 57scanf("%s", &input_c->nm);
 58
 59if (strcmp(input_c->nm, "0") == 0)
 60{
 61break;
 62}
 63
 64head_c = insert_nm(head_c, input_c);
 65}
 66
 67input->mz = head_c;
 68head = insert(head, input);
 69
 70head_c = NULL;
 71}
 72
 73print(head);
 74}
 75
 76/************************************************************************/
 77/* insert link
 78/************************************************************************/
 79LINKNODE * insert(LINKNODE *head, LINKNODE *input)
 80{
 81LINKNODE *p0, *p1, *p2;
 82p0 = input;
 83p1 = head;
 84
 85if (head == NULL)
 86{
 87head = p0;
 88p0->next = NULL;
 89}
 90else
 91{
 92//compare p0 with p1 to p(n-1)
 93while (p0->birthYear > p1->birthYear && p1->next != NULL)
 94{
 95p2 = p1;
 96p1 = p1->next;
 97}
 98
 99//compare with last one
100if (p0->birthYear <= p1->birthYear)
101{
102if (p1 == head) //top
103{
104head = p0;
105p0->next = p1;
106}
107else //middle
108{
109p2->next = p0;
110p0->next = p1;
111}
112}
113else //end
114{
115p1->next = p0;
116p0->next = NULL;
117}
118}
119
120return head;
121}
122
123/************************************************************************/
124/* insert child link
125/************************************************************************/
126CHILDNODE * insert_nm(CHILDNODE *head, CHILDNODE *input)
127{
128CHILDNODE *p0, *p1, *p2;
129p0 = input;
130p1 = head;
131
132if (head == NULL)
133{
134head = p0;
135p0->next = NULL;
136}
137else
138{
139while (strcmp(p0->nm, p1->nm)>0 && p1->next != NULL)
140{
141p2 = p1;
142p1 = p1->next;
143}
144
145if (strcmp(p0->nm, p1->nm) < 0) { if (p1 == head) { head = p0; p0->next = p1;
146}
147else
148{
149p2->next = p0;
150p0->next = p1;
151}
152}
153else
154{
155p1->next = p0;
156p0->next = NULL;
157}
158
159}
160
161return head;
162}
163
164/************************************************************************/
165/* print link
166/************************************************************************/
167void print(LINKNODE *head)
168{
169LINKNODE *pOut;
170CHILDNODE *pOut_c;
171
172pOut = head;
173
174while (pOut != NULL)
175{
176printf("\n\nBirthYear: %d\nChild:", pOut->birthYear);
177
178pOut_c = pOut->mz;
179while (pOut_c != NULL)
180{
181printf(" %s", pOut_c->nm);
182
183pOut_c = pOut_c->next;
184}
185pOut = pOut->next;
186
187}
188}