帖子地址点击打开链接,太长了,放这里吧。
回答帖子的主人:里面的printf和scanf运行起来怪怪的,总是运行很多次,感觉莫名其妙的,我用了cout和cin。
改的地方还是有的,自己可以对比一下。
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <malloc.h>
#include <string>//加的
using namespace std;
#define LEN sizeof(struct person)
struct person
{
int num;
char name[10];
int age;
int salary;
struct person * next;
};
int a=0;
person * creat()//创建带头结点的链表。
{
cout<<"--------------- creat() 被调用 ---------------"<<endl;
person *head;//指向头结点的指针
person *current;//指向当前节点的指针
head = (struct person * )malloc(LEN);
cout<<"建立第一个节点,依次输入:员工号,姓名,年龄,薪水"<<endl;
current = (struct person * )malloc(LEN);
cin>>current->num;
cin>>current->name;
cin>>current->age;
cin>>current->salary;
head=current;
a++;
while (1)
{
char c;
cout<<"继续?(y/n) ";
cin>>c;
if (c=='n')
{
break;
}
cout<<"依次输入:员工号,姓名,年龄,薪水"<<endl;
person * newnode = (struct person * )malloc(LEN);
cin>>newnode->num;
cin>>newnode->name;
cin>>newnode->age;
cin>>newnode->salary;
a++;
current->next=newnode;
current=newnode;
}
current->next=NULL;//这个是必须的,否则打印的时候出错
return (head);
}
void print(person * p)
{
cout<<"--------------- print() 被调用 ---------------"<<endl;
while(p!= NULL)
{
cout<<p->num<<" "<<p->name<<" "<<p->age<<" "<<p->salary<<endl;
p = p->next;
}
}
void search1( person * p)//按照员工号查找
{
cout<<"--------------- search1() 被调用 ---------------"<<endl;
int b;
cout<<"Please input a person number: ";
cin>>b;
if(p != NULL)
{
while(b!=p->num && p->next!=NULL)
{
p = p->next;
}
if (b==p->num)
{
cout<<"number="<<p->num<<" name="<<p->name<<" age="<<p->age<<" salary="<<p->salary<<endl;
}
else
{
cout<<"未发现目标!"<<endl;
}
}
else
printf("没头的链表,这不可能存在的!!\n");
}
void search2( person * p)//按照员工姓名查找
{
cout<<"--------------- search1() 被调用 ---------------"<<endl;
char name[10];
cout<<"Please input a person name: ";
cin>>name;
if(p != NULL)
{
while(strcmp(name,p->name) && p->next!=NULL)
{
p = p->next;
}
if (!strcmp(name,p->name))
{
cout<<"number="<<p->num<<" name="<<p->name<<" age="<<p->age<<" salary="<<p->salary<<endl;
}
else
{
cout<<"未发现目标!"<<endl;
}
}
else
printf("没头的链表,这不可能存在的!!\n");
}
person * insert( person * p)//在尾部追加一个节点
{
cout<<"--------------- insert() 被调用 ---------------"<<endl;
person * newnode;//新加的节点
person *current;//指向当前节点的指针
current=p;
while (current->next != NULL)
{
current = current->next;
}
cout<<"依次输入:员工号,姓名,年龄,薪水"<<endl;
newnode=(person*)malloc(LEN);
cin>>newnode->num;
cin>>newnode->name;
cin>>newnode->age;
cin>>newnode->salary;
a++;
current->next=newnode;
current=newnode;
current->next = NULL;
return (p);
}
person * del1( person * p)//按照员工号删除一个节点
{
cout<<"--------------- del1() 被调用 ---------------"<<endl;
person *current;//指向当前节点的指针
person *q;//找到的那个节点,需要删除的
int num;
if (a==1)
{
cout<<"只有头结点,不能删除!"<<endl;
return p;
}
cout<<"Please input a person number : ";
cin>>num;
current=p->next;//当前节点从头部之后的节点开始,寻找目标
while (current->next != NULL && current->next->num!=num)
{
current = current->next;
}
if (current->next==NULL)
{
cout<<"未发现目标,无法删除!"<<endl;
return p;
}
q=current->next;
current->next=current->next->next;
delete q;
a--;
return(p);
}
person * del2( person * p)//按照员姓名删除一个节点
//删除当前节点的下一个节点
{
cout<<"--------------- del2() 被调用 ---------------"<<endl;
person *current;//指向当前节点的指针
char name[10];
if (a==1)
{
cout<<"只有头结点,不能删除!"<<endl;
return p;
}
cout<<"Please input a person name : ";
cin>>name;
current=p;//当前节点从头部之后的节点开始,寻找目标
while (current->next != NULL && strcmp(name,current->next->name))
{
current = current->next;
}
if (current->next->next!=NULL)
{
current->next=current->next->next;
a--;
}
return(p);
}
person * change( person * p)//改变某个用户的信息,索引项为name
{
cout<<"--------------- change() 被调用 ---------------"<<endl;
person *current;//指向当前节点的指针
char name[10];
cout<<"Please input a person name : ";
cin>>name;
current=p;//当前节点从头部节点开始,寻找目标
while (current->next != NULL && strcmp(name,p->name))
{
current = current->next;
}
if (current==NULL)
{
cout<<"未发现目标,无法更改信息!"<<endl;
return p;
}
cout<<"更改信息,依次输入:员工号,姓名,年龄,薪水 "<<endl;
cin>>current->num;
cin>>current->name;
cin>>current->age;
cin>>current->salary;
return(p);
}
person * sort( person * p)//按员工号升序排序
{
cout<<"--------------- sort() 被调用 ---------------"<<endl;
//用交换排序,时间复杂度为O(n*n)
person * pi;//有序序列的头指针
person * pj;//有序序列的为指针
person * temp=(struct person * )malloc(LEN);
for (pi=p;pi!=NULL;pi=pi->next)
{
for (pj=pi;pj!=NULL;pj=pj->next)
{
if (pj->num < pi->num)
{
temp->num=pj->num;
strcpy(temp->name,pj->name);
temp->age=pj->age;
temp->salary=pj->salary;
pj->num=pi->num;
strcpy(pj->name,pi->name);
pj->age=pi->age;
pj->salary=pi->salary;
pi->num=temp->num;
strcpy(pi->name,temp->name);
pi->age=temp->age;
pi->salary=temp->salary;
//赋值比较麻烦,可以重载赋值“=”运算
}
}
}
return p;
}
int _tmain(int argc, _TCHAR* argv[])
{
int m ; //用户的order number
person * h;
printf("############员工信息菜单#########################\n");
printf("# 1.输入信息 \n");
printf("# 2.打印信息 \n");
printf("# 3.按员工号查找 \n");
printf("# 4.按姓名查找 \n");
printf("# 5.插入信息 \n");
printf("# 6.按员工号删除信息\n");
printf("# 7.按姓名删除信息 \n");
printf("# 8.更改信息 \n");
printf("#################################################\n");
while (1)
{
cout<<"选择菜单选项:";
cin>>m;
if (m<1 || m>9)
{
break;;
}
switch(m)
{
case 1: h = creat();break;
case 2: print(h);break;
case 3: search1(h);break;
case 4: search2(h);break;
case 5: h=insert(h);break;
case 6: h=del1(h);break;
case 7: h=del2(h);break;
case 8: h=change(h);break;
case 9: h=sort(h);break;
default : printf("ERROR!\n");break;
}
}
system("pause");
return 0;
}
一直都不喜欢黑黑的菜单,用起来也觉得不舒服。
代码已经调试通过,如果还有问题,日后改正。
应该是这个月的最后一篇了。下个月要写论文了,不能这么玩下去了。
祝大家好运!