#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include <stdlib.h> #include<assert.h> #include<stdbool.h> #define NAME_MAX 100 #define SEX_MAX 10 #define TEL_MAX 15 #define ADDR_MAX 100 typedef struct PersonInfo { char name[NAME_MAX]; char sex[SEX_MAX]; int age; char tel[TEL_MAX]; char addr[ADDR_MAX]; }PeoInfo; typedef struct PersonInfo SLDataType; typedef struct SListNode { SLDataType data; struct SListNode* next; }SLNode; typedef SLNode contact; void InitList(SLNode** phead)//Initialize node { *phead = NULL; } void SLPrint(SLNode* phead)//Print function { //Backup phead, temporary variable storage SLNode* pcur = phead;//pcur is the address of node1 while (pcur) { printf("%d -> ", pcur->data); pcur = pcur->next;//pcur->next is the next address } printf("NULL\\ "); } SLNode* SLBuyNode(SLDataType x)//Create a new node { SLNode* node = (SLNode*)malloc(sizeof(SLNode)); if (node == NULL) return; node->data = x;//The structure can pass values directly node->next = NULL; return node; } void SLPushBack(SLNode** pphead, SLDataType x)//Tail insertion //SLPushBack( & amp;plist, 1) passes in the address of the address and uses a secondary pointer to receive it. { assert(pphead); SLNode* node = SLBuyNode(x); if (*pphead == NULL)//The linked list is empty, create a new node { *pphead = node; return;//Skip the following parts } SLNode* pcur = *pphead;//Backup //At this time pcur points to the first node while (pcur->next) { pcur = pcur->next; //Find the penultimate node } pcur->next = node; } void SLPushFront(SLNode** pphead, SLDataType x)//Header plug { assert(pphead); SLNode* node = SLBuyNode(x); //If the head node is empty, the following code can also perform head insertion operations node->next = *pphead;//*pphead is the address of the first node *pphead = node;//node becomes the new head node } void SLPopBack(SLNode** pphead)//Tail deletion { assert(pphead); assert(*pphead);//The first node cannot be empty //When there is only one node if ((*pphead)->next == NULL) { free(*pphead); *pphead = NULL; return; } SLNode* ptail = *pphead;//ptail points to the last node SLNode* prev = NULL; //The penultimate node //Find the tail node and the node before the tail node while (ptail->next != NULL) { prev = ptail; ptail = ptail->next; } prev->next = NULL; free(ptail); ptail = NULL; } void SLFrontBack(SLNode** pphead)//head deletion { assert(pphead); assert(*pphead);//The first node cannot be empty SLNode* del = *pphead;//Backup head node *pphead = (*pphead)->next;//Points to the new head node free(del); del = NULL; } /* //Find the node according to x SLNode* SLFind(SLNode** pphead, SLDataType x) { SLNode* pcur = *pphead; while (pcur) { if (pcur->data == x) return pcur; pcur = pcur->next; } return NULL; } */ //Insert data before the specified position void SLInsert(SLNode** pphead, SLNode* pos, SLDataType x) { assert(pphead);//The linked list is not empty assert(*pphead);//The first node is not empty assert(pos);//The position is not empty SLNode* node = SLBuyNode(x); //When there is only one node (pos points to the first node) if (pos == *pphead) { node->next = *pphead; *pphead = node; return; } //First find the node before pos SLNode* prev = *pphead; while (prev->next != pos) { prev = prev->next; } // prev node pos node->next = pos; prev->next = node; } //Insert data after the specified position void SLInsertAfter(SLNode* pos, SLDataType x) { assert(pos);//The position is not empty SLNode* node = SLBuyNode(x); node->next = pos->next; pos->next = node; //At this time, there is the address of pos but no pos->next. It cannot be reversed. } //Delete the specified pos node void SLErase(SLNode** pphead, SLNode* pos) { assert(pphead); assert(*pphead); assert(pos); //To delete the head node if (pos == *pphead) { *pphead = (*pphead)->next; free(pos); return; } //Find the node before pos SLNode* prev = *pphead; while (prev->next != pos) { prev = prev->next; } prev->next = pos->next; free(pos); pos = NULL; } //Delete the nodes after the specified pos node void SLEraseAfter(SLNode* pos) { // pos and pos->next are not empty assert(pos & amp; & amp; pos->next); SLNode* del = pos->next; //Save pos->next first pos->next = del->next; free(del); } //Destroy the linked list void SLDestroy(SLNode** pphead) { assert(pphead); SLNode* pcur = *pphead; //Before destroying the node, save the address of the next node while (pcur) { SLNode* del = pcur->next;//Storage the next node free(pcur); pcur = del; } *pphead = NULL; } SL *s is equivalent to contact * con void InitContact(contact** con)//Contact book initialization { InitList(con); } void DestoryContact(contact** con)//Destroy the address book { SLDestroy(con); } void ShowContact(contact* con)//Print address book { printf("\\ %-15s %-15s %-15s %-15s %-15s\\ ", "Name", "Gender", "Age", "Telephone number\ ", "address"); contact* cur = con; while (cur) { printf("%-15s %-15s %-15d %-15s %-15s\\ ", cur->data.name, cur->data.sex, cur->data.age, cur->data.tel, cur->data.addr); cur = cur->next; } printf("\\ "); } void AddContact(contact** con)//Add contact { PeoInfo info; printf("Please enter the information of the contact you want to add:\\ "); printf("Please enter the contact name:\\ "); scanf("%s", info.name); printf("Please enter the contact's gender:\\ "); scanf("%s", info.sex); printf("Please enter the age of the contact:\\ "); scanf("%d", & amp;(info.age)); printf("Please enter the contact number:\\ "); scanf("%s", info.tel); printf("Please enter the contact address:\\ "); scanf("%s", info.addr); //Data is stored in info //Insert data into the address book (sequence table) SLPushBack(con, info); printf("\\ "); } contact* FindByName(contact* con, char name[])//Find subscript { contact* cur = con; while (cur) { if (strcmp(cur->data.name, name) == 0) { return cur; } cur = cur->next; } return NULL; } void FindContact(contact* con)//Find the contact { printf("Please enter the name of the contact you want to find: "); char name[NAME_MAX]; scanf("%s", name); contact* pos = FindByName(con, name); if (pos == NULL) { printf("The person you are looking for does not exist!\\ "); return; } printf("\\ %s information is as follows:\\ ", name); printf("%-15s %-15s %-15s %-15s %-15s\\ ", "Name", "Gender", "Age", "Telephone number", \ "address"); printf("%-15s %-15s %-15d %-15s %-15s\\ ", pos->data.name, pos->data.sex, pos->data.age, pos->data.tel, pos->data.addr); } void DelContact(contact** con)//Delete contact { char name[NAME_MAX]; printf("Please enter the name of the user to be deleted: \\ "); scanf("%s", name); contact* pos = FindByName(*con, name); if (pos == NULL) { printf("The user to be deleted does not exist!\\ "); return; } SLErase(con, pos); printf("Deletion successful!\\ "); } void ModifyContact(contact** con)//Modify contact { printf("\\ Please enter the name of the contact you want to modify: "); char name[NAME_MAX]; scanf("%s", name); contact* pos = FindByName(*con, name); if (pos == NULL) { printf("The person you are looking for does not exist!\\ "); return; } printf("\\ Please enter the contact information to be modified:\\ "); printf("Please enter the contact name to be modified:\\ "); scanf("%s", pos->data.name); printf("Please enter the gender of the contact you want to modify:\\ "); scanf("%s", pos->data.sex); printf("Please enter the age of the contact you want to modify:\\ "); scanf("%d", & amp;(pos->data.age)); printf("Please enter the contact number you want to modify:\\ "); scanf("%s", pos->data.tel); printf("Please enter the contact address to be modified:\\ "); scanf("%s", pos->data.addr); printf("\\ "); } void DestroyContactFile(contact** con)//Destroy the address book { SLDestroy(con); } void menu() { printf("\\ "); printf("********************Address Book********************\\ \ "); printf("**** 1. Add contact 2. Delete contact ****\\ "); printf("**** 3. Modify contact 4. Find contact ****\\ "); printf("**** 5. View address book 0. Exit address book ****\\ "); printf("********************************************** \\ "); printf("\\ "); } int main() { //slttrst(); //slttrst1(); contact* con;// SeqList con InitContact( & amp;con); int op = -1; do { menu(); printf("Please make your selection: \\ "); scanf("%d", & amp;op); switch(op) { case 1: AddContact( & amp;con); break; case 2: DelContact( & amp;con); break; case 3: ModifyContact( & amp;con); break; case 4: FindContact(con); break; case 5: ShowContact(con); break; case 0: printf("About to exit the address book!\\ "); break; default: printf("Incorrect input please re-enter:\\ "); break; } } while (op != 0); return 0; }
The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Algorithm skill tree Home page Overview 57032 people are learning the system