单链表的删除
#include <stdio.h>#include <stdlib.h>
#include <string.h>
char adminname = "admin1";
char adminpassword = "a123456";
struct Date
{
int year;
int month;
int day;
};
typedef struct Infor
{
char name;
char gender;
int age;
char address;
char buytype;
char color;
long price;
struct Date date;
struct Infor *next;
}INFOR,*Pointer;
Pointer infor = NULL;
int login(void);
void Menushow(void);
void Add(Pointer *infor);
void Modify(Pointer *infor);
void Show(Pointer *infor);
void Search_name(Pointer *infor);
void Printuser(Pointer *infor);
void Del(Pointer *infor);
//void Sort(Pointer infor);
void Loadfromfile(void);
void Xuigai(char adminname[],char adminpassword[]);
void Release(Pointer *infor);
void Release(Pointer *infor)
{
Pointer temp, p =*infor;
while(p == NULL)
{
temp = p;
p = p->next;
free(temp);
}
}
int login(void)
{
char username;
char userpassword;
printf("请输入用户名:");
gets(username);
printf("请输入密码:");
gets(userpassword);
if(!strcmp(adminname,username) && !strcmp(adminpassword,userpassword))
{
return 1;
}
else
{
return 0;
}
}
void Menushow(void)
{
int i;
printf("\n");
printf("|----1.增加购买电瓶车用户信息-----|\n");
printf("|----2.修改购买电瓶车用户信息-----|\n");
printf("|----3.显示购买电瓶车用户信息-----|\n");
printf("|----4.查询用户信息---------------|\n");
printf("|----5.删除用户信息---------------|\n");
printf("|----6.按购买日期进行排序---------|\n");
printf("|----7.从文件中读取用户信息-------|\n");
printf("|----8.将购买用户信息保存到文件---|\n");
printf("|----9.修改密码-------------------|\n");
printf("|----10.退出系统------------------|\n");
printf("\n");
printf("请输入你选择的编号:");
scanf("%d",&i);
do
{
switch(i)
{
case 1:Add(&infor);break;
case 2:Modify(&infor);break;
case 3:Show(&infor);break;
case 4:Search_name(&infor);break;
case 5:Del(&infor);break;
//case 6:Sort(&infor);break;
case 7:Loadfromfile();break;
//case 8:Save();break;
case 9:Xuigai(adminname,adminpassword);break;
//case 10:Quit();break;
}
}while(i < 1 || i > 10);
}
void Add(Pointer *infor)
{
Pointer p;
static Pointer tail;
char ch;
do{
p = (Pointer)malloc(sizeof(INFOR));
if(p == NULL)
{
printf("内存分配失败\n");
exit(1);
}
getchar();
printf("请输入姓名:");
gets(p->name);
printf("请输入性别(男/女):");
scanf("%s",p->gender );
printf("请输入年龄:");
scanf("%d",&p->age);
printf("请输入地址:");
scanf("%s",p->address );
printf("请输入购品牌和型号:");
scanf("%s",p->buytype );
printf("请输入购买的颜色:");
scanf("%s",p->color );
printf("请输入价格:");
scanf("%ld",&p->price );
printf("请输入购买时间:(yyyy-mm-dd):");
scanf("%d-%d-%d",&p->date.year,&p->date.month ,&p->date.day);
getchar();
printf("请问是否需要继续插入用户信息(Y/N):");
ch = getchar();
if((*infor )!= NULL)
{
tail->next = p;
p->next = NULL;
}
else
{
(*infor )= p;
p->next = NULL;
}
tail = p;
}while(ch == 'Y' || ch == 'y');
}
void Modify(Pointer *infor)
{
int t = 0;
Pointer p = *infor;
int flag = 0;
char sename;
if(p == NULL)
{
printf("并未存在任何数据,不能进行修改!!");
return ;
}
getchar();
printf("请输入你要修改的用户名字:");
gets(sename);
while(p != NULL)
{
if(!strcmp(sename,p->name))
{
flag = 0;
printf("请输入修改后的信息:\n");
printf("请输入姓名:");
scanf("%s",p->name );
printf("请输入性别(男/女):");
scanf("%s",p->gender );
printf("请输入年龄:");
scanf("%d",&p->age);
printf("请输入地址:");
scanf("%s",p->address );
printf("请输入购品牌和型号:");
scanf("%s",p->buytype );
printf("请输入购买的颜色:");
scanf("%s",p->color );
printf("请输入价格:");
scanf("%ld",&p->price );
printf("请输入购买时间:(yyyy-mm-dd):");
scanf("%d-%d-%d",&p->date.year,&p->date.month,&p->date.day);
break;
}
else
{
flag = 1;
p = p->next;
}
if(!flag)
{
t = 1;
}
}
if(!t)
{
printf("并未查询到存在该用户信息!!\n");
}
}
void Show(Pointer *infor)
{
Pointer p = *infor;
if(p == NULL)
{
printf("\n暂时还未存入数据!!\n");
printf("显示失败!!\n\n");
}
else
{
while(p != NULL)
{
Printuser(&p);
printf("^_^==========================^_^\n");
p = p->next ;
}
}
}
void Search_name(Pointer *infor)
{
Pointer p = *infor;
char sename;
getchar();
printf("请输入你要查询的用户姓名:");
gets(sename);
while(p != NULL)
{
if(!strcmp(sename,(p)->name))
{
printf("已查询到信息\n是否打印该用户信息(Y/N):\n");
char ch = getchar();
if(ch == 'Y'|| ch == 'y')
{
Printuser(&p);
}
}
p = p->next;
}
}
void Printuser(Pointer *infor)
{
Pointer p = *infor;
printf("姓名:%s\n",p->name );
printf("性别:%s\n",p->gender );
printf("年龄:%d\n",p->age );
printf("地址:%s\n",p->address );
printf("购买品牌型号:%s\n",p->buytype );
printf("颜色:%s\n",p->color );
printf("价格:%ld\n",p->price);
printf("购买时间:%d-%d-%d\n",p->date.year ,p->date.month ,p->date.day);
}
void Del(Pointer *infor)
{
Pointer previous,current,new;
getchar();
current = *infor;
previous = NULL;
char delname;
printf("请输入你要删除的用户姓名:");
gets(delname);
while(strcmp(delname,current->name) && current != NULL)
{
previous = current;
current = current->next ;
printf("进行循环了");
}
if(current == NULL)
{
printf("并未找到该用户的信息");
return ;
}
else
{
if(previous == NULL)
{
current = current->next;
}
else
{
previous->next = current->next ;
}
printf("删除成功");
free(current);
}
}
void Sort(Pointer infor)
{
;
}
void Xuigai(char adminname[],char adminpassword[])
{
char username;
char userpassword;
getchar();
printf("请输入用户名:");
gets(username);
printf("请输入密码:");
gets(userpassword);
if(!strcmp(adminname,username) && !strcmp(adminpassword,userpassword))
{
while(1){
printf("请输入修改后的用户名:");
gets(adminname);
printf("请输入密码:");
gets(adminpassword);
printf("请再次输入密码:");
gets(userpassword);
if(!strcmp(adminpassword,userpassword))
{
printf("修改完毕");
return;
}
}
}
else
{
printf("用户名或密码输入错误,系统自动退出!!\n");
}
}
void Loadfromfile(void)
{
FILE *fp = fopen("stdin","r");
if(fp == NULL)
{
printf("读取文件失败!!");
exit(0);
};
char xinxi;
fgets(xinxi,1023,fp);
fclose(fp);
}
int main(void)
{
int count = 3;
while(1)
{
if(login())
{
printf("\n欢迎使用电瓶车信息管理系统^_^");
printf("\n登陆成功\n");
while(1)
{
Menushow();
}
}
else
{
count -= 1;
printf("\n用户或密码输入错误\n请重新输入\n");
if (count== 0)
{
printf("用户名或密码输入错误超过3次\n系统自动退出\n\n");
break;
}
else
{
printf("你还有%d次机会\n\n",count);
}
}
}
return 0;
}
我的删除函数不能删除插入的第一个,为什莫,我明明写了
current = current->next
求解答 本帖最后由 jhq999 于 2021-12-14 17:38 编辑
void Del(Pointer *infor)
{
Pointer previous,current,new;
getchar();
current = *infor;
previous = NULL;
char delname;
printf("请输入你要删除的用户姓名:");
gets(delname);
while(strcmp(delname,current->name) && current != NULL)
{
previous = current;
current = current->next ;
printf("进行循环了");
}
if(current == NULL)
{
printf("并未找到该用户的信息");
return ;
}
else
{
if(previous == NULL)
{
*infor = current->next;///////////既然是要删除开头(current),就应该把开头的位置交给第二个(current->next)
}
else
{
previous->next = current->next ;
}
printf("删除成功");
free(current);
}
}
页:
[1]