回顾通用链表(亲测代码示例)
【摘要】 这些天开始刷LC上的链表题,渐觉力不从心,想起一年前出学编程时,使用通用链表和文件做过的项目,决定先对链表进行一个基础全面的复习。
文章目录
public.h文件my_list.c通用链表使用示例
public.h文件
#include <stdio.h> //初学者,C语言开手
#include <conio.h>
#include <...
这些天开始刷LC上的链表题,渐觉力不从心,想起一年前出学编程时,使用通用链表和文件做过的项目,决定先对链表进行一个基础全面的复习。
public.h文件
#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;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
my_list.c
//创建结点/
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++; }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
通用链表使用示例
//这里以学生结构体为例,就插两条啊
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);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
附送一个模糊查询吧:
//用户名模糊查询//
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
文章来源: lion-wu.blog.csdn.net,作者:看,未来,版权归原作者所有,如需转载,请联系作者。
原文链接:lion-wu.blog.csdn.net/article/details/105833080
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)