数据结构之栈

#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值