這些天開始刷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;
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++;
}
}
通用鏈表使用示例
//這裏以學生結構體爲例,就插兩條啊
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);
}
附送一個模糊查詢吧:
//////////////////////用戶名模糊查詢//////////////////////////////
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;
}