本文主要是介绍单链表应用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
基于单链表实现通讯录项目
//Contact.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
#include"list.h"//初始化通讯录
void InitContact(contact** con)
{con = NULL;}
//添加通讯录数据
void AddContact(contact** con)
{PeoInfo info;printf("address:");scanf_s("%s", info.address, ADDR_MAX);printf("name:");scanf_s("%s", info.name, NAME_MAX);printf("sex:");scanf_s("%s", info.sex, SEX_MAX);printf("tel:");scanf_s("%s", info.tel, TEL_MAX);printf("age:");scanf_s("%d", &info.age);SLTPushBack(con, info);
}
//删除通讯录数据
void DelContact(contact** con)
{SListDesTroy(con);
}
//展示通讯录数据
void ShowContact(contact* con)
{SLTPrint(con);
}
//查找通讯录数据
void FindContact(contact* con)
{PeoInfo info;info.name[NAME_MAX];scanf_s("%s", info.name, NAME_MAX);printf("%p\n", SLTFind(con, info));
}
//修改通讯录数据
void ModifyContact(contact** con)
{PeoInfo info;info.name[NAME_MAX];scanf_s("%s", info.name, NAME_MAX);SLTNode* find = SLTFind(*con, info);if (find){printf("address:");scanf_s("%s", find->val.address, ADDR_MAX);printf("name:");scanf_s("%s", find->val.name, NAME_MAX);printf("sex:");scanf_s("%s", find->val.sex, SEX_MAX);printf("tel:");scanf_s("%s", find->val.tel, TEL_MAX);printf("age:");scanf_s("%d", &find->val.age);printf("修改完成\n");return;}else{printf("没找到");}
}
//销毁通讯录数据
void DestroyContact(contact** con)
{SListDesTroy(con);
}
//Contact.h
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#define NAME_MAX 100
#define SEX_MAX 4
#define TEL_MAX 11
#define ADDR_MAX 100//前置声明
typedef struct SListNode contact;//用户数据
typedef struct PersonInfo
{char name[NAME_MAX];char sex[SEX_MAX];int age;char tel[TEL_MAX];char address[ADDR_MAX];
}PeoInfo;//初始化通讯录
void InitContact(contact** con);
//添加通讯录数据
void AddContact(contact** con);
//删除通讯录数据
void DelContact(contact** con);
//展示通讯录数据
void ShowContact(contact* con);
//查找通讯录数据
void FindContact(contact* con);
//修改通讯录数据
void ModifyContact(contact** con);
//销毁通讯录数据
void DestroyContact(contact** con);
//list.c
#include"list.h"
#include<stdlib.h>
#include<string.h>
void SLTPrint(SLTNode* phead)
{printf("address name sex tel age\n");while (phead){printf("%s %8s %8s %8s %8d\n", (*phead).val.address, (*phead).val.name, (*phead).val.sex, (*phead).val.tel, (*phead).val.age);phead = phead->next;}
}
SLTNode* buyNode(SLTDataType x)
{SLTNode* node = (SLTNode*)malloc(sizeof(SLTNode));node->next = NULL;node->val = x;return node;
}
//头部插入删除/尾部插入删除
void SLTPushBack(SLTNode** pphead, SLTDataType x)
{SLTNode* node = buyNode(x);if (*pphead == NULL){*pphead = node;return;}SLTNode* cur = (*pphead);while (cur->next){cur = cur->next;}cur->next = node;
}
void SLTPushFront(SLTNode** pphead, SLTDataType x)
{SLTNode* node = buyNode(x);if (pphead == NULL){*pphead = node;return;}node->next = (*pphead);(*pphead) = node;
}
void SLTPopBack(SLTNode** pphead)
{SLTNode* cur = (*pphead);if (cur->next == NULL){free(cur);cur = NULL;return;}SLTNode* prev = NULL;while (cur->next){prev = cur;cur = cur->next;}free(cur);prev->next = NULL;
}
void SLTPopFront(SLTNode** pphead)
{SLTNode* cur = (*pphead);if (cur->next == NULL){free(cur);return;}SLTNode* prev = cur;cur = cur->next;free(prev);prev = NULL;
}//查找
SLTNode* SLTFind(SLTNode* phead, SLTDataType x)
{while (phead){if (!strcmp(phead->val.name, x.name)){return phead;}phead = phead->next;}return NULL;
}
//在指定位置之前插入数据
void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
{SLTNode* cur = (*pphead);SLTNode* node = buyNode(x);if (cur == pos){SLTPushFront(pphead, x);return;}while (cur->next == pos){cur = cur->next;}node->next = cur->next;cur->next = node;
}
//删除pos节点
void SLTErase(SLTNode** pphead, SLTNode* pos)
{SLTNode* cur = (*pphead);if (cur == pos){SLTPopFront(pphead);return;}while (cur->next == pos){cur = cur->next;}SLTNode* next = cur->next;cur->next = next->next;free(next);next = NULL;
}
//在指定位置之后插入数据
void SLTInsertAfter(SLTNode* pos, SLTDataType x)
{SLTNode* node = buyNode(x);SLTNode* next = pos->next;pos->next = node;node->next = next;
}
//删除pos之后的节点
void SLTEraseAfter(SLTNode* pos)
{SLTNode* next = pos->next;while (next){SLTNode* nnext = next->next;free(next);next = nnext;}pos->next = NULL;
}
//销毁链表
void SListDesTroy(SLTNode** pphead)
{SLTNode* cur = (*pphead);while (cur){SLTNode* next = cur->next;free(cur);cur = next;}*pphead = NULL;pphead = NULL;}
//list.h
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"Contact.h"
typedef PeoInfo SLTDataType;
typedef struct SListNode
{SLTDataType val;struct SListNode* next;
}SLTNode;void SLTPrint(SLTNode* phead);
SLTNode* buyNode(SLTDataType x);
//头部插入删除/尾部插入删除
void SLTPushBack(SLTNode** pphead, SLTDataType x);
void SLTPushFront(SLTNode** pphead, SLTDataType x);
void SLTPopBack(SLTNode** pphead);
void SLTPopFront(SLTNode** pphead);//查找
SLTNode* SLTFind(SLTNode* phead, SLTDataType x);
//在指定位置之前插入数据
void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x);
//删除pos节点
void SLTErase(SLTNode** pphead, SLTNode* pos);
//在指定位置之后插入数据
void SLTInsertAfter(SLTNode* pos, SLTDataType x);
//删除pos之后的节点
void SLTEraseAfter(SLTNode* pos);
//销毁链表
void SListDesTroy(SLTNode** pphead);
//test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"list.h"
int main()
{SLTNode* ps = 0;InitContact(&ps);AddContact(&ps);AddContact(&ps);AddContact(&ps);ShowContact(ps);printf("-------------------------------------------------\n");/*DelContact(&ps);ShowContact(ps);printf("-------------------------------------------------\n");*/FindContact(ps);printf("-------------------------------------------------\n");ModifyContact(&ps);ShowContact(ps);printf("-------------------------------------------------\n");DestroyContact(&ps);return 0;
}
运行结果:
谢谢观看
这篇关于单链表应用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!