《数据结构》课程设计题目急急!!!!

2024-10-30 10:23:21
推荐回答(1个)
回答1:

又来了
这个是学生的管理的 改改吧
#include
#include"string.h"
#include "process.h"
#include
struct Node
{
int age;
char* name;
Node* next;

};
struct newnode
{
char newname[20];
int newage;

};
void AddTohead(Node* node,char* name,int age)
{
if(!node)
return;
Node* newnode = new Node;
if(name)
{
int cd = strlen(name)+1;
newnode->name = new char [cd];
strcpy(newnode->name,name);
}
else newnode->name = NULL;
newnode->age = age;
newnode->next = node->next;
node->next = newnode;

}
void Show(Node* node)
{
Node* p = NULL;

p = node->next;
while(p!=NULL&&p->name!=NULL)//我的软肋 我没有注意指针的真正的指向 以后要注意 这是我看以前的程序中看出来的问题 while(p)这是我原先写 切记切记
{ //看完程序后我还是没写对 我写对了
cout<name<<" "<age<
p = p->next;

}

}
void Delete(Node* node)
{
Node* del;
while(node->next)
{
del = node->next;
if(del->next)
delete [] del->name;
node->next = del->next;
delete del;

}

}
void AddToList(Node* node,char* name,int age)
{
if(!node)
return;
Node* newnode = new Node;
while(node->next)
node = node->next;
if(name)
{
int cd = strlen(name)+1;
newnode->name = new char [cd];
strcpy(newnode->name,name);
}
else newnode->name = NULL;
newnode->age = age;
node->next = newnode;
newnode->next = NULL;

}
void FindAndAdd(Node* node,char* findname,char* addname,int addage,int canshu)
{
if(!node)
return;
Node* head = node;
Node* m = NULL;
m = node->next;
do
{
if (!strcmp(m->name,findname))
break;
m = m->next;
} while (m);

if(m==NULL)
{
cout<<"nobody"< }
else
{
switch(canshu)
{
case 1:
{
Node* p = head;
while(p->next!= m )
{
p = p->next;
}
Node* newnode = new Node;
if(addname)
{
int cd = strlen(addname)+1;
newnode->name = new char [cd];
strcpy(newnode->name,addname);
}
else
{
newnode->name = new char;
strcpy(newnode->name,"somebody");
}
newnode->age = addage;
newnode->next = p->next;
p->next = newnode;

} break;
case 2:
{
Node* newnode = new Node;
if(addname)
{
int cd = strlen(addname)+1;
newnode->name = new char [cd];
strcpy(newnode->name,addname);

}
else
{
newnode->name = new char;
strcpy(newnode->name,"somebody");
}
newnode->age = addage;
newnode->next = m->next;
m->next = newnode;

} break;
default:break;
}

}

}
void DelSomeBody(Node* node,char* name)
{
if(!node)
return;
Node* head = node;
if(!name)
cout<<"the name please";
else
{
Node* p = node->next;
Node* m = node;
while(m)
{
if(!strcmp(p->name,name))
break;
m = m->next;
p = p->next;
}
if(p==NULL)
{
cout<<"no one";
}
else
{
while(head->next!= p)
head = head->next;
delete [] p->name;
head->next = p->next;
delete p;

}
}
}

void Change(Node* node,char* name,char* newname,int age)
{
if(!node)
return;
Node* head = node;
if(!name)
cout<<"the name please";
else
{
Node* p = node->next;
Node* m = node;
while(m)
{
if(!strcmp(p->name,name))
break;
m = m->next;
p = p->next;
}
if(p==NULL)
{
cout<<"no one";
}
else
{
int cd = strlen(newname)+1;
p->name = new char [cd];

strcpy(p->name,newname);
p->age = age;
}

}

}
void Find(Node* node,char* findname)
{

if(!node)
return;
Node* head = node;
Node* m = NULL;
m = node->next;
if(!m)
cout<<"链表为空的 不存在 请选择操作"< return;
do
{
if (!strcmp(m->name,findname))
break;
m = m->next;
} while (m);

if(m==NULL)
{
cout<<"nobody"< }
else
{
cout<name<<"的年龄是"<age<
}

}

