#include<stdio.h>
#include<malloc.h>
#include<string.h>
struct user
{
int ino;
char name[10];
char password[10];
struct user *nest;
struct user *pre;
};
//遍历
void searchall(struct user * head)
{
struct user * temp=head;
if (temp==0)
{
printf("空链表!\n");
}
while(1)
{
printf("%d\t%s\t%s\n",temp->ino,temp->name,temp->password);
if(temp->nest==head) break;
temp=temp->nest;
}
}
//查找
int search(struct user *head,char *pfindname)
{
struct user *temp=head;
while(1)
{
if(strcmp(temp->name,pfindname)==0)
{
printf("%d\t%s\t%s\n",temp->ino,temp->name,temp->password);
return 1;
}
if(temp->nest==head) break;
temp=temp->nest;
}
return -1;
}
//出栈
struct user* pop(struct user *head)
{
struct user *temp=head;
if (head->nest==head)
{ temp=0;
printf("出栈成功!\n");
free(head);
return temp;
}
head=temp->pre;
head->pre->nest=head->nest;
head->nest->pre=head->pre;
free(head);
printf("出栈成功!\n");
return temp;
}
//入栈
struct user *push(struct user *head,struct user *adduser)
{
struct user * temp=head;
struct user * pnew=NULL;
if(head==NULL)
{
pnew=(struct user *)malloc(sizeof(struct user ));
*pnew=*adduser;
pnew->nest=pnew;
pnew->pre=pnew;
temp=pnew;
printf("入栈成功!\n");
return temp;
}
pnew=(struct user *)malloc(sizeof(struct user ));
*pnew=*adduser;
pnew->nest=pnew;
pnew->pre=pnew;
head->pre->nest=pnew;
pnew->nest=head;
pnew->pre=head->pre;
head->pre=pnew;
printf("入栈成功!\n");
return temp;
}
int main()
{
int icount=0;
struct user *head=NULL;
int i=0;
int select=0;
int iScanfResult;
while(1)
{
printf("------------------------------------\n");
printf("------------用户管理----------------\n");
printf("-------------2 遍历-----------------\n");
printf("-------------3 查找-----------------\n");
printf("-------------6 出栈-----------------\n");
printf("-------------8 入栈-----------------\n");
printf("-------------0 退出------------------\n");
printf("------------------------------------\n");
printf("\n");
while(1)
{
printf("请选择:");
iScanfResult=scanf("%d",&select);;
if (iScanfResult>0) break;
else
{
printf("请输入正确的数字!\n");
fflush(stdin);
}
}
switch(select)
{
case 2:
{
//遍历
searchall(head);
}break;
case 3:
{
char pfindname[10];// 查找一个临时名字
int ispfindname=0;
int i=0;
int flag=0;
printf("请你输入一个你要查找的用户的名字\n");
scanf("%s",&pfindname);
ispfindname=search(head,pfindname);
if(-1==ispfindname)
{printf("没有找到\n");
}
}break;
case 6:
{//删除
head=pop(head);
}break;
case 8:
{
struct user insertpuser;
printf("请输入新的用户的ID\n");
scanf("%d", &insertpuser.ino);
printf("请输入新的用户的名字\n");
scanf("%s",insertpuser.name);
printf("请输入新的用户的密码\n");
scanf("%s",insertpuser.password);
head=push(head,&insertpuser);
}break;
case 0:
{
}break;
}
}
return 0;
}
数据结构之栈
最新推荐文章于 2024-09-05 16:03:39 发布