ITeye博客

c语言链表实现学生管理

aircoder · 2013-10-28 14:13 · c语言 · 编程语言 · Markdown

#include<stdio.h> #include<stdlib.h> #include<string.h>

printf(); 最后要加\n 否则有可能看不见输出的对象 typedef struct{

int id;
char name;
char tel;

} student;

typedef struct Node {

student * student;
    struct Node * next ; // 要这样,否则在后期转化有问题 node *tmp = (*nodes)-         >next;

} node;

void priinfo(){

printf("**********************************************\n");
printf("--->>>  1: addi\n");
printf("--->>>  2: del\n");
printf("--->>>  3: query\n");
printf("--->>>  4: print\n");
printf("**********************************************\n");

}

int pda(node *node){ printf("id:%d, name: %s, tel: %s\n", node->student->id, node->student->name,node->student->tel); } int pridata(node **nodes){

if (*nodes == NULL){
	printf(" db is empty\n");
	return 1;
}
pda(*nodes);

node *tmp = (*nodes)->next;
while(tmp != NULL){
	pda(tmp);
	tmp = tmp->next;
	}

return 1;

}

int del(node **nodes){

if(*nodes == NULL){

	return 1;
}

int id =-1;
scanf("%d",&id);

if((*nodes) ->student->id == id){
	
	node * ft = *nodes;
	*nodes = (*nodes) ->next;
	student * sf = ft->student;
	free(sf);
	free(ft);
	return 1;

}
node *tmp = *nodes;
node *btmp = *nodes;
	
while( tmp !=NULL && (tmp->student->id) !=id){
	btmp = tmp;	
	tmp = tmp->next;
}
if(tmp == NULL){
		printf("del not find \n");
		return 1;
	}
btmp->next = tmp->next;
student *s = tmp->student;
free(s);
free(tmp);
printf ("del success\n");	
return 1;

} int add(node **nodes){

     	student *st =(student *)malloc(sizeof(student));	
	printf("id:");
	scanf("%d",&(st->id));
	printf("name:");
	scanf("%s",(st->name));
	printf("tel:");
	scanf("%s",(st->tel));
	if(*nodes == NULL){
		*nodes = (node*)(malloc(sizeof(node)));
		(*nodes)->student = st;	
		(*nodes)->next = NULL;	

	}
	else{

		node *tmp =  (*nodes) ;
		while(tmp->next != NULL){
			
			tmp = tmp->next;
		}
		node* tmpnode = (malloc(sizeof(node)));
		tmpnode ->student = st;
		tmpnode -> next = NULL;
		tmp->next = (node *)tmpnode;
		

	}
	return 1;

} int query(node **nodes){

if(*nodes == NULL){

	return 1;	
}
int id =-1;
scanf("%d",&id);

if((*nodes) ->student->id == id){
	
	pda(*nodes);
	return 1;

}
node *tmp = *nodes;
	
while( tmp !=NULL && (tmp->student->id) !=id){
	tmp = tmp->next;
}
if(tmp !=NULL){

	pda(tmp);
}
return 1;

} int wf(int c , node **node){

switch(c){
	
	case 1:printf(" add a student\n enter id  name  tel\n"); add(node); return 1;
	case 2:printf(" del a student\n enter id will delte  \n"); del(node); return 1;
	case 3:printf(" query a student\n enter id will query  \n"); query(node); return 1;
	case 4:printf(" print all student:\n"); pridata(node); return 1;

	default :printf(" error, \n"); return 0 ;

}

}

int main(){ node *node = NULL; int c = 0; while(1){ priinfo(); printf("Enter you kind:\n"); scanf("%d",&c); if(!wf(c, &node)){ return 1; }

}

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。