单链表应用

基于单链表实现通讯录项目

//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;
}

运行结果:

 

谢谢观看 

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的单链表应用实例代码,使用C语言实现: ```c #include <stdio.h> #include <stdlib.h> // 定义链表结构体 typedef struct Node { int data; struct Node* next; } node; // 创建新节点 node* create_node(int data) { node* new = (node*)malloc(sizeof(node)); new->data = data; new->next = NULL; return new; } // 在链表末尾插入节点 void insert_node(node** head, int data) { node* new = create_node(data); if (*head == NULL) { *head = new; return; } node* current = *head; while (current->next != NULL) { current = current->next; } current->next = new; } // 删除指定值的节点 void delete_node(node** head, int data) { if (*head == NULL) { return; } if ((*head)->data == data) { node* temp = *head; *head = (*head)->next; free(temp); return; } node* current = *head; while (current->next != NULL && current->next->data != data) { current = current->next; } if (current->next != NULL) { node* temp = current->next; current->next = current->next->next; free(temp); } } // 打印链表 void print_list(node* head) { printf("Linked List: "); while (head != NULL) { printf("%d ", head->data); head = head->next; } printf("\n"); } int main() { node* head = NULL; insert_node(&head, 1); insert_node(&head, 2); insert_node(&head, 3); insert_node(&head, 4); insert_node(&head, 5); print_list(head); delete_node(&head, 3); print_list(head); delete_node(&head, 1); print_list(head); return 0; } ``` 该代码实现了一个单链表的基本操作,包括创建节点、在末尾插入节点、删除指定值的节点和打印链表。可以通过在 `main` 函数中调用这些函数来使用这个单链表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值