简单的学生信息链表

题目设定是:

建立一个链表,每个节点的信息为:学号是16位数字,学生信息包括学号,姓名,年龄,籍贯基本信息,完成以下功能。支持学生信息增加、查找、删除。

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<conio.h>
using namespace std;
typedef struct student
{
	char stuid[20];
	char name[10];
	int  age;
	char birthplace[10];
	struct student *next;
}node;

char redo(){
	char action;

	cout<<"Next operation:"<<endl;
	cout<<"1: To continue on the current operation"<<endl;
	cout<<"2: To return to the main menu"<<endl;

	// 不停的等待用户输入,直到输入正确
	while(1){
		cin>>action;
		if(action!='1' && action!='2'){
			cout<<"No corresponding operation! Please check and reset:";
		}else{
			return action;
			break;
		}
	}
}

void Print(node *p)
{
	printf("%16s%16s%16d%16s\n",p->stuid,p->name,p->age,p->birthplace);
}

void printAll(node* head)
{
	node *p;
	p = head->next;
	printf("%16s%16s%16s%16s\n","STUID","NAME","AGE","BIRTHPLACE");
	for(int i = 0; i<64 ; i++)
		cout<<"*";
	cout<<endl;
	while(p != NULL)
	{
		printf("%16s%16s%16d%16s\n", p->stuid, p->name, p->age, p->birthplace);
		p = p->next;
	}
	printf("\nNo corresponding menu.Please press any key to return the main menu..."); 
	getch();
}

void seek(node *head, char num)//
{
	char str[20];
	int count = 0;
	cout<<"please input the information you seek:";
	cin>>str;
	node *p = head->next;
	while(p != NULL)
	{
		switch(num)
		{
		case '1': if(strcmp(p->stuid, str) == 0) {count++;Print(p);} 	break;
		case '2': if(strcmp(p->name , str) == 0) {count++;Print(p);}	break;
		}
		p = p->next;
	}
	if(count == 0)
		cout<<"No relative information about '"<<str<<"'!"<<endl;
}

void menu(void)
{
	cout<<"* * * * * * * * * * * * * * * * * *"<<endl;
	cout<<"*          Linklist Menu          *"<<endl;
	cout<<"*           1:   Add              *"<<endl;
	cout<<"*           2:  Search            *"<<endl;
	cout<<"*           3:  Delete            *"<<endl;
	cout<<"*           4:  Print             *"<<endl;
	cout<<"*           0:   Exit             *"<<endl;
	cout<<"* * * * * * * * * * * * * * * * * *"<<endl;
	cout<<endl;
	cout<<"Input Linklist Menu Number:";
}

bool select(char option)
{
	bool decision;
	switch(option)
	{
	case 'y':	
	case 'Y':	return decision = true;		break;
	default:	return decision = false;	break;
	}
	return decision;
}

node *create(void)//创建空链表,返回表头,head
{
	node *head;
	head = (node*)malloc(sizeof(node));
	head->next = NULL;
	return head;
}
node *add(node *head)//返回表头
{
	cout<<"-*- - - - - - - - - - -Add- - - - - - - - - - - -*-"<<endl;
	cout<<"Please input STUID, NAME, AGE, BIRTHPLACE in order!"<<endl;
	node *p, *s;
	bool cycle = true;
	char selection;
	p = head;
	while(p->next != NULL)
	{
		p = p->next;
	}
	while(cycle)
	{
		cout<<"Add Infu:"<<endl;
		s = (node*)malloc(sizeof(node));
		p->next = s;
		cin>>s->stuid;
		cin>>s->name;
		cin>>s->age;
		cin>>s->birthplace;
		p = s;
		cout<<"Continue to enter(Y/any key):";
		cin>>selection;
		cycle = select(selection);
	}
	p->next =NULL;
	cout<<endl;
	return head;
}

node *search(node *head)
{
	cout<<"* * * * * * * * * * * * * * * * * * * * * "<<endl;
	cout<<"*           Search Preferences          *"<<endl;
	cout<<"*             1.by stuid                *"<<endl;
	cout<<"*             2.by name                 *"<<endl;
	cout<<"* * * * * * * * * * * * * * * * * * * * *"<<endl;
	cout<<endl;
	cout<<"Input Search Mode:";
	char i;
	while(1)
	{
		cin>>i;
		if(i != '1' && i != '2')
			cout<<"No corresponding menu. Please reset:";
		else
			break;
	}
	seek(head, i);
	return head;
}

node* del(node *head)//删除节点操作需要三个节点,前一个h0,当前h1,下一个h2.
{
	cout<<"-*- - - - - - - - - - -Del- - - - - - - - - - - -*-"<<endl;
	cout<<endl;

	node *p0, *p, *s;//p存储当前的节点h1,p0存储p的前一个节点h0。
	int count = 0;
	p0 = head;
	p = head->next;
	cout<<"Please input the NAME in the information you will delete: ";
	char str1[20];
	cin>>str1;
	while(p != NULL)
	{
		if(strcmp(p->name ,str1) == 0)//若p为所删节点h1
		{
			count++;
			s = p;                    //p赋给s,s为h1
			p = p->next;			  //此时p指向下一个节点为h2
			p0->next = p;             //p0为h0,所以要释放h1的空间,需要h0->next指向h2(也就是p)的首地址。
			free(s);
			cout<<"Deleted Successfully!"<<endl;
			continue;
		}
		p0 = p;					
		p  = p->next;
	}
	if(count == 0)
	{
		cout<<"No relative information about that you will delete!"<<endl;
	}
	return head;
}

void main(void)
{
	node *head = NULL;
	char num;
	head = create();
	while(1)
	{
		system("CLS");
		menu();
		cin>>num;
		system("CLS");
		switch(num)
		{
		case '1': head = add(head);	    break;
		case '2': while(1)
				  {
					  head = search(head); 
					  if(redo() == '1')
						  system("CLS");
					  else
						  break;
				  }
				  break;
		case '3': while(1)
				  {
					  head = del(head);
					  if(redo() == '1')
						  system("CLS");
					  else
						  break;
				  }	
				  break;
		case '4': printAll(head);		break;
		case '0': exit(0);				break;
		default : printf("No corresponding menu.Please press any key to return the main menu..."); getch();
		}
	}
}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值