Demo entry 6763779

dd

   

Submitted by dd on Oct 25, 2018 at 16:30
Language: C. Code size: 6.3 kB.

#include<stdio.h>
#include<malloc.h>
#include<string.h>
//链表的结构体 
struct List{
	char name[10];
	char num[20]; 
	struct List *next; 
};
//主菜单界面 
void MainGUI(struct List *head); 
struct List * cretaNode();
//初始化节点 
struct List *Init();
//添加一个节点及其界面 
void Add(struct List *head,struct List *Node);
void AddGUI(struct List *head);
//查询节点和查询节点的界面 
struct List * Soso(struct List *head,struct List *Node);
void SosoGUI(struct List *head);
int SosoInt(struct List *head,struct List *Node); 
struct List * Sd8osoByInt(struct List *head,int i);
//删除节点和删除节点的界面
void Del(struct List *head,struct List *Node);
void DelGUI(struct List *head);
//更改节点和更新节点的界面 
void Update(struct List *head,struct List *Node,struct List *NweNode);
void UpdateGUI(struct List *head);
//打印一个 和打印全部节点 
void PritOne(struct List *Node); 
void PriAll(struct List *head); 
//打印 
void PritOne(struct List *Node){
	if(Node!=NULL)
	 printf("姓名:%s\t电话:%s\n",Node->name,Node->num);
	else
		printf("NULL\n"); 
} 
//打印全部的节点 
void PriAll(struct List *head){
	struct List *p=head->next;
	int i=1;
	while(p!=NULL){
		printf("序号:%d\t姓名:%s\t电话:%s\n",i++,p->name,p->num);
		p=p->next;
	}
}
//添加一个数据
void Add(struct List *head,struct List *Node){
	struct List *p=head;
	while(p->next)
			p=p->next; 
	p->next=Node;
	Node->next=NULL;
} 
//初始化链表  
struct List *Init(){
	struct List *Head=(struct List *)malloc(sizeof(struct List));
	struct List *Node1=(struct List *)malloc(sizeof(struct List));
	struct List *Node2=(struct List *)malloc(sizeof(struct List));
	struct List *Node3=(struct List *)malloc(sizeof(struct List));
	struct List *Node4=(struct List *)malloc(sizeof(struct List));
 	 
	 strcpy(Node1->name,"小红");
 	 strcpy(Node2->name,"小明");
 	 strcpy(Node3->name,"小黑");
 	 strcpy(Node4->name,"小白"); 
 
  	 strcpy(Node1->num,"123");
 	 strcpy(Node2->num,"1234");
 	 strcpy(Node3->num,"12345"); 
 	 strcpy(Node4->num,"123456"); 
	
 
 	
 	Head->next=Node1;
	Node1->next=Node2;
	Node2->next=Node3; 
 	Node3->next=Node4; 
 	Node4->next=NULL; 
	return Head;
} 

//查询节点 
struct List * Soso(struct List *head,struct List *Node){
	struct List *p;
	p=head->next;
	if(p!=NULL){
		while(p){
			
			if(strcmp(p->name,Node->name )==0)break;
			p=p->next;
		}
	}
	return p;
} 
//返回搜索的 
int  SosoInt(struct List *head,struct List *Node){
	struct List *p;
	p=head->next;
	int i=-1;
	if(p!=NULL){

	while(p){
		i++;
		if(strcmp(p->name,Node->name )==0)return i;
		p=p->next;
	}
	}
	return -1;
} 
//通过序号查询节点 
struct List *  SosoByInt(struct List *head,int i){
	struct List *p;
	p=head->next;
 
	if(p!=NULL){

		while(p){
			i--;
			if(i==0)return p;
			p=p->next;
		}
	}
	return NULL;
} 
//删除节点 
void Del(struct List *head,struct List *Node){
 			int i=SosoInt(head,Node);
 			printf("%d\n",i);
		  	i-=1;
 			printf("%d\n",i);
 			 struct List *p=head,*temp;
		if(i>-1){
			 
			
			 while(i>-1){
			 	p=p->next;
				i--;
			}
			temp=p->next;
			p->next=p->next->next;
		
		}else if(i==-1){
			 temp=p->next;
			p->next=p->next->next;
		}
			free(temp);
			
} 
//修改数据 
void Update(struct List *head,struct List *Node,struct List *NweNode){
		struct List *p= Soso(head,Node);
		strcpy(p->num,NweNode->num); 
} 
//添加数据的界面
void AddGUI(struct List *head){
	struct List *temp=(struct List *)malloc(sizeof(struct List));
		printf("添加数据的姓名==>");
	scanf("%s",temp->name);
	printf("添加数据的电话==>");
	scanf("%s",temp->num);
	Add(head,temp);
} 
//删除数据的界面 
void DelGUI(struct List *head){
	struct List *Node=(struct List*)malloc(sizeof(struct List)) ;
	printf("请输入删除数据的姓名==>"); 
		scanf("%s",Node->name);
	 
	struct List *temp=Soso(head,Node);
	if(temp!=NULL){
		printf("你确定删除+姓名:%\t电话:%s+这条数据吗?",temp->name,temp->num); 
		printf("输入Y删除数据,输入任意键不删除数据\n");
		fflush(stdin);
			if(getchar()=='Y'){
		  Del( head, Node);
			printf("删除数据成功!\n");
		}else{
			printf("删除数据失败!\n");
		}
	}else{
			printf("没有找到==>姓名:%s这条数据\n",Node->name);
	}
	free(Node);
	free(temp);

} 
//修改数据的界面 
void UpdateGUI(struct List *head){
		struct List *p,*temp=(struct List*)malloc(sizeof(struct List));
		int i=0;
	printf("请输入更改数据的名字==>");
		scanf("%s",temp->name);
			p=Soso(head,temp); 
			if(p!=NULL){
				 printf("要修改的数据==>姓名:%s\t电话:%s\n修改姓名按1\t修改电话号码按2\n",p->name,p->num);
				 scanf("%d",&i);
				 if(i==1){
				 	printf("名字==>");
 				 	scanf("%s",p->name);
 					 
 						printf("修改数据成功!\n");
 							printf("新数据==>姓名:%s\t电话:%s",p->name,p->num); 
 				}else if(i==2){
				 	printf("电话==>");
 						scanf("%s",p->num);
 						 
 						printf("修改数据成功!\n");
 							printf("新数据==>姓名:%\t电话:%s",p->name,p->num); 
				 }else{
 					printf("输入错误!\n");
 				}
 			 
			}else{
				 printf("没有名字为  %s  的数据\n",temp->name);
			}
			free(temp); 
}
//查询数据的界面 
void SosoGUI(struct List *head){
	printf("按1序号查询 按2姓名查询\n");
		struct List *p,*temp=(struct List*)malloc(sizeof(struct List));
	int i=0;
	scanf("%d",&i);
	if(i==1){
		printf("输入序号==>");
		scanf("%d",&i);
 		p= SosoByInt(head,i);
 		if(p!=NULL){
		 	printf("查询到的数据==>姓名:%s\t电话;%s\n",p->name,p->num);
		 }else{
 			 printf("没有序号为%d的数据\n",i);
 		}
		
		
	}else if(i==2){
			printf("输入姓名==>");
			scanf("%s",temp->name);
			p=Soso(head,temp); 
			if(p!=NULL){
				 printf("查到的数据==>姓名:%s\t电话:%s\n",p->name,p->num);
			}else{
				 printf("没有名字为  %s  的数据\n",temp->name);
			}
	}else{
		printf("输入错误!\n");
	}
	free(temp);//释放空间 
}
//菜单主界面 
void MainGUI(struct List *head){
		int i=0;
		while(1){
			printf("按1添加数据 按2删除数据 按3更改数据 按4查询数据 按5打印全部数据 按6退出程序\n");
			scanf("%d",&i);
			fflush(stdin);//万一用户输入的是汉字 
			switch(i){
				case 1:
				        AddGUI(head);//添加数据 
				    				break;
				case 2:
				        DelGUI(head); //删除数据 
									break;
				 case 3:   				
			  			UpdateGUI(head); //修改数据 
			  							break; 
			 	 case 4:   				 1
			  			SosoGUI(head);//搜索数据 
			  							break;
				case 5:
				      PriAll(head);//打印全部数据 
				    				break;
				case 6://退出系统 
					return; 
				default:
					printf("输入错误!\n");
				 
		
			}	
		}
}

int main(){
	struct List *head=Init(); 
	PriAll(head);
	MainGUI(head);
	 printf("\n再见亲爱的兄弟!\n");
	return 0;
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).