void CleanTheWindow()
{
system("cls");
}

void Keep(Node* node,char* fname)
{
Node* p = node->next;
newnode newd;
FILE* ip = fopen(fname,"wb");
if(!ip)
{
cout<<"wrong"< return;
}
while(p)
{
strcpy(newd.newname,p->name);
newd.newage = p->age;
fwrite(&newd,sizeof(newnode),1,ip);
p = p->next;

}
fclose(ip);
}
void CreatList(Node* node,char* fname)
{
newnode newd;
FILE* ip =fopen(fname,"rb");
if(!ip)
{
cout<<"wrong"< return;
}
Delete(node);
while(!feof(ip))
{
int a =fread(&newd,sizeof(newnode),1,ip);
if(a==1)
AddTohead(node,newd.newname,newd.newage);

}
fclose(ip);

}

void main()
{
Node node;
node.age = NULL;
node.name = NULL;
node.next = NULL;

while(1)

{
cout<<"请选择您的操作1:添加到头部(输入姓名,年龄)"< cout<<" 2:添加到尾部(输入姓名,年龄)"< cout<<" 3:查找某人的年龄 "< cout<<" 4:在某人的前面或后面插入"< cout<<" (输入查找的姓名,输入要插入的名字,输入要插入的名字,和年龄)"< cout<<" (输入插入类型1:在前插入 2:在后插入)"< cout<<" 5:修改某人的信息(输入要修改人的名字 输入要修改的信息姓名,年龄)"< cout<<" 6:删除某人的信息(输入你要删除的人的姓名)"< cout<<" 7:删除链表"< cout<<" 8:显示"< cout<<" 9:清屏"< cout<<" 10:保存"< cout<<" 11:显示保存的内容"< cout<<" 0:退出"< int a;
cin>>a;//如何对输入的数据进行判断,比如我输入adfd字符型的时候,我想提示用户输入错误

switch(a)
{
case 1:
{
cout<<"请输入要加在头部的姓名和年龄"< char* name = new char;
int age;
cin>>name;
cin>>age;
AddTohead(&node,name,age);
cout<<"加入头部完成 请选择下一步操作"< } break;
case 2:
{
cout<<"请输入要加在尾部的姓名和年龄"< char* name = new char;
int age;
cin>>name;
cin>>age;
AddToList(&node,name,age);
cout<<"加入尾部完成 请选择下一步操作"< } break;
case 3:
{
cout<<"请输入要查找人的姓名"< char* name = new char;
cin>>name;
Find(&node,name);
} break;
case 4:
{
cout<<"请按上面的输入"< char* name = new char;
char* name2 = new char;
int age;
int cs;
cin>>name;
cin>>name2;
cin>>age;
cin>>cs;
FindAndAdd(&node,name,name2,age,cs);
cout<<"4输入完成"<
} break;
case 5:
{
cout<<"输入要修改的信息和修改的信息"< char* name = new char;
char* name2 = new char;
int age;
cin>>name;
cin>>name2;
cin>>age;
Change(&node,name,name2,age);
cout<<"修改完成"<
} break;
case 6:
{
cout<<"输入要删除的姓名"< char* name = new char;
cin>>name;
DelSomeBody(&node,name);
cout<<"删除完成"< } break;
case 7:
{
cout<<"确定要删除全部的链表吗?1 是 2否"< int npd;
cin>>npd;
if(npd==1)
{
Delete(&node);
}
else break;
} break;
case 8:
{
cout<<"当前的链表是"< Show(&node);
} break;
case 9:
{
CleanTheWindow();
} break;
case 10:
{
cout<<"请输入要保存的文件的名称"< char a[10];
cin>>a;
Keep(&node,a);
}
break;
case 11:
{
cout<<"请输入保存的文件的名称"< char a[10];
cin>>a;
CreatList(&node,a);
Show(&node);
}
break;
case 0:
{
goto stop;

} break;
default :cout<<"输入有误重新输入"<
}
}

stop: cout<<"exit"<
}
其实我也很菜的