澳门与香港最准一肖一码一特6澳门一肖一码精准100王中王今天
以下是一个简单的C语言动态通讯录的示例代码,使用了结构体和动态内存分配来实现。
#include <stdio.h> #include <stdlib.h> #include <string.h> // 定义联系人结构体 struct Contact { char name[50]; char phone[20]; struct Contact *next; // 下一个联系人的指针 }; // 添加联系人 struct Contact* addContact(struct Contact *head, const char *name, const char *phone) { struct Contact *newContact = (struct Contact*)malloc(sizeof(struct Contact)); if (newContact == NULL) { printf("Memory allocation failed\\n"); return head; } strcpy(newContact->name, name); strcpy(newContact->phone, phone); newContact->next = head; return newContact; } // 显示所有联系人 void displayContacts(struct Contact *head) { printf("Contact List:\\n"); struct Contact *current = head; while (current != NULL) { printf("Name: %s, Phone: %s\\n", current->name, current->phone); current = current->next; } } // 释放内存 void freeContacts(struct Contact *head) { struct Contact *current = head; while (current != NULL) { struct Contact *temp = current; current = current->next; free(temp); } } int main() { struct Contact *contactList = NULL; // 添加联系人 contactList = addContact(contactList, "Alice", "123-456"); contactList = addContact(contactList, "Bob", "789-012"); contactList = addContact(contactList, "Charlie", "345-678"); // 显示联系人列表 displayContacts(contactList); // 释放内存 freeContacts(contactList); return 0; }
在这个示例中,使用了一个联系人的结构体,并使用了动态内新澳门传真内部绝密信封 存分配来创建联系人的节点。 函数用于添加新的联系人到通讯录, 函数用于显示所有联系人的信息, 函数用于释放动态分配的内存。
请注意,这只是一个简单的示例,实际应用中可能需要添加更多功能,如查找联系人、编辑联系人信息等。同时,动态内存分配后需要记得释放以避免内存泄漏。