#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct Node { char name[200]; char sex[200]; char year[200]; char mouth[200]; char day[200]; struct Node *next; }stu;//stu才是类型 //struct Node stu; //创建/插入节点 int main() { stu *pHead = NULL; stu *pTail = NULL; while (true) { char choice[32] = { 0 }; char name[32] = { 0 }; char sex[32] = { 0 }; char year[32] = { 0 }; char mouth[32] = { 0 }; char day[32] = { 0 }; scanf("%s", choice); if (strcmp(choice, "quit") == 0) { break; } //else if (strcmp(choice, "printf") == 0) //{ // stu *pNode = pHead;//不要改变phead的值,从头部遍历 // while (pNode != NULL) // { // printf("%s %s %s %s %s\n", pNode->name, pNode->sex, pNode->year, pNode->mouth, pNode->day); // pNode = pNode->next; // } // printf("\n"); //} else if (strcmp(choice, "name") == 0) { //遍历链表 scanf("%s", name); stu *pNode = pHead; while (pNode != NULL) { if (strcmp(pNode->name, name) == 0) { printf("%s %s %s-%s-%s\n", pNode->name, pNode->sex, pNode->year, pNode->mouth, pNode->day); break; } pNode = pNode->next; } } else if (strcmp(choice, "sex") == 0) { //遍历链表 scanf("%s", sex); stu *pNode = pHead; while (pNode != NULL) { if (strcmp(pNode->sex, sex) == 0) { printf("%s %s %s-%s-%s\n", pNode->name, pNode->sex, pNode->year, pNode->mouth, pNode->day); } pNode = pNode->next; } } else if (strcmp(choice, "add") == 0) { scanf("%s %s %s %s %s", name, sex, year, mouth, day); stu *pNode = (stu*)malloc(sizeof(stu)); strcpy(pNode->name, name); strcpy(pNode->sex, sex); strcpy(pNode->year, year); strcpy(pNode->mouth, mouth); strcpy(pNode->day, day); pNode->next = NULL; if (pHead == NULL) { pHead = pNode; pTail = pNode;//第一个节点即是头又是尾部 } else { pTail->next = pNode; pTail = pNode; } } } } |
Double click to view unformatted code.