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

#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 10:58
摄政王:多尔滚
Rank: 1
等 级:新手上路
帖 子:148
专家分:0
注 册:2006-7-9
收藏
得分:0 
C or C++

C = C++ or C != C++

C is C++

C is not C++


19世纪是火车的时代,20世纪是汽车的时代,21世纪是网络的时代。有谁不同意?
2006-07-15 11:15
菜鸟1号
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2006-4-7
收藏
得分:0 
c++

虽然我是一只菜鸟...但是菜鸟也有飞上蓝天的那一天...
2006-07-15 15:41
穆扬
Rank: 1
等 级:禁止发言
帖 子:1910
专家分:0
注 册:2006-6-1
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽

2006-07-15 15:43
菜鸟1号
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2006-4-7
收藏
得分:0 

怎么说?


虽然我是一只菜鸟...但是菜鸟也有飞上蓝天的那一天...
2006-07-15 15:57
工藤♀新一
Rank: 1
等 级:新手上路
帖 子:140
专家分:0
注 册:2006-5-4
收藏
得分:0 
以下是引用菜鸟1号在2006-7-15 10:58:10的发言:

#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 employee *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次。。。可是我找不到。。。指针对指针赋值应该没关系的啊


很高兴能和大家一起学习程序! QQ:114109098
2006-07-15 15:57
快速回复:[求助]进来讨论一下指针赋值问题!
数据加载中...
 
   



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

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