题目设定是:
建立一个链表,每个节点的信息为:学号是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();
}
}
}