前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >回顾通用链表(亲测代码示例)

回顾通用链表(亲测代码示例)

作者头像
看、未来
发布2020-08-26 10:30:33
2930
发布2020-08-26 10:30:33
举报

public.h文件

代码语言:javascript
复制
#include <stdio.h>	//初学者,C语言开手
#include <conio.h>
#include <stdlib.h>
#include <memory.h>
#include <assert.h>

//节点数据结构体
typedef struct test
{	

	char name[12];		//名字
	char pwd[8];		//密码
	int number;			//编号
	int flag;			//区分管理员和用户	// 	0 超级管理员 1 管理员  2 普通用户 3 屏蔽用户
	int money;			//仅用户有存款,初始500
	
} TEST_T;


//如果不多来一个数据域,怎么能体现出通用链表的优势
typedef struct reported
{
	int amount;//交易金额
	int rflag; //交易方式	1、存款 2、取款 3、转账转出 4、转账转入
	int lastmoney;//余额
	int lastmoney2;//收款者的余额
	int number1;//付款账户
	int number2;//入款账户
	char time[12];//操作时间
	
} REPORT_T;


//节点描述结构体
typedef struct point
{
	void *pData;				//指向数据域
	struct point *next;			//指向下一个节点
	
} POINT_T;

POINT_T * head ;
extern POINT_T * head;

my_list.c

代码语言:javascript
复制
//创建结点/
POINT_T * creat(void *data )	//创建一个属于结构体point的函数,
//传入结构体test的指针便可以用以操作test变量,
{								//并返回一个point的指针用以操作point函数
	
	POINT_T *p=NULL;
	
	p=(POINT_T *)malloc(sizeof(POINT_T));
	if(p==NULL)
	{
		gotoxy(36,14);	
		printf("申请内存失败");
		exit(-1);
	}
	memset(p,0,sizeof(POINT_T));
	
	p->pData=data;
	p->next=NULL;
	return p;
}

/新增节点///
void add(POINT_T * the_head,void *data )				//这里的data不会和上面那个冲突吗?
{
	
	POINT_T * pNode=the_head;
	POINT_T *ls=creat(data);
	//后面再接上一个
	while (pNode->next != NULL)							//遍历链表,找到最后一个节点
	{
		pNode=pNode->next;
	}
	pNode->next=ls;			//ls 临时
}

删除节点/
void Del (POINT_T * the_head, int index)
{
	POINT_T *pFree=NULL;
	
	POINT_T *pNode=the_head;
	int flag=0;
	while (pNode->next!=NULL)
	{
		if(flag==index-1)
		{
			pFree=pNode->next;				//再指向数据域就爆了
			pNode->next=pNode->next->next;
			free(pFree->pData);
			free(pFree);
			break;
		}
		pNode=pNode->next;
		flag++;	
	}	
}

///计算节点数
int Count(POINT_T * the_head)
{
	int count=0;
	POINT_T *pNode1=the_head;
	while (pNode1->next!=NULL)
	{
		if(pNode1->next!=NULL)
		{
			pNode1=pNode1->next;
			count++;
		}		
	}	
	return count;
}

/查找固定节点数据//
POINT_T * find(POINT_T *the_head,int index)
{
	int f=0;
	POINT_T *pNode=NULL;
	int count=0;
	pNode=the_head;
	
	count=Count(the_head);
	
	if(count<index)	
		printf("find nothing");
	
	while(pNode->next!=NULL)
	{
		if(index==f)
		{
			return pNode;
		}
		pNode=pNode->next;
		f++;		
	}
}

通用链表使用示例

//这里以学生结构体为例,就插两条啊

代码语言:javascript
复制
void newuser(char * filename)
{		
	TEST_T * pData = NULL;
	
	head=creat(NULL);
		pData=(TEST_T *)malloc(sizeof(TEST_T));
		
		if(pData==NULL)
		{
			gotoxy(36,14);
			printf("申请内存失败");
			exit(1);
		}
		memset(pData,0,sizeof(TEST_T));
		
		strcpy(pData->name,"admin");
		strcpy(pData->pwd,"123456");//填充第一个数据
		pData->number=10000001;
		pData->flag=0;
		add(head,pData);
		
		
		pData=(TEST_T *)malloc(sizeof(TEST_T));
		if(pData==NULL)
		{
			gotoxy(36,14);
			printf("申请内存失败");
			exit(1);
		}
		memset(pData,0,sizeof(TEST_T));
		strcpy(pData->name,"fck");
		strcpy(pData->pwd,"123456");
		pData->number=10000002;
		pData->flag=1;
		add(head,pData);
}

附送一个模糊查询吧:

代码语言:javascript
复制
//用户名模糊查询//
void fuzzyreserch(POINT_T * head)
{

	char r[12]={0};//用来接收收入的名字
	char s[12]={0};//用来接收数据域中的名字
	POINT_T * pTempp = head;
	TEST_T * tTempp = NULL;
	system("cls");
	printf("请输入用户名中的连续一串");
	getstr(r,7,0,0);
	while(pTempp->next!=NULL)
	{
		pTempp=pTempp->next;
		tTempp=pTempp->pData;
		s[12]=NULL;
		strcpy(s,tTempp->name);
	
		if (my_strstr(s,r) == NULL)
		{
			printf("");
		}
		else if (my_strstr(s,r) != NULL)
		{
			printf("\n");
			printf("用户名:%s\t用户账号:%d\t用户密码:%s\t用户余额:\t",tTempp->name,tTempp->number,tTempp->pwd,tTempp->money);

			if (tTempp->flag==0)
			{
				printf("超级管理员\n");
			} 
			else if(tTempp->flag==1)
			{
				printf("普通管理员\n");
			}
			else if (tTempp->flag==2)
			{
				printf("普通用户\n");
			}
			else
			{
				printf("已被注销用户\n");
			}
		}
	}
	printf("此为所有查询结果\n按任意键继续");
	getch();
}
//辅助函数/
char* my_strstr(const char* dest, const char* src)
{
	char* start = (char*)dest;//在这里需要强制类型转换成char*
	char* substart = (char*)src;
	char* cp = (char*)dest;//cp就是用来保存首地址的
	assert(dest != NULL);
	assert(src != NULL);
	while (*cp)
	{
		start = cp;
		while (*start != '\0' && *substart !='\0' && *start == *substart)
		{
			start++;
			substart++;
		}
		if (*substart == '\0')
		{
			return cp;
		}
		substart = (char*)src;
		cp++;//cp++可以得到原起始位置的下一个位置
	}
	return NULL;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-04-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • public.h文件
  • my_list.c
  • 通用链表使用示例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档