Open shencang opened 6 years ago
'''c++
using namespace std;
typedef long int datatype_studentnum;
typedef string datatype_name;
typedef long int datatype_phonenum;
typedef struct link_node
{
datatype_name name;
datatype_phonenum pnum;
datatype_studentnum snum;
struct link_node *next;
}node;
node *init()
return NULL;
}
node find(node head,int i)
int j=1;
node *p=head;
if(i<1)
while(p&&i!=j)
p=p->next;
j++;
return p;
} node insert(node head,datatype_name name,datatype_phonenum pnum,datatype_studentnum snum,int i)
node p=NULL,q;
q=find(head,i);
if(p&&i!=0)
printf("\n找不到第%d个节点,不能插入%s的通讯录。",i,name);
else
p=(node*)malloc(sizeof(node));
p->name=name;
p->pnum=pnum;
p->snum=snum; if(i==0)
p->next=head;
head=p;
p->next;
q->next=p;
} }
return head;
node deletedata(node head,datatype_name name,datatype_phonenum pnum,datatype_studentnum snum)
{ node pre=NULL,p;
if(!head)
printf("单链表是空的.");
return head; }
p=head;
while(p&&p->name!=name){
pre=p;
if(p)
if(!pre)
head =head->next; }else
pre->next=p->next;
free(p); } }
void display(node *head)
node *p;
p= head;
printf("单链表是空的."); }else
printf("\n输出所有信息:");
while(p)
}printf("\n姓名%s\n学号:%d\n联系方式:%d",p->name,p->snum,p->pnum);
} } node *inport()
{ node head,s;
head=NULL;
printf("请输入姓名:\n");
scanf_s("%s",&name);
printf("请输入学号:\n");
scanf_s("%d",&snum);
printf("请输入电话:\n");
scanf_s("%d",&pnum);
while (pnum!=0) /以0结束输入/
{ s=(node)malloc(sizeof(node)); /生成待插入结点*/
s->pnum=pnum;
s->name=name;
s->snum=snum;
s->next=head; /将新结点插入到链表最前面/
head=s;
return head; /返回建立的单链表/
} void ins(node *head){
string s;
long int a,b;
cout<<" *****输入执行的操作****"<<endl;
cout<<" *****1.建立通讯录**"<<endl;
cout<<" *****2.插入通讯信息****"<<endl;
cout<<" *****3.删除信息****"<<endl;
cout<<" *****4.显示信息****"<<endl;
cout<<" *****0.退出程序****"<<endl;
int c;
cin>>c;
switch(c)
case 1: head = init();break;
case 2: {
cin>>s>>a>>b;
}head= insert(head,s,a,b,1);
break;
case 3:
head=deletedata(head,s,a,b);break;
case 4: display(head);break;
case 0: exit(1);
default:
cout<<"输入错误";
ins(head);
}}
void main(){
// node *head;
// head=inport();
//// int a[N];
//// for(int i=0;i<N;i++)
//// {
//// a[i]=(NiNiN9+10000)i-999;
//// }
////for(int i=0;i<N;i++)
//// printf("%d\t",a[i]);
//
// string s[SN];
// for(int i=0;i<SN;i++)
// {
// s[i]="无名氏";
// }
// int stu[SN],phonenum[SN];
// stu[i]=(SNiSNiSN9+10000)i-999;
// phonenum[i]=(SNiSNiSN9+10000)i-999;
// //incsert(head,s[i],stu[i],phonenum[i],i);
//display(head);
node *head=NULL,p;
'''c++
include
include
include
using namespace std;
define SN 10
typedef long int datatype_studentnum;
typedef string datatype_name;
typedef long int datatype_phonenum;
typedef struct link_node
{
datatype_name name;
datatype_phonenum pnum;
datatype_studentnum snum;
struct link_node *next;
}node;
node *init()
{
return NULL;
}
node find(node head,int i)
{
int j=1;
node *p=head;
if(i<1)
{
return NULL;
}
while(p&&i!=j)
{
p=p->next;
j++;
}
return p;
} node insert(node head,datatype_name name,datatype_phonenum pnum,datatype_studentnum snum,int i)
{
node p=NULL,q;
q=find(head,i);
if(p&&i!=0)
{
printf("\n找不到第%d个节点,不能插入%s的通讯录。",i,name);
}
else
{
p=(node*)malloc(sizeof(node));
p->name=name;
p->pnum=pnum;
p->snum=snum; if(i==0)
{
p->next=head;
head=p;
}
else
{
p->next;
q->next=p;
} }
return head;
}
node deletedata(node head,datatype_name name,datatype_phonenum pnum,datatype_studentnum snum)
{ node pre=NULL,p;
if(!head)
{
printf("单链表是空的.");
return head; }
p=head;
while(p&&p->name!=name){
pre=p;
p=p->next;
}
if(p)
{
if(!pre)
{
head =head->next; }else
{
pre->next=p->next;
free(p); } }
return head;
}
void display(node *head)
{
node *p;
p= head;
if(!head)
{
printf("单链表是空的."); }else
{
printf("\n输出所有信息:");
while(p)
{
}printf("\n姓名%s\n学号:%d\n联系方式:%d",p->name,p->snum,p->pnum);
p=p->next;
} } node *inport()
{ node head,s;
datatype_name name;
datatype_phonenum pnum;
datatype_studentnum snum;
head=NULL;
printf("请输入姓名:\n");
scanf_s("%s",&name);
printf("请输入学号:\n");
scanf_s("%d",&snum);
printf("请输入电话:\n");
scanf_s("%d",&pnum);
while (pnum!=0) /以0结束输入/
{ s=(node)malloc(sizeof(node)); /生成待插入结点*/
s->pnum=pnum;
s->name=name;
s->snum=snum;
s->next=head; /将新结点插入到链表最前面/
head=s;
printf("请输入姓名:\n");
scanf_s("%s",&name);
printf("请输入学号:\n");
scanf_s("%d",&snum);
printf("请输入电话:\n");
scanf_s("%d",&pnum);
}
return head; /返回建立的单链表/
} void ins(node *head){
string s;
long int a,b;
cout<<" *****输入执行的操作****"<<endl;
cout<<" *****1.建立通讯录**"<<endl;
cout<<" *****2.插入通讯信息****"<<endl;
cout<<" *****3.删除信息****"<<endl;
cout<<" *****4.显示信息****"<<endl;
cout<<" *****0.退出程序****"<<endl;
int c;
cin>>c;
switch(c)
{
case 1: head = init();break;
case 2: {
cin>>s>>a>>b;
}head= insert(head,s,a,b,1);
break;
case 3:
{
cin>>s>>a>>b;
}
head=deletedata(head,s,a,b);break;
case 4: display(head);break;
case 0: exit(1);
default:
cout<<"输入错误";
ins(head);
}}
void main(){
// node *head;
// head=inport();
//// int a[N];
//// for(int i=0;i<N;i++)
//// {
//// a[i]=(NiNiN9+10000)i-999;
//// }
////for(int i=0;i<N;i++)
//// {
//// printf("%d\t",a[i]);
//// }
//
// string s[SN];
// for(int i=0;i<SN;i++)
// {
// s[i]="无名氏";
// }
//
// int stu[SN],phonenum[SN];
// for(int i=0;i<SN;i++)
// {
// stu[i]=(SNiSNiSN9+10000)i-999;
// }
// for(int i=0;i<SN;i++)
// {
// phonenum[i]=(SNiSNiSN9+10000)i-999;
// }
// for(int i=0;i<SN;i++)
// {
// //incsert(head,s[i],stu[i],phonenum[i],i);
//
// }
//display(head);
node *head=NULL,p;
ins(head);
}