| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 383 人关注过本帖
标题:[求助]进来讨论一下指针赋值问题!
取消只看楼主 加入收藏
菜鸟1号
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2006-4-7
收藏
 问题点数:0 回复次数:0 
[求助]进来讨论一下指针赋值问题!

#include<iostream.h>
#include<iomanip.h>
#include<stdlib.h>
const int namelength=20;
const int phonelength=20;
struct employee//结构体
{
char name[namelength];
char num[phonelength];
long ID;
struct* next;
};

struct employee* creat(void);//创建链表
struct employee* insert(struct employee*);//插入链表
void show(struct employee*);//显示链表
struct employee* modify(struct employee*);//修改链表
struct employee* DELETE(struct employee*);//删除链表
//函数声明


void main()//主函数
{long id;
char answer;
struct employee* head=NULL;
cout<<"A.creat an initial linked list of names and phone numbers"<<endl;//界面窗口
cout<<"B.insert a new structure into the linked list"<<endl;
cout<<"C.modify an existing structure in the linked list"<<endl;
cout<<"D.delete an existing structure from the list"<<endl;
cout<<"E.exit from the program"<<endl;

do
{

cout<<"ENTER YOUR CHOICE"<<endl;

cin>>answer;

switch(answer)

{


case 'A':
head=creat();
break;
case 'B':
head=insert(head);
break;
case 'C':
head=modify(head);
break;
case 'D':
cout<<"please enter the id of which you want to delete"<<endl;
cin>>id;
head=DELETE(head);
break;
case 'E':
system("cls");

break;
default:
break;

}

show(head);
cout<<"continue?Y/N"<<endl;

}while(answer=='y'||answer=='Y');


}


struct employee* creat(void)
{
int n=0;
struct employee *head,*p1,*p2;
p1=p2=new employee;
head=NULL;
cout<<"enter name number and ID:"<<endl;
cin>>p1->name>>p1->num>>p1->ID;
while(p1->ID)
{n++;
if(n==1)
{
head=p1;
p1->next=NULL;
}
else
p2->next=p1;
p2=p1;

p1=new employee;
cout<<"enter name number and ID:"<<endl;
cin>>p1->name>>p1->num>>p1->ID;

}
p2->next=NULL;
cout<<"the new linked list created successfully!"<<endl;
return(head);
}

struct employee* insert(struct employee* head)
{
struct employee newlist;
cout<<"please initial the new list(name number ID)"<<endl;
cin>>newlist.name>>newlist.num>>newlist.ID;
newlist.next=NULL;

struct employee *p0=&newlist,*p1,*p2;
p1=head;

if(head==NULL)
{head=p0;
p0->next=NULL;
}
else
while(p1->next!=NULL&&p1->ID<p0->ID)
{p2=p1;
p1=p1->next;
}
if(p1->ID<p0->ID)
{p1->next=p0;
p0->next=NULL;
}
else
{p2->next=p0;
p0->next=p1;
}
cout<<"insert successfully!"<<endl;
return(head);
}

void show(struct employee* head)
{
cout<<"date below"<<endl;
cout<<"******************************************************"<<endl;

while(head->next)
{cout<<head->name<<" "<<head->num<<" "<<head->ID<<endl;
head=head->next;
}
}
struct employee* modify(struct employee*head)
{
struct employee* p;
int temp;
long id;
cout<<"employee in which ID you want to modify?"<<endl;
cin>>id;
while(p->next!=NULL&&p->ID!=id)
p=p->next;
if(p->ID=id)
{
cout<<"which one do you want to madify 1.name 2.num?"<<endl;

cin>>temp;
if(temp=1)
cin.getline(p->name,namelength);
else if(temp=2)
cin.getline(p->num,phonelength);

}
else
cout<<"fail to modify!"<<endl;
return(head);
}



struct employee* DELETE(struct employee*head)
{
struct employee *p1,*p2;
p1=p2=head;
long id;
cout<<"which one do you want to delete?"<<endl;
cin>>id;
while(p1->next!=NULL&&p1->ID!=id)
{
p2=p1;

p1=p1->next;
}
if(p1->ID=id)
p2->next=p1->next;
else
cout<<"can't find the list!"<<endl;
return (head);
}



一个相同的问题出现了8次。。。找不出来

搜索更多相关主题的帖子: 指针 赋值 
2006-07-15 11:01
快速回复:[求助]进来讨论一下指针赋值问题!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031458 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved